Skip to content

Commit 5076f46

Browse files
willie-yaosaschagrunert
authored andcommitted
Fix broken links by removing module name from github links
1 parent 52ae1b8 commit 5076f46

File tree

8 files changed

+57
-28
lines changed

8 files changed

+57
-28
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ stdenv: &stdenv
99
executors:
1010
container:
1111
docker:
12-
- image: cimg/go:1.20
12+
- image: cimg/go:1.21
1313
user: circleci
1414
<<: *stdenv
1515
working_directory: *workdir

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ linters:
1212
- containedctx
1313
- contextcheck
1414
- decorder
15-
- depguard
1615
- dogsled
1716
- dupl
1817
- dupword
@@ -89,6 +88,7 @@ linters:
8988
- whitespace
9089
- wrapcheck
9190
# - cyclop
91+
# - depguard
9292
# - exhaustruct
9393
# - forbidigo
9494
# - funlen

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ $(GO_MODIFF_STATIC):
4141

4242
$(GOLANGCI_LINT):
4343
export \
44-
VERSION=v1.52.2 \
44+
VERSION=v1.55.2 \
4545
URL=https://raw.githubusercontent.com/golangci/golangci-lint \
4646
BINDIR=$(BUILD_PATH) && \
4747
curl -sfL $$URL/$$VERSION/install.sh | sh -s $$VERSION

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/saschagrunert/go-modiff
22

3-
go 1.20
3+
go 1.21
44

