Repository navigation
feat(project): scan a branch or tag with project scan create #73
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
arunanshub
wants to merge
5
commits into
main
Choose a base branch
from
feat/project-scan-ref
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
5 commits
Select commit
Hold shift + click to select a range
156ea4b
feat(project): scan a branch or tag with project scan create --ref
arunanshub 11c9afe
docs(agents): add engineering, test and writing rules
arunanshub 9307913
feat(project): add --branch and --tag to project scan create
arunanshub 57ab284
fix(project): refuse a --ref that is not a branch or a tag
arunanshub d821574
docs(project): show --ref and --branch in GitHub Actions workflows
arunanshub 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
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,107 @@ | ||
| package project | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/safedep/dry/usefulerror" | ||
| "google.golang.org/grpc/status" | ||
| ) | ||
|
|
||
| const ( | ||
| refPrefix = "refs/" | ||
| branchRefPrefix = "refs/heads/" | ||
| tagRefPrefix = "refs/tags/" | ||
|
|
||
| refFlag = "ref" | ||
| branchFlag = "branch" | ||
| tagFlag = "tag" | ||
| ) | ||
|
|
||
| // refFlags holds the three ways to name the ref to scan. Set records which | ||
| // flags the user passed, so an empty value is told apart from an absent flag. | ||
| type refFlags struct { | ||
| Ref string | ||
| Branch string | ||
| Tag string | ||
| Set []string | ||
| } | ||
|
|
||
| // qualified is the full ref the API requires, or "" to scan the default | ||
| // branch. --branch and --tag name the namespace, so the CLI never guesses. | ||
| // A plain --ref name is a branch, as in "git switch <name>". A full branch or | ||
| // tag ref goes through unchanged, so the server owns the ref name rules. | ||
| func (f refFlags) qualified() (string, error) { | ||
| switch len(f.Set) { | ||
| case 0: | ||
| return "", nil | ||
| case 1: | ||
| default: | ||
| return "", invalidRefError( | ||
| fmt.Errorf("--%s cannot be combined", strings.Join(f.Set, ", --")), | ||
| "Pass only one of --branch, --tag or --ref.", | ||
| ) | ||
| } | ||
|
|
||
| switch f.Set[0] { | ||
| case branchFlag: | ||
| return shortRef(branchFlag, f.Branch, branchRefPrefix) | ||
| case tagFlag: | ||
| return shortRef(tagFlag, f.Tag, tagRefPrefix) | ||
| } | ||
| if f.Ref == "" { | ||
| return "", invalidRefError( | ||
| fmt.Errorf("--ref must not be empty"), | ||
| "Pass a branch name, refs/heads/<branch> or refs/tags/<tag>, or omit --ref to scan the default branch.", | ||
| ) | ||
| } | ||
| if strings.HasPrefix(f.Ref, branchRefPrefix) || strings.HasPrefix(f.Ref, tagRefPrefix) { | ||
| return f.Ref, nil | ||
| } | ||
| // The API scans only branches and tags. Another ref, such as a pull | ||
| // request ref, fails here with advice instead of as a ref the repository | ||
| // does not have. | ||
| if strings.HasPrefix(f.Ref, refPrefix) { | ||
| return "", invalidRefError( | ||
| fmt.Errorf("--ref %q is not a branch or a tag", f.Ref), | ||
| "Only branches and tags can be scanned. Pass --branch, --tag, or a ref under refs/heads/ or refs/tags/.", | ||
| ) | ||
| } | ||
| return branchRefPrefix + f.Ref, nil | ||
| } | ||
|
|
||
| func shortRef(flag, name, prefix string) (string, error) { | ||
| if name == "" { | ||
| return "", invalidRefError( | ||
| fmt.Errorf("--%s must not be empty", flag), | ||
| fmt.Sprintf("Pass a %s name, or omit --%s to scan the default branch.", flag, flag), | ||
| ) | ||
| } | ||
| if strings.HasPrefix(name, refPrefix) { | ||
| return "", invalidRefError( | ||
| fmt.Errorf("--%s takes a short name, not %q", flag, name), | ||
| fmt.Sprintf("Pass the name without refs/, or pass the full ref with --ref %s.", name), | ||
| ) | ||
| } | ||
| return prefix + name, nil | ||
| } | ||
|
|
||
| func invalidRefError(cause error, help string) error { | ||
| return newProjectError(usefulerror.ErrBadRequest, "Invalid ref", help, cause) | ||
| } | ||
|
|
||
| // refRejectedError explains a server rejection of the requested ref. The | ||
| // generic InvalidArgument rendering hides the server message behind | ||
| // --verbose, and that message names the project and the ref that failed. | ||
| func refRejectedError(err error, ref string) error { | ||
| help := fmt.Sprintf("%s. Check that %s exists in every selected repository.", status.Convert(err).Message(), ref) | ||
| if name, ok := strings.CutPrefix(ref, branchRefPrefix); ok { | ||
| help += fmt.Sprintf(" If %s is a tag, pass --tag %s.", name, name) | ||
| } | ||
| return newProjectError( | ||
| usefulerror.ErrBadRequest, | ||
| "Ref cannot be scanned", | ||
| help, | ||
| fmt.Errorf("project scan create: %w", err), | ||
| ) | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: safedep/cli
Length of output: 469
Script executed:
Repository: safedep/cli
Length of output: 18322
Script executed:
Repository: safedep/cli
Length of output: 18322
Script executed:
Repository: safedep/cli
Length of output: 7729
Web query:
Go 1.26 documentation go test fuzz seed corpus go test ./... nested modules workspace package patternsResult:
Script executed:
Repository: safedep/cli
Length of output: 22144
Web query:
site:pkg.go.dev/cmd/go@go1.26.2 "Directories that contain a go.mod file" "workspace" package patternsResult:
Web query:
Go 1.26.2 cmd/go package lists patterns nested go.mod "./..." workspace exclude directories site:pkg.go.dev/cmd/go@go1.26.2Result:
Script executed:
Repository: safedep/cli
Length of output: 1989
Script executed:
Repository: safedep/cli
Length of output: 9230
Web query:
site:go.dev/ref/mod "packages in nested modules" "./..." module packages pattern goResult:
Cover the scripts module in CI seed replay.
If a fuzz test is added under
scripts,make testskips its seeds becausescriptsis a separate module. CI can then miss regressions those seeds would catch. Add the module to CI testing or limit the guidance to the packages CI tests.Script executed:
Repository: safedep/cli
Length of output: 1360
Script executed:
Repository: safedep/cli
Length of output: 14661
Web query:
Go 1.26 go test -fuzz regexp flag requires pattern argument documentationResult:
Web query:
site:github.com/golang/go "go1.26.2" "-fuzz regexp" go testResult:
Script executed:
Repository: safedep/cli
Length of output: 695
Include the fuzz test pattern in the command.
go test -fuzzfails without a pattern. Usego test -fuzz=<test-name>from the test’s package directory so contributors can run the required fuzz test.