Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding github and affiliated subpackage functionality #23

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4667b29
removed cruft comments from discord_test.go
TimothyStiles Dec 24, 2023
98784ab
got basic mocking and requests working without exposing secrets in pr…
TimothyStiles Dec 24, 2023
a731816
refactored to use go-github structs for input and output.
TimothyStiles Dec 24, 2023
1f41b88
have decent example of mocking github client going on.
TimothyStiles Dec 26, 2023
7b0e400
successful mocking and running of gist creator.
TimothyStiles Dec 26, 2023
095d09d
put gists into their own github specific subpackage. Mocks work. May …
TimothyStiles Dec 26, 2023
024fb24
implemented gist existence check.
TimothyStiles Dec 26, 2023
6e86284
updated comments.
TimothyStiles Dec 26, 2023
004f6c3
created and mocked edit for EditGistFile.
TimothyStiles Dec 26, 2023
8f8da27
created and mocked edit for EditGistFile.
TimothyStiles Dec 26, 2023
e799b7f
fixed comment.
TimothyStiles Dec 26, 2023
034e103
successfully mocked gist part of badge creation.
TimothyStiles Dec 27, 2023
1dbec1d
removed comment cruft from badges package.
TimothyStiles Dec 27, 2023
e6a7adc
got basic badge url construction working.
TimothyStiles Dec 27, 2023
75641c7
added error handling.
TimothyStiles Dec 27, 2023
bc9ed7d
removed fmt.Println.
TimothyStiles Dec 27, 2023
31e21ec
got arbitrary badge creation working.
TimothyStiles Dec 27, 2023
f449935
added coverage badge generator.
TimothyStiles Dec 31, 2023
47c4caa
testing tests.
TimothyStiles Dec 31, 2023
8fb2e1c
fixed TestUpsert in badges_test.go
TimothyStiles Dec 31, 2023
8c9a00f
got rid of just tooling and renamed token reference.
TimothyStiles Dec 31, 2023
723253e
deleted test2.yml.
TimothyStiles Dec 31, 2023
cc09dc8
fixed path.
TimothyStiles Dec 31, 2023
b17537d
renamed cmd_test.go to main_test.go
TimothyStiles Jan 1, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Build Coverage Badge

on:
push:
branches:
- main
pull_request:

jobs:
coverage-badge:
name: Create Coverage Badge
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.21
- name: Checkout code
uses: actions/checkout@v2
- name: Generate coverage report
run: go test -v -coverprofile=profile.cov ./...
- name: Parse overall coverage
run: coverage=$(go tool cover -func profile.cov | tail -1 | rev | cut -f 1 | rev) && echo "COVERAGE=$coverage" >> $GITHUB_ENV
- name: Create the Badge
run: go run ./github/badges/cmd/main.go -v ./...
env:
GITHUB_GIST_TOKEN: ${{ secrets.GITHUB_GIST_TOKEN }}
GITHUB_GIST_FILENAME: buster-coverage.json
GITHUB_GIST_LABEL: Coverage
GITHUB_GIST_MESSAGE: ${{ env.COVERAGE }}
GITHUB_GIST_COLOR": green

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@

# Dependency directories (remove the comment below to include it)
# vendor/

# MacOS
*.DS_Store
89 changes: 89 additions & 0 deletions github/badges/badges.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package badges

import (
"encoding/json"
"errors"
"net/url"

"github.com/TimothyStiles/buster/github/gists"
"github.com/google/go-github/v57/github"
)

type Shield struct {
Label string `json:"label"`
Message string `json:"message"`
SchemaVersion string `json:"schemaVersion"`
Color string `json:"color"`
}

type Badge struct {
URL string
Filename string
Shield Shield
Gist *github.Gist
}

func (badge *Badge) BuildURL(gist *github.Gist) error {
urlBase := "https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/"
owner := gist.Owner.GetLogin()
if owner == "" {
return errors.New("gist owner not found")
}

id := gist.GetID()
if id == "" {
return errors.New("gist id not found")
}

filename := badge.Filename
if filename == "" {
return errors.New("gist filename not found")
}

raw := "raw"
result, err := url.JoinPath(owner, id, raw, filename)
if err != nil {
return err
}
badge.URL = urlBase + result

return nil
}

