From 9b6b76c683efb3ca311952f3599470d038720ccd Mon Sep 17 00:00:00 2001 From: "Mr. GD" Date: Tue, 16 Sep 2025 01:03:41 +0530 Subject: [PATCH] fix: stabilize dependency updater commit message order --- dependency_updater/dependency_updater.go | 45 +++++++++++++----- dependency_updater/dependency_updater_test.go | 46 +++++++++++++++++++ 2 files changed, 79 insertions(+), 12 deletions(-) create mode 100644 dependency_updater/dependency_updater_test.go diff --git a/dependency_updater/dependency_updater.go b/dependency_updater/dependency_updater.go index 74b0760f..228b9458 100644 --- a/dependency_updater/dependency_updater.go +++ b/dependency_updater/dependency_updater.go @@ -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) @@ -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 { diff --git a/dependency_updater/dependency_updater_test.go b/dependency_updater/dependency_updater_test.go new file mode 100644 index 00000000..9e2ac712 --- /dev/null +++ b/dependency_updater/dependency_updater_test.go @@ -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) + } +}