Skip to content

Commit a8be14e

Browse files
feat: improve the display of repositories pr url at the end (#307)
1 parent 7152392 commit a8be14e

File tree

4 files changed

+60
-30
lines changed

4 files changed

+60
-30
lines changed

main.go

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,9 @@ func main() {
163163
updated, pr, err := repo.Update(ctx, updaters, options.UpdateOptions)
164164

165165
result := repository.RepoUpdateResult{
166-
Owner: repo.Owner,
167-
Repo: repo.Name,
166+
Owner: repo.Owner,
167+
Repo: repo.Name,
168+
IsUpdated: updated,
168169
}
169170

170171
if err != nil {
@@ -201,27 +202,55 @@ func main() {
201202

202203
logrus.WithField("repositories-count", len(repositories)).Info("Updates finished")
203204

204-
resultFile := repository.ResultFile{}
205-
hadError := false
205+
updatedPRURLs, notUpdatedPRURLs, resultFile, hadError := processResults(results)
206206

207+
logUpdatesSummary(updatedPRURLs, notUpdatedPRURLs)
208+
209+
if options.outputResults != "" {
210+
err := writeResults(&resultFile, options.outputResults)
211+
if err != nil {
212+
logrus.Fatalf("Failed to write results: %s", err)
213+
}
214+
}
215+
216+
if options.failOnError && hadError {
217+
logrus.Fatal("Some repository updates failed")
218+
}
219+
}
220+
221+
func processResults(results chan repository.RepoUpdateResult) (updatedPRURLs []string, notUpdatedPRURLs []string, resultFile repository.ResultFile, hadError bool) {
207222
for r := range results {
223+
if r.PullRequest != nil && r.PullRequest.URL != "" {
224+
if r.IsUpdated {
225+
updatedPRURLs = append(updatedPRURLs, r.PullRequest.URL)
226+
} else {
227+
notUpdatedPRURLs = append(notUpdatedPRURLs, r.PullRequest.URL)
228+
}
229+
}
230+
208231
resultFile.Repos = append(resultFile.Repos, r)
209232

210233
if r.Error != nil {
211234
hadError = true
212235
}
213236
}
214237

215-
if options.outputResults != "" {
216-
err := writeResults(&resultFile, options.outputResults)
238+
return updatedPRURLs, notUpdatedPRURLs, resultFile, hadError
239+
}
217240

218-
if err != nil {
219-
logrus.Fatalf("Failed to write results: %s", err)
241+
func logUpdatesSummary(updatedPRURLs, notUpdatedPRURLs []string) {
242+
if len(notUpdatedPRURLs) > 0 {
243+
logrus.WithField("unrevised-repository-count", len(notUpdatedPRURLs)).Info("Update summary (Unrevised)")
244+
for _, url := range notUpdatedPRURLs {
245+
logrus.WithField("unrevised-repository-pr-url", url).Info("Update summary (Unrevised)")
220246
}
221247
}
222248

223-
if options.failOnError && hadError {
224-
logrus.Fatal("Some repository updates failed")
249+
if len(updatedPRURLs) > 0 {
250+
logrus.WithField("updated-repository-count", len(updatedPRURLs)).Info("Update summary (Updated)")
251+
for _, url := range updatedPRURLs {
252+
logrus.WithField("updated-repository-pr-url", url).Info("Update summary (Updated)")
253+
}
225254
}
226255
}
227256

repository/result.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type RepoUpdateResult struct {
99
Repo string `json:"repo"`
1010
Error *string `json:"error"`
1111
PullRequest *PullRequestResult `json:"pr"`
12+
IsUpdated bool
1213
}
1314

1415
type PullRequestResult struct {

repository/strategy_append.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,44 +44,44 @@ func (s *AppendStrategy) Run(ctx context.Context) (bool, *github.PullRequest, er
4444
})
4545
}
4646
if err != nil {
47-
return false, nil, fmt.Errorf("failed to switch to branch %s: %w", branchName, err)
47+
return false, existingPR, fmt.Errorf("failed to switch to branch %s: %w", branchName, err)
4848
}
4949

5050
repoUpdated, err := s.Repository.runUpdaters(ctx, s.Updaters, s.RepoPath)
5151
if err != nil {
52-
return false, nil, fmt.Errorf("failed to update repository %s: %w", s.Repository.FullName(), err)
52+
return false, existingPR, fmt.Errorf("failed to update repository %s: %w", s.Repository.FullName(), err)
5353
}
5454
if !repoUpdated {
55-
return false, nil, nil
55+
return false, existingPR, nil
5656
}
5757

5858
if err = s.Options.Git.setDefaultValues(s.Updaters, templateExecutorFor(s.Options, s.Repository, s.RepoPath)); err != nil {
59-
return false, nil, fmt.Errorf("failed to set default git values: %w", err)
59+
return false, existingPR, fmt.Errorf("failed to set default git values: %w", err)
6060
}
6161
if err = s.Options.GitHub.setDefaultValues(s.Options.Git, templateExecutorFor(s.Options, s.Repository, s.RepoPath)); err != nil {
62-
return false, nil, fmt.Errorf("failed to set default github values: %w", err)
62+
return false, existingPR, fmt.Errorf("failed to set default github values: %w", err)
6363
}
6464
s.Options.GitHub.setDefaultUpdateOperation(IgnoreUpdateOperation)
6565

6666
changesCommitted, err := commitChanges(ctx, gitRepo, s.Options)
6767
if err != nil {
68-
return false, nil, fmt.Errorf("failed to commit changes to git repository %s: %w", s.Repository.FullName(), err)
68+
return false, existingPR, fmt.Errorf("failed to commit changes to git repository %s: %w", s.Repository.FullName(), err)
6969
}
7070
if !changesCommitted {
7171
logrus.WithField("repository", s.Repository.FullName()).Debug("No changes recorded, nothing to push")
72-
return false, nil, nil
72+
return false, existingPR, nil
7373
}
7474
if s.Options.DryRun {
7575
logrus.WithField("repository", s.Repository.FullName()).Warning("Running in dry-run mode, not pushing changes")
76-
return false, nil, nil
76+
return false, existingPR, nil
7777
}
7878

7979
err = pushChanges(ctx, gitRepo, pushOptions{
8080
GitHubOpts: s.Options.GitHub,
8181
BranchName: branchName,
8282
})
8383
if err != nil {
84-
return false, nil, fmt.Errorf("failed to push changes to git repository %s: %w", s.Repository.FullName(), err)
84+
return false, existingPR, fmt.Errorf("failed to push changes to git repository %s: %w", s.Repository.FullName(), err)
8585
}
8686

8787
var pr *github.PullRequest
@@ -91,7 +91,7 @@ func (s *AppendStrategy) Run(ctx context.Context) (bool, *github.PullRequest, er
9191
pr, err = s.Repository.createPullRequest(ctx, s.Options.GitHub, branchName)
9292
}
9393
if err != nil {
94-
return false, nil, fmt.Errorf("failed to create or update Pull Request: %w", err)
94+
return false, existingPR, fmt.Errorf("failed to create or update Pull Request: %w", err)
9595
}
9696

9797
return true, pr, nil

repository/strategy_reset.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,36 +42,36 @@ func (s *ResetStrategy) Run(ctx context.Context) (bool, *github.PullRequest, err
4242
CreateBranch: true,
4343
})
4444
if err != nil {
45-
return false, nil, fmt.Errorf("failed to switch to branch %s: %w", branchName, err)
45+
return false, existingPR, fmt.Errorf("failed to switch to branch %s: %w", branchName, err)
4646
}
4747

4848
repoUpdated, err := s.Repository.runUpdaters(ctx, s.Updaters, s.RepoPath)
4949
if err != nil {
50-
return false, nil, fmt.Errorf("failed to update repository %s: %w", s.Repository.FullName(), err)
50+
return false, existingPR, fmt.Errorf("failed to update repository %s: %w", s.Repository.FullName(), err)
5151
}
5252
if !repoUpdated {
53-
return false, nil, nil
53+
return false, existingPR, nil
5454
}
5555

5656
if err = s.Options.Git.setDefaultValues(s.Updaters, templateExecutorFor(s.Options, s.Repository, s.RepoPath)); err != nil {
57-
return false, nil, fmt.Errorf("failed to set default git values: %w", err)
57+
return false, existingPR, fmt.Errorf("failed to set default git values: %w", err)
5858
}
5959
if err = s.Options.GitHub.setDefaultValues(s.Options.Git, templateExecutorFor(s.Options, s.Repository, s.RepoPath)); err != nil {
60-
return false, nil, fmt.Errorf("failed to set default github values: %w", err)
60+
return false, existingPR, fmt.Errorf("failed to set default github values: %w", err)
6161
}
6262
s.Options.GitHub.setDefaultUpdateOperation(ReplaceUpdateOperation)
6363

6464
changesCommitted, err := commitChanges(ctx, gitRepo, s.Options)
6565
if err != nil {
66-
return false, nil, fmt.Errorf("failed to commit changes to git repository %s: %w", s.Repository.FullName(), err)
66+
return false, existingPR, fmt.Errorf("failed to commit changes to git repository %s: %w", s.Repository.FullName(), err)
6767
}
6868
if !changesCommitted {
6969
logrus.WithField("repository", s.Repository.FullName()).Debug("No changes recorded, nothing to push")
70-
return false, nil, nil
70+
return false, existingPR, nil
7171
}
7272
if s.Options.DryRun {
7373
logrus.WithField("repository", s.Repository.FullName()).Warning("Running in dry-run mode, not pushing changes")
74-
return false, nil, nil
74+
return false, existingPR, nil
7575
}
7676

7777
err = pushChanges(ctx, gitRepo, pushOptions{
@@ -80,7 +80,7 @@ func (s *ResetStrategy) Run(ctx context.Context) (bool, *github.PullRequest, err
8080
ForcePush: true,
8181
})
8282
if err != nil {
83-
return false, nil, fmt.Errorf("failed to push changes to git repository %s: %w", s.Repository.FullName(), err)
83+
return false, existingPR, fmt.Errorf("failed to push changes to git repository %s: %w", s.Repository.FullName(), err)
8484
}
8585

8686
var pr *github.PullRequest
@@ -90,7 +90,7 @@ func (s *ResetStrategy) Run(ctx context.Context) (bool, *github.PullRequest, err
9090
pr, err = s.Repository.createPullRequest(ctx, s.Options.GitHub, branchName)
9191
}
9292
if err != nil {
93-
return false, nil, fmt.Errorf("failed to create or update Pull Request: %w", err)
93+
return false, existingPR, fmt.Errorf("failed to create or update Pull Request: %w", err)
9494
}
9595

9696
return true, pr, nil

0 commit comments

Comments
 (0)