From e5ff24120125cf8ec9da1b61de75671c3b51fbfc Mon Sep 17 00:00:00 2001 From: Dan Rollo Date: Fri, 27 Sep 2024 18:59:29 -0400 Subject: [PATCH] If we detect any missing commit signatures, add a link to the comment that tells how to sign git commits. Fixes Issue# 119. --- github/github.go | 49 +++++++++++++++++++++++++++++-------------- github/github_test.go | 36 +++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 16 deletions(-) diff --git a/github/github.go b/github/github.go index 2e777f5..205655b 100644 --- a/github/github.go +++ b/github/github.go @@ -343,24 +343,11 @@ func EvaluatePullRequest(logger *zap.Logger, postgres db.IClaDB, evalInfo *types } } - message := `Thanks for the contribution. Unfortunately some of your commits don't meet our standards. All commits must be signed and have author information set. - -The commits to review are: - -%s` - commitsMessage := "" - for _, c := range commitsMissingAuthor { - commitsMessage += fmt.Sprintf(`- %s - missing author :cop: -`, *c.HTMLURL, *c.SHA) - } - for _, c := range commitsMissingVerification { - commitsMessage += fmt.Sprintf(`- %s - unsigned commit :key: -`, *c.HTMLURL, *c.SHA) - } - logger.Debug("Adding Comment to Issue", zap.Int("Issue #", int(evalInfo.PRNumber)), zap.String("Comment", fmt.Sprintf(message, commitsMessage))) + commentMessage := buildCommentMessage(commitsMissingAuthor, commitsMissingVerification) + logger.Debug("Adding Comment to Issue", zap.Int("Issue #", int(evalInfo.PRNumber)), zap.String("Comment", commentMessage)) _, err = addCommentToIssueIfNotExists( client.Issues, evalInfo.RepoOwner, evalInfo.RepoName, int(evalInfo.PRNumber), - fmt.Sprintf(message, commitsMessage)) + commentMessage) if err != nil { return err } @@ -444,6 +431,36 @@ The commits to review are: return nil } +const buildCommentPrefix = `Thanks for the contribution. Unfortunately some of your commits don't meet our standards. All commits must be signed and have author information set. + +The commits to review are: + +%s%s` + +const buildCommentSuffixSignedCommits = ` +See [Signed Commits](https://contribute.sonatype.com/docs/contributing/submitting/#signed-commits). +` + +func buildCommentMessage(commitsMissingAuthor []github.RepositoryCommit, commitsMissingVerification []github.RepositoryCommit) string { + + commitsMessage := "" + for _, c := range commitsMissingAuthor { + commitsMessage += fmt.Sprintf(`- %s - missing author :cop: +`, *c.HTMLURL, *c.SHA) + } + for _, c := range commitsMissingVerification { + commitsMessage += fmt.Sprintf(`- %s - unsigned commit :key: +`, *c.HTMLURL, *c.SHA) + } + + buildCommentSuffix := "" + if len(commitsMissingVerification) > 0 { + buildCommentSuffix = buildCommentSuffixSignedCommits + } + commentMessage := fmt.Sprintf(buildCommentPrefix, commitsMessage, buildCommentSuffix) + return commentMessage +} + func createRepoStatus(repositoryService RepositoriesService, owner, repo, sha, state, description, botName string) error { _, _, err := repositoryService.CreateStatus(context.Background(), owner, repo, sha, &github.RepoStatus{State: &state, Description: &description, Context: &botName}) if err != nil { diff --git a/github/github_test.go b/github/github_test.go index bc98655..9286bf7 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -500,6 +500,40 @@ func TestHandlePullRequestMissingPemFile(t *testing.T) { assert.EqualError(t, err, "could not read private key: open the-cla.pem: no such file or directory") } +func TestBuildCommentMessageEmptyArgs(t *testing.T) { + commentMessage := buildCommentMessage([]github.RepositoryCommit{}, []github.RepositoryCommit{}) + assert.Equal(t, fmt.Sprintf(buildCommentPrefix, "", ""), commentMessage) +} + +func TestBuildCommentMessageMissingAuthor(t *testing.T) { + myUrl := "https://github.com" + mySha := "sha" + commentMessage := buildCommentMessage([]github.RepositoryCommit{ + {HTMLURL: &myUrl, SHA: &mySha}, + }, []github.RepositoryCommit{}) + assert.Equal(t, fmt.Sprintf(buildCommentPrefix, "- sha - missing author :cop:\n", ""), commentMessage) +} + +func TestBuildCommentMessageMissingVerification(t *testing.T) { + myUrl := "https://github.com" + mySha := "sha" + commentMessage := buildCommentMessage([]github.RepositoryCommit{}, []github.RepositoryCommit{ + {HTMLURL: &myUrl, SHA: &mySha}, + }) + assert.Equal(t, fmt.Sprintf(buildCommentPrefix, "- sha - unsigned commit :key:\n", buildCommentSuffixSignedCommits), commentMessage) +} + +func TestBuildCommentMessageMissingAuthorAndVerification(t *testing.T) { + myUrl := "https://github.com" + mySha := "sha" + commentMessage := buildCommentMessage([]github.RepositoryCommit{ + {HTMLURL: &myUrl, SHA: &mySha}, + }, []github.RepositoryCommit{ + {HTMLURL: &myUrl, SHA: &mySha}, + }) + assert.Equal(t, fmt.Sprintf(buildCommentPrefix, "- sha - missing author :cop:\n- sha - unsigned commit :key:\n", buildCommentSuffixSignedCommits), commentMessage) +} + func TestHandlePullRequestListCommitsNoAuthor(t *testing.T) { origGHAppIDEnvVar := os.Getenv(EnvGhAppId) defer func() { @@ -561,6 +595,8 @@ The commits to review are: - johnSHA - missing author :cop: - johnSHA - unsigned commit :key: + +See [Signed Commits](https://contribute.sonatype.com/docs/contributing/submitting/#signed-commits). `, )}, }, // comment