Last active
January 25, 2023 21:43
-
-
Save gmlewis/680621bc9ed2477e6cfa5832fcb7194e to your computer and use it in GitHub Desktop.
Work in progress to bump the version in a repo and push back to repo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main() { | |
ctx := context.Background() | |
if _, err := common.Build(ctx); err != nil { | |
log.Fatal(err) | |
} | |
if err := bumpMinorVersion(ctx); err != nil { | |
log.Fatal(err) | |
} | |
log.Printf("Done.") | |
} | |
var versionRE = regexp.MustCompile(`VERSION = '(\d+)\.(\d+)\.(\d+)'`) | |
func bumpMinorVersion(ctx context.Context) error { | |
githubUser := "Automated Minor Version Bump" | |
if v := os.Getenv("GITHUB_USER"); v != "" { | |
githubUser = v | |
} | |
githubEmail := "[email protected]" | |
if v := os.Getenv("GITHUB_EMAIL"); v != "" { | |
githubEmail = v | |
} | |
currentBranch := os.Getenv("GITHUB_HEAD_REF") | |
// if currentBranch == "" { | |
// return errors.New("missing env var GITHUB_HEAD_REF; aborting") | |
// } | |
log.Printf("current branch: %v", currentBranch) | |
client, err := common.NewDaggerClient(ctx) | |
if err != nil { | |
return err | |
} | |
defer client.Close() | |
hostRepo := common.HostRepo(client, dagger.HostDirectoryOpts{ | |
Exclude: []string{"node_modules"}, | |
}) | |
versionFile, err := hostRepo.Directory("src").File("version.js").Contents(ctx) | |
if err != nil { | |
return err | |
} | |
log.Printf("version.js contents:\n%v", versionFile) | |
m := versionRE.FindStringSubmatch(versionFile) | |
if got, want := len(m), 4; got != want { | |
return fmt.Errorf("unable to parse version.js file: len(m)=%v, want %v", got, want) | |
} | |
minorVersion, err := strconv.Atoi(m[2]) | |
if err != nil { | |
return err | |
} | |
minorVersion++ | |
newVersion := fmt.Sprintf("VERSION = '%v.%v.0'", m[1], minorVersion) | |
versionFile = strings.Replace(versionFile, m[0], newVersion, 1) | |
log.Printf("new version.js contents:\n%v", versionFile) | |
hostRepo = hostRepo.Directory("src").WithNewFile("version.js", versionFile) | |
golang, err := common.GoContainer(ctx, client) | |
if err != nil { | |
return err | |
} | |
workDir := "/temp-go-workdir" | |
golang = golang.WithMountedDirectory(workDir, hostRepo).WithWorkdir(workDir). | |
WithExec([]string{"git", "config", "user.name", githubUser}). | |
WithExec([]string{"git", "config", "user.email", githubEmail}). | |
WithExec([]string{"git", "fetch"}). | |
WithExec([]string{"git", "checkout", currentBranch}) | |
out, err := golang.Stdout(ctx) | |
if err != nil { | |
return err | |
} | |
log.Printf("%v", out) | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment