Skip to content

Commit

Permalink
handle non pr issue comments (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
RoryQ authored Feb 24, 2023
1 parent e414bdf commit 70e6366
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
7 changes: 6 additions & 1 deletion pkg/checkmate/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ func inspect(checklists []Checklist, action *githubactions.Action) error {
}

func getPullRequestBody(ghctx *githubactions.GitHubContext) (string, error) {
body, ok := ghctx.Event["pull_request"].(map[string]any)["body"]
pullRequest, ok := ghctx.Event["pull_request"]
if !ok {
return "", nil
}

body, ok := pullRequest.(map[string]any)["body"]
if !ok || body == nil {
return "", nil
}
Expand Down
14 changes: 10 additions & 4 deletions pkg/checkmate/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,34 @@ import (

func TestRun(t *testing.T) {
t.Run("CheckedSuccess", func(t *testing.T) {
action, _ := setupAction("edited-checked")
action, _ := setupAction("pull-request.edited-checked")
err := Run(context.Background(), new(Config), action, nil)
assert.NoError(t, err)
})

t.Run("UncheckedFailure", func(t *testing.T) {
action, _ := setupAction("edited")
action, _ := setupAction("pull-request.edited")
err := Run(context.Background(), new(Config), action, nil)
require.Error(t, err)
assert.Equal(t, "not all checklists are completed", err.Error())
})

t.Run("OpenedWithNullBody", func(t *testing.T) {
action, _ := setupAction("opened.with-null-body")
action, _ := setupAction("pull-request.opened.with-null-body")
err := Run(context.Background(), new(Config), action, nil)
assert.NoError(t, err)
})

t.Run("IssueComment", func(t *testing.T) {
action, _ := setupAction("issue-comment.created")
err := Run(context.Background(), new(Config), action, nil)
assert.NoError(t, err)
})
}

func setupAction(input string) (*githubactions.Action, *bytes.Buffer) {
envMap := map[string]string{
"GITHUB_EVENT_PATH": fmt.Sprintf("../../test/events/pull-request.%s.json", input),
"GITHUB_EVENT_PATH": fmt.Sprintf("../../test/events/%s.json", input),
"GITHUB_STEP_SUMMARY": "/dev/null",
"GITHUB_REPOSITORY": "RoryQ/checkmate",
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/checkmate/commenter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Test_commenter(t *testing.T) {
assetsChecklist := cfg.PathsChecklists[assetsGlob].ToChecklistItemsMD(assetsGlob)

t.Run("NoMatchingFiles", func(t *testing.T) {
action, _ := setupAction("edited")
action, _ := setupAction("pull-request.edited")
ghMockAPI := mock.NewMockedHTTPClient(
mock.WithRequestMatch(
mock.GetReposPullsFilesByOwnerByRepoByPullNumber,
Expand All @@ -66,7 +66,7 @@ func Test_commenter(t *testing.T) {
})

t.Run("MatchingFilesNoExistingComment", func(t *testing.T) {
action, _ := setupAction("edited")
action, _ := setupAction("pull-request.edited")
ghMockAPI := mock.NewMockedHTTPClient(
mock.WithRequestMatch(
mock.GetReposPullsFilesByOwnerByRepoByPullNumber,
Expand Down Expand Up @@ -103,7 +103,7 @@ func Test_commenter(t *testing.T) {
})

t.Run("MatchingFilesForSelectList", func(t *testing.T) {
action, _ := setupAction("edited")
action, _ := setupAction("pull-request.edited")
ghMockAPI := mock.NewMockedHTTPClient(
mock.WithRequestMatch(
mock.GetReposPullsFilesByOwnerByRepoByPullNumber,
Expand Down Expand Up @@ -141,7 +141,7 @@ func Test_commenter(t *testing.T) {
})

t.Run("MatchingFilesWithExistingComment", func(t *testing.T) {
action, _ := setupAction("edited")
action, _ := setupAction("pull-request.edited")
ghMockAPI := mock.NewMockedHTTPClient(
mock.WithRequestMatch(
mock.GetReposPullsFilesByOwnerByRepoByPullNumber,
Expand All @@ -168,7 +168,7 @@ func Test_commenter(t *testing.T) {
})

t.Run("MatchingFilesWithExistingCommentAndChangedFiles", func(t *testing.T) {
action, _ := setupAction("edited")
action, _ := setupAction("pull-request.edited")
schemaChecked := strings.ReplaceAll(schemaMigrationsChecklist, "[ ]", "[x]")
ghMockAPI := mock.NewMockedHTTPClient(
mock.WithRequestMatch(
Expand Down

0 comments on commit 70e6366

Please sign in to comment.