Skip to content

Commit

Permalink
test: add tests for github provider
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Münch <[email protected]>
  • Loading branch information
muenchdo committed Dec 19, 2024
1 parent 22bd83c commit 6eb1f6c
Show file tree
Hide file tree
Showing 3 changed files with 367 additions and 6 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/spf13/cast v1.7.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
Expand Down
84 changes: 78 additions & 6 deletions internal/gitprovider/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,42 @@ func init() {
gitprovider.Register(ProviderName, registration)
}

type githubClient interface {
CreatePullRequest(
ctx context.Context,
owner string,
repo string,
pull *github.NewPullRequest,
) (*github.PullRequest, *github.Response, error)

ListPullRequests(
ctx context.Context,
owner string,
repo string,
opts *github.PullRequestListOptions,
) ([]*github.PullRequest, *github.Response, error)

GetPullRequests(
ctx context.Context,
owner string,
repo string,
number int,
) (*github.PullRequest, *github.Response, error)

AddLabelsToIssue(
ctx context.Context,
owner string,
repo string,
number int,
labels []string,
) ([]*github.Label, *github.Response, error)
}

// provider is a GitHub implementation of gitprovider.Interface.
type provider struct { // nolint: revive
owner string
repo string
client *github.Client
client githubClient
}

// NewProvider returns a GitHub-based implementation of gitprovider.Interface.
Expand Down Expand Up @@ -81,10 +112,51 @@ func NewProvider(
return &provider{
owner: owner,
repo: repo,
client: client,
client: &githubClientWrapper{client},
}, nil
}

type githubClientWrapper struct {
client *github.Client
}

func (g githubClientWrapper) CreatePullRequest(
ctx context.Context,
owner string,
repo string,
pull *github.NewPullRequest,
) (*github.PullRequest, *github.Response, error) {
return g.client.PullRequests.Create(ctx, owner, repo, pull)
}

func (g githubClientWrapper) ListPullRequests(
ctx context.Context,
owner string,
repo string,
opts *github.PullRequestListOptions,
) ([]*github.PullRequest, *github.Response, error) {
return g.client.PullRequests.List(ctx, owner, repo, opts)
}

func (g githubClientWrapper) GetPullRequests(
ctx context.Context,
owner string,
repo string,
number int,
) (*github.PullRequest, *github.Response, error) {
return g.client.PullRequests.Get(ctx, owner, repo, number)
}

func (g githubClientWrapper) AddLabelsToIssue(
ctx context.Context,
owner string,
repo string,
number int,
labels []string,
) ([]*github.Label, *github.Response, error) {
return g.client.Issues.AddLabelsToIssue(ctx, owner, repo, number, labels)
}

// CreatePullRequest implements gitprovider.Interface.
func (p *provider) CreatePullRequest(
ctx context.Context,
Expand All @@ -93,7 +165,7 @@ func (p *provider) CreatePullRequest(
if opts == nil {
opts = &gitprovider.CreatePullRequestOpts{}
}
ghPR, _, err := p.client.PullRequests.Create(ctx,
ghPR, _, err := p.client.CreatePullRequest(ctx,
p.owner,
p.repo,
&github.NewPullRequest{
Expand All @@ -112,7 +184,7 @@ func (p *provider) CreatePullRequest(
}
pr := convertGithubPR(*ghPR)
if len(opts.Labels) > 0 {
_, _, err = p.client.Issues.AddLabelsToIssue(ctx,
_, _, err = p.client.AddLabelsToIssue(ctx,
p.owner,
p.repo,
int(pr.Number),
Expand All @@ -130,7 +202,7 @@ func (p *provider) GetPullRequest(
ctx context.Context,
id int64,
) (*gitprovider.PullRequest, error) {
ghPR, _, err := p.client.PullRequests.Get(ctx, p.owner, p.repo, int(id))
ghPR, _, err := p.client.GetPullRequests(ctx, p.owner, p.repo, int(id))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -171,7 +243,7 @@ func (p *provider) ListPullRequests(
}
prs := []gitprovider.PullRequest{}
for {
ghPRs, res, err := p.client.PullRequests.List(ctx, p.owner, p.repo, &listOpts)
ghPRs, res, err := p.client.ListPullRequests(ctx, p.owner, p.repo, &listOpts)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 6eb1f6c

Please sign in to comment.