-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub_client.go
More file actions
70 lines (59 loc) · 3.5 KB
/
github_client.go
File metadata and controls
70 lines (59 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Package github provides a GitHub service to interact with the GitHub API.
package github
import (
"context"
"net/url"
"time"
"github.com/google/go-github/v68/github"
)
const defaultTimeout = 30 * time.Second
// This client is a thin wrapper around the go-github library. It provides an interface to the GitHub client
// The main purpose of this client is to provide an interface to the GitHub client which can be mocked in tests.
// As such this MUST be as thin as possible and MUST not contain any business logic, since it is not testable.
type iGithubClient interface {
GetRepository(owner string, repo string) (*github.Repository, *github.Response, error)
GetOrganizationRepositories(org string, opts *github.RepositoryListByOrgOptions) ([]*github.Repository, *github.Response, error)
GetUserRepositories(user string, opts *github.RepositoryListByUserOptions) ([]*github.Repository, *github.Response, error)
GetArchiveLink(owner string, repo string, archiveFormat github.ArchiveFormat, opts *github.RepositoryContentGetOptions) (*url.URL, *github.Response, error)
ListRepositoryIssues(owner string, repo string, opts *github.IssueListByRepoOptions) ([]*github.Issue, *github.Response, error)
CreateIssue(owner string, repo string, issue *github.IssueRequest) (*github.Issue, *github.Response, error)
UpdateIssue(owner string, repo string, number int, issue *github.IssueRequest) (*github.Issue, *github.Response, error)
}
type githubClient struct {
client *github.Client
}
func (c *githubClient) ListRepositoryIssues(owner, repo string, opts *github.IssueListByRepoOptions) ([]*github.Issue, *github.Response, error) {
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
return c.client.Issues.ListByRepo(ctx, owner, repo, opts)
}
func (c *githubClient) CreateIssue(owner, repo string, issue *github.IssueRequest) (*github.Issue, *github.Response, error) {
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
return c.client.Issues.Create(ctx, owner, repo, issue)
}
func (c *githubClient) UpdateIssue(owner, repo string, number int, issue *github.IssueRequest) (*github.Issue, *github.Response, error) {
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
return c.client.Issues.Edit(ctx, owner, repo, number, issue)
}
func (c *githubClient) GetRepository(owner string, repo string) (*github.Repository, *github.Response, error) {
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
return c.client.Repositories.Get(ctx, owner, repo)
}
func (c *githubClient) GetOrganizationRepositories(org string, opts *github.RepositoryListByOrgOptions) ([]*github.Repository, *github.Response, error) {
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
return c.client.Repositories.ListByOrg(ctx, org, opts)
}
func (c *githubClient) GetUserRepositories(user string, opts *github.RepositoryListByUserOptions) ([]*github.Repository, *github.Response, error) {
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
return c.client.Repositories.ListByUser(ctx, user, opts)
}
func (c *githubClient) GetArchiveLink(owner string, repo string, archiveFormat github.ArchiveFormat, opts *github.RepositoryContentGetOptions) (*url.URL, *github.Response, error) {
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
return c.client.Repositories.GetArchiveLink(ctx, owner, repo, archiveFormat, opts, 3)
}