Repository navigation
Block on missing refs and commits, and say how to fix them #153
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
889d255
Block on refs that do not exist and name them
nodeselector c99368e
Title missing refs plainly and show how to fix them
nodeselector 5fa4dda
Name missing commits by short SHA and update the impostor scenario
nodeselector c00dd52
Keep short-SHA guidance and drop the re-run hint for missing refs
nodeselector 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http" | ||
| "os" | ||
| "regexp" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/github/gh-actions-lock/internal/ghapi/httpmock" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestMissingRefNamesTheCause proves a ref that does not exist fails with an | ||
| // error finding that names it, rather than a fixable ref-changed plus a | ||
| // reachability warning. | ||
| func TestMissingRefNamesTheCause(t *testing.T) { | ||
| refs := []struct { | ||
| name, ref, detail, remedy string | ||
| }{ | ||
| {"tag", "v999.0.0", "ref v999.0.0 does not exist in actions/checkout", | ||
| "point `uses:` at a tag or branch that exists"}, | ||
| {"commit", "7b403c9ec14bd3ae0bbf793c2bee8815a7ac920b", "commit 7b403c9ec14b does not exist or is not reachable in actions/checkout", | ||
| "pin a commit that exists in actions/checkout, or point `uses:` at a tag or branch"}, | ||
| {"short sha", "7b403c9", "ref 7b403c9 does not resolve in actions/checkout", | ||
| "if this is a commit, use the full 40-character SHA; otherwise point `uses:` at a tag or branch that exists"}, | ||
| } | ||
| for _, tt := range refs { | ||
| for _, proxy := range []bool{false, true} { | ||
| t.Run(tt.name+"/"+map[bool]string{false: "graphql", true: "dependabot proxy"}[proxy], func(t *testing.T) { | ||
| testMissingRef(t, tt.ref, tt.detail, tt.remedy, proxy) | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func testMissingRef(t *testing.T, ref, detail, remedy string, proxy bool) { | ||
| t.Helper() | ||
| reg := &httpmock.Registry{} | ||
| // Each resolve pass (plan, then diagnosis) asks again, for both | ||
| // the JSON and the terminal run. | ||
| for range 6 { | ||
| registerMissingRef(reg, ref, proxy) | ||
| } | ||
| if proxy { | ||
| t.Setenv("GH_ACTIONS_LOCK_DEPENDABOT_PROXY", "1") | ||
| useDefaultTransport(t, reg) | ||
| } | ||
| path := writeTempWorkflow(t, ` | ||
| name: ci | ||
| on: push | ||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4.2.2 | ||
| `, "actions/checkout@v4.2.2") | ||
| wf, err := os.ReadFile(path) | ||
| require.NoError(t, err) | ||
| wf = []byte(strings.Replace(string(wf), "@v4.2.2", "@"+ref, 1)) | ||
| require.NoError(t, os.WriteFile(path, wf, 0o600)) | ||
| lockBefore := readTempLockfilePins(t) | ||
|
|
||
| stdout, _, err := runCommandWithHTTP(t, reg, | ||
| "--no-onboard", "--no-narrow", "--no-interactive", "--json=findings", path) | ||
| require.ErrorIs(t, err, errSilent) | ||
| var out struct { | ||
| Findings []struct { | ||
| Category string `json:"category"` | ||
| Severity string `json:"severity"` | ||
| Detail string `json:"detail"` | ||
| Remediation string `json:"remediation"` | ||
| DocURL string `json:"doc_url"` | ||
| } `json:"findings"` | ||
| } | ||
| require.NoError(t, json.Unmarshal([]byte(stdout), &out), stdout) | ||
| found := false | ||
| for _, f := range out.Findings { | ||
| if f.Category == "not-pinned" && f.Severity == "error" { | ||
| found = true | ||
| assert.Equal(t, detail, f.Detail) | ||
| assert.Equal(t, remedy, f.Remediation) | ||
| assert.Empty(t, f.DocURL, "the security-hardening page doesn't explain a missing ref") | ||
| } | ||
| } | ||
| assert.True(t, found, stdout) | ||
| assert.Equal(t, lockBefore, readTempLockfilePins(t)) | ||
| after, err := os.ReadFile(path) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, string(wf), string(after)) | ||
|
|
||
| _, stderr, err := runCommandWithHTTP(t, reg, | ||
| "--no-onboard", "--no-narrow", "--no-interactive", path) | ||
| require.ErrorIs(t, err, errSilent) | ||
| assert.Contains(t, stderr, "Ref does not exist actions/checkout@"+ref) | ||
| assert.Contains(t, stderr, detail) | ||
| assert.Contains(t, stderr, remedy) | ||
| assert.NotContains(t, stderr, "Re-run without") | ||
| assert.NotContains(t, stderr, "Not pinned") | ||
| assert.NotContains(t, stderr, "security-hardening") | ||
| } | ||
|
|
||
| func registerMissingRef(reg *httpmock.Registry, ref string, proxy bool) { | ||
| if !proxy { | ||
| reg.Register(httpmock.GraphQL(`repository\(owner:`), httpmock.JSONResponse(map[string]any{ | ||
| "data": map[string]any{"a0": map[string]any{"nameWithOwner": "actions/checkout", "object": nil}}, | ||
| })) | ||
| return | ||
| } | ||
| reg.Register(httpmock.REST("GET", `^/repos/actions/checkout$`), httpmock.JSONResponse(map[string]any{ | ||
| "full_name": "actions/checkout", "id": 1, "owner": map[string]any{"id": 1}, | ||
| })) | ||
| reg.Register(httpmock.REST("GET", `^/repos/actions/checkout/commits/`+regexp.QuoteMeta(ref)+`$`), | ||
| httpmock.StatusResponse(http.StatusUnprocessableEntity)) | ||
| } |
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
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.
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.