Skip to content

Commit

Permalink
Extract version from target repo, to avoid duplicate PRs
Browse files Browse the repository at this point in the history
Signed-off-by: Jonathan West <[email protected]>
  • Loading branch information
jgwest committed Feb 13, 2024
1 parent e6aabb6 commit 260df5e
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 47 deletions.
2 changes: 2 additions & 0 deletions hack/upgrade-rollouts-script/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
settings.env
argo-rollouts-manager
7 changes: 7 additions & 0 deletions hack/upgrade-rollouts-script/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@ The Go code and script this in this directory will automatically open a pull req
### Configure and run the tool

```bash

# Set required Environment Variables
export GITHUB_FORK_USERNAME="(your username here)"
export GH_TOKEN="(a GitHub personal access token that can clone/push/open PRs against argo-rollouts-manager repo)"

# or, you can set these values in the settings.env file:
# cp settings_template.env settings.env
# Then set env vars in settings.env (which is excluded in the .gitignore)

./init-repo.sh
./go-run.sh
```
9 changes: 7 additions & 2 deletions hack/upgrade-rollouts-script/go-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

cd $SCRIPT_DIR

go run .

# Read Github Token and Username from settings.env, if it exists
vars_file="$SCRIPT_DIR/settings.env"
if [[ -f "$vars_file" ]]; then
source "$vars_file"
fi

# Run the upgrade code
go run .
10 changes: 10 additions & 0 deletions hack/upgrade-rollouts-script/init-repo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

cd $SCRIPT_DIR

# Read Github Token and Username from settings.env, if it exists
vars_file="$SCRIPT_DIR/settings.env"
if [[ -f "$vars_file" ]]; then
source "$vars_file"
fi

# Clone fork of argo-rollouts-manager repo

rm -rf "$SCRIPT_DIR/argo-rollouts-manager" || true

git clone "[email protected]:$GITHUB_FORK_USERNAME/argo-rollouts-manager"
cd argo-rollouts-manager

# Add a remote back to the original repo

git remote add parent "[email protected]:argoproj-labs/argo-rollouts-manager"
git fetch parent

152 changes: 107 additions & 45 deletions hack/upgrade-rollouts-script/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,27 @@ import (
"github.com/google/go-github/v58/github"
)

// These can be set while debugging
const (
PRTitle = "Upgrade to Argo Rollouts "
argoRolloutsRepoOrg = "argoproj"
argoRolloutsRepoName = "argo-rollouts"

argoprojlabsRepoOrg = "argoproj-labs"
argoRolloutsManagerRepoName = "argo-rollouts-manager"

skipInitialPRCheck = false // default to false

// if readOnly is true:
// - PRs will not be opened
// - Git commits will not be pushed to fork
// This is roughly equivalent to a dry run
readOnly = false // default to false

)

const (
PRTitle = "Upgrade to Argo Rollouts "
argoRolloutsRepoOrg = "argoproj"
argoRolloutsRepoName = "argo-rollouts"

argoprojlabsRepoOrg = "argoproj-labs"
argoRolloutsManagerRepoName = "argo-rollouts-manager"

controllersDefaultGo = "controllers/default.go"
)

func main() {
Expand Down Expand Up @@ -88,7 +94,13 @@ func main() {
newBranchName := "upgrade-to-rollouts-" + *firstProperRelease.TagName

// 3) Create, commit, and push a new branch
if err := createNewCommitAndBranch(*firstProperRelease.TagName, "quay.io/argoproj/argo-rollouts", newBranchName, pathToGitHubRepo); err != nil {
if repoAlreadyUpToDate, err := createNewCommitAndBranch(*firstProperRelease.TagName, "quay.io/argoproj/argo-rollouts", newBranchName, pathToGitHubRepo); err != nil {

if repoAlreadyUpToDate {
fmt.Println("* Exiting as target repository is already up to date.")
return
}

exitWithError(err)
return
}
Expand All @@ -101,8 +113,13 @@ func main() {
bodyText += ": " + *firstProperRelease.HTMLURL
}

bodyText += `
Before merging this PR, ensure you check the Argo Rollouts change logs and release notes:
- ensure there are no changes to the Argo Rollouts install YAML that we need to respond to with changes in the operator
- ensure there are no backwards incompatible API/behaviour changes in the change logs`

// 4) Create PR if it doesn't exist
if stdout, stderr, err := runCommandWithWD(pathToGitHubRepo, "gh", "pr", "create",
if stdout, stderr, err := runCommandWithWorkDir(pathToGitHubRepo, "gh", "pr", "create",
"-R", argoprojlabsRepoOrg+"/"+argoRolloutsManagerRepoName,
"--title", PRTitle+(*firstProperRelease.TagName), "--body", bodyText); err != nil {
fmt.Println(stdout, stderr)
Expand All @@ -113,32 +130,71 @@ func main() {

}

func createNewCommitAndBranch(latestReleaseVersionTag string, latestReleaseVersionImage, newBranchName, pathToGitRepo string) error {
// return true if the argo-rollouts-manager repo is already up to date
func createNewCommitAndBranch(latestReleaseVersionTag string, latestReleaseVersionImage, newBranchName, pathToGitRepo string) (bool, error) {

commands := [][]string{
{"git", "stash"},
{"git", "fetch", "parent"},
{"git", "checkout", "main"},
{"git", "rebase", "parent/main"},
{"git", "checkout", "-b", newBranchName},
}

if err := runCommandListWithWorkDir(pathToGitRepo, commands); err != nil {
return err
return false, err
}

if repoTargetVersion, err := extractCurrentTargetVersionFromRepo(pathToGitRepo); err != nil {
return false, fmt.Errorf("unable to extract current target version from repo")
} else if repoTargetVersion == latestReleaseVersionTag {
return true, fmt.Errorf("target repository is already on the most recent version")
}

if err := regenerateControllersDefaultGo(latestReleaseVersionTag, latestReleaseVersionImage, pathToGitRepo); err != nil {
return err
return false, err
}

if err := regenerateGoMod(latestReleaseVersionTag, pathToGitRepo); err != nil {
return err
return false, err
}

if err := regenerateArgoRolloutsE2ETestScriptMod(latestReleaseVersionTag, pathToGitRepo); err != nil {
return err
return false, err
}

if err := copyCRDsFromRolloutsRepo(latestReleaseVersionTag, pathToGitRepo); err != nil {
return false, fmt.Errorf("unable to copy rollouts CRDs: %w", err)
}

commands = [][]string{
{"go", "mod", "tidy"},
{"make", "generate", "manifests"},
{"make", "bundle"},
{"make", "fmt"},
{"git", "add", "--all"},
{"git", "commit", "-s", "-m", PRTitle + latestReleaseVersionTag},
}
if err := runCommandListWithWorkDir(pathToGitRepo, commands); err != nil {
return false, err
}

rolloutsRepoPath, err := checkoutRolloutsRepo(latestReleaseVersionTag)
if !readOnly {
commands = [][]string{
{"git", "push", "-f", "--set-upstream", "origin", newBranchName},
}
if err := runCommandListWithWorkDir(pathToGitRepo, commands); err != nil {
return false, err
}
}

return false, nil

}

func copyCRDsFromRolloutsRepo(latestReleaseVersionTag string, pathToGitRepo string) error {

rolloutsRepoPath, err := checkoutRolloutsRepoIntoTempDir(latestReleaseVersionTag)
if err != nil {
return err
}
Expand Down Expand Up @@ -196,60 +252,38 @@ func createNewCommitAndBranch(latestReleaseVersionTag string, latestReleaseVersi

}

commands = [][]string{
{"go", "mod", "tidy"},
{"make", "generate", "manifests"},
{"make", "bundle"},
{"make", "fmt"},
{"git", "add", "--all"},
{"git", "commit", "-s", "-m", PRTitle + latestReleaseVersionTag},
}
if err := runCommandListWithWorkDir(pathToGitRepo, commands); err != nil {
return err
}

if !readOnly {
commands = [][]string{
{"git", "push", "-f", "--set-upstream", "origin", newBranchName},
}
if err := runCommandListWithWorkDir(pathToGitRepo, commands); err != nil {
return err
}
}

return nil

}

func checkoutRolloutsRepo(latestReleaseVersionTag string) (string, error) {
func checkoutRolloutsRepoIntoTempDir(latestReleaseVersionTag string) (string, error) {

tmpDir, err := os.MkdirTemp("", "argo-rollouts-src")
if err != nil {
return "", err
}

if _, _, err := runCommandWithWD(tmpDir, "git", "clone", "https://github.com/argoproj/argo-rollouts"); err != nil {
if _, _, err := runCommandWithWorkDir(tmpDir, "git", "clone", "https://github.com/argoproj/argo-rollouts"); err != nil {
return "", err
}

newWD := filepath.Join(tmpDir, "argo-rollouts")
newWorkDir := filepath.Join(tmpDir, "argo-rollouts")

commands := [][]string{
{"git", "checkout", latestReleaseVersionTag},
}

if err := runCommandListWithWorkDir(newWD, commands); err != nil {
if err := runCommandListWithWorkDir(newWorkDir, commands); err != nil {
return "", err
}

return newWD, nil
return newWorkDir, nil
}

func runCommandListWithWorkDir(workingDir string, commands [][]string) error {

for _, command := range commands {

_, _, err := runCommandWithWD(workingDir, command...)
_, _, err := runCommandWithWorkDir(workingDir, command...)
if err != nil {
return err
}
Expand Down Expand Up @@ -325,11 +359,39 @@ func regenerateArgoRolloutsE2ETestScriptMod(latestReleaseVersionTag string, path

}

// extractCurrentTargetVersionFromRepo read the contents of the argo-rollouts-manager repo and determine which argo-rollouts version is being targeted.
func extractCurrentTargetVersionFromRepo(pathToGitRepo string) (string, error) {

// Style of text string to parse:
// DefaultArgoRolloutsVersion = "sha256:995450a0a7f7843d68e96d1a7f63422fa29b245c58f7b57dd0cf9cad72b8308f" //v1.4.1

path := filepath.Join(pathToGitRepo, controllersDefaultGo)

fileBytes, err := os.ReadFile(path)
if err != nil {
return "", err
}

for _, line := range strings.Split(string(fileBytes), "\n") {
if strings.Contains(line, "DefaultArgoRolloutsVersion") {

indexOfForwardSlash := strings.LastIndex(line, "/")
if indexOfForwardSlash != -1 {
return strings.TrimSpace(line[indexOfForwardSlash+1:]), nil
}

}
}

return "", fmt.Errorf("no version found in '" + controllersDefaultGo + "'")
}

func regenerateControllersDefaultGo(latestReleaseVersionTag string, latestReleaseVersionImage, pathToGitRepo string) error {

// Style of text string to replace:
// DefaultArgoRolloutsVersion = "sha256:995450a0a7f7843d68e96d1a7f63422fa29b245c58f7b57dd0cf9cad72b8308f" //v1.4.1

path := filepath.Join(pathToGitRepo, "controllers/default.go")
path := filepath.Join(pathToGitRepo, controllersDefaultGo)

fileBytes, err := os.ReadFile(path)
if err != nil {
Expand Down Expand Up @@ -358,7 +420,7 @@ func regenerateControllersDefaultGo(latestReleaseVersionTag string, latestReleas

}

func runCommandWithWD(workingDir string, cmdList ...string) (string, string, error) {
func runCommandWithWorkDir(workingDir string, cmdList ...string) (string, string, error) {

fmt.Println(cmdList)

Expand Down
2 changes: 2 additions & 0 deletions hack/upgrade-rollouts-script/settings_template.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export GITHUB_FORK_USERNAME="(your username here)"
export GH_TOKEN="(a GitHub personal access token that can clone/push/open PRs against argo-rollouts-manager repo)"

0 comments on commit 260df5e

Please sign in to comment.