Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
45 changes: 33 additions & 12 deletions dependency_updater/dependency_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,16 @@ func updater(token string, repoPath string, commit bool, githubAction bool) erro
}

func createCommitMessage(updatedDependencies []VersionUpdateInfo, repoPath string, githubAction bool) error {
var repos []string
commitTitle := "chore: updated "
commitDescription := "Updated dependencies for: "

for _, dependency := range updatedDependencies {
repo, tag := dependency.Repo, dependency.To
commitDescription += repo + " => " + tag + " (" + dependency.DiffUrl + ") "
repos = append(repos, repo)
if len(updatedDependencies) == 0 {
return nil
}
commitDescription = strings.TrimSuffix(commitDescription, " ")
commitTitle += strings.Join(repos, ", ")

commitTitle, commitDescription := buildCommitMessageParts(updatedDependencies)

if githubAction {
err := writeToGithubOutput(commitTitle, commitDescription, repoPath)
if err != nil {
return fmt.Errorf("error creating git commit message: %s", err)
return fmt.Errorf("failed to create git commit message: %s", err)
}
} else if !githubAction {
cmd := exec.Command("git", "commit", "-am", commitTitle, "-m", commitDescription)
Expand All @@ -158,6 +152,33 @@ func createCommitMessage(updatedDependencies []VersionUpdateInfo, repoPath strin
return nil
}

func buildCommitMessageParts(updatedDependencies []VersionUpdateInfo) (string, string) {
sorted := slices.Clone(updatedDependencies)
slices.SortFunc(sorted, func(a, b VersionUpdateInfo) int {
return strings.Compare(a.Repo, b.Repo)
})

repos := make([]string, 0, len(sorted))
var builder strings.Builder
builder.WriteString("Updated dependencies for: ")

for _, dependency := range sorted {
repo, tag := dependency.Repo, dependency.To
repos = append(repos, repo)
builder.WriteString(repo)
builder.WriteString(" => ")
builder.WriteString(tag)
builder.WriteString(" (")
builder.WriteString(dependency.DiffUrl)
builder.WriteString(") ")
}

commitDescription := strings.TrimSuffix(builder.String(), " ")
commitTitle := "chore: updated " + strings.Join(repos, ", ")

return commitTitle, commitDescription
}

func getAndUpdateDependency(ctx context.Context, client *github.Client, dependencyType string, repoPath string, dependencies Dependencies) (VersionUpdateInfo, error) {
version, commit, updatedDependency, err := getVersionAndCommit(ctx, client, dependencies, dependencyType)
if err != nil {
Expand Down
46 changes: 46 additions & 0 deletions dependency_updater/dependency_updater_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"reflect"
"testing"
)

func TestBuildCommitMessagePartsDeterministic(t *testing.T) {
t.Helper()

deps := []VersionUpdateInfo{
{Repo: "op_geth", To: "v1.0.0", DiffUrl: "diff-geth"},
{Repo: "node-reth", To: "v0.1.0", DiffUrl: "diff-reth"},
{Repo: "optimism", To: "op-node/v1.13.4", DiffUrl: "diff-node"},
}

title, description := buildCommitMessageParts(deps)

wantTitle := "chore: updated node-reth, op_geth, optimism"
if title != wantTitle {
t.Fatalf("unexpected commit title: got %q want %q", title, wantTitle)
}

wantDescription := "Updated dependencies for: node-reth => v0.1.0 (diff-reth) op_geth => v1.0.0 (diff-geth) optimism => op-node/v1.13.4 (diff-node)"
if description != wantDescription {
t.Fatalf("unexpected commit description: got %q want %q", description, wantDescription)
}
}

func TestBuildCommitMessagePartsDoesNotMutateInput(t *testing.T) {
t.Helper()

deps := []VersionUpdateInfo{
{Repo: "op_geth", To: "v1.0.0", DiffUrl: "diff-geth"},
{Repo: "node-reth", To: "v0.1.0", DiffUrl: "diff-reth"},
}

original := make([]VersionUpdateInfo, len(deps))
copy(original, deps)

buildCommitMessageParts(deps)

if !reflect.DeepEqual(deps, original) {
t.Fatalf("buildCommitMessageParts mutated input slice: got %+v want %+v", deps, original)
}
}