diff --git a/pkg/checkmate/commenter.go b/pkg/checkmate/commenter.go index 6716b65..2b29109 100644 --- a/pkg/checkmate/commenter.go +++ b/pkg/checkmate/commenter.go @@ -2,6 +2,8 @@ package checkmate import ( "context" + "errors" + "net/http" "sort" "strings" @@ -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 } @@ -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 +} diff --git a/pkg/checkmate/commenter_test.go b/pkg/checkmate/commenter_test.go index a5f6664..745b477 100644 --- a/pkg/checkmate/commenter_test.go +++ b/pkg/checkmate/commenter_test.go @@ -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(