Skip to content
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

Option in adv guide to print yaml advisory to console #1077

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/advisory/data_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ func (ds DataSession) Push(ctx context.Context) error {
return nil
}

// ModifiedPackages returns a map of package name o=to document for all modified packages
func (ds DataSession) ModifiedPackages() map[string]configs.Entry[v2.Document] {
modified := map[string]configs.Entry[v2.Document]{}
for _, n := range slices.Compact(ds.modifiedPackages) {
modified[n] = ds.index.Select().WhereName(n).Entries()[0]
}
return modified
}

// OpenPullRequest opens a pull request for the changes made during the session.
func (ds DataSession) OpenPullRequest(ctx context.Context) (*PullRequest, error) {
slices.Sort(ds.modifiedPackages)
Expand Down
6 changes: 3 additions & 3 deletions pkg/advisory/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
return err
}

if req.VulnerabilityID != "" {
return errors.New("vulnerability should be empty")
}
// if req.VulnerabilityID != "" {

Check failure on line 39 in pkg/advisory/request.go

View workflow job for this annotation

GitHub Actions / lint

commentedOutCode: may want to remove commented-out code (gocritic)
// return errors.New("vulnerability should be empty")
// }

Comment on lines +39 to 42
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to cause wolfictl adv guide to crash with

2024/07/23 21:35:03 ERRO adding advisory data: creating advisory: vulnerability should be empty

if req.Event.IsZero() {
return errors.New("event cannot be zero")
Expand Down
21 changes: 20 additions & 1 deletion pkg/cli/advisory_guide.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
question2 "github.com/wolfi-dev/wolfictl/pkg/question"
"github.com/wolfi-dev/wolfictl/pkg/scan"
"github.com/wolfi-dev/wolfictl/pkg/vuln"
"gopkg.in/yaml.v3"
)

//nolint:gocyclo
Expand Down Expand Up @@ -237,7 +238,7 @@ func cmdAdvisoryGuide() *cobra.Command {
actions = append(actions, customActionBrowser)
}
if sess.Modified() {
actions = append(actions, newCustomActionPR(ctx, sess))
actions = append(actions, newCustomActionPR(ctx, sess), newCustomActionOut(ctx, sess))
}

vaPickerOpts := picker.Options[resultWithAPKs]{
Expand Down Expand Up @@ -389,6 +390,24 @@ var (
},
}
}

newCustomActionOut = func(_ context.Context, sess *advisory.DataSession) picker.CustomAction[resultWithAPKs] {
return picker.CustomAction[resultWithAPKs]{
Key: "o",
Description: "print changes to stdout",
Do: func(_ resultWithAPKs) tea.Cmd {
o := []string{}
for name, doc := range sess.ModifiedPackages() {
b, err := yaml.Marshal(doc.Configuration())
if err != nil {
return picker.ErrCmd(fmt.Errorf("marshalling yaml for %s: %w", name, err))
}
o = append(o, fmt.Sprintf("# %s.advisories.yaml\n%s\n\n", name, string(b)))
}
return tea.Printf(strings.Join(o, "\n"))
},
}
}
)

type advisoryGuideParams struct {
Expand Down
Loading