Skip to content
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
68 changes: 67 additions & 1 deletion src/app/generate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ This is a release note
- This is in the past and should not be included
`

//nolint:funlen,paralleltest
//nolint:funlen,maintidx,paralleltest
func TestGenerate(t *testing.T) {
for _, tc := range []struct {
name string
Expand Down Expand Up @@ -120,6 +120,72 @@ dependencies:
commit: chore(deps): bump anotherdep from 0.0.1 to 0.0.2 (#69)
`) + "\n",
},
{
name: "Markdown_Reverted_Dependency",
md: strings.TrimSpace(`
# Changelog
This is based on blah blah blah

## Unreleased

## v1.2.3 - 20YY-DD-MM

### Enhancements
- This is in the past and should not be included
`) + "\n",
args: "",
author: "not a bot <not-a@bot.com>",
commits: []string{
"Revert \"chore(deps): bump thisdep from 1.7.0 to 1.10.1\"",
},
expected: strings.TrimSpace(`
notes: ""
changes: []
dependencies:
- name: thisdep
from: 1.10.1
to: 1.7.0
meta:
commit: Revert "chore(deps): bump thisdep from 1.7.0 to 1.10.1"
`) + "\n",
},
{
// This test case does not represent the behavior we would like, but servers as an acknowledgement
// of the limitations of the current behavior.
name: "Markdown_Dependency_And_Revert",
md: strings.TrimSpace(`
# Changelog
This is based on blah blah blah

## Unreleased

## v1.2.3 - 20YY-DD-MM

### Enhancements
- This is in the past and should not be included
`) + "\n",
args: "",
author: "dependabot <dependabot@github.com>",
commits: []string{
"chore(deps): bump thisdep from 1.7.0 to 1.10.1",
"Revert \"chore(deps): bump thisdep from 1.7.0 to 1.10.1\"",
},
expected: strings.TrimSpace(`
notes: ""
changes: []
dependencies:
- name: thisdep
from: 1.7.0
to: 1.10.1
meta:
commit: chore(deps): bump thisdep from 1.7.0 to 1.10.1
- name: thisdep
from: 1.10.1
to: 1.7.0
meta:
commit: Revert "chore(deps): bump thisdep from 1.7.0 to 1.10.1"
`) + "\n",
},
{
name: "Markdown_Dependabot",
md: mdChangelog,
Expand Down
20 changes: 16 additions & 4 deletions src/changelog/sources/dependabot/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
log "github.com/sirupsen/logrus"
)

var commitRegex = regexp.MustCompile(`(?m)[Bb]ump (\S+)(?: from (\S+))?(?: to (\S+))?(?:.+\([#!](\d+)\)$)?`)
var commitRegex = regexp.MustCompile(`(?m)[Bb]ump (\S+)(?: from (\S+))?(?: to (\S+))?(?:.+\([#!](\d+)\)$)?(?:")?`)

const dependabotAuthor = "dependabot"

Expand All @@ -28,6 +28,7 @@ func NewSource(tagsVersionGetter git.TagsVersionGetter, commitsGetter git.Commit
}
}

