-
Notifications
You must be signed in to change notification settings - Fork 9
fix(attest): don't fail when a CI-defaulted --commit has no repository #1202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mbevc1
wants to merge
7
commits into
main
Choose a base branch
from
20260917_fix_attest
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1423e9e
fix(attest): don't fail when a CI-defaulted --commit has no repository
mbevc1 c601e76
test(helpers): resolve symlinks in CloneGitRepo before cloning
mbevc1 fc209f5
fix(attest): fail on an explicit --repo-root and cover the CI-default…
mbevc1 3b73508
docs(attest): say when --commit is dropped with a warning
mbevc1 f8964ef
chore: note CloneGitRepo's precondition and the commit rules in the a…
mbevc1 a8a2cbe
style(attest): keep only the comments that add information
mbevc1 07848ba
fix(attest): treat repo-root at its default as unset, and unify expli…
mbevc1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/go-git/go-git/v5" | ||
| "github.com/stretchr/testify/suite" | ||
| ) | ||
|
|
||
| // AttestationCommitInfoTestSuite covers how a failed commit lookup is reported | ||
| // (kosli-dev/server#6094, kosli-dev/server#5615). The CI default exists only | ||
| // while KOSLI_TESTS is unset, because DefaultValue returns "" under it, so inCI | ||
| // unsets it around the command run. | ||
| type AttestationCommitInfoTestSuite struct { | ||
| suite.Suite | ||
| headHash string | ||
| defaultKosliArguments string | ||
| } | ||
|
|
||
| const ( | ||
| commitInfoTestFingerprint = "7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9" | ||
| // A well-formed SHA that is not in this repository, as in a shallow clone. | ||
| absentSHA = "0d4c1e1b7f5c2a9e8b3d6f0a1c4e7b2d5a8f3c60" | ||
| ) | ||
|
|
||
| func (suite *AttestationCommitInfoTestSuite) SetupTest() { | ||
| repo, err := git.PlainOpen("../..") | ||
| suite.Require().NoError(err) | ||
| head, err := repo.Head() | ||
| suite.Require().NoError(err) | ||
| suite.headHash = head.Hash().String() | ||
|
|
||
| global = &GlobalOpts{ | ||
| ApiToken: "DRY_RUN", | ||
| Org: "test-org", | ||
| Host: "http://localhost:8001", | ||
| DryRun: true, | ||
| } | ||
| suite.defaultKosliArguments = " --dry-run --host http://localhost:8001 --org test-org --api-token DRY_RUN" | ||
| } | ||
|
|
||
| // inCI runs f with the CI defaults live, as in a GitHub Actions job whose | ||
| // GITHUB_SHA is sha. | ||
| func (suite *AttestationCommitInfoTestSuite) inCI(sha string, f func()) { | ||
| if value, set := os.LookupEnv("KOSLI_TESTS"); set { | ||
| suite.Require().NoError(os.Unsetenv("KOSLI_TESTS")) | ||
| defer func() { suite.Require().NoError(os.Setenv("KOSLI_TESTS", value)) }() | ||
| } | ||
| suite.T().Setenv("GITHUB_RUN_NUMBER", "1") | ||
| suite.T().Setenv("GITHUB_SHA", sha) | ||
| f() | ||
| } | ||
|
|
||
| func (suite *AttestationCommitInfoTestSuite) attestGeneric(extraFlags string) string { | ||
| return "attest generic --fingerprint " + commitInfoTestFingerprint + " --name foo --flow f --trail t " + extraFlags + suite.defaultKosliArguments | ||
| } | ||
|
|
||
| func (suite *AttestationCommitInfoTestSuite) beginTrail(extraFlags string) string { | ||
| return "begin trail t --flow f " + extraFlags + suite.defaultKosliArguments | ||
| } | ||
|
|
||
| func (suite *AttestationCommitInfoTestSuite) TestCIDefaultedCommitWithoutRepositoryWarns() { | ||
| // --repo-root defaults to ".", so run where there is no repository at all. | ||
| suite.T().Chdir(suite.T().TempDir()) | ||
| suite.inCI(suite.headHash, func() { | ||
| for _, cmd := range []string{suite.attestGeneric(""), suite.beginTrail("")} { | ||
| _, out, _, _, err := executeCommandC(cmd) | ||
| suite.Require().NoError(err, cmd) | ||
| suite.Contains(out, "[warning] proceeding without commit info", cmd) | ||
| suite.Contains(out, "--commit "+suite.headHash+" (defaulted from the CI environment)", cmd) | ||
| suite.Contains(out, "repository does not exist", cmd) | ||
| suite.Contains(out, "THIS IS A DRY-RUN", cmd) | ||
| suite.NotContains(out, "git_commit_info", cmd) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func (suite *AttestationCommitInfoTestSuite) TestCIDefaultedCommitNotInRepositoryWarns() { | ||
| suite.T().Chdir("../..") | ||
| suite.inCI(absentSHA, func() { | ||
| _, out, _, _, err := executeCommandC(suite.attestGeneric("")) | ||
| suite.Require().NoError(err) | ||
| suite.Contains(out, "[warning] proceeding without commit info") | ||
| suite.Contains(out, "--commit "+absentSHA+" (defaulted from the CI environment)") | ||
| suite.NotContains(out, "git_commit_info") | ||
| }) | ||
| } | ||
|
|
||
| func (suite *AttestationCommitInfoTestSuite) TestCommitFromEnvVarIsExplicit() { | ||
| suite.T().Chdir(suite.T().TempDir()) | ||
| suite.T().Setenv("KOSLI_COMMIT", suite.headHash) | ||
| _, out, _, _, err := executeCommandC(suite.attestGeneric("")) | ||
| suite.Require().Error(err) | ||
| suite.Contains(err.Error(), "failed to get commit info for --commit "+suite.headHash+":") | ||
| suite.NotContains(err.Error(), "defaulted from the CI environment") | ||
| suite.NotContains(out, "[warning] proceeding without commit info") | ||
| } | ||
|
|
||
| // A --repo-root set via the environment or config to its own "." default | ||
| // must not count as explicit: bindFlags marks the flag Changed regardless of | ||
| // whether the applied value differs from the default. | ||
| func (suite *AttestationCommitInfoTestSuite) TestRepoRootFromEnvVarAtDefaultValueStillWarns() { | ||
| suite.T().Chdir(suite.T().TempDir()) | ||
| suite.T().Setenv("KOSLI_REPO_ROOT", ".") | ||
| suite.inCI(suite.headHash, func() { | ||
| _, out, _, _, err := executeCommandC(suite.attestGeneric("")) | ||
| suite.Require().NoError(err) | ||
| suite.Contains(out, "[warning] proceeding without commit info") | ||
| }) | ||
| } | ||
|
|
||
| func (suite *AttestationCommitInfoTestSuite) TestCIDefaultedCommitWithExplicitRepoRootFails() { | ||
| suite.inCI(suite.headHash, func() { | ||
| for _, cmd := range []string{suite.attestGeneric("--repo-root testdata"), suite.beginTrail("--repo-root testdata")} { | ||
| _, out, _, _, err := executeCommandC(cmd) | ||
| suite.Require().Error(err, cmd) | ||
| suite.Contains(err.Error(), "failed to get commit info for --commit "+suite.headHash+" (defaulted from the CI environment)", cmd) | ||
| suite.Contains(err.Error(), "repository does not exist", cmd) | ||
| suite.Contains(err.Error(), "Point --repo-root at a repository containing it", cmd) | ||
| suite.NotContains(out, "[warning] proceeding without commit info", cmd) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func (suite *AttestationCommitInfoTestSuite) TestExplicitCommitWithoutRepositoryFails() { | ||
| tests := []cmdTestCase{ | ||
| { | ||
| wantError: true, | ||
| name: "attest generic: an explicit --commit fails when --repo-root has no repository", | ||
| cmd: suite.attestGeneric("--commit " + suite.headHash + " --repo-root testdata"), | ||
| goldenRegex: "Error: failed to get commit info for --commit " + suite.headHash + ": .*repository does not exist\\. Point --repo-root at a repository containing it\n", | ||
| }, | ||
| { | ||
| wantError: true, | ||
| name: "begin trail: an explicit --commit fails when --repo-root has no repository", | ||
| cmd: suite.beginTrail("--commit " + suite.headHash + " --repo-root testdata"), | ||
| goldenRegex: "Error: failed to get commit info for --commit " + suite.headHash + ": .*repository does not exist\\. Point --repo-root at a repository containing it\n", | ||
| }, | ||
| } | ||
| runTestCmd(suite.T(), tests) | ||
| } | ||
|
|
||
| func (suite *AttestationCommitInfoTestSuite) TestExplicitCommitIsAttached() { | ||
| tests := []cmdTestCase{ | ||
| { | ||
| name: "attest generic: an explicit --commit is resolved and sent", | ||
| cmd: suite.attestGeneric("--commit " + suite.headHash + " --repo-root ../.."), | ||
| goldenRegex: `(?s)"git_commit_info": \{.*"sha1": "` + suite.headHash + `"`, | ||
| }, | ||
| { | ||
| name: "begin trail: an explicit --commit is resolved and sent", | ||
| cmd: suite.beginTrail("--commit " + suite.headHash + " --repo-root ../.."), | ||
| goldenRegex: `(?s)"git_commit_info": \{.*"sha1": "` + suite.headHash + `"`, | ||
| }, | ||
| } | ||
| runTestCmd(suite.T(), tests) | ||
| } | ||
|
|
||
| func (suite *AttestationCommitInfoTestSuite) TestCommandsNeedingTheCommitFail() { | ||
| suite.T().Chdir(suite.T().TempDir()) | ||
| suite.inCI(suite.headHash, func() { | ||
| for _, tc := range []struct{ cmd, need string }{ | ||
| {"attest pullrequest github --name foo --flow f --trail t --github-token tok --github-org o --repository r" + suite.defaultKosliArguments, "find pull requests"}, | ||
| {"attest jira --name foo --flow f --trail t --jira-base-url https://x.atlassian.net --jira-username u --jira-api-token tok" + suite.defaultKosliArguments, "search for Jira issue keys"}, | ||
| } { | ||
| _, out, _, _, err := executeCommandC(tc.cmd) | ||
| suite.Require().Error(err, tc.cmd) | ||
| suite.Contains(err.Error(), "failed to get commit info for --commit "+suite.headHash+" (defaulted from the CI environment)", tc.cmd) | ||
| suite.Contains(err.Error(), "The commit is required to "+tc.need, tc.cmd) | ||
| suite.NotContains(out, "[warning] proceeding without commit info", tc.cmd) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // Unreachable from the CLI while RequireFlags keeps --commit non-empty; guards | ||
| // the nil dereference that would follow if that changed. | ||
| func (suite *AttestationCommitInfoTestSuite) TestCommandsNeedingTheCommitFailWithoutOne() { | ||
| o := &CommonAttestationOptions{ | ||
| fingerprintOptions: &fingerprintOptions{}, | ||
| attestationNameTemplate: "foo", | ||
| commitRequiredFor: "find pull requests", | ||
| } | ||
| err := o.run([]string{}, &CommonAttestationPayload{}) | ||
| suite.Require().Error(err) | ||
| suite.Contains(err.Error(), "the commit is required to find pull requests") | ||
| } | ||
|
|
||
| func TestAttestationCommitInfoTestSuite(t *testing.T) { | ||
| suite.Run(t, new(AttestationCommitInfoTestSuite)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.