55
require (
66
github.com/onsi/ginkgo/v2 v2.9.2

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
7373
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
7474
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
7575
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
76+
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
7677
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2/go.mod h1:k9Qvh+8juN+UKMCS/3jFtGICgW8O96FVaZsaxdzDkR4=
7778
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a/go.mod h1:ryS0uhF+x9jgbj/N71xsEqODy9BN81/GonCZiOzirOk=
7879
github.com/golangci/errcheck v0.0.0-20181223084120-ef45e06d44b6/go.mod h1:DbHgvLiFKX1Sh2T1w8Q/h4NAI8MHIpzCdnBUDTXU3I0=
@@ -325,6 +326,7 @@ google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoA
325326
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
326327
google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
327328
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
329+
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
328330
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
329331
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
330332
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

pkg/modiff/modiff.go

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@ import (
66
"fmt"
77
"os"
88
"os/exec"
9+
"slices"
910
"sort"
1011
"strings"
1112

1213
"github.com/sirupsen/logrus"
1314
)
1415

15-
type versions struct {
16-
before string
17-
after string
16+
type entry struct {
17+
beforeVersion string
18+
afterVersion string
19+
linkPrefix string
1820
}
1921

20-
type modules = map[string]versions
22+
type modules = map[string]entry
2123

2224
// Config is the structure passed to `Run`
2325
type Config struct {
@@ -100,29 +102,45 @@ func diffModules(mods modules, addLinks bool, headerLevel uint) string {
100102
var added, removed, changed []string
101103
for name, mod := range mods {
102104
txt := fmt.Sprintf("- %s: ", name)
103-
if mod.before == "" { //nolint: gocritic
104-
if addLinks && isGitHubURL(name) {
105-
txt += fmt.Sprintf("[%s](%s/tree/%s)",
106-
mod.after, toURL(name), sanitizeTag(mod.after))
105+
splitLinkPrefix := strings.Split(mod.linkPrefix, "/")
106+
prefixWithTree := fmt.Sprintf("%s/%s", mod.linkPrefix, "tree")
107+
if mod.beforeVersion == "" { //nolint: gocritic
108+
if addLinks && isGitHubURL(mod.linkPrefix) {
109+
// Insert the tree part of the URL at index 3 to account for tag names with slashes
110+
if len(splitLinkPrefix) >= 3 {
111+
prefixWithTree = strings.Join(slices.Insert(splitLinkPrefix, 3, "tree"), "/")
112+
}
113+
txt += fmt.Sprintf("[%s](%s/%s)",
114+
mod.afterVersion, toURL(prefixWithTree), sanitizeTag(mod.afterVersion))
107115
} else {
108-
txt += mod.after
116+
txt += mod.afterVersion
109117
}
110118
added = append(added, txt)
111-
} else if mod.after == "" {
112-
if addLinks && isGitHubURL(name) {
113-
txt += fmt.Sprintf("[%s](%s/tree/%s)",
114-
mod.before, toURL(name), sanitizeTag(mod.before))
119+
} else if mod.afterVersion == "" {
120+
if addLinks && isGitHubURL(mod.linkPrefix) {
121+
if len(splitLinkPrefix) >= 3 {
122+
prefixWithTree = strings.Join(slices.Insert(splitLinkPrefix, 3, "tree"), "/")
123+
}
124+
txt += fmt.Sprintf("[%s](%s/%s)",
125+
mod.beforeVersion, toURL(prefixWithTree), sanitizeTag(mod.beforeVersion))
115126
} else {
116-
txt += mod.before
127+
txt += mod.beforeVersion
117128
}
118129
removed = append(removed, txt)
119-
} else if mod.before != mod.after {
120-
if addLinks && isGitHubURL(name) {
121-
txt += fmt.Sprintf("[%s → %s](%s/compare/%s...%s)",
122-
mod.before, mod.after, toURL(name),
123-
sanitizeTag(mod.before), sanitizeTag(mod.after))
130+
} else if mod.beforeVersion != mod.afterVersion {
131+
if addLinks && isGitHubURL(mod.linkPrefix) {
132+
prefixWithCompare := fmt.Sprintf("%s/%s", mod.linkPrefix, "compare")
133+
// Insert tag prefix to the afterVersion to account for tag names with slashes
134+
afterVersion := sanitizeTag(mod.afterVersion)
135+
if len(splitLinkPrefix) > 3 {
136+
prefixWithCompare = strings.Join(slices.Insert(splitLinkPrefix, 3, "compare"), "/")
137+
afterVersion = fmt.Sprintf("%s/%s", strings.Join(splitLinkPrefix[3:], "/"), afterVersion)
138+
}
139+
txt += fmt.Sprintf("[%s → %s](%s/%s...%s)",
140+
mod.beforeVersion, mod.afterVersion, toURL(prefixWithCompare),
141+
sanitizeTag(mod.beforeVersion), afterVersion)
124142
} else {
125-
txt += fmt.Sprintf("%s → %s", mod.before, mod.after)
143+
txt += fmt.Sprintf("%s → %s", mod.beforeVersion, mod.afterVersion)
126144
}
127145
changed = append(changed, txt)
128146
}
@@ -172,7 +190,7 @@ func getModules(workDir, from, to string) (modules, error) {
172190

173191
// Parse the modules
174192
res := modules{}
175-
forEach := func(input string, do func(res *versions, version string)) {
193+
forEach := func(input string, do func(res *entry, version string)) {
176194
scanner := bufio.NewScanner(strings.NewReader(input))
177195
for scanner.Scan() {
178196
// Skip version-less modules, like the local one
@@ -193,7 +211,13 @@ func getModules(workDir, from, to string) (modules, error) {
193211
split[1] = split[4]
194212
}
195213
}
214+
196215
name := strings.TrimSpace(split[0])
216+
linkPrefix := name
217+
// Remove the module name from the link
218+
if splitLink := strings.Split(linkPrefix, "/"); len(splitLink) == 4 {
219+
linkPrefix = strings.Join(splitLink[:3], "/")
220+
}
197221
version := strings.TrimSpace(split[1])
198222

199223
// Prettify pseudo versions
@@ -210,16 +234,17 @@ func getModules(workDir, from, to string) (modules, error) {
210234
}
211235

212236
// Process the entry
213-
entry := &versions{}
237+
entry := &entry{}
214238
if val, ok := res[name]; ok {
215239
entry = &val
216240
}
217241
do(entry, version)
242+
entry.linkPrefix = linkPrefix
218243
res[name] = *entry
219244
}
220245
}
221-
forEach(before, func(res *versions, v string) { res.before = v })
222-
forEach(after, func(res *versions, v string) { res.after = v })
246+
forEach(before, func(res *entry, v string) { res.beforeVersion = v })
247+
forEach(after, func(res *entry, v string) { res.afterVersion = v })
223248

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

pkg/modiff/modiff_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package modiff_test
22

3+
//nolint:revive // test file
34
import (
45
. "github.com/onsi/ginkgo/v2"
56
. "github.com/onsi/gomega"

pkg/modiff/suite_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package modiff_test
22

3+
//nolint:revive // test file
34
import (
45
"testing"
56

0 commit comments

Comments
 (0)