Skip to content

Commit 67dc040

Browse files
committed
github: Split the interfaces
In an upcoming commit we will be testing the `ci` and `pr` subcommands. Splitting the interfaces into smaller components will make this a bit easier, since those testsuites will only have to fake what they need.
1 parent f288765 commit 67dc040

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

cmd/wait-for-github/ci.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func handleCIStatus(status github.CIStatus, recheckInterval time.Duration) cli.E
101101
}
102102

103103
type checkAllCI struct {
104-
githubClient github.GithubClient
104+
githubClient github.CheckCIStatus
105105
owner string
106106
repo string
107107
ref string

cmd/wait-for-github/pr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ type commitInfo struct {
101101
type prCheck struct {
102102
prConfig
103103

104-
githubClient github.GithubClient
104+
githubClient github.CheckPRMerged
105105
}
106106

107107
func (pr prCheck) Check(ctx context.Context, recheckInterval time.Duration) error {

internal/github/github.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ import (
3030
log "github.com/sirupsen/logrus"
3131
)
3232

33-
type GithubClient interface {
33+
type CheckPRMerged interface {
3434
IsPRMergedOrClosed(ctx context.Context, owner, repo string, pr int) (string, bool, int64, error)
35+
}
36+
37+
type CheckCIStatus interface {
3538
GetCIStatus(ctx context.Context, owner, repo string, commitHash string) (CIStatus, error)
3639
GetCIStatusForChecks(ctx context.Context, owner, repo string, commitHash string, checkNames []string) (CIStatus, []string, error)
3740
}
@@ -48,7 +51,7 @@ type GHClient struct {
4851
client *github.Client
4952
}
5053

51-
func NewGithubClient(ctx context.Context, authInfo AuthInfo) (GithubClient, error) {
54+
func NewGithubClient(ctx context.Context, authInfo AuthInfo) (GHClient, error) {
5255
// If a GitHub token is provided, use it to authenticate in preference to
5356
// App authentication
5457
if authInfo.GithubToken != "" {
@@ -72,30 +75,30 @@ func cachingRetryableTransport() http.RoundTripper {
7275
}
7376

7477
// AuthenticateWithToken authenticates with a GitHub token
75-
func AuthenticateWithToken(ctx context.Context, token string) GithubClient {
78+
func AuthenticateWithToken(ctx context.Context, token string) GHClient {
7679
src := oauth2.StaticTokenSource(
7780
&oauth2.Token{AccessToken: token},
7881
)
7982
ctx = context.WithValue(ctx, oauth2.HTTPClient, &http.Client{Transport: cachingRetryableTransport()})
8083
httpClient := oauth2.NewClient(ctx, src)
8184
githubClient := github.NewClient(httpClient)
8285

83-
return &GHClient{client: githubClient}
86+
return GHClient{client: githubClient}
8487
}
8588

8689
// AuthenticateWithApp authenticates with a GitHub App
87-
func AuthenticateWithApp(ctx context.Context, privateKey []byte, appID, installationID int64) (GithubClient, error) {
90+
func AuthenticateWithApp(ctx context.Context, privateKey []byte, appID, installationID int64) (GHClient, error) {
8891
itr, err := ghinstallation.New(cachingRetryableTransport(), appID, installationID, privateKey)
8992
if err != nil {
90-
return nil, fmt.Errorf("failed to create transport: %w", err)
93+
return GHClient{}, fmt.Errorf("failed to create transport: %w", err)
9194
}
9295

9396
githubClient := github.NewClient(&http.Client{Transport: itr})
9497

95-
return &GHClient{client: githubClient}, nil
98+
return GHClient{client: githubClient}, nil
9699
}
97100

98-
func (c *GHClient) IsPRMergedOrClosed(ctx context.Context, owner, repo string, prNumber int) (string, bool, int64, error) {
101+
func (c GHClient) IsPRMergedOrClosed(ctx context.Context, owner, repo string, prNumber int) (string, bool, int64, error) {
99102
pr, _, err := c.client.PullRequests.Get(ctx, owner, repo, prNumber)
100103
if err != nil {
101104
return "", false, -1, fmt.Errorf("failed to query GitHub: %w", err)
@@ -123,7 +126,22 @@ const (
123126
CIStatusUnknown
124127
)
125128

126-
func (c *GHClient) GetCIStatus(ctx context.Context, owner, repoName string, ref string) (CIStatus, error) {
129+
func (c CIStatus) String() string {
130+
switch c {
131+
case CIStatusPassed:
132+
return "passed"
133+
case CIStatusFailed:
134+
return "failed"
135+
case CIStatusPending:
136+
return "pending"
137+
case CIStatusUnknown:
138+
return "unknown"
139+
default:
140+
return "unknown"
141+
}
142+
}
143+
144+
func (c GHClient) GetCIStatus(ctx context.Context, owner, repoName string, ref string) (CIStatus, error) {
127145
opt := &github.ListOptions{
128146
PerPage: 100,
129147
}
@@ -145,7 +163,7 @@ func (c *GHClient) GetCIStatus(ctx context.Context, owner, repoName string, ref
145163
return CIStatusUnknown, nil
146164
}
147165

148-
func (c *GHClient) getOneStatus(ctx context.Context, owner, repoName, ref, check string) (CIStatus, error) {
166+
func (c GHClient) getOneStatus(ctx context.Context, owner, repoName, ref, check string) (CIStatus, error) {
149167
listOptions := github.ListOptions{
150168
PerPage: 100,
151169
}
@@ -213,7 +231,7 @@ func (c *GHClient) getOneStatus(ctx context.Context, owner, repoName, ref, check
213231

214232
// GetCIStatusForCheck returns the CI status for a specific commit. It looks at
215233
// both 'checks' and 'statuses'.
216-
func (c *GHClient) GetCIStatusForChecks(ctx context.Context, owner, repoName string, ref string, checkNames []string) (CIStatus, []string, error) {
234+
func (c GHClient) GetCIStatusForChecks(ctx context.Context, owner, repoName string, ref string, checkNames []string) (CIStatus, []string, error) {
217235
allFinished := true
218236
awaitedChecks := make(map[string]bool, len(checkNames))
219237
var status CIStatus

0 commit comments

Comments
 (0)