From c6ab19bae7f35446a0cc847dded5355b17df39ed Mon Sep 17 00:00:00 2001 From: Paul Horton Date: Fri, 13 Sep 2024 08:29:25 +0100 Subject: [PATCH 01/10] upgraded github.com/google/go-github from v42 to v64 Signed-off-by: Paul Horton --- db/db_test.go | 2 +- github/github.go | 2 +- github/github_mocks.go | 2 +- github/github_test.go | 2 +- go.mod | 2 +- go.sum | 4 ++-- oauth/oauth.go | 5 +++-- oauth/oauth_test.go | 11 +++++++---- server_test.go | 2 +- 9 files changed, 18 insertions(+), 14 deletions(-) diff --git a/db/db_test.go b/db/db_test.go index e79fd47..361d7e0 100644 --- a/db/db_test.go +++ b/db/db_test.go @@ -27,7 +27,7 @@ import ( "time" "github.com/DATA-DOG/go-sqlmock" - "github.com/google/go-github/v42/github" + "github.com/google/go-github/v64/github" "github.com/sonatype-nexus-community/the-cla/types" "github.com/stretchr/testify/assert" ) diff --git a/github/github.go b/github/github.go index 12d9da2..31389e3 100644 --- a/github/github.go +++ b/github/github.go @@ -31,7 +31,7 @@ import ( "go.uber.org/zap" "github.com/bradleyfalzon/ghinstallation/v2" - "github.com/google/go-github/v42/github" + "github.com/google/go-github/v64/github" "github.com/sonatype-nexus-community/the-cla/db" "github.com/sonatype-nexus-community/the-cla/types" webhook "gopkg.in/go-playground/webhooks.v5/github" diff --git a/github/github_mocks.go b/github/github_mocks.go index 3c7e189..40065eb 100644 --- a/github/github_mocks.go +++ b/github/github_mocks.go @@ -25,7 +25,7 @@ import ( "os" "testing" - "github.com/google/go-github/v42/github" + "github.com/google/go-github/v64/github" "github.com/stretchr/testify/assert" ) diff --git a/github/github_test.go b/github/github_test.go index 554f2c4..6de28db 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -27,12 +27,12 @@ import ( "testing" "time" + "github.com/google/go-github/v64/github" "github.com/sonatype-nexus-community/the-cla/db" "github.com/sonatype-nexus-community/the-cla/types" "go.uber.org/zap" "go.uber.org/zap/zaptest" - "github.com/google/go-github/v42/github" "github.com/stretchr/testify/assert" webhook "gopkg.in/go-playground/webhooks.v5/github" ) diff --git a/go.mod b/go.mod index a770167..cd1853d 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/DATA-DOG/go-sqlmock v1.5.2 github.com/bradleyfalzon/ghinstallation/v2 v2.11.0 github.com/golang-migrate/migrate/v4 v4.17.1 - github.com/google/go-github/v42 v42.0.0 + github.com/google/go-github/v64 v64.0.0 github.com/google/uuid v1.6.0 github.com/joho/godotenv v1.5.1 github.com/labstack/echo/v4 v4.12.0 diff --git a/go.sum b/go.sum index 719fc9a..37d0f8a 100644 --- a/go.sum +++ b/go.sum @@ -29,10 +29,10 @@ github.com/golang-migrate/migrate/v4 v4.17.1/go.mod h1:m8hinFyWBn0SA4QKHuKh175Pm github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/google/go-github/v42 v42.0.0 h1:YNT0FwjPrEysRkLIiKuEfSvBPCGKphW5aS5PxwaoLec= -github.com/google/go-github/v42 v42.0.0/go.mod h1:jgg/jvyI0YlDOM1/ps6XYh04HNQ3vKf0CVko62/EhRg= github.com/google/go-github/v62 v62.0.0 h1:/6mGCaRywZz9MuHyw9gD1CwsbmBX8GWsbFkwMmHdhl4= github.com/google/go-github/v62 v62.0.0/go.mod h1:EMxeUqGJq2xRu9DYBMwel/mr7kZrzUOfQmmpYrZn2a4= +github.com/google/go-github/v64 v64.0.0 h1:4G61sozmY3eiPAjjoOHponXDBONm+utovTKbyUb2Qdg= +github.com/google/go-github/v64 v64.0.0/go.mod h1:xB3vqMQNdHzilXBiO2I+M7iEFtHf+DP/omBOv6tQzVo= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= diff --git a/oauth/oauth.go b/oauth/oauth.go index 8c63e1c..e208084 100644 --- a/oauth/oauth.go +++ b/oauth/oauth.go @@ -21,10 +21,11 @@ package oauth import ( "context" - "go.uber.org/zap" "net/http" - "github.com/google/go-github/v42/github" + "go.uber.org/zap" + + "github.com/google/go-github/v64/github" ourGithub "github.com/sonatype-nexus-community/the-cla/github" "golang.org/x/oauth2" githuboauth "golang.org/x/oauth2/github" diff --git a/oauth/oauth_test.go b/oauth/oauth_test.go index 850e4b8..123abcc 100644 --- a/oauth/oauth_test.go +++ b/oauth/oauth_test.go @@ -21,14 +21,15 @@ package oauth import ( "context" - "github.com/google/go-github/v42/github" - "go.uber.org/zap" - "go.uber.org/zap/zaptest" - "golang.org/x/oauth2" "net/http" "os" "testing" + "github.com/google/go-github/v64/github" + "go.uber.org/zap" + "go.uber.org/zap/zaptest" + "golang.org/x/oauth2" + "github.com/stretchr/testify/assert" ) @@ -75,12 +76,14 @@ type OAuthMock struct { var _ OAuthInterface = (*OAuthMock)(nil) // Exchange takes the code and returns a real token. +// //goland:noinspection GoUnusedParameter func (o *OAuthMock) Exchange(ctx context.Context, code string, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error) { return o.exchangeToken, o.exchangeError } // Client returns a new http.Client. +// //goland:noinspection GoUnusedParameter func (o *OAuthMock) Client(ctx context.Context, t *oauth2.Token) *http.Client { return &http.Client{} diff --git a/server_test.go b/server_test.go index 935177b..3b3aa29 100644 --- a/server_test.go +++ b/server_test.go @@ -30,7 +30,7 @@ import ( "time" "github.com/DATA-DOG/go-sqlmock" - "github.com/google/go-github/v42/github" + "github.com/google/go-github/v64/github" "github.com/labstack/echo/v4" "github.com/sonatype-nexus-community/the-cla/db" ourGithub "github.com/sonatype-nexus-community/the-cla/github" From 9e5eeb9eaf5d086663012e91c04531b2f8f4e97b Mon Sep 17 00:00:00 2001 From: Paul Horton Date: Tue, 17 Sep 2024 10:16:24 +0100 Subject: [PATCH 02/10] added tests to prove #109 Signed-off-by: Paul Horton --- github/github_test.go | 128 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 1 deletion(-) diff --git a/github/github_test.go b/github/github_test.go index 6de28db..ff8fc9b 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -561,7 +561,6 @@ func TestHandlePullRequestListCommits(t *testing.T) { mockInstallation: &github.Installation{ AppSlug: &appSlug, }, - //mockAppErr: forcedError, mockAppResp: &github.Response{Response: &http.Response{StatusCode: http.StatusOK}}, mockApp: &github.App{ExternalURL: &mockExternalUrl}, }, @@ -611,6 +610,133 @@ func TestHandlePullRequestListCommits(t *testing.T) { assert.NoError(t, err) } +func TestHandlePullRequestListCommitsNoAuthor(t *testing.T) { + origGHAppIDEnvVar := os.Getenv(EnvGhAppId) + defer func() { + resetEnvVariable(t, EnvGhAppId, origGHAppIDEnvVar) + }() + assert.NoError(t, os.Setenv(EnvGhAppId, "-1")) + + resetPemFileImpl := SetupTestPemFile(t) + defer resetPemFileImpl() + + resetGHJWTImpl := SetupMockGHJWT() + defer resetGHJWTImpl() + mockExternalUrl := "fakeExternalURL" + GHJWTImpl = &GHJWTMock{ + AppsMock: AppsMock{ + mockInstallation: &github.Installation{ + AppSlug: &appSlug, + }, + mockAppResp: &github.Response{Response: &http.Response{StatusCode: http.StatusOK}}, + mockApp: &github.App{ExternalURL: &mockExternalUrl}, + }, + } + + origGithubImpl := GHImpl + defer func() { + GHImpl = origGithubImpl + }() + mockRepositoryCommits := []*github.RepositoryCommit{ + { + Commit: &github.Commit{ + Author: &github.CommitAuthor{ + Name: github.String("someuser"), + Email: github.String("someuser@some.where.tld"), + // Date: github.Timestamp.Local(), + }, + }, + SHA: github.String("johnSHA"), + }, + } + GHImpl = &GHInterfaceMock{ + PullRequestsMock: PullRequestsMock{ + mockRepositoryCommits: mockRepositoryCommits, + }, + IssuesMock: IssuesMock{ + mockGetLabel: &github.Label{}, + MockGetLabelResponse: &github.Response{ + Response: &http.Response{}, + }, + MockRemoveLabelResponse: &github.Response{ + Response: &http.Response{}, + }, + }, + } + + prEvent := webhook.PullRequestPayload{} + + mockDB, logger := setupMockDB(t, false) + err := HandlePullRequest(logger, mockDB, prEvent, 0, "") + assert.NoError(t, err) +} + +func TestHandlePullRequestListCommitsUnsignedCommit(t *testing.T) { + origGHAppIDEnvVar := os.Getenv(EnvGhAppId) + defer func() { + resetEnvVariable(t, EnvGhAppId, origGHAppIDEnvVar) + }() + assert.NoError(t, os.Setenv(EnvGhAppId, "-1")) + + resetPemFileImpl := SetupTestPemFile(t) + defer resetPemFileImpl() + + resetGHJWTImpl := SetupMockGHJWT() + defer resetGHJWTImpl() + mockExternalUrl := "fakeExternalURL" + GHJWTImpl = &GHJWTMock{ + AppsMock: AppsMock{ + mockInstallation: &github.Installation{ + AppSlug: &appSlug, + }, + mockAppResp: &github.Response{Response: &http.Response{StatusCode: http.StatusOK}}, + mockApp: &github.App{ExternalURL: &mockExternalUrl}, + }, + } + + origGithubImpl := GHImpl + defer func() { + GHImpl = origGithubImpl + }() + mockRepositoryCommits := []*github.RepositoryCommit{ + { + Commit: &github.Commit{ + Author: &github.CommitAuthor{ + Name: github.String("someuser"), + Email: github.String("someuser@some.where.tld"), + }, + Verification: &github.SignatureVerification{ + Verified: github.Bool(false), + Reason: github.String("unsigned"), + Signature: nil, + Payload: nil, + }, + }, + SHA: github.String("johnSHA"), + }, + } + GHImpl = &GHInterfaceMock{ + PullRequestsMock: PullRequestsMock{ + mockRepositoryCommits: mockRepositoryCommits, + }, + IssuesMock: IssuesMock{ + mockGetLabel: &github.Label{}, + MockGetLabelResponse: &github.Response{ + Response: &http.Response{}, + }, + MockRemoveLabelResponse: &github.Response{ + Response: &http.Response{}, + }, + }, + } + + prEvent := webhook.PullRequestPayload{} + + mockDB, logger := setupMockDB(t, false) + err := HandlePullRequest(logger, mockDB, prEvent, 0, "") + assert.NoError(t, err) +} + func Test_removeLabelFromIssueIfExists_Removed(t *testing.T) { issuesMock := &IssuesMock{ MockRemoveLabelResponse: &github.Response{ From 3fe96547fda5b9ae6198670b3c9ea257424fb9d9 Mon Sep 17 00:00:00 2001 From: Paul Horton Date: Tue, 17 Sep 2024 11:17:10 +0100 Subject: [PATCH 03/10] feat: assess commits on PR for `Author` and `Verification` Signed-off-by: Paul Horton --- github/github.go | 81 ++++++++++++++++++++++++++++++++++++++++++- github/github_test.go | 50 +++++++++++++++++++++----- 2 files changed, 121 insertions(+), 10 deletions(-) diff --git a/github/github.go b/github/github.go index 31389e3..21147d7 100644 --- a/github/github.go +++ b/github/github.go @@ -248,11 +248,34 @@ func EvaluatePullRequest(logger *zap.Logger, postgres db.IClaDB, evalInfo *types // The following loop will change a loop as a result var usersNeedingToSignCLA []types.UserSignature var usersSigned []types.UserSignature + var commitsMissingAuthor []github.RepositoryCommit + var commitsMissingVerification []github.RepositoryCommit for _, v := range commits { // It is important to use GetAuthor() instead of v.Commit.GetCommitter() because the committer can be the GH webflow user, whereas the author is // the canonical author of the commit - author := *v.GetAuthor() + author := v.GetAuthor() + commitFailedChecks := false + + if author == nil { + commitsMissingAuthor = append(commitsMissingAuthor, *v) + commitFailedChecks = true + } + + commitVerified := false + commitVerification := v.Commit.GetVerification() + if commitVerification != nil { + commitVerified = *v.Commit.Verification.Verified + } + if commitVerified == false { + commitsMissingVerification = append(commitsMissingVerification, *v) + commitFailedChecks = true + logger.Debug("Commit failed verification check", zap.Any("Commit", v)) + } + + if commitFailedChecks { + continue + } // if author is a collaborator, that author need not sign the cla. var isCollaborator bool @@ -293,6 +316,59 @@ func EvaluatePullRequest(logger *zap.Logger, postgres db.IClaDB, evalInfo *types } } + logger.Info( + fmt.Sprintf("Commits Reviewed for PR #%d", evalInfo.PRNumber), + zap.Int("Missing Author", len(commitsMissingAuthor)), + zap.Int("Missing Verification", len(commitsMissingVerification)), + ) + + if len(commitsMissingAuthor) > 0 || len(commitsMissingVerification) > 0 { + if len(commitsMissingAuthor) > 0 { + err := createRepoLabel(logger, client.Issues, evalInfo.RepoOwner, evalInfo.RepoName, labelNameCommitsNoAuthor, "B60205", "Commits are missing author information - this must be resolved", evalInfo.PRNumber) + if err != nil { + return err + } + } + + if len(commitsMissingVerification) > 0 { + err := createRepoLabel( + logger, + client.Issues, + evalInfo.RepoOwner, + evalInfo.RepoName, + labelNameCommitsMissingVerification, + "B60205", + "Some commits are not signed - this must be resolved", + evalInfo.PRNumber, + ) + if err != nil { + return err + } + } + + 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 += commitsMessage + fmt.Sprintf(`- %s - missing author :cop:`, *c.HTMLURL, *c.SHA) + } + for _, c := range commitsMissingVerification { + commitsMessage += 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))) + _, err = addCommentToIssueIfNotExists( + client.Issues, evalInfo.RepoOwner, evalInfo.RepoName, int(evalInfo.PRNumber), + fmt.Sprintf(message, commitsMessage)) + if err != nil { + return err + } + + return nil + } + if len(usersNeedingToSignCLA) > 0 { err := createRepoLabel(logger, client.Issues, evalInfo.RepoOwner, evalInfo.RepoName, labelNameCLANotSigned, "ff3333", "The CLA needs to be signed", evalInfo.PRNumber) if err != nil { @@ -374,6 +450,8 @@ func createRepoStatus(repositoryService RepositoriesService, owner, repo, sha, s const labelNameCLANotSigned string = ":monocle_face: cla not signed" const labelNameCLASigned string = ":heart_eyes: cla signed" +const labelNameCommitsNoAuthor string = ":unamused: commits missing author" +const labelNameCommitsMissingVerification string = ":anguished: commits missing verification" func createRepoLabel(logger *zap.Logger, issuesService IssuesService, @@ -435,6 +513,7 @@ func addCommentToIssueIfNotExists(issuesService IssuesService, owner, repo strin } if !alreadyCommented { + prComment := &github.IssueComment{} prComment.Body = &message diff --git a/github/github_test.go b/github/github_test.go index ff8fc9b..7bb5ceb 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -289,10 +289,12 @@ func TestHandlePullRequestIsCollaboratorError(t *testing.T) { GHImpl = origGithubImpl }() mockAuthorLogin := "myAuthorLogin" - mockRepositoryCommits := []*github.RepositoryCommit{{Author: &github.User{Login: &mockAuthorLogin}}} + mockRepositoryCommits := getMockRepositoryCommitsSigned(mockAuthorLogin) forcedError := fmt.Errorf("forced IsCollaborator error") GHImpl = &GHInterfaceMock{ - PullRequestsMock: PullRequestsMock{mockRepositoryCommits: mockRepositoryCommits}, + PullRequestsMock: PullRequestsMock{ + mockRepositoryCommits: mockRepositoryCommits, + }, RepositoriesMock: RepositoriesMock{ isCollaboratorErr: forcedError, }, @@ -325,9 +327,11 @@ func TestHandlePullRequestIsCollaboratorTrueCollaborator(t *testing.T) { GHImpl = origGithubImpl }() mockAuthorLogin := "myAuthorLogin" - mockRepositoryCommits := []*github.RepositoryCommit{{Author: &github.User{Login: &mockAuthorLogin}}} + mockRepositoryCommits := getMockRepositoryCommitsSigned(mockAuthorLogin) GHImpl = &GHInterfaceMock{ - PullRequestsMock: PullRequestsMock{mockRepositoryCommits: mockRepositoryCommits}, + PullRequestsMock: PullRequestsMock{ + mockRepositoryCommits: mockRepositoryCommits, + }, RepositoriesMock: RepositoriesMock{ isCollaboratorResult: true, }, @@ -370,7 +374,7 @@ func TestHandlePullRequestCreateLabelError(t *testing.T) { GHImpl = origGithubImpl }() mockAuthorLogin := "myAuthorLogin" - mockRepositoryCommits := []*github.RepositoryCommit{{Author: &github.User{Login: &mockAuthorLogin}}} + mockRepositoryCommits := getMockRepositoryCommitsSigned(mockAuthorLogin) forcedError := fmt.Errorf("forced CreateLabel error") GHImpl = &GHInterfaceMock{ PullRequestsMock: PullRequestsMock{mockRepositoryCommits: mockRepositoryCommits}, @@ -407,7 +411,7 @@ func TestHandlePullRequestAddLabelsToIssueError(t *testing.T) { GHImpl = origGithubImpl }() mockAuthorLogin := "myAuthorLogin" - mockRepositoryCommits := []*github.RepositoryCommit{{Author: &github.User{Login: &mockAuthorLogin}}} + mockRepositoryCommits := getMockRepositoryCommitsSigned(mockAuthorLogin) forcedError := fmt.Errorf("forced AddLabelsToIssue error") GHImpl = &GHInterfaceMock{ PullRequestsMock: PullRequestsMock{mockRepositoryCommits: mockRepositoryCommits}, @@ -457,7 +461,7 @@ func TestHandlePullRequestGetAppError(t *testing.T) { GHImpl = origGithubImpl }() mockAuthorLogin := "myAuthorLogin" - mockRepositoryCommits := []*github.RepositoryCommit{{Author: &github.User{Login: &mockAuthorLogin}}} + mockRepositoryCommits := getMockRepositoryCommitsSigned(mockAuthorLogin) GHImpl = &GHInterfaceMock{ PullRequestsMock: PullRequestsMock{mockRepositoryCommits: mockRepositoryCommits}, IssuesMock: IssuesMock{ @@ -578,6 +582,11 @@ func TestHandlePullRequestListCommits(t *testing.T) { Login: github.String(login), Email: github.String("j@gmail.com"), }, + Commit: &github.Commit{ + Verification: &github.SignatureVerification{ + Verified: github.Bool(true), + }, + }, SHA: github.String("johnSHA"), }, { @@ -585,6 +594,11 @@ func TestHandlePullRequestListCommits(t *testing.T) { Login: github.String(login2), Email: github.String("d@gmail.com"), }, + Commit: &github.Commit{ + Verification: &github.SignatureVerification{ + Verified: github.Bool(true), + }, + }, SHA: github.String("doeSHA"), }, } @@ -646,7 +660,8 @@ func TestHandlePullRequestListCommitsNoAuthor(t *testing.T) { // Date: github.Timestamp.Local(), }, }, - SHA: github.String("johnSHA"), + SHA: github.String("johnSHA"), + HTMLURL: github.String("https://github.com"), }, } GHImpl = &GHInterfaceMock{ @@ -712,7 +727,8 @@ func TestHandlePullRequestListCommitsUnsignedCommit(t *testing.T) { Payload: nil, }, }, - SHA: github.String("johnSHA"), + SHA: github.String("johnSHA"), + HTMLURL: github.String("https://github.com"), }, } GHImpl = &GHInterfaceMock{ @@ -905,3 +921,19 @@ func TestReviewPriorPRs(t *testing.T) { assert.NoError(t, ReviewPriorPRs(logger, mockDB, &user)) } + +func getMockRepositoryCommitsSigned(mockAuthorLogin string) []*github.RepositoryCommit { + mockRepositoryCommits := []*github.RepositoryCommit{ + { + Author: &github.User{ + Login: &mockAuthorLogin, + }, + Commit: &github.Commit{ + Verification: &github.SignatureVerification{ + Verified: github.Bool(true), + }, + }, + }, + } + return mockRepositoryCommits +} From 808a5a31bf6cd4c72e2704937b78944166ad2b8b Mon Sep 17 00:00:00 2001 From: Paul Horton Date: Tue, 17 Sep 2024 11:44:40 +0100 Subject: [PATCH 04/10] chore: address code duplication in tests Signed-off-by: Paul Horton --- github/github_test.go | 767 ++++++++++++++++++------------------------ 1 file changed, 321 insertions(+), 446 deletions(-) diff --git a/github/github_test.go b/github/github_test.go index 7bb5ceb..8c0f05e 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -37,163 +37,6 @@ import ( webhook "gopkg.in/go-playground/webhooks.v5/github" ) -func TestCreateLabelIfNotExists_GetLabelError(t *testing.T) { - origGithubImpl := GHImpl - defer func() { - GHImpl = origGithubImpl - }() - forcedError := fmt.Errorf("forced GetLabel error") - GHImpl = &GHInterfaceMock{ - IssuesMock: IssuesMock{ - mockGetLabelError: forcedError, - MockGetLabelResponse: &github.Response{ - Response: &http.Response{}, - }, - }, - } - - client := GHImpl.NewClient(nil) - - label, err := _createRepoLabelIfNotExists(zaptest.NewLogger(t), client.Issues, "", "", "", "", "") - assert.EqualError(t, err, forcedError.Error()) - assert.Nil(t, label) -} - -func TestCreateLabelIfNotExists_LabelExists(t *testing.T) { - origGithubImpl := GHImpl - defer func() { - GHImpl = origGithubImpl - }() - labelName := "we already got one" - existingLabel := &github.Label{Name: &labelName} - GHImpl = &GHInterfaceMock{ - IssuesMock: IssuesMock{ - mockGetLabel: existingLabel, - MockGetLabelResponse: &github.Response{ - Response: &http.Response{}, - }, - }, - } - - client := GHImpl.NewClient(nil) - - label, err := _createRepoLabelIfNotExists(zaptest.NewLogger(t), client.Issues, "", "", "", "", "") - assert.NoError(t, err) - assert.Equal(t, label, existingLabel) -} - -func TestCreateLabelIfNotExists_CreateError(t *testing.T) { - origGithubImpl := GHImpl - defer func() { - GHImpl = origGithubImpl - }() - forcedError := fmt.Errorf("forced CreateLabel error") - GHImpl = &GHInterfaceMock{IssuesMock: IssuesMock{ - MockGetLabelResponse: &github.Response{ - Response: &http.Response{StatusCode: http.StatusNotFound}, - }, - mockCreateLabelError: forcedError}, - } - client := GHImpl.NewClient(nil) - - label, err := _createRepoLabelIfNotExists(zaptest.NewLogger(t), client.Issues, "", "", "", "", "") - assert.EqualError(t, err, forcedError.Error()) - assert.Nil(t, label) -} - -func TestCreateLabelIfNotExists(t *testing.T) { - origGithubImpl := GHImpl - defer func() { - GHImpl = origGithubImpl - }() - labelName := labelNameCLANotSigned - labelColor := "fa3a3a" - labelDescription := "The CLA is not signed" - labelToCreate := &github.Label{Name: &labelName, Color: &labelColor, Description: &labelDescription} - GHImpl = &GHInterfaceMock{IssuesMock: IssuesMock{ - MockGetLabelResponse: &github.Response{ - Response: &http.Response{StatusCode: http.StatusNotFound}, - }, - mockCreateLabel: labelToCreate}, - } - - client := GHImpl.NewClient(nil) - - label, err := _createRepoLabelIfNotExists(zaptest.NewLogger(t), client.Issues, "", "", "", "", "") - assert.NoError(t, err) - assert.Equal(t, label, labelToCreate) -} - -func TestAddLabelToIssueIfNotExists_ListLabelsByIssueError(t *testing.T) { - origGithubImpl := GHImpl - defer func() { - GHImpl = origGithubImpl - }() - forcedError := fmt.Errorf("forced ListLabelsByIssue error") - GHImpl = &GHInterfaceMock{IssuesMock: IssuesMock{mockListLabelsByIssueError: forcedError}} - - client := GHImpl.NewClient(nil) - - label, err := _addLabelToIssueIfNotExists(zaptest.NewLogger(t), client.Issues, "", "", 0, "") - assert.EqualError(t, err, forcedError.Error()) - assert.Nil(t, label) -} - -func TestAddLabelToIssueIfNotExists_LabelAlreadyExists(t *testing.T) { - origGithubImpl := GHImpl - defer func() { - GHImpl = origGithubImpl - }() - labelName := labelNameCLANotSigned - existingLabel := &github.Label{Name: &labelName} - existingLabelList := []*github.Label{existingLabel} - GHImpl = &GHInterfaceMock{ - IssuesMock: IssuesMock{mockListLabelsByIssue: existingLabelList}, - } - - client := GHImpl.NewClient(nil) - - label, err := _addLabelToIssueIfNotExists(zaptest.NewLogger(t), client.Issues, "", "", 0, labelName) - assert.NoError(t, err) - assert.Equal(t, existingLabel, label) -} - -func Test_AddLabelToIssueIfNotExists_AddLabelError(t *testing.T) { - origGithubImpl := GHImpl - defer func() { - GHImpl = origGithubImpl - }() - forcedError := fmt.Errorf("forced AddLabels error") - GHImpl = &GHInterfaceMock{ - IssuesMock: IssuesMock{mockAddLabelsError: forcedError}, - } - - client := GHImpl.NewClient(nil) - - label, err := _addLabelToIssueIfNotExists(zaptest.NewLogger(t), client.Issues, "", "", 0, "") - assert.EqualError(t, err, forcedError.Error()) - assert.Nil(t, label) -} - -func Test_AddLabelToIssueIfNotExists(t *testing.T) { - origGithubImpl := GHImpl - defer func() { - GHImpl = origGithubImpl - }() - labelName := labelNameCLANotSigned - labelColor := "fa3a3a" - labelDescription := "The CLA is not signed" - labelToCreate := &github.Label{Name: &labelName, Color: &labelColor, Description: &labelDescription} - GHImpl = &GHInterfaceMock{IssuesMock: IssuesMock{mockAddLabels: []*github.Label{labelToCreate}}} - - client := GHImpl.NewClient(nil) - - label, err := _addLabelToIssueIfNotExists(zaptest.NewLogger(t), client.Issues, "", "", 0, labelNameCLANotSigned) - assert.NoError(t, err) - // real gitHub API returns different result, but does not matter to us now - assert.Nil(t, label) -} - type mockCLADb struct { t *testing.T assertParameters bool @@ -271,45 +114,137 @@ func (m mockCLADb) RemovePRsForUsers(usersSigned []types.UserSignature, evalInfo return m.removePRsError } -func TestHandlePullRequestIsCollaboratorError(t *testing.T) { - origGHAppIDEnvVar := os.Getenv(EnvGhAppId) +func TestWithJustGHImpl(t *testing.T) { + // Setup Code before tests + origGithubImpl := GHImpl defer func() { - resetEnvVariable(t, EnvGhAppId, origGHAppIDEnvVar) + GHImpl = origGithubImpl }() - assert.NoError(t, os.Setenv(EnvGhAppId, "-1")) - resetPemFileImpl := SetupTestPemFile(t) - defer resetPemFileImpl() + t.Run("TestCreateLabelIfNotExists", func(t *testing.T) { + labelName := labelNameCLANotSigned + labelColor := "fa3a3a" + labelDescription := "The CLA is not signed" + labelToCreate := &github.Label{Name: &labelName, Color: &labelColor, Description: &labelDescription} + GHImpl = &GHInterfaceMock{IssuesMock: IssuesMock{ + MockGetLabelResponse: &github.Response{ + Response: &http.Response{StatusCode: http.StatusNotFound}, + }, + mockCreateLabel: labelToCreate}, + } - resetGHJWTImpl := SetupMockGHJWT() - defer resetGHJWTImpl() + client := GHImpl.NewClient(nil) + label, err := _createRepoLabelIfNotExists(zaptest.NewLogger(t), client.Issues, "", "", "", "", "") + assert.NoError(t, err) + assert.Equal(t, label, labelToCreate) + }) - origGithubImpl := GHImpl - defer func() { - GHImpl = origGithubImpl - }() - mockAuthorLogin := "myAuthorLogin" - mockRepositoryCommits := getMockRepositoryCommitsSigned(mockAuthorLogin) - forcedError := fmt.Errorf("forced IsCollaborator error") - GHImpl = &GHInterfaceMock{ - PullRequestsMock: PullRequestsMock{ - mockRepositoryCommits: mockRepositoryCommits, - }, - RepositoriesMock: RepositoriesMock{ - isCollaboratorErr: forcedError, - }, - } + t.Run("TestCreateLabelIfNotExists_CreateError", func(t *testing.T) { + forcedError := fmt.Errorf("forced CreateLabel error") + GHImpl = &GHInterfaceMock{IssuesMock: IssuesMock{ + MockGetLabelResponse: &github.Response{ + Response: &http.Response{StatusCode: http.StatusNotFound}, + }, + mockCreateLabelError: forcedError}, + } + client := GHImpl.NewClient(nil) + label, err := _createRepoLabelIfNotExists(zaptest.NewLogger(t), client.Issues, "", "", "", "", "") + assert.EqualError(t, err, forcedError.Error()) + assert.Nil(t, label) + }) + + t.Run("TestCreateLabelIfNotExists_GetLabelError", func(t *testing.T) { + forcedError := fmt.Errorf("forced GetLabel error") + GHImpl = &GHInterfaceMock{ + IssuesMock: IssuesMock{ + mockGetLabelError: forcedError, + MockGetLabelResponse: &github.Response{ + Response: &http.Response{}, + }, + }, + } - prEvent := webhook.PullRequestPayload{} + client := GHImpl.NewClient(nil) + label, err := _createRepoLabelIfNotExists(zaptest.NewLogger(t), client.Issues, "", "", "", "", "") + assert.EqualError(t, err, forcedError.Error()) + assert.Nil(t, label) + }) + + t.Run("TestCreateLabelIfNotExists_LabelExists", func(t *testing.T) { + labelName := "we already got one" + existingLabel := &github.Label{Name: &labelName} + GHImpl = &GHInterfaceMock{ + IssuesMock: IssuesMock{ + mockGetLabel: existingLabel, + MockGetLabelResponse: &github.Response{ + Response: &http.Response{}, + }, + }, + } - mockDB, logger := setupMockDB(t, true) - mockDB.hasAuthorSignedLogin = mockAuthorLogin + client := GHImpl.NewClient(nil) + label, err := _createRepoLabelIfNotExists(zaptest.NewLogger(t), client.Issues, "", "", "", "", "") + assert.NoError(t, err) + assert.Equal(t, label, existingLabel) + }) + + t.Run("TestAddLabelToIssueIfNotExists", func(t *testing.T) { + labelName := labelNameCLANotSigned + labelColor := "fa3a3a" + labelDescription := "The CLA is not signed" + labelToCreate := &github.Label{Name: &labelName, Color: &labelColor, Description: &labelDescription} + GHImpl = &GHInterfaceMock{IssuesMock: IssuesMock{mockAddLabels: []*github.Label{labelToCreate}}} + + client := GHImpl.NewClient(nil) + + label, err := _addLabelToIssueIfNotExists(zaptest.NewLogger(t), client.Issues, "", "", 0, labelNameCLANotSigned) + assert.NoError(t, err) + // real gitHub API returns different result, but does not matter to us now + assert.Nil(t, label) + }) + + t.Run("TestAddLabelToIssueIfNotExists_ListLabelsByIssueError", func(t *testing.T) { + forcedError := fmt.Errorf("forced ListLabelsByIssue error") + GHImpl = &GHInterfaceMock{IssuesMock: IssuesMock{mockListLabelsByIssueError: forcedError}} + + client := GHImpl.NewClient(nil) + + label, err := _addLabelToIssueIfNotExists(zaptest.NewLogger(t), client.Issues, "", "", 0, "") + assert.EqualError(t, err, forcedError.Error()) + assert.Nil(t, label) + }) + + t.Run("TestAddLabelToIssueIfNotExists_AddLabelError", func(t *testing.T) { + forcedError := fmt.Errorf("forced AddLabels error") + GHImpl = &GHInterfaceMock{ + IssuesMock: IssuesMock{mockAddLabelsError: forcedError}, + } - err := HandlePullRequest(logger, mockDB, prEvent, 0, "") - assert.EqualError(t, err, forcedError.Error()) + client := GHImpl.NewClient(nil) + + label, err := _addLabelToIssueIfNotExists(zaptest.NewLogger(t), client.Issues, "", "", 0, "") + assert.EqualError(t, err, forcedError.Error()) + assert.Nil(t, label) + }) + + t.Run("TestAddLabelToIssueIfNotExists_LabelAlreadyExists", func(t *testing.T) { + labelName := labelNameCLANotSigned + existingLabel := &github.Label{Name: &labelName} + existingLabelList := []*github.Label{existingLabel} + GHImpl = &GHInterfaceMock{ + IssuesMock: IssuesMock{mockListLabelsByIssue: existingLabelList}, + } + + client := GHImpl.NewClient(nil) + + label, err := _addLabelToIssueIfNotExists(zaptest.NewLogger(t), client.Issues, "", "", 0, labelName) + assert.NoError(t, err) + assert.Equal(t, existingLabel, label) + }) } -func TestHandlePullRequestIsCollaboratorTrueCollaborator(t *testing.T) { +func TestWithFullEnvironment(t *testing.T) { + // Setup Code before tests origGHAppIDEnvVar := os.Getenv(EnvGhAppId) defer func() { resetEnvVariable(t, EnvGhAppId, origGHAppIDEnvVar) @@ -326,111 +261,226 @@ func TestHandlePullRequestIsCollaboratorTrueCollaborator(t *testing.T) { defer func() { GHImpl = origGithubImpl }() - mockAuthorLogin := "myAuthorLogin" - mockRepositoryCommits := getMockRepositoryCommitsSigned(mockAuthorLogin) - GHImpl = &GHInterfaceMock{ - PullRequestsMock: PullRequestsMock{ - mockRepositoryCommits: mockRepositoryCommits, - }, - RepositoriesMock: RepositoriesMock{ - isCollaboratorResult: true, - }, - IssuesMock: IssuesMock{ - //mockGetLabel: &github.Label{}, - MockGetLabelResponse: &github.Response{ - Response: &http.Response{}, + + t.Run("TestHandlePullRequestCreateLabelError", func(t *testing.T) { + mockAuthorLogin := "myAuthorLogin" + mockRepositoryCommits := getMockRepositoryCommitsSigned(mockAuthorLogin) + forcedError := fmt.Errorf("forced CreateLabel error") + GHImpl = &GHInterfaceMock{ + PullRequestsMock: PullRequestsMock{mockRepositoryCommits: mockRepositoryCommits}, + IssuesMock: IssuesMock{ + MockGetLabelResponse: &github.Response{Response: &http.Response{StatusCode: http.StatusNotFound}}, + mockCreateLabelError: forcedError, }, - MockRemoveLabelResponse: &github.Response{ - Response: &http.Response{}, + } + + prEvent := webhook.PullRequestPayload{} + + mockDB, logger := setupMockDB(t, true) + mockDB.hasAuthorSignedLogin = mockAuthorLogin + + err := HandlePullRequest(logger, mockDB, prEvent, 0, "") + assert.EqualError(t, err, forcedError.Error()) + }) + + t.Run("TestHandlePullRequestAddLabelsToIssueError", func(t *testing.T) { + mockAuthorLogin := "myAuthorLogin" + mockRepositoryCommits := getMockRepositoryCommitsSigned(mockAuthorLogin) + forcedError := fmt.Errorf("forced AddLabelsToIssue error") + GHImpl = &GHInterfaceMock{ + PullRequestsMock: PullRequestsMock{mockRepositoryCommits: mockRepositoryCommits}, + IssuesMock: IssuesMock{ + mockGetLabel: &github.Label{}, + mockAddLabelsError: forcedError, + MockGetLabelResponse: &github.Response{ + Response: &http.Response{}, + }, }, - }, - } + } - prEvent := webhook.PullRequestPayload{} + prEvent := webhook.PullRequestPayload{} - mockDB, logger := setupMockDB(t, true) - mockDB.hasAuthorSignedLogin = mockAuthorLogin - mockDB.removePRsEvalInfo = &types.EvaluationInfo{} + mockDB, logger := setupMockDB(t, true) + mockDB.hasAuthorSignedLogin = mockAuthorLogin - err := HandlePullRequest(logger, mockDB, prEvent, 0, "") - assert.NoError(t, err) -} + err := HandlePullRequest(logger, mockDB, prEvent, 0, "") + assert.EqualError(t, err, forcedError.Error()) + }) -func TestHandlePullRequestCreateLabelError(t *testing.T) { - origGHAppIDEnvVar := os.Getenv(EnvGhAppId) - defer func() { - resetEnvVariable(t, EnvGhAppId, origGHAppIDEnvVar) - }() - assert.NoError(t, os.Setenv(EnvGhAppId, "-1")) + t.Run("TestHandlePullRequestIsCollaboratorError", func(t *testing.T) { + mockAuthorLogin := "myAuthorLogin" + mockRepositoryCommits := getMockRepositoryCommitsSigned(mockAuthorLogin) + forcedError := fmt.Errorf("forced IsCollaborator error") + GHImpl = &GHInterfaceMock{ + PullRequestsMock: PullRequestsMock{ + mockRepositoryCommits: mockRepositoryCommits, + }, + RepositoriesMock: RepositoriesMock{ + isCollaboratorErr: forcedError, + }, + } - resetPemFileImpl := SetupTestPemFile(t) - defer resetPemFileImpl() + prEvent := webhook.PullRequestPayload{} - resetGHJWTImpl := SetupMockGHJWT() - defer resetGHJWTImpl() + mockDB, logger := setupMockDB(t, true) + mockDB.hasAuthorSignedLogin = mockAuthorLogin - origGithubImpl := GHImpl - defer func() { - GHImpl = origGithubImpl - }() - mockAuthorLogin := "myAuthorLogin" - mockRepositoryCommits := getMockRepositoryCommitsSigned(mockAuthorLogin) - forcedError := fmt.Errorf("forced CreateLabel error") - GHImpl = &GHInterfaceMock{ - PullRequestsMock: PullRequestsMock{mockRepositoryCommits: mockRepositoryCommits}, - IssuesMock: IssuesMock{ - MockGetLabelResponse: &github.Response{Response: &http.Response{StatusCode: http.StatusNotFound}}, - mockCreateLabelError: forcedError, - }, - } + err := HandlePullRequest(logger, mockDB, prEvent, 0, "") + assert.EqualError(t, err, forcedError.Error()) + }) - prEvent := webhook.PullRequestPayload{} + t.Run("TestHandlePullRequestIsCollaboratorTrueCollaborator", func(t *testing.T) { + mockAuthorLogin := "myAuthorLogin" + mockRepositoryCommits := getMockRepositoryCommitsSigned(mockAuthorLogin) + GHImpl = &GHInterfaceMock{ + PullRequestsMock: PullRequestsMock{ + mockRepositoryCommits: mockRepositoryCommits, + }, + RepositoriesMock: RepositoriesMock{ + isCollaboratorResult: true, + }, + IssuesMock: IssuesMock{ + //mockGetLabel: &github.Label{}, + MockGetLabelResponse: &github.Response{ + Response: &http.Response{}, + }, + MockRemoveLabelResponse: &github.Response{ + Response: &http.Response{}, + }, + }, + } - mockDB, logger := setupMockDB(t, true) - mockDB.hasAuthorSignedLogin = mockAuthorLogin + prEvent := webhook.PullRequestPayload{} - err := HandlePullRequest(logger, mockDB, prEvent, 0, "") - assert.EqualError(t, err, forcedError.Error()) -} + mockDB, logger := setupMockDB(t, true) + mockDB.hasAuthorSignedLogin = mockAuthorLogin + mockDB.removePRsEvalInfo = &types.EvaluationInfo{} -func TestHandlePullRequestAddLabelsToIssueError(t *testing.T) { - origGHAppIDEnvVar := os.Getenv(EnvGhAppId) - defer func() { - resetEnvVariable(t, EnvGhAppId, origGHAppIDEnvVar) - }() - assert.NoError(t, os.Setenv(EnvGhAppId, "-1")) + err := HandlePullRequest(logger, mockDB, prEvent, 0, "") + assert.NoError(t, err) + }) - resetPemFileImpl := SetupTestPemFile(t) - defer resetPemFileImpl() + t.Run("TestHandlePullRequestListCommitsError", func(t *testing.T) { + forcedError := fmt.Errorf("forced ListCommits error") + GHImpl = &GHInterfaceMock{ + RepositoriesMock: *setupMockRepositoriesService(t, false), + PullRequestsMock: PullRequestsMock{ + mockListCommitsError: forcedError, + }, + } - resetGHJWTImpl := SetupMockGHJWT() - defer resetGHJWTImpl() + prEvent := webhook.PullRequestPayload{} + mockDB, logger := setupMockDB(t, true) + err := HandlePullRequest(logger, mockDB, prEvent, 0, "") + assert.EqualError(t, err, forcedError.Error()) + }) + + t.Run("TestHandlePullRequestListCommits", func(t *testing.T) { + mockExternalUrl := "fakeExternalURL" + GHJWTImpl = &GHJWTMock{ + AppsMock: AppsMock{ + mockInstallation: &github.Installation{ + AppSlug: &appSlug, + }, + mockAppResp: &github.Response{Response: &http.Response{StatusCode: http.StatusOK}}, + mockApp: &github.App{ExternalURL: &mockExternalUrl}, + }, + } - origGithubImpl := GHImpl - defer func() { - GHImpl = origGithubImpl - }() - mockAuthorLogin := "myAuthorLogin" - mockRepositoryCommits := getMockRepositoryCommitsSigned(mockAuthorLogin) - forcedError := fmt.Errorf("forced AddLabelsToIssue error") - GHImpl = &GHInterfaceMock{ - PullRequestsMock: PullRequestsMock{mockRepositoryCommits: mockRepositoryCommits}, - IssuesMock: IssuesMock{ - mockGetLabel: &github.Label{}, - mockAddLabelsError: forcedError, - MockGetLabelResponse: &github.Response{ - Response: &http.Response{}, + origGithubImpl := GHImpl + defer func() { + GHImpl = origGithubImpl + }() + login := "john" + login2 := "doe" + mockRepositoryCommits := []*github.RepositoryCommit{ + { + Author: &github.User{ + Login: github.String(login), + Email: github.String("j@gmail.com"), + }, + Commit: &github.Commit{ + Verification: &github.SignatureVerification{ + Verified: github.Bool(true), + }, + }, + SHA: github.String("johnSHA"), }, - }, - } + { + Author: &github.User{ + Login: github.String(login2), + Email: github.String("d@gmail.com"), + }, + Commit: &github.Commit{ + Verification: &github.SignatureVerification{ + Verified: github.Bool(true), + }, + }, + SHA: github.String("doeSHA"), + }, + } + GHImpl = &GHInterfaceMock{ + PullRequestsMock: PullRequestsMock{ + mockRepositoryCommits: mockRepositoryCommits, + }, + IssuesMock: IssuesMock{ + mockGetLabel: &github.Label{}, + MockGetLabelResponse: &github.Response{ + Response: &http.Response{}, + }, + MockRemoveLabelResponse: &github.Response{ + Response: &http.Response{}, + }, + }, + } - prEvent := webhook.PullRequestPayload{} + prEvent := webhook.PullRequestPayload{} - mockDB, logger := setupMockDB(t, true) - mockDB.hasAuthorSignedLogin = mockAuthorLogin + mockDB, logger := setupMockDB(t, false) + err := HandlePullRequest(logger, mockDB, prEvent, 0, "") + assert.NoError(t, err) + }) - err := HandlePullRequest(logger, mockDB, prEvent, 0, "") - assert.EqualError(t, err, forcedError.Error()) + t.Run("TestHandlePullRequestListCommitsUnsignedCommit", func(t *testing.T) { + mockRepositoryCommits := []*github.RepositoryCommit{ + { + Commit: &github.Commit{ + Author: &github.CommitAuthor{ + Name: github.String("someuser"), + Email: github.String("someuser@some.where.tld"), + }, + Verification: &github.SignatureVerification{ + Verified: github.Bool(false), + Reason: github.String("unsigned"), + Signature: nil, + Payload: nil, + }, + }, + SHA: github.String("johnSHA"), + HTMLURL: github.String("https://github.com"), + }, + } + GHImpl = &GHInterfaceMock{ + PullRequestsMock: PullRequestsMock{ + mockRepositoryCommits: mockRepositoryCommits, + }, + IssuesMock: IssuesMock{ + mockGetLabel: &github.Label{}, + MockGetLabelResponse: &github.Response{ + Response: &http.Response{}, + }, + MockRemoveLabelResponse: &github.Response{ + Response: &http.Response{}, + }, + }, + } + + prEvent := webhook.PullRequestPayload{} + + mockDB, logger := setupMockDB(t, false) + err := HandlePullRequest(logger, mockDB, prEvent, 0, "") + assert.NoError(t, err) + }) } func TestHandlePullRequestGetAppError(t *testing.T) { @@ -516,114 +566,6 @@ func TestHandlePullRequestMissingPemFile(t *testing.T) { assert.EqualError(t, err, "could not read private key: open the-cla.pem: no such file or directory") } -func TestHandlePullRequestListCommitsError(t *testing.T) { - origGHAppIDEnvVar := os.Getenv(EnvGhAppId) - defer func() { - resetEnvVariable(t, EnvGhAppId, origGHAppIDEnvVar) - }() - assert.NoError(t, os.Setenv(EnvGhAppId, "-1")) - - resetPemFileImpl := SetupTestPemFile(t) - defer resetPemFileImpl() - - resetGHJWTImpl := SetupMockGHJWT() - defer resetGHJWTImpl() - - origGithubImpl := GHImpl - defer func() { - GHImpl = origGithubImpl - }() - forcedError := fmt.Errorf("forced ListCommits error") - GHImpl = &GHInterfaceMock{ - RepositoriesMock: *setupMockRepositoriesService(t, false), - PullRequestsMock: PullRequestsMock{ - mockListCommitsError: forcedError, - }, - } - - prEvent := webhook.PullRequestPayload{} - mockDB, logger := setupMockDB(t, true) - err := HandlePullRequest(logger, mockDB, prEvent, 0, "") - assert.EqualError(t, err, forcedError.Error()) -} - -func TestHandlePullRequestListCommits(t *testing.T) { - origGHAppIDEnvVar := os.Getenv(EnvGhAppId) - defer func() { - resetEnvVariable(t, EnvGhAppId, origGHAppIDEnvVar) - }() - assert.NoError(t, os.Setenv(EnvGhAppId, "-1")) - - resetPemFileImpl := SetupTestPemFile(t) - defer resetPemFileImpl() - - resetGHJWTImpl := SetupMockGHJWT() - defer resetGHJWTImpl() - mockExternalUrl := "fakeExternalURL" - GHJWTImpl = &GHJWTMock{ - AppsMock: AppsMock{ - mockInstallation: &github.Installation{ - AppSlug: &appSlug, - }, - mockAppResp: &github.Response{Response: &http.Response{StatusCode: http.StatusOK}}, - mockApp: &github.App{ExternalURL: &mockExternalUrl}, - }, - } - - origGithubImpl := GHImpl - defer func() { - GHImpl = origGithubImpl - }() - login := "john" - login2 := "doe" - mockRepositoryCommits := []*github.RepositoryCommit{ - { - Author: &github.User{ - Login: github.String(login), - Email: github.String("j@gmail.com"), - }, - Commit: &github.Commit{ - Verification: &github.SignatureVerification{ - Verified: github.Bool(true), - }, - }, - SHA: github.String("johnSHA"), - }, - { - Author: &github.User{ - Login: github.String(login2), - Email: github.String("d@gmail.com"), - }, - Commit: &github.Commit{ - Verification: &github.SignatureVerification{ - Verified: github.Bool(true), - }, - }, - SHA: github.String("doeSHA"), - }, - } - GHImpl = &GHInterfaceMock{ - PullRequestsMock: PullRequestsMock{ - mockRepositoryCommits: mockRepositoryCommits, - }, - IssuesMock: IssuesMock{ - mockGetLabel: &github.Label{}, - MockGetLabelResponse: &github.Response{ - Response: &http.Response{}, - }, - MockRemoveLabelResponse: &github.Response{ - Response: &http.Response{}, - }, - }, - } - - prEvent := webhook.PullRequestPayload{} - - mockDB, logger := setupMockDB(t, false) - err := HandlePullRequest(logger, mockDB, prEvent, 0, "") - assert.NoError(t, err) -} - func TestHandlePullRequestListCommitsNoAuthor(t *testing.T) { origGHAppIDEnvVar := os.Getenv(EnvGhAppId) defer func() { @@ -686,73 +628,6 @@ func TestHandlePullRequestListCommitsNoAuthor(t *testing.T) { assert.NoError(t, err) } -func TestHandlePullRequestListCommitsUnsignedCommit(t *testing.T) { - origGHAppIDEnvVar := os.Getenv(EnvGhAppId) - defer func() { - resetEnvVariable(t, EnvGhAppId, origGHAppIDEnvVar) - }() - assert.NoError(t, os.Setenv(EnvGhAppId, "-1")) - - resetPemFileImpl := SetupTestPemFile(t) - defer resetPemFileImpl() - - resetGHJWTImpl := SetupMockGHJWT() - defer resetGHJWTImpl() - mockExternalUrl := "fakeExternalURL" - GHJWTImpl = &GHJWTMock{ - AppsMock: AppsMock{ - mockInstallation: &github.Installation{ - AppSlug: &appSlug, - }, - mockAppResp: &github.Response{Response: &http.Response{StatusCode: http.StatusOK}}, - mockApp: &github.App{ExternalURL: &mockExternalUrl}, - }, - } - - origGithubImpl := GHImpl - defer func() { - GHImpl = origGithubImpl - }() - mockRepositoryCommits := []*github.RepositoryCommit{ - { - Commit: &github.Commit{ - Author: &github.CommitAuthor{ - Name: github.String("someuser"), - Email: github.String("someuser@some.where.tld"), - }, - Verification: &github.SignatureVerification{ - Verified: github.Bool(false), - Reason: github.String("unsigned"), - Signature: nil, - Payload: nil, - }, - }, - SHA: github.String("johnSHA"), - HTMLURL: github.String("https://github.com"), - }, - } - GHImpl = &GHInterfaceMock{ - PullRequestsMock: PullRequestsMock{ - mockRepositoryCommits: mockRepositoryCommits, - }, - IssuesMock: IssuesMock{ - mockGetLabel: &github.Label{}, - MockGetLabelResponse: &github.Response{ - Response: &http.Response{}, - }, - MockRemoveLabelResponse: &github.Response{ - Response: &http.Response{}, - }, - }, - } - - prEvent := webhook.PullRequestPayload{} - - mockDB, logger := setupMockDB(t, false) - err := HandlePullRequest(logger, mockDB, prEvent, 0, "") - assert.NoError(t, err) -} - func Test_removeLabelFromIssueIfExists_Removed(t *testing.T) { issuesMock := &IssuesMock{ MockRemoveLabelResponse: &github.Response{ From a3b2790ecb717b1396e1a86d5fb6c5d3774eb0e9 Mon Sep 17 00:00:00 2001 From: Paul Horton Date: Tue, 17 Sep 2024 12:58:27 +0100 Subject: [PATCH 05/10] chore: address code duplication in tests - one test failing... Signed-off-by: Paul Horton --- github/github_test.go | 255 +++++++++++++++++++----------------------- 1 file changed, 115 insertions(+), 140 deletions(-) diff --git a/github/github_test.go b/github/github_test.go index 8c0f05e..13a4bed 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -263,97 +263,80 @@ func TestWithFullEnvironment(t *testing.T) { }() t.Run("TestHandlePullRequestCreateLabelError", func(t *testing.T) { - mockAuthorLogin := "myAuthorLogin" - mockRepositoryCommits := getMockRepositoryCommitsSigned(mockAuthorLogin) + authors := []string{"myAuthorLogin"} forcedError := fmt.Errorf("forced CreateLabel error") - GHImpl = &GHInterfaceMock{ - PullRequestsMock: PullRequestsMock{mockRepositoryCommits: mockRepositoryCommits}, - IssuesMock: IssuesMock{ - MockGetLabelResponse: &github.Response{Response: &http.Response{StatusCode: http.StatusNotFound}}, - mockCreateLabelError: forcedError, - }, + issuesMock := IssuesMock{ + MockGetLabelResponse: &github.Response{Response: &http.Response{StatusCode: http.StatusNotFound}}, + mockCreateLabelError: forcedError, } + GHImpl = getGHMock( + getMockRepositoryCommits(authors, true), + &issuesMock, + nil, + ) prEvent := webhook.PullRequestPayload{} mockDB, logger := setupMockDB(t, true) - mockDB.hasAuthorSignedLogin = mockAuthorLogin + mockDB.hasAuthorSignedLogin = authors[0] err := HandlePullRequest(logger, mockDB, prEvent, 0, "") assert.EqualError(t, err, forcedError.Error()) }) t.Run("TestHandlePullRequestAddLabelsToIssueError", func(t *testing.T) { - mockAuthorLogin := "myAuthorLogin" - mockRepositoryCommits := getMockRepositoryCommitsSigned(mockAuthorLogin) + authors := []string{"myAuthorLogin2"} forcedError := fmt.Errorf("forced AddLabelsToIssue error") - GHImpl = &GHInterfaceMock{ - PullRequestsMock: PullRequestsMock{mockRepositoryCommits: mockRepositoryCommits}, - IssuesMock: IssuesMock{ - mockGetLabel: &github.Label{}, - mockAddLabelsError: forcedError, - MockGetLabelResponse: &github.Response{ - Response: &http.Response{}, - }, + issuesMock := IssuesMock{ + mockGetLabel: &github.Label{}, + mockAddLabelsError: forcedError, + MockGetLabelResponse: &github.Response{ + Response: &http.Response{}, }, } - + GHImpl = getGHMock( + getMockRepositoryCommits(authors, true), + &issuesMock, + nil, + ) prEvent := webhook.PullRequestPayload{} mockDB, logger := setupMockDB(t, true) - mockDB.hasAuthorSignedLogin = mockAuthorLogin + mockDB.hasAuthorSignedLogin = authors[0] err := HandlePullRequest(logger, mockDB, prEvent, 0, "") assert.EqualError(t, err, forcedError.Error()) }) t.Run("TestHandlePullRequestIsCollaboratorError", func(t *testing.T) { - mockAuthorLogin := "myAuthorLogin" - mockRepositoryCommits := getMockRepositoryCommitsSigned(mockAuthorLogin) + authors := []string{"myAuthorLogin3"} forcedError := fmt.Errorf("forced IsCollaborator error") - GHImpl = &GHInterfaceMock{ - PullRequestsMock: PullRequestsMock{ - mockRepositoryCommits: mockRepositoryCommits, - }, - RepositoriesMock: RepositoriesMock{ - isCollaboratorErr: forcedError, - }, + repositoriesMock := RepositoriesMock{ + isCollaboratorErr: forcedError, } + GHImpl = getGHMock( + getMockRepositoryCommits(authors, true), + nil, + &repositoriesMock, + ) prEvent := webhook.PullRequestPayload{} mockDB, logger := setupMockDB(t, true) - mockDB.hasAuthorSignedLogin = mockAuthorLogin + mockDB.hasAuthorSignedLogin = authors[0] err := HandlePullRequest(logger, mockDB, prEvent, 0, "") assert.EqualError(t, err, forcedError.Error()) }) t.Run("TestHandlePullRequestIsCollaboratorTrueCollaborator", func(t *testing.T) { - mockAuthorLogin := "myAuthorLogin" - mockRepositoryCommits := getMockRepositoryCommitsSigned(mockAuthorLogin) - GHImpl = &GHInterfaceMock{ - PullRequestsMock: PullRequestsMock{ - mockRepositoryCommits: mockRepositoryCommits, - }, - RepositoriesMock: RepositoriesMock{ - isCollaboratorResult: true, - }, - IssuesMock: IssuesMock{ - //mockGetLabel: &github.Label{}, - MockGetLabelResponse: &github.Response{ - Response: &http.Response{}, - }, - MockRemoveLabelResponse: &github.Response{ - Response: &http.Response{}, - }, - }, - } + authors := []string{"anAuthor4"} + GHImpl = getGHMock(getMockRepositoryCommits(authors, true), nil, nil) prEvent := webhook.PullRequestPayload{} mockDB, logger := setupMockDB(t, true) - mockDB.hasAuthorSignedLogin = mockAuthorLogin + mockDB.hasAuthorSignedLogin = authors[0] mockDB.removePRsEvalInfo = &types.EvaluationInfo{} err := HandlePullRequest(logger, mockDB, prEvent, 0, "") @@ -369,6 +352,8 @@ func TestWithFullEnvironment(t *testing.T) { }, } + // GHImpl = getGHMock(nil, nil, setupMockRepositoriesService(t, false)) + prEvent := webhook.PullRequestPayload{} mockDB, logger := setupMockDB(t, true) err := HandlePullRequest(logger, mockDB, prEvent, 0, "") @@ -391,90 +376,17 @@ func TestWithFullEnvironment(t *testing.T) { defer func() { GHImpl = origGithubImpl }() - login := "john" - login2 := "doe" - mockRepositoryCommits := []*github.RepositoryCommit{ - { - Author: &github.User{ - Login: github.String(login), - Email: github.String("j@gmail.com"), - }, - Commit: &github.Commit{ - Verification: &github.SignatureVerification{ - Verified: github.Bool(true), - }, - }, - SHA: github.String("johnSHA"), - }, - { - Author: &github.User{ - Login: github.String(login2), - Email: github.String("d@gmail.com"), - }, - Commit: &github.Commit{ - Verification: &github.SignatureVerification{ - Verified: github.Bool(true), - }, - }, - SHA: github.String("doeSHA"), - }, - } - GHImpl = &GHInterfaceMock{ - PullRequestsMock: PullRequestsMock{ - mockRepositoryCommits: mockRepositoryCommits, - }, - IssuesMock: IssuesMock{ - mockGetLabel: &github.Label{}, - MockGetLabelResponse: &github.Response{ - Response: &http.Response{}, - }, - MockRemoveLabelResponse: &github.Response{ - Response: &http.Response{}, - }, - }, - } - + authors := []string{"john", "doe"} + GHImpl = getGHMock(getMockRepositoryCommits(authors, true), nil, nil) prEvent := webhook.PullRequestPayload{} - mockDB, logger := setupMockDB(t, false) err := HandlePullRequest(logger, mockDB, prEvent, 0, "") assert.NoError(t, err) }) t.Run("TestHandlePullRequestListCommitsUnsignedCommit", func(t *testing.T) { - mockRepositoryCommits := []*github.RepositoryCommit{ - { - Commit: &github.Commit{ - Author: &github.CommitAuthor{ - Name: github.String("someuser"), - Email: github.String("someuser@some.where.tld"), - }, - Verification: &github.SignatureVerification{ - Verified: github.Bool(false), - Reason: github.String("unsigned"), - Signature: nil, - Payload: nil, - }, - }, - SHA: github.String("johnSHA"), - HTMLURL: github.String("https://github.com"), - }, - } - GHImpl = &GHInterfaceMock{ - PullRequestsMock: PullRequestsMock{ - mockRepositoryCommits: mockRepositoryCommits, - }, - IssuesMock: IssuesMock{ - mockGetLabel: &github.Label{}, - MockGetLabelResponse: &github.Response{ - Response: &http.Response{}, - }, - MockRemoveLabelResponse: &github.Response{ - Response: &http.Response{}, - }, - }, - } - + authors := []string{"john", "doe"} + GHImpl = getGHMock(getMockRepositoryCommits(authors, false), nil, nil) prEvent := webhook.PullRequestPayload{} mockDB, logger := setupMockDB(t, false) @@ -510,8 +422,8 @@ func TestHandlePullRequestGetAppError(t *testing.T) { defer func() { GHImpl = origGithubImpl }() - mockAuthorLogin := "myAuthorLogin" - mockRepositoryCommits := getMockRepositoryCommitsSigned(mockAuthorLogin) + authors := []string{"myAuthorLogin"} + mockRepositoryCommits := getMockRepositoryCommits(authors, true) GHImpl = &GHInterfaceMock{ PullRequestsMock: PullRequestsMock{mockRepositoryCommits: mockRepositoryCommits}, IssuesMock: IssuesMock{ @@ -528,12 +440,13 @@ func TestHandlePullRequestGetAppError(t *testing.T) { prEvent := webhook.PullRequestPayload{} mockDB, logger := setupMockDB(t, true) - mockDB.hasAuthorSignedLogin = mockAuthorLogin + mockDB.hasAuthorSignedLogin = authors[0] mockDB.storeUsersNeedingToSignEvalInfo = &types.EvaluationInfo{ UserSignatures: []types.UserSignature{ { User: types.User{ - Login: mockAuthorLogin, + Login: authors[0], + Email: "myAuthorLogin@somewhere.tld", }, }, }, @@ -797,18 +710,80 @@ func TestReviewPriorPRs(t *testing.T) { assert.NoError(t, ReviewPriorPRs(logger, mockDB, &user)) } -func getMockRepositoryCommitsSigned(mockAuthorLogin string) []*github.RepositoryCommit { - mockRepositoryCommits := []*github.RepositoryCommit{ - { +func getSignedSignatureVerification() *github.SignatureVerification { + return &github.SignatureVerification{ + Verified: github.Bool(true), + Reason: github.String("valid"), + Signature: github.String("some-signature"), + Payload: github.String("some-payload"), + } +} + +func getUnsignedSignatureVerification() *github.SignatureVerification { + return &github.SignatureVerification{ + Verified: github.Bool(false), + Reason: github.String("unsigned"), + Signature: nil, + Payload: nil, + } +} + +func getMockRepositoryCommits(mockAuthorLogins []string, signed bool) []*github.RepositoryCommit { + mockRepositoryCommits := make([]*github.RepositoryCommit, 0) + + for _, author := range mockAuthorLogins { + email := fmt.Sprintf("%s@somewhere.tld", author) + var signatureVerification *github.SignatureVerification = getSignedSignatureVerification() + if signed == false { + signatureVerification = getUnsignedSignatureVerification() + } + + commit := github.RepositoryCommit{ Author: &github.User{ - Login: &mockAuthorLogin, + Login: &author, + Email: &email, }, Commit: &github.Commit{ - Verification: &github.SignatureVerification{ - Verified: github.Bool(true), - }, + Verification: signatureVerification, }, - }, + HTMLURL: github.String("https://github.com"), + SHA: github.String(author + "SHA"), + } + mockRepositoryCommits = append(mockRepositoryCommits, &commit) } return mockRepositoryCommits } + +func getRepositoriesMock(forcedError error) RepositoriesMock { + return RepositoriesMock{ + isCollaboratorErr: forcedError, + } +} + +func getGHMock(repo_commits []*github.RepositoryCommit, issues_mock *IssuesMock, repositories_mock *RepositoriesMock) *GHInterfaceMock { + mock := &GHInterfaceMock{ + PullRequestsMock: PullRequestsMock{ + mockRepositoryCommits: repo_commits, + }, + } + + if issues_mock != nil { + mock.IssuesMock = *issues_mock + } else { + mock.IssuesMock = IssuesMock{ + mockGetLabel: &github.Label{}, + MockGetLabelResponse: &github.Response{ + Response: &http.Response{}, + }, + MockRemoveLabelResponse: &github.Response{ + Response: &http.Response{}, + }, + } + } + + if repositories_mock != nil { + mock.RepositoriesMock = *repositories_mock + } + + return mock +} From 8d50c332387bf1e885b2b7ecab86f6a38394238b Mon Sep 17 00:00:00 2001 From: Paul Horton Date: Tue, 17 Sep 2024 13:09:39 +0100 Subject: [PATCH 06/10] resolved test error Signed-off-by: Paul Horton --- github/github_test.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/github/github_test.go b/github/github_test.go index 13a4bed..bc958a5 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -331,7 +331,18 @@ func TestWithFullEnvironment(t *testing.T) { t.Run("TestHandlePullRequestIsCollaboratorTrueCollaborator", func(t *testing.T) { authors := []string{"anAuthor4"} - GHImpl = getGHMock(getMockRepositoryCommits(authors, true), nil, nil) + issuesMock := IssuesMock{ + MockGetLabelResponse: &github.Response{ + Response: &http.Response{}, + }, + MockRemoveLabelResponse: &github.Response{ + Response: &http.Response{}, + }, + } + repositoriesMock := RepositoriesMock{ + isCollaboratorResult: true, + } + GHImpl = getGHMock(getMockRepositoryCommits(authors, true), &issuesMock, &repositoriesMock) prEvent := webhook.PullRequestPayload{} From 6d0d45ba1e8e56ba057c7ffefbb9baf367e826c9 Mon Sep 17 00:00:00 2001 From: Paul Horton Date: Tue, 17 Sep 2024 13:13:36 +0100 Subject: [PATCH 07/10] lint errors resolved Signed-off-by: Paul Horton --- github/github.go | 2 +- github/github_test.go | 6 ------ 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/github/github.go b/github/github.go index 21147d7..1c42cc2 100644 --- a/github/github.go +++ b/github/github.go @@ -267,7 +267,7 @@ func EvaluatePullRequest(logger *zap.Logger, postgres db.IClaDB, evalInfo *types if commitVerification != nil { commitVerified = *v.Commit.Verification.Verified } - if commitVerified == false { + if !commitVerified { commitsMissingVerification = append(commitsMissingVerification, *v) commitFailedChecks = true logger.Debug("Commit failed verification check", zap.Any("Commit", v)) diff --git a/github/github_test.go b/github/github_test.go index bc958a5..f7b9087 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -765,12 +765,6 @@ func getMockRepositoryCommits(mockAuthorLogins []string, signed bool) []*github. return mockRepositoryCommits } -func getRepositoriesMock(forcedError error) RepositoriesMock { - return RepositoriesMock{ - isCollaboratorErr: forcedError, - } -} - func getGHMock(repo_commits []*github.RepositoryCommit, issues_mock *IssuesMock, repositories_mock *RepositoriesMock) *GHInterfaceMock { mock := &GHInterfaceMock{ PullRequestsMock: PullRequestsMock{ From 7128d657eac82ba283dede45dff3d1afd6857382 Mon Sep 17 00:00:00 2001 From: Paul Horton Date: Tue, 17 Sep 2024 13:16:11 +0100 Subject: [PATCH 08/10] code style erorrs reported by SonarCloud Signed-off-by: Paul Horton --- github/github_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/github/github_test.go b/github/github_test.go index f7b9087..b76be75 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -765,15 +765,15 @@ func getMockRepositoryCommits(mockAuthorLogins []string, signed bool) []*github. return mockRepositoryCommits } -func getGHMock(repo_commits []*github.RepositoryCommit, issues_mock *IssuesMock, repositories_mock *RepositoriesMock) *GHInterfaceMock { +func getGHMock(repoCommits []*github.RepositoryCommit, issuesMock *IssuesMock, repositoriesMock *RepositoriesMock) *GHInterfaceMock { mock := &GHInterfaceMock{ PullRequestsMock: PullRequestsMock{ - mockRepositoryCommits: repo_commits, + mockRepositoryCommits: repoCommits, }, } - if issues_mock != nil { - mock.IssuesMock = *issues_mock + if issuesMock != nil { + mock.IssuesMock = *issuesMock } else { mock.IssuesMock = IssuesMock{ mockGetLabel: &github.Label{}, @@ -786,8 +786,8 @@ func getGHMock(repo_commits []*github.RepositoryCommit, issues_mock *IssuesMock, } } - if repositories_mock != nil { - mock.RepositoriesMock = *repositories_mock + if repositoriesMock != nil { + mock.RepositoriesMock = *repositoriesMock } return mock From 30eea51408f3d34b274487ccdb0119bbfa32d851 Mon Sep 17 00:00:00 2001 From: Paul Horton Date: Tue, 17 Sep 2024 16:46:05 +0100 Subject: [PATCH 09/10] Update github/github_test.go Co-authored-by: Dan Rollo Signed-off-by: Paul Horton --- github/github_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/github_test.go b/github/github_test.go index b76be75..c21cae3 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -744,7 +744,7 @@ func getMockRepositoryCommits(mockAuthorLogins []string, signed bool) []*github. for _, author := range mockAuthorLogins { email := fmt.Sprintf("%s@somewhere.tld", author) - var signatureVerification *github.SignatureVerification = getSignedSignatureVerification() + var signatureVerification = getSignedSignatureVerification() if signed == false { signatureVerification = getUnsignedSignatureVerification() } From bdb1472e971ad60cb9e97caf534c443813f3ce2b Mon Sep 17 00:00:00 2001 From: Dan Rollo Date: Tue, 17 Sep 2024 15:07:36 -0400 Subject: [PATCH 10/10] inline some code to see if it helps with the duplicate code warnings --- github/github_test.go | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/github/github_test.go b/github/github_test.go index c21cae3..573c69b 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -299,12 +299,10 @@ func TestWithFullEnvironment(t *testing.T) { &issuesMock, nil, ) - prEvent := webhook.PullRequestPayload{} - mockDB, logger := setupMockDB(t, true) mockDB.hasAuthorSignedLogin = authors[0] - err := HandlePullRequest(logger, mockDB, prEvent, 0, "") + err := HandlePullRequest(logger, mockDB, webhook.PullRequestPayload{}, 0, "") assert.EqualError(t, err, forcedError.Error()) }) @@ -320,12 +318,10 @@ func TestWithFullEnvironment(t *testing.T) { &repositoriesMock, ) - prEvent := webhook.PullRequestPayload{} - mockDB, logger := setupMockDB(t, true) mockDB.hasAuthorSignedLogin = authors[0] - err := HandlePullRequest(logger, mockDB, prEvent, 0, "") + err := HandlePullRequest(logger, mockDB, webhook.PullRequestPayload{}, 0, "") assert.EqualError(t, err, forcedError.Error()) }) @@ -344,13 +340,11 @@ func TestWithFullEnvironment(t *testing.T) { } GHImpl = getGHMock(getMockRepositoryCommits(authors, true), &issuesMock, &repositoriesMock) - prEvent := webhook.PullRequestPayload{} - mockDB, logger := setupMockDB(t, true) mockDB.hasAuthorSignedLogin = authors[0] mockDB.removePRsEvalInfo = &types.EvaluationInfo{} - err := HandlePullRequest(logger, mockDB, prEvent, 0, "") + err := HandlePullRequest(logger, mockDB, webhook.PullRequestPayload{}, 0, "") assert.NoError(t, err) }) @@ -365,9 +359,8 @@ func TestWithFullEnvironment(t *testing.T) { // GHImpl = getGHMock(nil, nil, setupMockRepositoriesService(t, false)) - prEvent := webhook.PullRequestPayload{} mockDB, logger := setupMockDB(t, true) - err := HandlePullRequest(logger, mockDB, prEvent, 0, "") + err := HandlePullRequest(logger, mockDB, webhook.PullRequestPayload{}, 0, "") assert.EqualError(t, err, forcedError.Error()) }) @@ -389,19 +382,16 @@ func TestWithFullEnvironment(t *testing.T) { }() authors := []string{"john", "doe"} GHImpl = getGHMock(getMockRepositoryCommits(authors, true), nil, nil) - prEvent := webhook.PullRequestPayload{} mockDB, logger := setupMockDB(t, false) - err := HandlePullRequest(logger, mockDB, prEvent, 0, "") + err := HandlePullRequest(logger, mockDB, webhook.PullRequestPayload{}, 0, "") assert.NoError(t, err) }) t.Run("TestHandlePullRequestListCommitsUnsignedCommit", func(t *testing.T) { authors := []string{"john", "doe"} GHImpl = getGHMock(getMockRepositoryCommits(authors, false), nil, nil) - prEvent := webhook.PullRequestPayload{} - mockDB, logger := setupMockDB(t, false) - err := HandlePullRequest(logger, mockDB, prEvent, 0, "") + err := HandlePullRequest(logger, mockDB, webhook.PullRequestPayload{}, 0, "") assert.NoError(t, err) }) }