Skip to content
Merged
Changes from 13 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 70 additions & 16 deletions dependency_updater/dependency_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@ import (
"context"
"encoding/json"
"fmt"
"slices"
"time"

"github.com/ethereum-optimism/optimism/op-service/retry"
"github.com/google/go-github/v72/github"
"github.com/urfave/cli/v3"
"slices"
"time"

"log"
"os"
"os/exec"
"strings"
)

type Info struct {
Tag string `json:"tag"`
Commit string `json:"commit"`
TagPrefix string `json:"tagPrefix,omitempty"`
Owner string `json:"owner`
Repo string `json:"repo`
Tag string `json:"tag"`
Commit string `json:"commit"`
TagPrefix string `json:"tagPrefix,omitempty"`
Owner string `json:"owner`
Repo string `json:"repo`
}

type VersionTag []struct {
Expand Down Expand Up @@ -49,9 +51,14 @@ func main() {
Usage: "Specifies repo location to run the version updater on",
Required: true,
},
&cli.BoolFlag{
Name: "commit",
Usage: "Stages updater changes and creates commit message",
Required: false,
},
},
Action: func(ctx context.Context, cmd *cli.Command) error {
err := updater(string(cmd.String("token")), string(cmd.String("repo")))
err := updater(string(cmd.String("token")), string(cmd.String("repo")), cmd.Bool("commit"))
if err != nil {
return fmt.Errorf("error running updater: %s", err)
}
Expand All @@ -64,7 +71,7 @@ func main() {
}
}

func updater(token string, repoPath string) error {
func updater(token string, repoPath string, commit bool) error {
var err error

f, err := os.ReadFile(repoPath + "/versions.json")
Expand All @@ -75,6 +82,7 @@ func updater(token string, repoPath string) error {
client := github.NewClient(nil).WithAuthToken(token)
ctx := context.Background()

var updatedDependencies [][]string
Comment thread
JenabaBa marked this conversation as resolved.
Outdated
var dependencies Dependencies

err = json.Unmarshal(f, &dependencies)
Expand All @@ -90,6 +98,7 @@ func updater(token string, repoPath string) error {
dependency,
repoPath,
dependencies,
&updatedDependencies,
)
})

Expand All @@ -98,6 +107,13 @@ func updater(token string, repoPath string) error {
}
}

if commit {
err := createCommitMessage(updatedDependencies)
if err != nil {
return fmt.Errorf("error creating commit message: %s", err)
}
}

e := createVersionsEnv(repoPath, dependencies)
if e != nil {
return fmt.Errorf("error creating versions.env: %s", e)
Expand All @@ -106,8 +122,26 @@ func updater(token string, repoPath string) error {
return nil
}

func getAndUpdateDependency(ctx context.Context, client *github.Client, dependencyType string, repoPath string, dependencies Dependencies) error {
version, commit, err := getVersionAndCommit(ctx, client, dependencies, dependencyType)
func createCommitMessage(updatedDependencies [][]string) error {
commitTitle := "chore: updated "
var commitDescription = "Updated dependencies for: \n"
for _, dependency := range updatedDependencies {
if len(dependency) != 0 {
repo, tag := dependency[0], dependency[1]
commitDescription += repo + " => " + tag + " (" + dependency[2] + ")" + "\n"
commitTitle += repo + ", "
Comment thread
JenabaBa marked this conversation as resolved.
Outdated
}
}
commitTitle = strings.TrimSuffix(commitTitle, ", ")
cmd := exec.Command("git", "commit", "-am", commitTitle, "-m", commitDescription)
if err := cmd.Run(); err != nil {
return fmt.Errorf("error running git commit -m: %s", err)
}
return nil
}

func getAndUpdateDependency(ctx context.Context, client *github.Client, dependencyType string, repoPath string, dependencies Dependencies, updatedDependencies *[][]string) error {
version, commit, err := getVersionAndCommit(ctx, client, dependencies, dependencyType, updatedDependencies)
if err != nil {
return err
}
Expand All @@ -120,9 +154,11 @@ func getAndUpdateDependency(ctx context.Context, client *github.Client, dependen
return nil
}

func getVersionAndCommit(ctx context.Context, client *github.Client, dependencies Dependencies, dependencyType string) (string, string, error) {
func getVersionAndCommit(ctx context.Context, client *github.Client, dependencies Dependencies, dependencyType string, updatedDependencies *[][]string) (string, string, error) {
var version *github.RepositoryRelease
var err error
var updates []string
var diffUrl string
foundPrefixVersion := false
options := &github.ListOptions{Page: 1}

Expand All @@ -139,12 +175,28 @@ func getVersionAndCommit(ctx context.Context, client *github.Client, dependencie

if dependencies[dependencyType].TagPrefix == "" {
version = releases[0]
if *version.TagName != dependencies[dependencyType].Tag {
diffUrl = "https://github.com/" +
dependencies[dependencyType].Owner + "/" +
dependencies[dependencyType].Repo + "/compare/" +
dependencies[dependencyType].Tag + "..." + *version.TagName

updates = append(updates, dependencies[dependencyType].Repo, *version.TagName, diffUrl)
}
break
} else if dependencies[dependencyType].TagPrefix != ""{
} else if dependencies[dependencyType].TagPrefix != "" {
for release := range releases {
if strings.HasPrefix(*releases[release].TagName, dependencies[dependencyType].TagPrefix) {
version = releases[release]
foundPrefixVersion = true
if *version.TagName != dependencies[dependencyType].Tag {
diffUrl = "https://github.com/" +
Comment thread
JenabaBa marked this conversation as resolved.
Outdated
dependencies[dependencyType].Owner + "/" +
dependencies[dependencyType].Repo + "/compare/" +
dependencies[dependencyType].Tag + "..." + *version.TagName

updates = append(updates, dependencies[dependencyType].Repo, *version.TagName, diffUrl)
}
break
}
}
Expand All @@ -157,6 +209,8 @@ func getVersionAndCommit(ctx context.Context, client *github.Client, dependencie
}
}

*updatedDependencies = append(*updatedDependencies, updates)

commit, _, err := client.Repositories.GetCommit(
ctx,
dependencies[dependencyType].Owner,
Expand Down Expand Up @@ -204,9 +258,9 @@ func createVersionsEnv(repoPath string, dependencies Dependencies) error {
envLines := []string{}

for dependency := range dependencies {
repoUrl := "https://github.com/" +
dependencies[dependency].Owner + "/" +
dependencies[dependency].Repo + ".git"
repoUrl := "https://github.com/" +
dependencies[dependency].Owner + "/" +
dependencies[dependency].Repo + ".git"

dependencyPrefix := strings.ToUpper(dependency)

Expand Down