Skip to content

Commit a44391e

Browse files
authored
Fix links in release doc so they point to the repo with the right tag (#161)
1 parent 4497e54 commit a44391e

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

.github/cmd/pkg/cmd/export_md.go

+18-8
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,32 @@ var exportMDCommand = &cobra.Command{
2727
"complexity.md",
2828
}
2929

30+
conf := &problems.ParseConf{
31+
Dir: dir,
32+
ReplacePackageWithMain: cmd.Flag(replacePackageWithMainFlag).Value.String() == "true",
33+
ReplaceWithLiveLinks: cmd.Flag(replaceWithLiveLinksFlag).Value.String() == "true",
34+
Version: cmd.Flag(versionFlag).Value.String(),
35+
}
36+
3037
for _, file := range nonSectionMDFiles {
3138
content, err := os.ReadFile(filepath.Join(dir, file))
3239
if err != nil {
3340
log.Fatalf("Error reading file: %s", err)
3441
}
35-
fmt.Println(string(content))
42+
43+
contentStr := string(content)
44+
45+
if conf.ReplaceWithLiveLinks {
46+
contentStr = problems.ReplaceWithLiveLinks(contentStr, "", conf)
47+
}
48+
49+
fmt.Println(string(contentStr))
3650
}
3751

3852
for _, section := range problems.OrderedSections {
39-
parsedSection, err := problems.ParseSection(&problems.ParseConf{
40-
Dir: dir,
41-
Section: section,
42-
ReplacePackageWithMain: cmd.Flag(replacePackageWithMainFlag).Value.String() == "true",
43-
ReplaceWithLiveLinks: cmd.Flag(replaceWithLiveLinksFlag).Value.String() == "true",
44-
Version: cmd.Flag(versionFlag).Value.String(),
45-
})
53+
conf.Section = section
54+
55+
parsedSection, err := problems.ParseSection(conf)
4656
if err != nil {
4757
log.Fatalf("Error parsing section: %s", err)
4858
}

.github/cmd/pkg/problems/parse.go

+25
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path/filepath"
99
"regexp"
10+
"strings"
1011
)
1112

1213
type ParseConf struct {
@@ -17,6 +18,11 @@ type ParseConf struct {
1718
Version string
1819
}
1920

21+
const (
22+
repoAddress = "https://github.com/spring1843/go-dsa/tree/"
23+
mainBranch = "main"
24+
)
25+
2026
var rehearsalRegex = regexp.MustCompile(`(?s)(## Rehearsal\n.*?)(\n##|\z)`)
2127

2228
func ParseSection(conf *ParseConf) (string, error) {
@@ -30,6 +36,10 @@ func ParseSection(conf *ParseConf) (string, error) {
3036
return "", fmt.Errorf("error replacing the rehearsal section in file: %w", err)
3137
}
3238

39+
if conf.ReplaceWithLiveLinks {
40+
preparedContent = ReplaceWithLiveLinks(preparedContent, conf.Section, conf)
41+
}
42+
3343
return preparedContent, nil
3444
}
3545

@@ -50,3 +60,18 @@ func replaceRehearsal(input string, conf *ParseConf) (string, error) {
5060
replacement := fmt.Sprintf("## Rehearsal\n%s", stringRehearsalEntries(conf, newRehearsals))
5161
return rehearsalRegex.ReplaceAllString(input, replacement), nil
5262
}
63+
64+
// ReplaceWithLiveLinks replaces the relative links in the input with links to the repo that work.
65+
func ReplaceWithLiveLinks(input, section string, conf *ParseConf) string {
66+
repoBase := repoAddress + conf.Version
67+
if conf.Version == "" {
68+
repoBase = repoAddress + mainBranch
69+
}
70+
71+
output := input
72+
if section != "" {
73+
output = strings.ReplaceAll(output, "](../", "]("+repoBase+"/")
74+
}
75+
76+
return strings.ReplaceAll(output, "](./", "]("+repoBase+"/")
77+
}

.github/workflows/release.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
steps:
1616
- uses: actions/checkout@v4
1717
- uses: actions/setup-go@v5
18-
- run: make build
18+
- run: make build VERSION=${{ github.ref }}
1919
- name: Create Release
2020
id: create_release
2121
uses: actions/create-release@v1

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ all: build
22

33
build:
44
mkdir -p ./bin
5-
go run ./.github/cmd/main.go export-md > ./bin/go-dsa.md
5+
go run "./.github/cmd/main.go" "export-md" "--version=$(VERSION)" > "./bin/go-dsa.md"
66

77
test:
88
go test --race -v -coverprofile=profile.cov ./...

0 commit comments

Comments
 (0)