@@ -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
224256func 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