func (badge *Badge) Upsert(service gists.GistsServiceInterface) error {
// Marshal the shield into JSON
shieldJSON, err := json.Marshal(badge.Shield)
if err != nil {
return err
}
shieldString := string(shieldJSON)

// check if gist exists

var gistExists bool
var gistID string
if gistID == "" {
gistExists = false
} else {
gistID, err = gists.GetGistID(service, badge.Filename)

if err != nil {
return err
}
if gistID != "" {
gistExists = true
}

}

var gist *github.Gist
if gistExists {
gist, err = gists.EditGistFile(service, badge.Filename, shieldString, gistID)
} else {
gist, err = gists.CreateGist(service, badge.Filename, shieldString)
}

err = badge.BuildURL(gist)

return err
}
89 changes: 89 additions & 0 deletions github/badges/badges_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package badges

import (
"net/http"
"testing"

"github.com/TimothyStiles/buster/github/gists"
"github.com/google/go-github/v57/github"
"github.com/stretchr/testify/mock"
)

func TestUpsert(t *testing.T) {
// Create a mock GistsServiceInterface
mockGists := new(gists.MockGistsService)

// Create a mock response
mockResponse := &github.Response{
Response: &http.Response{
StatusCode: 200,
},
}

// Create a test badge
testBadge := Badge{
Filename: "test.txt",
Shield: Shield{
Label: "test",
Message: "test",
Color: "blue",
SchemaVersion: "1",
},
}

// Create a mock gist
filename := "test.txt"
content := "Hello, World!"
file := github.GistFile{
Filename: &filename,
Content: &content,
}
mockGist := github.Gist{
Files: map[github.GistFilename]github.GistFile{
github.GistFilename(filename): file,
},
Description: github.String("test.txt"),
ID: github.String("test"),
Owner: &github.User{Login: github.String("TimothyStiles")},
}

// Expect the ListAll method to be called once with any arguments, and return a slice containing the mock gist, the mock response and no error
mockGists.On("ListAll", mock.Anything, mock.Anything).Return([]*github.Gist{&mockGist}, mockResponse, nil)
mockGists.On("Edit", mock.Anything, mock.Anything, mock.Anything).Return(&mockGist, mockResponse, nil)
mockGists.On("Create", mock.Anything, mock.Anything).Return(&mockGist, mockResponse, nil)

err := testBadge.Upsert(mockGists)
if err != nil {
t.Errorf("Upsert returned an error: %v", err)
}
}
func TestBuildURL(t *testing.T) {
// Create a mock gist
filename := "coverage.json"
content := "Hello, World!"
file := github.GistFile{
Filename: &filename,
Content: &content,
}
mockGist := github.Gist{
Files: map[github.GistFilename]github.GistFile{
github.GistFilename(filename): file,
},
Description: github.String("coverage.json"),
ID: github.String("e58f265655ac0acacdd1a38376ccd32a"),
Owner: &github.User{
Login: github.String("TimothyStiles"),
},
}

badge := Badge{
Filename: "coverage.json",
}

err := badge.BuildURL(&mockGist)
if err != nil {
t.Errorf("BuildURL returned an error: %v", err)
}

// Add your assertions here
}
65 changes: 65 additions & 0 deletions github/badges/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"context"
"os"

"github.com/TimothyStiles/buster/github/badges"
"github.com/google/go-github/v57/github"
"golang.org/x/oauth2"
)

func main() {

// Get GitHub Gist token from environment variable
token, badge := getEnv()

// Create a real GitHub client
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)

client := github.NewClient(tc)

// Upsert the badge
err := badge.Upsert(client.Gists)

if err != nil {
panic(err)
}

// Print the url to the badge
println(badge.URL)
}

