diff --git a/action.yml b/action.yml index f2e7353..3695d75 100644 --- a/action.yml +++ b/action.yml @@ -52,7 +52,7 @@ inputs: description: Close the advisory of an issue a maintainer closed as a duplicate, credit its reporter on the original report and tell them on the closed advisory. Needs `issues-repository`. default: "false" comment: - description: Post the triage as a comment on the advisory. Off by default, every triage then only reaches `project`, which must be set. + description: Post the triage as a comment on the advisory, and with `issues-repository` the link to each issue tamis creates. Off by default, every triage then only reaches `project`, which must be set. default: "false" references: description: Whitespace-separated local git references, including each supported release branch. The first is analysed, and the cited code is located on it. All must have been fetched. diff --git a/docs/configuration.md b/docs/configuration.md index e676cd9..6f183f5 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -16,7 +16,7 @@ | `engine` | `claude` | `claude` for Claude Code and the Anthropic API, `openai` for Codex and the OpenAI API. | | `model` | `claude-opus-5-5` or `gpt-5.5` | Model of the reader, the duplicate search and the exact confirmation. `gpt-6.1-sol` with a Codex access token. | | `daybreak` | `false` | Turns on OpenAI's Daybreak cyber access program in Codex. Needs `engine: openai` and an API key whose organisation OpenAI approved for it. | -| `comment` | `false` | Post the triage as a comment on the advisory. Off, every triage only reaches `project`, which must then be set, and the board records the failed attempts in `Attempts`. | +| `comment` | `false` | Post the triage as a comment on the advisory, and with `issues-repository` a comment linking each advisory to the issue tamis creates for it. Off, every triage only reaches `project`, which must then be set, and the board records the failed attempts in `Attempts`. | | `project` | | Private organisation project kept in sync with the advisories, as `org/number`. See [the project board](setup.md#5-project-board). | | `issues-repository` | | Private repository the board items are created in as issues, so duplicates become sub-issues of their original report. Drafts when unset. See [the project board](setup.md#5-project-board). | | `issues-token` | | Installation token for `issues-repository` only, with the "Issues" and organisation "Projects" permissions in read and write. Required with `issues-repository`. | diff --git a/internal/board/board.go b/internal/board/board.go index cb801d3..2390650 100644 --- a/internal/board/board.go +++ b/internal/board/board.go @@ -145,6 +145,8 @@ type Board struct { ig Issues // unlisted holds the issues of a previous run that never reached the board, by lowercase GHSA identifier. unlisted map[string]github.Issue + // created holds the link of each issue this run created, by GHSA identifier. + created map[string]string } // issueMarker names the advisory of an issue, so an issue created by a run that failed to put it on the board is @@ -233,7 +235,7 @@ func Open(ctx context.Context, gh GitHub, ig Issues, project, issues, advisories return nil, errors.New("the issues repository must be private") } } - b := &Board{gh: gh, org: org, number: number, ids: map[string]int64{}, options: map[string]map[string]string{}, items: map[string]*item{}, issues: issues, ig: ig} + b := &Board{gh: gh, org: org, number: number, ids: map[string]int64{}, options: map[string]map[string]string{}, items: map[string]*item{}, issues: issues, ig: ig, created: map[string]string{}} fields, err := gh.ListProjectFields(ctx, org, number) if err != nil { return nil, err @@ -435,6 +437,7 @@ func (b *Board) put(ctx context.Context, a github.Advisory, priority string, t * return err } b.unlisted[key] = issue + b.created[a.GHSAID] = issue.HTMLURL } added, err := b.ig.AddProjectIssue(ctx, b.org, b.number, issue.ID) if err != nil { @@ -693,6 +696,19 @@ func (b *Board) CloseDuplicates(ctx context.Context, gh Advisories, repo string, return errors.Join(errs...) } +// LinkIssues tells each advisory whose issue this run created where the maintainers track it. An issue found +// again from a failed run is left out, since that run may have linked it already. +func (b *Board) LinkIssues(ctx context.Context, gh Advisories, repo string) error { + var errs []error + for ghsa, link := range b.created { + if err := gh.PostComment(ctx, repo, ghsa, fmt.Sprintf("Tracked in %s.", link)); err != nil { + errs = append(errs, fmt.Errorf("linking the issue of %s: %w", ghsa, err)) + } + } + + return errors.Join(errs...) +} + // closeDuplicate comments before closing, since the next run leaves a closed advisory alone. A close that failed // after its comment finds that comment on the next run instead of posting it again. It returns the original and // the login credited on it, empty when the original credits nobody for it. diff --git a/internal/board/board_test.go b/internal/board/board_test.go index 2cd9f53..ae1b952 100644 --- a/internal/board/board_test.go +++ b/internal/board/board_test.go @@ -1157,3 +1157,38 @@ func TestSyncKeepsTheIssueOfAClosedDuplicateClosedAsSuch(t *testing.T) { require.Equal(t, "closed", p.Value(p.Find("GHSA-dup"), "State")) require.Equal(t, "not_planned", p.IssueOf(p.Find("GHSA-orig")).StateReason) } + +func TestLinkIssuesCommentsOnTheAdvisoriesOfTheIssuesItCreated(t *testing.T) { + p := boardtest.New() + p.Issues[1] = &boardtest.Issue{ID: 1001, Repo: "acme/private-issues", Title: "s", Body: "", State: "open"} + b, err := Open(t.Context(), p, p, "php/16", "acme/private-issues", "php/php-src") + require.NoError(t, err) + require.NoError(t, b.Put(t.Context(), github.Advisory{GHSAID: "GHSA-new", Summary: "s", State: "triage"}, ToTriage, &Triage{}, "")) + require.NoError(t, b.Put(t.Context(), github.Advisory{GHSAID: "GHSA-aaaa-bbbb-cccc", Summary: "s", State: "triage"}, ToTriage, &Triage{}, "")) + f := &advisories{comments: map[string][]string{}} + + require.NoError(t, b.LinkIssues(t.Context(), f, "php/php-src")) + + require.Equal(t, map[string][]string{"GHSA-new": {"Tracked in https://github.com/acme/private-issues/issues/2."}}, f.comments, "an issue found again from a failed run was linked") +} + +func TestLinkIssuesReportsAFailedComment(t *testing.T) { + p := boardtest.New() + b, err := Open(t.Context(), p, p, "php/16", "acme/private-issues", "php/php-src") + require.NoError(t, err) + require.NoError(t, b.Put(t.Context(), github.Advisory{GHSAID: "GHSA-new", Summary: "s", State: "triage"}, ToTriage, &Triage{}, "")) + + require.ErrorContains(t, b.LinkIssues(t.Context(), &advisories{postErr: errors.New("HTTP 502")}, "php/php-src"), "linking the issue of GHSA-new: HTTP 502") +} + +func TestLinkIssuesLeavesDraftsAlone(t *testing.T) { + p := boardtest.New() + b, err := Open(t.Context(), p, p, "php/16", "", "php/php-src") + require.NoError(t, err) + require.NoError(t, b.Put(t.Context(), github.Advisory{GHSAID: "GHSA-new", Summary: "s", State: "triage"}, ToTriage, &Triage{}, "")) + f := &advisories{comments: map[string][]string{}} + + require.NoError(t, b.LinkIssues(t.Context(), f, "php/php-src")) + + require.Empty(t, f.comments) +} diff --git a/internal/board/boardtest/boardtest.go b/internal/board/boardtest/boardtest.go index fd2e52e..f225e9d 100644 --- a/internal/board/boardtest/boardtest.go +++ b/internal/board/boardtest/boardtest.go @@ -269,7 +269,7 @@ func (p *Project) CreateIssue(_ context.Context, repo, title, body string) (gith n := len(p.Issues) + 1 p.Issues[n] = &Issue{ID: int64(1000 + n), Repo: repo, Title: title, Body: body, State: "open"} - return github.Issue{ID: int64(1000 + n), Number: n}, nil + return github.Issue{ID: int64(1000 + n), Number: n, HTMLURL: fmt.Sprintf("https://github.com/%s/issues/%d", repo, n)}, nil } func (p *Project) UpdateIssue(_ context.Context, repo string, number int, fields map[string]string) error { diff --git a/internal/triage/triage.go b/internal/triage/triage.go index 7b1671a..ed57cbc 100644 --- a/internal/triage/triage.go +++ b/internal/triage/triage.go @@ -290,6 +290,12 @@ func Run(ctx context.Context, cfg Config, gh GitHub, reader Reader, eng engine.E triaged++ } + if b != nil && !cfg.NoComment { + if err := b.LinkIssues(ctx, gh, cfg.Repo); err != nil { + log.Error("linking the issues on their advisories failed", "reason", describe(err)) + boardErr = errors.Join(boardErr, errors.New("linking the issues: "+describe(err))) + } + } if b != nil { if err := b.Sync(ctx, all); err != nil { log.Error("syncing the project board failed", "reason", describe(err)) diff --git a/internal/triage/triage_test.go b/internal/triage/triage_test.go index 51e7a4b..d748a2f 100644 --- a/internal/triage/triage_test.go +++ b/internal/triage/triage_test.go @@ -1558,7 +1558,16 @@ func TestRunPutsTheTriageInAPrivateIssue(t *testing.T) { require.NotNil(t, issue, "the board triage is not an issue") require.Equal(t, "acme/private-issues", issue.Repo) require.Contains(t, issue.Body, report.Marker, "the issue misses the triage") - require.Empty(t, gh.posted, "the board triage posted a comment") + require.Equal(t, map[string]string{"GHSA-done": "Tracked in https://github.com/acme/private-issues/issues/1."}, gh.posted, "the advisory misses the link to its issue, or got the triage") +} + +func TestRunLinksNoIssueWithoutComments(t *testing.T) { + gh, eng := newFixture(), &fakeEngine{} + + require.NoError(t, run(t, Config{Advisory: "GHSA-done", Project: "php/16", IssuesRepository: "acme/private-issues", NoComment: true}, gh, eng)) + + require.NotNil(t, gh.IssueOf(gh.Find("GHSA-done"))) + require.Empty(t, gh.posted) } func TestRunClosesTheReportOfAnIssueClosedAsADuplicateOnlyWhenAsked(t *testing.T) {