//nolint:gocyclo,cyclop
func (r Source) Changelog() (*changelog.Changelog, error) {
lastHash, err := r.tagsVersionGetter.LastVersionHash()
if err != nil {
Expand All @@ -46,12 +47,19 @@ func (r Source) Changelog() (*changelog.Changelog, error) {

for _, c := range gitCommits {
commitLine := strings.Split(c.Message, "\n")[0]
if !strings.Contains(strings.ToLower(c.Author), dependabotAuthor) {
log.Debugf("skipping commit as it is not authored by dependabot\n> %q", commitLine)
isRevert := strings.HasPrefix(strings.ToLower(commitLine), "revert")
if !strings.Contains(strings.ToLower(c.Author), dependabotAuthor) && !isRevert {
log.Debugf("skipping commit as it is neither authored by dependabot nor a revert\n> %q", commitLine)
continue
}

capturingGroups := commitRegex.FindStringSubmatch(c.Message)
if isRevert {
// Revert commits usually surround the original commit line with double quotes.
// We could complicate the regex further, but stripping the quotes is allegedly easier.
commitLine = strings.Trim(commitLine, `"`)
}

capturingGroups := commitRegex.FindStringSubmatch(commitLine)
if len(capturingGroups) == 0 {
log.Debugf("skipping commit %s as it does not match dependabot pattern and no information can be retrieved", c.Message)
continue
Expand All @@ -67,6 +75,10 @@ func (r Source) Changelog() (*changelog.Changelog, error) {
log.Debugf("skipping dependency %q as it doesn't conform to semver %v", dependencyName, dependencyTo)
}

if isRevert {
dependencyTo, dependencyFrom = dependencyFrom, dependencyTo
}

dependencies = append(dependencies, changelog.Dependency{
Name: dependencyName,
From: dependencyFrom,
Expand Down
5 changes: 5 additions & 0 deletions src/changelog/sources/dependabot/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ Bumps [actions/github-script](https://github.com/actions/github-script) from 4.0
errExpected: errRandomError,
expected: nil,
},
{
name: "Revert_Matching_Commit_Single_Root_Minor",
commit: git.Commit{Message: "Revert \"chore(deps): bump my-dep from 1.2.3 to 2.0.0\"", Author: "Not-a-bot"},
expected: []changelog.Dependency{{Name: "my-dep", From: semver.MustParse("2.0.0"), To: semver.MustParse("1.2.3")}},
},
} {
tc := tc
if tc.name == "" {
Expand Down
13 changes: 11 additions & 2 deletions src/changelog/sources/renovate/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func NewSource(tagsVersionGetter git.TagsVersionGetter, commitsGetter git.Commit
}
}

//nolint:gocyclo,cyclop
func (r Source) Changelog() (*changelog.Changelog, error) {
lastHash, err := r.tagsVersionGetter.LastVersionHash()
if err != nil {
Expand All @@ -47,8 +48,9 @@ func (r Source) Changelog() (*changelog.Changelog, error) {
var commitDependencies []changelog.Dependency

commitLine := strings.Split(c.Message, "\n")[0]
if !strings.Contains(strings.ToLower(c.Author), renovateAuthor) {
log.Debugf("skipping commit as it is not authored by renovate\n> %q", commitLine)
isRevert := strings.HasPrefix(strings.ToLower(commitLine), "revert")
if !strings.Contains(strings.ToLower(c.Author), renovateAuthor) && !isRevert {
log.Debugf("skipping commit as it is neither authored by renovate nor a revert\n> %q", commitLine)
continue
}

Expand All @@ -66,6 +68,13 @@ func (r Source) Changelog() (*changelog.Changelog, error) {
commitDependencies = append(commitDependencies, r.titleDependencies(commitLine)...)
}

if isRevert {
// If this is a revert commit, swap to/from for all commitDependencies.
for i := range commitDependencies {
commitDependencies[i].From, commitDependencies[i].To = commitDependencies[i].To, commitDependencies[i].From
}
}

// Add commit hash and copy to dependencies
for _, dep := range commitDependencies {
dep.Meta.Commit = c.Hash
Expand Down
15 changes: 14 additions & 1 deletion src/changelog/sources/renovate/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (c *commitsGetterMock) Commits(_ string) ([]git.Commit, error) {
return commits, nil
}

//nolint:funlen
//nolint:funlen,maintidx
func TestSource_Source(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
Expand Down Expand Up @@ -196,6 +196,19 @@ func TestSource_Source(t *testing.T) {
},
},
},
{
name: "Matches_Reverts",
defaultAuthor: "not-a-bot",
commitMessages: []git.Commit{
{Message: "Revert \"update noprefix to v1.2.3\""},
},
expectedDependencies: []changelog.Dependency{
{
Name: "noprefix",
From: semver.MustParse("v1.2.3"),
},
},
},
{
name: "Error_No_Releases",
defaultAuthor: "renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>",
Expand Down