Skip to content

Commit

Permalink
Add new field for link prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
willie-yao committed Feb 1, 2024
1 parent 19d2701 commit 94be43b
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions pkg/modiff/modiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import (
"github.com/sirupsen/logrus"
)

type versions struct {
before string
after string
type entry struct {
beforeVersion string
afterVersion string
linkPrefix string
}

type modules = map[string]versions
type modules = map[string]entry

// Config is the structure passed to `Run`
type Config struct {
Expand Down Expand Up @@ -100,29 +101,29 @@ func diffModules(mods modules, addLinks bool, headerLevel uint) string {
var added, removed, changed []string
for name, mod := range mods {
txt := fmt.Sprintf("- %s: ", name)
if mod.before == "" { //nolint: gocritic
if addLinks && isGitHubURL(name) {
if mod.beforeVersion == "" { //nolint: gocritic
if addLinks && isGitHubURL(mod.linkPrefix) {

Check warning on line 105 in pkg/modiff/modiff.go

View check run for this annotation

Codecov / codecov/patch

pkg/modiff/modiff.go#L105

Added line #L105 was not covered by tests
txt += fmt.Sprintf("[%s](%s/tree/%s)",
mod.after, toURL(name), sanitizeTag(mod.after))
mod.afterVersion, toURL(mod.linkPrefix), sanitizeTag(mod.afterVersion))

Check warning on line 107 in pkg/modiff/modiff.go

View check run for this annotation

Codecov / codecov/patch

pkg/modiff/modiff.go#L107

Added line #L107 was not covered by tests
} else {
txt += mod.after
txt += mod.afterVersion

Check warning on line 109 in pkg/modiff/modiff.go

View check run for this annotation

Codecov / codecov/patch

pkg/modiff/modiff.go#L109

Added line #L109 was not covered by tests
}
added = append(added, txt)
} else if mod.after == "" {
if addLinks && isGitHubURL(name) {
} else if mod.afterVersion == "" {
if addLinks && isGitHubURL(mod.linkPrefix) {
txt += fmt.Sprintf("[%s](%s/tree/%s)",
mod.before, toURL(name), sanitizeTag(mod.before))
mod.beforeVersion, toURL(mod.linkPrefix), sanitizeTag(mod.beforeVersion))
} else {
txt += mod.before
txt += mod.beforeVersion
}
removed = append(removed, txt)
} else if mod.before != mod.after {
if addLinks && isGitHubURL(name) {
} else if mod.beforeVersion != mod.afterVersion {
if addLinks && isGitHubURL(mod.linkPrefix) {
txt += fmt.Sprintf("[%s → %s](%s/compare/%s...%s)",
mod.before, mod.after, toURL(name),
sanitizeTag(mod.before), sanitizeTag(mod.after))
mod.beforeVersion, mod.afterVersion, toURL(mod.linkPrefix),
sanitizeTag(mod.beforeVersion), sanitizeTag(mod.afterVersion))
} else {
txt += fmt.Sprintf("%s → %s", mod.before, mod.after)
txt += fmt.Sprintf("%s → %s", mod.beforeVersion, mod.afterVersion)
}
changed = append(changed, txt)
}
Expand Down Expand Up @@ -172,7 +173,7 @@ func getModules(workDir, from, to string) (modules, error) {

// Parse the modules
res := modules{}
forEach := func(input string, do func(res *versions, version string)) {
forEach := func(input string, do func(res *entry, version string)) {
scanner := bufio.NewScanner(strings.NewReader(input))
for scanner.Scan() {
// Skip version-less modules, like the local one
Expand All @@ -195,10 +196,11 @@ func getModules(workDir, from, to string) (modules, error) {
}

name := strings.TrimSpace(split[0])
linkPrefix := name
// Remove the module name from the link
if len(strings.Split(name, "/")) > 3 {
lastInd := strings.LastIndex(name, "/")
name = name[:lastInd]
linkPrefix = name[:lastInd]
}
version := strings.TrimSpace(split[1])

Expand All @@ -216,16 +218,17 @@ func getModules(workDir, from, to string) (modules, error) {
}

// Process the entry
entry := &versions{}
entry := &entry{}
if val, ok := res[name]; ok {
entry = &val
}
do(entry, version)
entry.linkPrefix = linkPrefix
res[name] = *entry
}
}
forEach(before, func(res *versions, v string) { res.before = v })
forEach(after, func(res *versions, v string) { res.after = v })
forEach(before, func(res *entry, v string) { res.beforeVersion = v })
forEach(after, func(res *entry, v string) { res.afterVersion = v })

logrus.Infof("%d modules found", len(res))

Expand Down

0 comments on commit 94be43b

Please sign in to comment.