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

feat: add authors for issue entries #41

Merged
merged 8 commits into from
Jan 24, 2023
Merged
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
83 changes: 50 additions & 33 deletions chronicle/release/releasers/github/summarizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var _ release.Summarizer = (*Summarizer)(nil)

type Config struct {
Host string
IncludeIssuePRAuthors bool
IncludeIssues bool
IncludePRs bool
ExcludeLabels []string
Expand Down Expand Up @@ -145,35 +146,34 @@ func (s *Summarizer) Changes(sinceRef, untilRef string) ([]change.Change, error)
logCommits(includeCommits)
}

if s.config.IncludePRs || (s.config.IssuesRequireLinkedPR && s.config.IncludeIssues) {
allMergedPRs, err := fetchMergedPRs(s.userName, s.repoName)
if err != nil {
return nil, err
}
allMergedPRs, err := fetchMergedPRs(s.userName, s.repoName)
if err != nil {
return nil, err
}

log.Debugf("total merged PRs discovered: %d", len(allMergedPRs))
log.Debugf("total merged PRs discovered: %d", len(allMergedPRs))

if s.config.IncludePRs {
changes = append(changes, changesFromPRs(s.config, allMergedPRs, sinceTag, untilTag, includeCommits)...)
}
if s.config.IssuesRequireLinkedPR && s.config.IncludeIssues {
if s.config.IncludePRs {
changes = append(changes, changesFromPRs(s.config, allMergedPRs, sinceTag, untilTag, includeCommits)...)
}

if s.config.IncludeIssues {
if s.config.IssuesRequireLinkedPR {
// extract closed linked issues with closed PRs from the PR list. Why do this here?
// githubs ontology has PRs as the source of truth for issue linking. Linked PR information
// is not available on the issue itself.
extractedIssues := issuesExtractedFromPRs(s.config, allMergedPRs, sinceTag, untilTag, includeCommits)
changes = append(changes, createChangesFromIssues(s.config, extractedIssues)...)
}
}

if s.config.IncludeIssues && !s.config.IssuesRequireLinkedPR {
allClosedIssues, err := fetchClosedIssues(s.userName, s.repoName)
if err != nil {
return nil, err
}
changes = append(changes, createChangesFromIssues(s.config, allMergedPRs, extractedIssues)...)
} else {
allClosedIssues, err := fetchClosedIssues(s.userName, s.repoName)
if err != nil {
return nil, err
}

log.Debugf("total closed issues discovered: %d", len(allClosedIssues))
log.Debugf("total closed issues discovered: %d", len(allClosedIssues))

changes = append(changes, changesFromIssues(s.config, allClosedIssues, sinceTag, untilTag)...)
changes = append(changes, changesFromIssues(s.config, allMergedPRs, allClosedIssues, sinceTag, untilTag)...)
}
}

return changes, nil
Expand Down Expand Up @@ -276,13 +276,13 @@ func logPRs(prs []ghPullRequest) {
}
}

