-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #145 from codingpot/feat-gh-1
feat: add GitHub interface/impl
- Loading branch information
Showing
7 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
//go:generate mockgen -source pkg/handlers/gh/gh.go -destination internal/mock_gh/mock_gh.go GitHubService | ||
package main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Package ghv3 provides GitHub V3 API implementation. | ||
package ghv3 | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/codingpot/pr12er/server/pkg/handlers/gh" | ||
"github.com/google/go-github/v36/github" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
const ( | ||
owner = "codingpot" | ||
repoName = "pr12er" | ||
) | ||
|
||
// GitHubV3 is the v3 implementation of gh.GitHubService. | ||
type GitHubV3 struct { | ||
client *github.Client | ||
} | ||
|
||
// CreateIssue create an issue. | ||
func (g GitHubV3) CreateIssue(title, body string, labels []string) (*gh.GitHubIssue, error) { | ||
create, _, err := g.client.Issues.Create( | ||
context.Background(), | ||
owner, | ||
repoName, | ||
&github.IssueRequest{ | ||
Title: &title, | ||
Body: &body, | ||
Labels: &labels, | ||
}) | ||
|
||
if create == nil || err != nil { | ||
return nil, err | ||
} | ||
return &gh.GitHubIssue{URL: create.GetHTMLURL()}, err | ||
} | ||
|
||
// Ensure the GitHubV3 implements gh.GitHubService interface. | ||
var _ gh.GitHubService = (*GitHubV3)(nil) | ||
|
||
func New(apiKey string) *GitHubV3 { | ||
client := github.NewClient(oauth2.NewClient(context.Background(), oauth2.StaticTokenSource( | ||
&oauth2.Token{AccessToken: apiKey}, | ||
))) | ||
return &GitHubV3{ | ||
client: client, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package ghv3 | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
|
||
"github.com/codingpot/pr12er/server/pkg/handlers/gh" | ||
"github.com/google/go-github/v36/github" | ||
"github.com/stretchr/testify/assert" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
func TestGitHubV3_CreateIssue(t *testing.T) { | ||
t.Skip("this will create an issue; so skip for now") | ||
|
||
githubAPIKey := os.Getenv("GITHUB_API_KEY") | ||
t.Logf("GITHUB_API_KEY=%s", githubAPIKey) | ||
|
||
type fields struct { | ||
client *github.Client | ||
} | ||
type args struct { | ||
title string | ||
body string | ||
labels []string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want *gh.GitHubIssue | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "It should create a ticket", | ||
fields: fields{ | ||
client: github.NewClient( | ||
oauth2.NewClient( | ||
context.Background(), | ||
oauth2.StaticTokenSource( | ||
&oauth2.Token{AccessToken: githubAPIKey}))), | ||
}, | ||
args: args{ | ||
title: "TEST TICKET 1", | ||
body: "TEST TICKET BODY", | ||
labels: []string{"data", "test"}, | ||
}, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := GitHubV3{ | ||
client: tt.fields.client, | ||
} | ||
got, err := g.CreateIssue(tt.args.title, tt.args.body, tt.args.labels) | ||
if tt.wantErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.NotEmpty(t, got.URL) | ||
} | ||
}) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package gh | ||
|
||
type GitHubIssue struct { | ||
URL string | ||
} | ||
|
||
type GitHubService interface { | ||
CreateIssue(title, body string, labels []string) (*GitHubIssue, error) | ||
} |