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

Fix broken links by removing module name from github links #171

Merged
merged 1 commit into from
Feb 5, 2024
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
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
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updating to go 1.21 so I can use the slices package.

user: circleci
<<: *stdenv
working_directory: *workdir
Expand Down
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ linters:
- containedctx
- contextcheck
- decorder
- depguard
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure what's up with depguard, but I had to update golangci for go 1.21 and it marked every import as not allowed.

Copy link

Choose a reason for hiding this comment

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

this is ok, lgtm

- dogsled
- dupl
- dupword
Expand Down Expand Up @@ -89,6 +88,7 @@ linters:
- whitespace
- wrapcheck
# - cyclop
# - depguard
Copy link

Choose a reason for hiding this comment

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

you can remove this from the comment

# - exhaustruct
# - forbidigo
# - funlen
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ $(GO_MODIFF_STATIC):

$(GOLANGCI_LINT):
export \
VERSION=v1.52.2 \
VERSION=v1.55.2 \
URL=https://raw.githubusercontent.com/golangci/golangci-lint \
BINDIR=$(BUILD_PATH) && \
curl -sfL $$URL/$$VERSION/install.sh | sh -s $$VERSION
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 @@
"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 @@
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 @@

// 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 @@
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 @@
}

// 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
1 change: 1 addition & 0 deletions pkg/modiff/modiff_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package modiff_test

//nolint:revive // test file
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down
1 change: 1 addition & 0 deletions pkg/modiff/suite_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package modiff_test

//nolint:revive // test file
import (
"testing"

Expand Down