func getEnv() (string, badges.Badge) {
// Get GitHub Gist token from environment variable
token := os.Getenv("GITHUB_GIST_TOKEN")
gistId := os.Getenv("GITHUB_GIST_ID")
filename := os.Getenv("GITHUB_GIST_FILENAME")
message := os.Getenv("GITHUB_GIST_MESSAGE")
color := os.Getenv("GITHUB_GIST_COLOR")
label := os.Getenv("GITHUB_GIST_LABEL")

schemaVersion := os.Getenv("SHIELD_IO_SCHEMA_VERSION")

// create a new badge
badge := badges.Badge{
Filename: filename,
Shield: badges.Shield{
Label: label,
Message: message,
Color: color,
SchemaVersion: schemaVersion,
},
}

badge.Gist = &github.Gist{
ID: github.String(gistId),
}

return token, badge

}
72 changes: 72 additions & 0 deletions github/badges/cmd/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"os"
"testing"
)

func TestGetEnv(t *testing.T) {
// Set the environment variables
os.Setenv("GITHUB_GIST_TOKEN", "your_token")
os.Setenv("GITHUB_GIST_FILENAME", "test.txt")
os.Setenv("GITHUB_GIST_ID", "")
os.Setenv("GITHUB_GIST_MESSAGE", "test_message")
os.Setenv("GITHUB_GIST_COLOR", "blue")
os.Setenv("GITHUB_GIST_LABEL", "test_label")
os.Setenv("SHIELD_IO_SCHEMA_VERSION", "1")

// Call the getEnv function
token, badge := getEnv()

// Assert the values
expectedToken := "your_token"
if token != expectedToken {
t.Errorf("Expected token to be %s, but got %s", expectedToken, token)
}

expectedFilename := "test.txt"
if badge.Filename != expectedFilename {
t.Errorf("Expected filename to be %s, but got %s", expectedFilename, badge.Filename)
}

expectedGistID := ""
if *badge.Gist.ID != expectedGistID {
t.Errorf("Expected Gist ID to be %s, but got %s", expectedGistID, *badge.Gist.ID)
}

expectedMessage := "test_message"
if badge.Shield.Message != expectedMessage {
t.Errorf("Expected shield message to be %s, but got %s", expectedMessage, badge.Shield.Message)
}

expectedColor := "blue"
if badge.Shield.Color != expectedColor {
t.Errorf("Expected shield color to be %s, but got %s", expectedColor, badge.Shield.Color)
}

expectedLabel := "test_label"
if badge.Shield.Label != expectedLabel {
t.Errorf("Expected shield label to be %s, but got %s", expectedLabel, badge.Shield.Label)
}

expectedSchemaVersion := "1"
if badge.Shield.SchemaVersion != expectedSchemaVersion {
t.Errorf("Expected shield schema version to be %s, but got %s", expectedSchemaVersion, badge.Shield.SchemaVersion)
}
}

// func TestCmd(t *testing.T) {
// // Set the environment variables
// os.Setenv("GITHUB_GIST_TOKEN", "insert_real_token_here")
// os.Setenv("GITHUB_GIST_FILENAME", "upsertbadgetest.json")
// os.Setenv("GITHUB_GIST_ID", "")
// os.Setenv("GITHUB_GIST_MESSAGE", "test_message")
// os.Setenv("GITHUB_GIST_COLOR", "blue")
// os.Setenv("GITHUB_GIST_LABEL", "test_label")
// os.Setenv("SHIELD_IO_SCHEMA_VERSION", "1")

// // Call the cmd function
// cmd()

// // TODO: Add assertions for the expected behavior of the cmd function
// }
38 changes: 38 additions & 0 deletions github/gists/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"context"
"os"

"github.com/TimothyStiles/buster/github/gists"
"github.com/google/go-github/v57/github"
"golang.org/x/oauth2"
)

func main() {

// Get GitHub Gist token from environment variable
token := os.Getenv("GITHUB_GIST_TOKEN")
filename := os.Getenv("GITHUB_GIST_FILENAME")
content := os.Getenv("GITHUB_GIST_CONTENT")

// Create a real GitHub client
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)

client := github.NewClient(tc)

// Use the client...

createdGist, err := gists.CreateGist(client.Gists, filename, content)
if err != nil {
panic(err)
}

// Print the URL to the created gist
println(createdGist.GetHTMLURL())

}
Loading
Loading