Skip to content

Commit 8cafcba

Browse files
authored
avoid fatal exiting from prservice methods (#1622)
1 parent 04a0807 commit 8cafcba

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

libs/ci/github/github.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ func (svc GithubService) GetChangedFiles(prNumber int) ([]string, error) {
6767
for {
6868
files, resp, err := svc.Client.PullRequests.ListFiles(context.Background(), svc.Owner, svc.RepoName, prNumber, &opts)
6969
if err != nil {
70-
log.Fatalf("error getting pull request files: %v", err)
70+
log.Printf("error getting pull request: %v", err)
71+
return nil, fmt.Errorf("error getting pull request: %v", err)
72+
7173
}
7274

7375
for _, file := range files {
@@ -91,7 +93,8 @@ func (svc GithubService) GetChangedFilesForCommit(owner string, repo string, com
9193
for {
9294
commit, resp, err := svc.Client.Repositories.GetCommit(context.Background(), owner, repo, commitID, &opts)
9395
if err != nil {
94-
log.Fatalf("error getting commitfiles: %v", err)
96+
log.Printf("error getting commitfiles: %v", err)
97+
return nil, fmt.Errorf("error getting commitfiles: %v", err)
9598
}
9699
for _, file := range commit.Files {
97100
fileNames = append(fileNames, *file.Filename)
@@ -117,7 +120,8 @@ func (svc GithubService) ListIssues() ([]*ci.Issue, error) {
117120
for {
118121
issues, resp, err := svc.Client.Issues.ListByRepo(context.Background(), svc.Owner, svc.RepoName, opts)
119122
if err != nil {
120-
log.Fatalf("error getting pull request files: %v", err)
123+
log.Printf("error getting pull request files: %v", err)
124+
return nil, fmt.Errorf("error getting pull request files: %v", err)
121125
}
122126
for _, issue := range issues {
123127
if issue.PullRequestLinks != nil {
@@ -216,7 +220,9 @@ func (svc GithubService) CreateCommentReaction(id string, reaction string) error
216220
func (svc GithubService) SetStatus(prNumber int, status string, statusContext string) error {
217221
pr, _, err := svc.Client.PullRequests.Get(context.Background(), svc.Owner, svc.RepoName, prNumber)
218222
if err != nil {
219-
log.Fatalf("error getting pull request: %v", err)
223+
log.Printf("error getting pull request : %v", err)
224+
return fmt.Errorf("error getting pull request : %v", err)
225+
220226
}
221227

222228
_, _, err = svc.Client.Repositories.CreateStatus(context.Background(), svc.Owner, svc.RepoName, *pr.Head.SHA, &github.RepoStatus{
@@ -230,12 +236,15 @@ func (svc GithubService) SetStatus(prNumber int, status string, statusContext st
230236
func (svc GithubService) GetCombinedPullRequestStatus(prNumber int) (string, error) {
231237
pr, _, err := svc.Client.PullRequests.Get(context.Background(), svc.Owner, svc.RepoName, prNumber)
232238
if err != nil {
233-
log.Fatalf("error getting pull request: %v", err)
239+
log.Printf("error getting pull request: %v", err)
240+
return "", fmt.Errorf("error getting pull request: %v", err)
234241
}
235242

236243
statuses, _, err := svc.Client.Repositories.GetCombinedStatus(context.Background(), svc.Owner, svc.RepoName, pr.Head.GetSHA(), nil)
237244
if err != nil {
238-
log.Fatalf("error getting combined status: %v", err)
245+
log.Printf("error getting combined status: %v", err)
246+
return "", fmt.Errorf("error getting combined status: %v", err)
247+
239248
}
240249

241250
return *statuses.State, nil
@@ -244,7 +253,8 @@ func (svc GithubService) GetCombinedPullRequestStatus(prNumber int) (string, err
244253
func (svc GithubService) MergePullRequest(prNumber int) error {
245254
pr, _, err := svc.Client.PullRequests.Get(context.Background(), svc.Owner, svc.RepoName, prNumber)
246255
if err != nil {
247-
log.Fatalf("error getting pull request: %v", err)
256+
log.Printf("error getting pull request: %v", err)
257+
return fmt.Errorf("error getting pull request: %v", err)
248258
}
249259

250260
_, _, err = svc.Client.PullRequests.Merge(context.Background(), svc.Owner, svc.RepoName, prNumber, "auto-merge", &github.PullRequestOptions{
@@ -272,26 +282,26 @@ func isMergeableState(mergeableState string) bool {
272282
func (svc GithubService) IsMergeable(prNumber int) (bool, error) {
273283
pr, _, err := svc.Client.PullRequests.Get(context.Background(), svc.Owner, svc.RepoName, prNumber)
274284
if err != nil {
275-
log.Fatalf("error getting pull request: %v", err)
276-
return false, err
285+
log.Printf("error getting pull request: %v", err)
286+
return false, fmt.Errorf("error getting pull request: %v", err)
277287
}
278288
return pr.GetMergeable() && isMergeableState(pr.GetMergeableState()), nil
279289
}
280290

281291
func (svc GithubService) IsMerged(prNumber int) (bool, error) {
282292
pr, _, err := svc.Client.PullRequests.Get(context.Background(), svc.Owner, svc.RepoName, prNumber)
283293
if err != nil {
284-
log.Fatalf("error getting pull request: %v", err)
285-
return false, err
294+
log.Printf("error getting pull request: %v", err)
295+
return false, fmt.Errorf("error getting pull request: %v", err)
286296
}
287297
return *pr.Merged, nil
288298
}
289299

290300
func (svc GithubService) IsClosed(prNumber int) (bool, error) {
291301
pr, _, err := svc.Client.PullRequests.Get(context.Background(), svc.Owner, svc.RepoName, prNumber)
292302
if err != nil {
293-
log.Fatalf("error getting pull request: %v", err)
294-
return false, err
303+
log.Printf("error getting pull request: %v", err)
304+
return false, fmt.Errorf("error getting pull request: %v", err)
295305
}
296306

297307
return pr.GetState() == "closed", nil
@@ -317,8 +327,8 @@ func (svc GithubService) SetOutput(prNumber int, key string, value string) error
317327
func (svc GithubService) GetBranchName(prNumber int) (string, string, error) {
318328
pr, _, err := svc.Client.PullRequests.Get(context.Background(), svc.Owner, svc.RepoName, prNumber)
319329
if err != nil {
320-
log.Fatalf("error getting pull request: %v", err)
321-
return "", "", err
330+
log.Printf("error getting pull request: %v", err)
331+
return "", "", fmt.Errorf("error getting pull request: %v", err)
322332
}
323333

324334
return pr.Head.GetRef(), pr.Head.GetSHA(), nil

0 commit comments

Comments
 (0)