@@ -30,8 +30,11 @@ import (
30
30
log "github.com/sirupsen/logrus"
31
31
)
32
32
33
- type GithubClient interface {
33
+ type CheckPRMerged interface {
34
34
IsPRMergedOrClosed (ctx context.Context , owner , repo string , pr int ) (string , bool , int64 , error )
35
+ }
36
+
37
+ type CheckCIStatus interface {
35
38
GetCIStatus (ctx context.Context , owner , repo string , commitHash string ) (CIStatus , error )
36
39
GetCIStatusForChecks (ctx context.Context , owner , repo string , commitHash string , checkNames []string ) (CIStatus , []string , error )
37
40
}
@@ -48,7 +51,7 @@ type GHClient struct {
48
51
client * github.Client
49
52
}
50
53
51
- func NewGithubClient (ctx context.Context , authInfo AuthInfo ) (GithubClient , error ) {
54
+ func NewGithubClient (ctx context.Context , authInfo AuthInfo ) (GHClient , error ) {
52
55
// If a GitHub token is provided, use it to authenticate in preference to
53
56
// App authentication
54
57
if authInfo .GithubToken != "" {
@@ -72,30 +75,30 @@ func cachingRetryableTransport() http.RoundTripper {
72
75
}
73
76
74
77
// AuthenticateWithToken authenticates with a GitHub token
75
- func AuthenticateWithToken (ctx context.Context , token string ) GithubClient {
78
+ func AuthenticateWithToken (ctx context.Context , token string ) GHClient {
76
79
src := oauth2 .StaticTokenSource (
77
80
& oauth2.Token {AccessToken : token },
78
81
)
79
82
ctx = context .WithValue (ctx , oauth2 .HTTPClient , & http.Client {Transport : cachingRetryableTransport ()})
80
83
httpClient := oauth2 .NewClient (ctx , src )
81
84
githubClient := github .NewClient (httpClient )
82
85
83
- return & GHClient {client : githubClient }
86
+ return GHClient {client : githubClient }
84
87
}
85
88
86
89
// 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 ) {
88
91
itr , err := ghinstallation .New (cachingRetryableTransport (), appID , installationID , privateKey )
89
92
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 )
91
94
}
92
95
93
96
githubClient := github .NewClient (& http.Client {Transport : itr })
94
97
95
- return & GHClient {client : githubClient }, nil
98
+ return GHClient {client : githubClient }, nil
96
99
}
97
100
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 ) {
99
102
pr , _ , err := c .client .PullRequests .Get (ctx , owner , repo , prNumber )
100
103
if err != nil {
101
104
return "" , false , - 1 , fmt .Errorf ("failed to query GitHub: %w" , err )
@@ -123,7 +126,22 @@ const (
123
126
CIStatusUnknown
124
127
)
125
128
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 ) {
127
145
opt := & github.ListOptions {
128
146
PerPage : 100 ,
129
147
}
@@ -145,7 +163,7 @@ func (c *GHClient) GetCIStatus(ctx context.Context, owner, repoName string, ref
145
163
return CIStatusUnknown , nil
146
164
}
147
165
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 ) {
149
167
listOptions := github.ListOptions {
150
168
PerPage : 100 ,
151
169
}
@@ -213,7 +231,7 @@ func (c *GHClient) getOneStatus(ctx context.Context, owner, repoName, ref, check
213
231
214
232
// GetCIStatusForCheck returns the CI status for a specific commit. It looks at
215
233
// 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 ) {
217
235
allFinished := true
218
236
awaitedChecks := make (map [string ]bool , len (checkNames ))
219
237
var status CIStatus
0 commit comments