diff --git a/cmd/kosli/attestPRGithub_test.go b/cmd/kosli/attestPRGithub_test.go index 9b9ea95c1..0e899c4a0 100644 --- a/cmd/kosli/attestPRGithub_test.go +++ b/cmd/kosli/attestPRGithub_test.go @@ -5,7 +5,9 @@ import ( "testing" ghUtils "github.com/kosli-dev/cli/internal/github" + "github.com/kosli-dev/cli/internal/gitview" "github.com/kosli-dev/cli/internal/types" + "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" ) @@ -23,8 +25,13 @@ func (suite *AttestGithubPRCommandTestSuite) SetupTest() { suite.flowName = "attest-github-pr" suite.trailName = "test-123" suite.artifactFingerprint = "7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9" - suite.commitWithPR = "480e5a00379a52b8e184d6815080242a878ca295" - suite.commitWithNoPR = "7d1db1c8b7e71ee0ce369f1b722cc8844d3a7af6" + + gv, err := gitview.New("../..") + require.NoError(suite.T(), err) + suite.commitWithPR, err = gv.ResolveRevision("HEAD") + require.NoError(suite.T(), err) + suite.commitWithNoPR, err = gv.ResolveRevision("HEAD~1") + require.NoError(suite.T(), err) ghUtils.NewGithubRetrieverFunc = func(token, baseURL, org, repository string) types.PRRetriever { return &ghUtils.FakeGitHubClient{ diff --git a/internal/github/github_test.go b/internal/github/github_test.go index cf72c8799..38bf8e0f1 100644 --- a/internal/github/github_test.go +++ b/internal/github/github_test.go @@ -2,10 +2,8 @@ package github import ( "context" - "os" "testing" - "github.com/kosli-dev/cli/internal/testHelpers" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" ) @@ -40,185 +38,6 @@ func (suite *GithubTestSuite) TestNewGithubClientFromToken() { } } -func (suite *GithubTestSuite) TestPREvidenceForCommit() { - type result struct { - wantError bool - numberOfPRs int - } - for _, t := range []struct { - name string - config *GithubConfig - commit string - requireEnvVars bool // indicates that a test case needs real credentials from env vars - result result - }{ - { - name: "invalid token causes an error", - config: &GithubConfig{ - Token: "some_fake_token", - Org: "kosli-dev", - Repository: "cli", - }, - result: result{ - wantError: true, - }, - }, - { - name: "can list pull requests for a commit.", - config: &GithubConfig{ - Org: "kosli-dev", - Repository: "cli", - }, - requireEnvVars: true, - result: result{ - numberOfPRs: 1, - }, - }, - { - name: "non-existing commit will cause an error.", - config: &GithubConfig{ - Org: "kosli-dev", - Repository: "cli", - }, - commit: "73d7fee2f31ade8e1a9c456c324255212c3tf45a", - requireEnvVars: true, - result: result{ - wantError: true, - }, - }, - } { - suite.Run(t.name, func() { - if t.requireEnvVars { - testHelpers.SkipIfEnvVarUnset(suite.T(), []string{"KOSLI_GITHUB_TOKEN"}) - t.config.Token = os.Getenv("KOSLI_GITHUB_TOKEN") - } - if t.commit == "" { - t.commit = testHelpers.GithubCommitWithPR() - } - prs, err := t.config.PREvidenceForCommitV2(t.commit) - if t.result.wantError { - require.Errorf(suite.T(), err, "expected an error but got: %s", err) - } else { - require.NoErrorf(suite.T(), err, "was NOT expecting error but got: %s", err) - require.Len(suite.T(), prs, t.result.numberOfPRs) - } - }) - } -} - -func (suite *GithubTestSuite) TestPullRequestsForCommit() { - type result struct { - wantError bool - numberOfPRs int - } - for _, t := range []struct { - name string - ghOrg string - repository string - commit string - result result - }{ - { - name: "can list pull requests for a commit.", - ghOrg: "kosli-dev", - repository: "cli", - result: result{ - wantError: false, - numberOfPRs: 1, - }, - }, - { - name: "non-existing commit will cause an error.", - ghOrg: "kosli-dev", - repository: "cli", - commit: "73d7fee2f31ade8e1a9c456c324255212c3tf45a", - result: result{ - wantError: true, - }, - }, - } { - suite.Run(t.name, func() { - testHelpers.SkipIfEnvVarUnset(suite.T(), []string{"KOSLI_GITHUB_TOKEN"}) - token := os.Getenv("KOSLI_GITHUB_TOKEN") - c := &GithubConfig{ - Token: token, - Org: t.ghOrg, - Repository: t.repository, - } - if t.commit == "" { - t.commit = testHelpers.GithubCommitWithPR() - } - prs, err := c.PullRequestsForCommit(t.commit) - if t.result.wantError { - require.Errorf(suite.T(), err, "expected an error but got: %s", err) - } else { - require.NoErrorf(suite.T(), err, "was NOT expecting error but got: %s", err) - require.Lenf(suite.T(), prs, t.result.numberOfPRs, "expected %d PRs but got %d", t.result.numberOfPRs, len(prs)) - } - }) - } -} - -func (suite *GithubTestSuite) TestGetPullRequestApprovers() { - type result struct { - wantError bool - approvers []string - } - for _, t := range []struct { - name string - ghOrg string - repository string - number int - result result - }{ - { - name: "get an empty list for a PR without approvers", - ghOrg: "kosli-dev", - repository: "cli", - number: 8, - result: result{ - approvers: []string{}, - }, - }, - { - name: "get the list of approvers for an approved PR", - ghOrg: "kosli-dev", - repository: "cli", - number: 6, - result: result{ - approvers: []string{"sami-alajrami"}, - }, - }, - { - name: "non-existing PR causes an error", - ghOrg: "kosli-dev", - repository: "cli", - number: 662, - result: result{ - wantError: true, - }, - }, - } { - suite.Run(t.name, func() { - testHelpers.SkipIfEnvVarUnset(suite.T(), []string{"KOSLI_GITHUB_TOKEN"}) - token := os.Getenv("KOSLI_GITHUB_TOKEN") - c := &GithubConfig{ - Token: token, - Org: t.ghOrg, - Repository: t.repository, - } - approvers, err := c.GetPullRequestApprovers(t.number) - if t.result.wantError { - require.Errorf(suite.T(), err, "expected an error but got: %s", err) - } else { - require.NoErrorf(suite.T(), err, "was NOT expecting error but got: %s", err) - require.ElementsMatchf(suite.T(), t.result.approvers, approvers, "want approvers: %v, got approvers: %v", - t.result.approvers, approvers) - } - }) - } -} - func (suite *GithubTestSuite) TestExtractRepoName() { for _, t := range []struct { name string