Skip to content

Commit ca6d93f

Browse files
authored
chore: update checks for modfiles (#91)
## Description Adds some guardrails to release while cross-team talks happen for release engineering of `pkg`.
1 parent 6bca4d8 commit ca6d93f

File tree

6 files changed

+142
-7
lines changed

6 files changed

+142
-7
lines changed

.github/actions/bump-and-notes/action.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ inputs:
44
module:
55
description: "The module to be released"
66
required: true
7+
pr-title:
8+
description: "The PR Title that will be merged"
9+
default: ""
710

811
outputs:
912
new-version:
@@ -23,10 +26,13 @@ runs:
2326
- name: bump module version
2427
env:
2528
MODULE: ${{ inputs.module }}
29+
PR_TITLE: ${{ inputs.pr-title }}
2630
id: bump-version
31+
# We run the go mod check twice and outside echo so as to not swallow the exit code / error message
2732
run: |
2833
cd internal/release
29-
echo "new-version=$(go run main.go "$MODULE")" >> "$GITHUB_OUTPUT"
34+
go run main.go "$MODULE" "$PR_TITLE"
35+
echo "new-version=$(go run main.go "$MODULE" "$PR_TITLE")" >> "$GITHUB_OUTPUT"
3036
shell: bash
3137

3238
- name: install git cliff

.github/workflows/check-exec.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Check Exec
2+
3+
on:
4+
pull_request:
5+
branches: [main]
6+
types: [opened, edited, synchronize]
7+
paths:
8+
- "exec/**"
9+
10+
permissions:
11+
contents: read
12+
13+
jobs:
14+
bump-version-and-release-notes:
15+
runs-on: ubuntu-latest
16+
outputs:
17+
new-version: ${{ steps.bump-version.outputs.new-version }}
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5
21+
with:
22+
fetch-depth: 0
23+
24+
- name: Bump Version and Generate Release Notes
25+
uses: ./.github/actions/bump-and-notes
26+
id: bump-version
27+
with:
28+
module: "exec"
29+
pr-title: ${{ github.event.pull_request.title }}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Check Helpers
2+
3+
on:
4+
pull_request:
5+
branches: [main]
6+
types: [opened, edited, synchronize]
7+
paths:
8+
- "helpers/**"
9+
10+
permissions:
11+
contents: read
12+
13+
jobs:
14+
bump-version-and-release-notes:
15+
runs-on: ubuntu-latest
16+
outputs:
17+
new-version: ${{ steps.bump-version.outputs.new-version }}
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5
21+
with:
22+
fetch-depth: 0
23+
24+
- name: Bump Version and Generate Release Notes
25+
uses: ./.github/actions/bump-and-notes
26+
id: bump-version
27+
with:
28+
module: "helpers"
29+
pr-title: ${{ github.event.pull_request.title }}

.github/workflows/check-oci.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Check OCI
2+
3+
on:
4+
pull_request:
5+
branches: [main]
6+
types: [opened, edited, synchronize]
7+
paths:
8+
- "oci/**"
9+
10+
permissions:
11+
contents: read
12+
13+
jobs:
14+
bump-version-and-release-notes:
15+
runs-on: ubuntu-latest
16+
outputs:
17+
new-version: ${{ steps.bump-version.outputs.new-version }}
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5
21+
with:
22+
fetch-depth: 0
23+
24+
- name: Bump Version and Generate Release Notes
25+
uses: ./.github/actions/bump-and-notes
26+
id: bump-version
27+
with:
28+
module: "oci"
29+
pr-title: ${{ github.event.pull_request.title }}

helpers/transport.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,6 @@ func (t *Transport) roundTrip(req *http.Request) (resp *http.Response, err error
9393
t.ProgressBar.Write(b)
9494
}
9595
}
96+
9697
return resp, err
9798
}

internal/release/main.go

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"path/filepath"
1212
"regexp"
1313
"sort"
14+
"strconv"
1415
"strings"
1516

