Skip to content

Commit

Permalink
Fix broken links by removing module name from github links
Browse files Browse the repository at this point in the history
  • Loading branch information
willie-yao committed Feb 1, 2024
1 parent 52ae1b8 commit e702c70
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ stdenv: &stdenv
executors:
container:
docker:
- image: cimg/go:1.20
- image: cimg/go:1.21
user: circleci
<<: *stdenv
working_directory: *workdir
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/saschagrunert/go-modiff

go 1.20
go 1.21

require (
github.com/onsi/ginkgo/v2 v2.9.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2/go.mod h1:k9Qvh+8juN+UKMCS/3jFtGICgW8O96FVaZsaxdzDkR4=
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a/go.mod h1:ryS0uhF+x9jgbj/N71xsEqODy9BN81/GonCZiOzirOk=
github.com/golangci/errcheck v0.0.0-20181223084120-ef45e06d44b6/go.mod h1:DbHgvLiFKX1Sh2T1w8Q/h4NAI8MHIpzCdnBUDTXU3I0=
Expand Down Expand Up @@ -325,6 +326,7 @@ google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoA
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
73 changes: 49 additions & 24 deletions pkg/modiff/modiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ import (
"fmt"
"os"
"os/exec"
"slices"
"sort"
"strings"

"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 +102,45 @@ 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) {
txt += fmt.Sprintf("[%s](%s/tree/%s)",
mod.after, toURL(name), sanitizeTag(mod.after))
splitLinkPrefix := strings.Split(mod.linkPrefix, "/")
prefixWithTree := fmt.Sprintf("%s/%s", mod.linkPrefix, "tree")
if mod.beforeVersion == "" { //nolint: gocritic
if addLinks && isGitHubURL(mod.linkPrefix) {

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

View check run for this annotation

Codecov / codecov/patch

pkg/modiff/modiff.go#L108

Added line #L108 was not covered by tests
// Insert the tree part of the URL at index 3 to account for tag names with slashes
if len(splitLinkPrefix) >= 3 {
prefixWithTree = strings.Join(slices.Insert(splitLinkPrefix, 3, "tree"), "/")

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

View check run for this annotation

Codecov / codecov/patch

pkg/modiff/modiff.go#L110-L111

Added lines #L110 - L111 were not covered by tests
}
txt += fmt.Sprintf("[%s](%s/%s)",
mod.afterVersion, toURL(prefixWithTree), sanitizeTag(mod.afterVersion))

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

View check run for this annotation

Codecov / codecov/patch

pkg/modiff/modiff.go#L113-L114

Added lines #L113 - L114 were not covered by tests
} else {
txt += mod.after
txt += mod.afterVersion

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

View check run for this annotation

Codecov / codecov/patch

pkg/modiff/modiff.go#L116

Added line #L116 was not covered by tests
}
added = append(added, txt)
} else if mod.after == "" {
if addLinks && isGitHubURL(name) {
txt += fmt.Sprintf("[%s](%s/tree/%s)",
mod.before, toURL(name), sanitizeTag(mod.before))
} else if mod.afterVersion == "" {
if addLinks && isGitHubURL(mod.linkPrefix) {
if len(splitLinkPrefix) >= 3 {
prefixWithTree = strings.Join(slices.Insert(splitLinkPrefix, 3, "tree"), "/")
}
txt += fmt.Sprintf("[%s](%s/%s)",
mod.beforeVersion, toURL(prefixWithTree), sanitizeTag(mod.beforeVersion))
} else {
txt += mod.before
txt += mod.beforeVersion
}
removed = append(removed, txt)
} else if mod.before != mod.after {
if addLinks && isGitHubURL(name) {
txt += fmt.Sprintf("[%s → %s](%s/compare/%s...%s)",
mod.before, mod.after, toURL(name),
sanitizeTag(mod.before), sanitizeTag(mod.after))
} else if mod.beforeVersion != mod.afterVersion {
if addLinks && isGitHubURL(mod.linkPrefix) {
prefixWithCompare := fmt.Sprintf("%s/%s", mod.linkPrefix, "compare")
// Insert tag prefix to the afterVersion to account for tag names with slashes
afterVersion := sanitizeTag(mod.afterVersion)
if len(splitLinkPrefix) > 3 {
prefixWithCompare = strings.Join(slices.Insert(splitLinkPrefix, 3, "compare"), "/")
afterVersion = fmt.Sprintf("%s/%s", strings.Join(splitLinkPrefix[3:], "/"), afterVersion)

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

View check run for this annotation

Codecov / codecov/patch

pkg/modiff/modiff.go#L136-L137

Added lines #L136 - L137 were not covered by tests
}
txt += fmt.Sprintf("[%s → %s](%s/%s...%s)",
mod.beforeVersion, mod.afterVersion, toURL(prefixWithCompare),
sanitizeTag(mod.beforeVersion), 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 +190,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 @@ -193,7 +211,13 @@ func getModules(workDir, from, to string) (modules, error) {
split[1] = split[4]
}
}

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

// Prettify pseudo versions
Expand All @@ -210,16 +234,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 e702c70

Please sign in to comment.