func changesFromIssues(config Config, allClosedIssues []ghIssue, sinceTag, untilTag *git.Tag) []change.Change {
func changesFromIssues(config Config, allMergedPRs []ghPullRequest, allClosedIssues []ghIssue, sinceTag, untilTag *git.Tag) []change.Change {
filteredIssues := filterIssues(allClosedIssues, standardIssueFilters(config, sinceTag, untilTag)...)

log.Debugf("issues contributing to changelog: %d", len(filteredIssues))
logIssues(filteredIssues)

return createChangesFromIssues(config, filteredIssues)
return createChangesFromIssues(config, allMergedPRs, filteredIssues)
}

func logIssues(issues []ghIssue) {
Expand All @@ -295,23 +295,40 @@ func logIssues(issues []ghIssue) {
}
}

func createChangesFromIssues(config Config, issues []ghIssue) (changes []change.Change) {
func createChangesFromIssues(config Config, allMergedPRs []ghPullRequest, issues []ghIssue) (changes []change.Change) {
for _, issue := range issues {
changeTypes := config.ChangeTypesByLabel.ChangeTypes(issue.Labels...)
if len(changeTypes) > 0 {
references := []change.Reference{
{
Text: fmt.Sprintf("Issue #%d", issue.Number),
URL: issue.URL,
},
}

if config.IncludeIssuePRAuthors {
for _, pr := range allMergedPRs {
if pr.Author == "" {
continue
}
for _, linkedIssue := range pr.LinkedIssues {
if linkedIssue.URL == issue.URL {
references = append(references, change.Reference{
Text: pr.Author,
URL: fmt.Sprintf("https://%s/%s", config.Host, pr.Author),
})
}
}
}
}

changes = append(changes, change.Change{
Text: issue.Title,
ChangeTypes: changeTypes,
Timestamp: issue.ClosedAt,
References: []change.Reference{
{
Text: fmt.Sprintf("Issue #%d", issue.Number),
URL: issue.URL,
},
// TODO: add assignee(s) name + url
},
EntryType: "githubIssue",
Entry: issue,
References: references,
EntryType: "githubIssue",
Entry: issue,
})
}
}
Expand Down
138 changes: 138 additions & 0 deletions chronicle/release/releasers/github/summarizer_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package github

import (
"encoding/json"
"reflect"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/anchore/chronicle/chronicle/release/change"
"github.com/anchore/chronicle/internal/git"
Expand Down Expand Up @@ -768,3 +771,138 @@ func Test_changesFromIssuesExtractedFromPRs(t *testing.T) {
})
}
}

func Test_createChangesFromIssues(t *testing.T) {
timeStart := time.Date(2021, time.September, 16, 19, 34, 0, 0, time.UTC)

patch := change.NewType("patch", change.SemVerPatch)

changeTypeSet := change.TypeSet{
"bug": patch,
}

issue1 := ghIssue{
Title: "Issue 1",
Number: 1,
URL: "issue-1-url",
ClosedAt: timeStart,
Labels: []string{"bug"},
}

issue2 := ghIssue{
Title: "Issue 2",
Number: 2,
URL: "issue-2-url",
ClosedAt: timeStart,
Labels: []string{"bug"},
}

prWithLinkedIssues := ghPullRequest{
Title: "pr with linked issues",
MergedAt: timeStart,
Number: 3,
Labels: []string{"bug"},
Author: "some-author-1",
URL: "some-url-1",
LinkedIssues: []ghIssue{
issue1,
},
}

prWithLinkedIssues2 := ghPullRequest{
Title: "pr with linked issues 2",
MergedAt: timeStart,
Number: 4,
Labels: []string{"another-label"},
Author: "some-author-2",
URL: "some-url-2",
LinkedIssues: []ghIssue{
issue2,
},
}

prWithoutLinkedIssues := ghPullRequest{
MergedAt: timeStart,
Title: "pr without linked issues",
Number: 6,
Author: "some-author",
URL: "some-url",
}

tests := []struct {
name string
config Config
inputPrs []ghPullRequest
issues []ghIssue
expectedChanges []change.Change
}{
{
name: "includes author for issues",
config: Config{
IncludeIssuePRAuthors: true,
ChangeTypesByLabel: changeTypeSet,
Host: "some-host",
},
inputPrs: []ghPullRequest{
prWithLinkedIssues,
prWithLinkedIssues2,
prWithoutLinkedIssues,
},
issues: []ghIssue{
issue1,
issue2,
},
expectedChanges: []change.Change{
{
Text: "Issue 1",
ChangeTypes: []change.Type{patch},
Timestamp: timeStart,
References: []change.Reference{
{
Text: "Issue #1",
URL: "issue-1-url",
},
{
Text: "some-author-1",
URL: "https://some-host/some-author-1",
},
},
EntryType: "githubIssue",
Entry: issue1,
},
{
Text: "Issue 2",
ChangeTypes: []change.Type{patch},
Timestamp: timeStart,
References: []change.Reference{
{
Text: "Issue #2",
URL: "issue-2-url",
},
{
Text: "some-author-2",
URL: "https://some-host/some-author-2",
},
},
EntryType: "githubIssue",
Entry: issue2,
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
changes := createChangesFromIssues(tt.config, tt.inputPrs, tt.issues)
if !reflect.DeepEqual(tt.expectedChanges, changes) {
// print out a JSON diff
toJson := func(changes []change.Change) string {
out, err := json.Marshal(changes)
require.NoError(t, err)
return string(out)
}
assert.JSONEq(t, toJson(tt.expectedChanges), toJson(changes))
}
})
}
}
3 changes: 3 additions & 0 deletions internal/config/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type githubSummarizer struct {
Host string `yaml:"host" json:"host" mapstructure:"host"`
ExcludeLabels []string `yaml:"exclude-labels" json:"exclude-labels" mapstructure:"exclude-labels"`
IncludeIssuePRAuthors bool `yaml:"include-issue-pr-authors" json:"include-issue-pr-authors" mapstructure:"include-issue-pr-authors"`
IncludePRs bool `yaml:"include-prs" json:"include-prs" mapstructure:"include-prs"`
IncludeIssues bool `yaml:"include-issues" json:"include-issues" mapstructure:"include-issues"`
IssuesRequireLinkedPR bool `yaml:"issues-require-linked-prs" json:"issues-require-linked-prs" mapstructure:"issues-require-linked-prs"`
Expand Down Expand Up @@ -40,6 +41,7 @@ func (cfg githubSummarizer) ToGithubConfig() (github.Config, error) {
}
return github.Config{
Host: cfg.Host,
IncludeIssuePRAuthors: cfg.IncludeIssuePRAuthors,
IncludeIssues: cfg.IncludeIssues,
IncludePRs: cfg.IncludePRs,
ExcludeLabels: cfg.ExcludeLabels,
Expand All @@ -54,6 +56,7 @@ func (cfg githubSummarizer) loadDefaultValues(v *viper.Viper) {
v.SetDefault("github.issues-require-linked-prs", false)
v.SetDefault("github.consider-pr-merge-commits", true)
v.SetDefault("github.include-prs", true)
v.SetDefault("github.include-issue-pr-authors", true)
v.SetDefault("github.include-issues", true)
v.SetDefault("github.exclude-labels", []string{"duplicate", "question", "invalid", "wontfix", "wont-fix", "release-ignore", "changelog-ignore", "ignore"})
v.SetDefault("github.changes", []githubChange{
Expand Down
2 changes: 1 addition & 1 deletion internal/git/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/anchore/chronicle/internal"
)

var remotePattern = regexp.MustCompile(`(?m)\[remote "origin"](\n.*)*url\s*=\s*(?P<url>.*)$`)
Copy link
Contributor Author

@kzantow kzantow Jan 24, 2023

Choose a reason for hiding this comment

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

Just sneaking in a fix -- this was reading a url from the vulnerability-match-labels or other remote because it was multiline and had a .* in the middle.

Copy link
Contributor

Choose a reason for hiding this comment

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

great catch

var remotePattern = regexp.MustCompile(`\[remote\s*"origin"]\s*\n\s*url\s*=\s*(?P<url>[^\s]+)\s+`)

// TODO: can't use r.Config for same validation reasons
func RemoteURL(p string) (string, error) {
Expand Down
5 changes: 4 additions & 1 deletion internal/git/test-fixtures/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ repos/commit-in-repo:
./create-commit-in-repo.sh

repos/tag-range-repo:
./create-tag-range-repo.sh
./create-tag-range-repo.sh

clean:
rm -rf repos/remote-repo repos/tagged-repo repos/commit-in-repo repos/tag-range-repo
1 change: 1 addition & 0 deletions internal/git/test-fixtures/create-remote-repo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ git config --local user.name "nope"
trap 'popd' EXIT

git remote add origin [email protected]:wagoodman/count-goober.git
git remote add upstream [email protected]:upstream/count-goober.git