1617
"github.com/Masterminds/semver"
@@ -29,29 +30,55 @@ func getProjectPath() (string, error) {
2930
return filepath.Dir(filepath.Dir(wd)), nil
3031
}
3132

32-
func validateModFile(path string) error {
33+
func validateModFile(path string, version *semver.Version) error {
3334
// Ensure the path is consistent with the go mod
3435
baseProjectPath, err := getProjectPath()
3536
if err != nil {
3637
return err
3738
}
39+
3840
modPath := filepath.Join(baseProjectPath, path, "go.mod")
3941
bytes, err := os.ReadFile(modPath)
4042
if err != nil {
4143
return err
4244
}
45+
4346
modFile, err := modfile.Parse(modPath, bytes, nil)
4447
if err != nil {
4548
return err
4649
}
50+
51+
actualModPath := modFile.Module.Mod.Path
52+
var actualMajorVersion int64
53+
54+
// Strip the /vX from the end of any mod paths
55+
actualModPathSections := strings.Split(actualModPath, "/")
56+
if len(actualModPathSections) == 5 {
57+
actualModPath = strings.Join(actualModPathSections[:4], "/")
58+
actualMajorVersion, err = strconv.ParseInt(strings.TrimPrefix(actualModPathSections[4], "v"), 10, 0)
59+
if err != nil {
60+
return err
61+
}
62+
}
63+
64+
// Check that the mod path is what we expect
4765
expectedModPath := fmt.Sprintf("github.com/defenseunicorns/pkg/%s", path)
48-
if expectedModPath != modFile.Module.Mod.Path {
66+
if expectedModPath != actualModPath {
4967
return fmt.Errorf("the module name is incorrect or a %s does not exist as a module", path)
5068
}
69+
70+
// Check that the mod version is what we expect
71+
expectedMajorVersion := version.Major()
72+
if actualMajorVersion == 0 && expectedMajorVersion > 1 {
73+
return fmt.Errorf("the module name does not end in /v%d for a major version > 1", expectedMajorVersion)
74+
} else if actualMajorVersion > 1 && actualMajorVersion != expectedMajorVersion {
75+
return fmt.Errorf("the expected module version /v%d does not match /v%d", expectedMajorVersion, actualMajorVersion)
76+
}
77+
5178
return nil
5279
}
5380

54-
func bumpVersion(module string) (*semver.Version, error) {
81+
func bumpVersion(module string, prTitle string) (*semver.Version, error) {
5582
repoPath, err := getProjectPath()
5683
if err != nil {
5784
return nil, err
@@ -92,6 +119,11 @@ func bumpVersion(module string) (*semver.Version, error) {
92119
if err != nil {
93120
return nil, err
94121
}
122+
123+
if prTitle != "" {
124+
commits = append(commits, prTitle)
125+
}
126+
95127
if len(commits) == 0 {
96128
return nil, fmt.Errorf("no commits affecting module %s since last tag", module)
97129
}
@@ -222,20 +254,29 @@ func getTypeOfChange(commits []string) string {
222254
}
223255

224256
func main() {
225-
if len(os.Args) != 2 {
257+
if len(os.Args) < 2 {
226258
panic("this program should be called with the module name. For example, \"go run main.go helpers\"")
227259
}
260+
261+
var prCommitMsg string
262+
if len(os.Args) == 3 {
263+
prCommitMsg = os.Args[2]
264+
}
265+
228266
module := os.Args[1]
229-
err := validateModFile(module)
267+
268+
newVersion, err := bumpVersion(module, prCommitMsg)
230269
if err != nil {
231270
fmt.Println("Error: ", err)
232271
os.Exit(1)
233272
}
234-
newVersion, err := bumpVersion(module)
273+
274+
err = validateModFile(module, newVersion)
235275
if err != nil {
236276
fmt.Println("Error: ", err)
237277
os.Exit(1)
238278
}
279+
239280
fmt.Printf("%s/v%s", module, newVersion.String())
240281
os.Exit(0)
241282
}

0 commit comments

Comments
 (0)