Skip to content

Commit

Permalink
ignore 404 on ListFiles call (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
RoryQ authored Jan 16, 2024
1 parent 625a2e6 commit 1290d0e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/checkmate/commenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package checkmate

import (
"context"
"errors"
"net/http"
"sort"
"strings"

Expand Down Expand Up @@ -138,6 +140,9 @@ func updateComment(ctx context.Context, action *githubactions.Action, cfg Config

func listPullRequestFiles(ctx context.Context, pr pullrequest.Client) ([]string, error) {
files, err := pr.ListFiles(ctx, nil)
if isNotFoundError(err) {
return nil, nil
}
if err != nil {
return nil, err
}
Expand All @@ -161,3 +166,13 @@ func sorted(ss []string) []string {
func join(s ...string) string {
return strings.Join(s[:len(s)-1], s[len(s)-1])
}

func isNotFoundError(err error) bool {
ghe := new(github.ErrorResponse)
if errors.As(err, &ghe) {
if ghe.Response.StatusCode == http.StatusNotFound {
return true
}
}
return false
}
16 changes: 16 additions & 0 deletions pkg/checkmate/commenter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ func Test_commenter(t *testing.T) {
assert.NoError(t, err)
})

t.Run("IssueCommentHasNoFiles", func(t *testing.T) {
action, _ := setupAction("issue-comment.created")
ghMockAPI := mock.NewMockedHTTPClient(
mock.WithRequestMatchHandler(
mock.GetReposPullsFilesByOwnerByRepoByPullNumber,
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
mock.WriteError(w, http.StatusNotFound, "Not Found")
}),
))

pr, err := pullrequest.NewClient(action, github.NewClient(ghMockAPI))
assert.NoError(t, err)
_, err = commenter(ctx, cfg, action, pr)
assert.NoError(t, err)
})

t.Run("MatchingFilesNoExistingComment", func(t *testing.T) {
action, _ := setupAction("pull-request.edited")
ghMockAPI := mock.NewMockedHTTPClient(
Expand Down

0 comments on commit 1290d0e

Please sign in to comment.