Skip to content

Commit 4b65953

Browse files
committed
correctly report migrated status for each merge request and fix stats on summary line
1 parent 736fa6b commit 4b65953

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

project.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,10 @@ func (p *project) migrateMergeRequests(ctx context.Context) {
279279
continue
280280
}
281281

282-
if err := p.migrateMergeRequest(ctx, mergeRequest); err != nil {
282+
if ok, err := p.migrateMergeRequest(ctx, mergeRequest); err != nil {
283283
sendErr(err)
284284
failureCount++
285-
} else {
285+
} else if ok {
286286
successCount++
287287
}
288288
}
@@ -292,10 +292,10 @@ func (p *project) migrateMergeRequests(ctx context.Context) {
292292
logger.Info("migrated merge requests from GitLab to GitHub", "name", p.gitlabPath[1], "group", p.gitlabPath[0], "successful", successCount, "failed", failureCount, "skipped", skippedCount)
293293
}
294294

295-
func (p *project) migrateMergeRequest(ctx context.Context, mergeRequest *gitlab.MergeRequest) error {
295+
func (p *project) migrateMergeRequest(ctx context.Context, mergeRequest *gitlab.MergeRequest) (bool, error) {
296296
// Check for context cancellation
297297
if err := ctx.Err(); err != nil {
298-
return fmt.Errorf("preparing to list pull requests: %v", err)
298+
return false, fmt.Errorf("preparing to list pull requests: %v", err)
299299
}
300300

301301
sourceBranchForClosedMergeRequest := fmt.Sprintf("migration-source-%d/%s", mergeRequest.IID, mergeRequest.SourceBranch)
@@ -309,7 +309,7 @@ func (p *project) migrateMergeRequest(ctx context.Context, mergeRequest *gitlab.
309309
query := fmt.Sprintf("repo:%s/%s AND is:pr AND (%s)", p.githubPath[0], p.githubPath[1], branchQuery)
310310
searchResult, err := getGithubSearchResults(ctx, query)
311311
if err != nil {
312-
return fmt.Errorf("listing pull requests: %v", err)
312+
return false, fmt.Errorf("listing pull requests: %v", err)
313313
}
314314

315315
// Look for an existing GitHub pull request
@@ -320,21 +320,21 @@ func (p *project) migrateMergeRequest(ctx context.Context, mergeRequest *gitlab.
320320

321321
// Check for context cancellation
322322
if err := ctx.Err(); err != nil {
323-
return fmt.Errorf("preparing to retrieve pull request: %v", err)
323+
return false, fmt.Errorf("preparing to retrieve pull request: %v", err)
324324
}
325325

326326
if issue.IsPullRequest() {
327327
// Extract the PR number from the URL
328328
prUrl, err := url.Parse(*issue.PullRequestLinks.URL)
329329
if err != nil {
330-
return fmt.Errorf("parsing pull request url: %v", err)
330+
return false, fmt.Errorf("parsing pull request url: %v", err)
331331
}
332332

333333
if m := regexp.MustCompile(".+/([0-9]+)$").FindStringSubmatch(prUrl.Path); len(m) == 2 {
334334
prNumber, _ := strconv.Atoi(m[1])
335335
pr, err := getGithubPullRequest(ctx, p.githubPath[0], p.githubPath[1], prNumber)
336336
if err != nil {
337-
return fmt.Errorf("retrieving pull request: %v", err)
337+
return false, fmt.Errorf("retrieving pull request: %v", err)
338338
}
339339

340340
if strings.Contains(pr.GetBody(), fmt.Sprintf("**GitLab MR Number** | %d", mergeRequest.IID)) ||
@@ -354,7 +354,7 @@ func (p *project) migrateMergeRequest(ctx context.Context, mergeRequest *gitlab.
354354
// Create a worktree
355355
worktree, err := p.repo.Worktree()
356356
if err != nil {
357-
return fmt.Errorf("creating worktree: %v", err)
357+
return false, fmt.Errorf("creating worktree: %v", err)
358358
}
359359

360360
// Generate temporary branch names
@@ -364,12 +364,12 @@ func (p *project) migrateMergeRequest(ctx context.Context, mergeRequest *gitlab.
364364
logger.Trace("retrieving commits for merge request", "name", p.gitlabPath[1], "group", p.gitlabPath[0], "project_id", p.project.ID, "merge_request_id", mergeRequest.IID)
365365
mergeRequestCommits, _, err := gl.MergeRequests.GetMergeRequestCommits(p.project.ID, mergeRequest.IID, &gitlab.GetMergeRequestCommitsOptions{OrderBy: "created_at", Sort: "asc"})
366366
if err != nil {
367-
return fmt.Errorf("retrieving merge request commits: %v", err)
367+
return false, fmt.Errorf("retrieving merge request commits: %v", err)
368368
}
369369

370370
// Some merge requests have no commits, disregard these
371371
if len(mergeRequestCommits) == 0 {
372-
return nil
372+
return false, nil
373373
}
374374

375375
// API is buggy, ordering is not respected, so we'll reorder by commit datestamp
@@ -378,26 +378,26 @@ func (p *project) migrateMergeRequest(ctx context.Context, mergeRequest *gitlab.
378378
})
379379

380380
if mergeRequestCommits[0] == nil {
381-
return fmt.Errorf("start commit for merge request %d is nil", mergeRequest.IID)
381+
return false, fmt.Errorf("start commit for merge request %d is nil", mergeRequest.IID)
382382
}
383383
if mergeRequestCommits[len(mergeRequestCommits)-1] == nil {
384-
return fmt.Errorf("end commit for merge request %d is nil", mergeRequest.IID)
384+
return false, fmt.Errorf("end commit for merge request %d is nil", mergeRequest.IID)
385385
}
386386

387387
logger.Trace("inspecting start commit", "name", p.gitlabPath[1], "group", p.gitlabPath[0], "project_id", p.project.ID, "merge_request_id", mergeRequest.IID, "sha", mergeRequestCommits[0].ShortID)
388388
startCommit, err := object.GetCommit(p.repo.Storer, plumbing.NewHash(mergeRequestCommits[0].ID))
389389
if err != nil {
390-
return fmt.Errorf("loading start commit: %v", err)
390+
return false, fmt.Errorf("loading start commit: %v", err)
391391
}
392392

393393
if startCommit.NumParents() == 0 {
394394
// Orphaned commit, we cannot open a pull request as GitHub rejects it
395395
if skipInvalidMergeRequests {
396396
logger.Info("skipping invalid merge request as start commit has no parents", "name", p.gitlabPath[1], "group", p.gitlabPath[0], "project_id", p.project.ID, "merge_request_id", mergeRequest.IID, "sha", startCommit.Hash)
397-
return nil
397+
return false, nil
398398
}
399399

400-
return fmt.Errorf("start commit %s for merge request %d has no parents", mergeRequestCommits[0].ShortID, mergeRequest.IID)
400+
return false, fmt.Errorf("start commit %s for merge request %d has no parents", mergeRequestCommits[0].ShortID, mergeRequest.IID)
401401
} else {
402402
// Sometimes we will be starting from a merge commit, so look for a suitable parent commit to branch out from
403403
var startCommitParent *object.Commit
@@ -413,7 +413,7 @@ func (p *project) migrateMergeRequest(ctx context.Context, mergeRequest *gitlab.
413413
}
414414

415415
if startCommitParent == nil {
416-
return fmt.Errorf("identifying suitable parent of start commit %s for merge request %d", mergeRequestCommits[0].ShortID, mergeRequest.IID)
416+
return false, fmt.Errorf("identifying suitable parent of start commit %s for merge request %d", mergeRequestCommits[0].ShortID, mergeRequest.IID)
417417
}
418418

419419
logger.Trace("creating target branch for merged/closed merge request", "name", p.gitlabPath[1], "group", p.gitlabPath[0], "project_id", p.project.ID, "merge_request_id", mergeRequest.IID, "branch", mergeRequest.TargetBranch, "sha", startCommitParent.Hash)
@@ -423,7 +423,7 @@ func (p *project) migrateMergeRequest(ctx context.Context, mergeRequest *gitlab.
423423
Branch: plumbing.NewBranchReferenceName(mergeRequest.TargetBranch),
424424
Hash: startCommitParent.Hash,
425425
}); err != nil {
426-
return fmt.Errorf("checking out temporary target branch: %v", err)
426+
return false, fmt.Errorf("checking out temporary target branch: %v", err)
427427
}
428428
}
429429

@@ -435,7 +435,7 @@ func (p *project) migrateMergeRequest(ctx context.Context, mergeRequest *gitlab.
435435
Branch: plumbing.NewBranchReferenceName(mergeRequest.SourceBranch),
436436
Hash: endHash,
437437
}); err != nil {
438-
return fmt.Errorf("checking out temporary source branch: %v", err)
438+
return false, fmt.Errorf("checking out temporary source branch: %v", err)
439439
}
440440

441441
logger.Debug("pushing branches for merged/closed merge request", "owner", p.githubPath[0], "repo", p.githubPath[1], "source_branch", mergeRequest.SourceBranch, "target_branch", mergeRequest.TargetBranch)
@@ -450,7 +450,7 @@ func (p *project) migrateMergeRequest(ctx context.Context, mergeRequest *gitlab.
450450
if errors.Is(err, git.NoErrAlreadyUpToDate) {
451451
logger.Trace("branch already exists and is up-to-date on GitHub", "owner", p.githubPath[0], "repo", p.githubPath[1], "source_branch", mergeRequest.SourceBranch, "target_branch", mergeRequest.TargetBranch)
452452
} else {
453-
return fmt.Errorf("pushing temporary branches to github: %v", err)
453+
return false, fmt.Errorf("pushing temporary branches to github: %v", err)
454454
}
455455
}
456456

@@ -484,7 +484,7 @@ func (p *project) migrateMergeRequest(ctx context.Context, mergeRequest *gitlab.
484484

485485
author, err := getGitlabUser(mergeRequest.Author.Username)
486486
if err != nil {
487-
return fmt.Errorf("retrieving gitlab user: %v", err)
487+
return false, fmt.Errorf("retrieving gitlab user: %v", err)
488488
}
489489
if author.WebsiteURL != "" {
490490
githubAuthorName = "@" + strings.TrimPrefix(strings.ToLower(author.WebsiteURL), "https://github.com/")
@@ -574,17 +574,17 @@ func (p *project) migrateMergeRequest(ctx context.Context, mergeRequest *gitlab.
574574
if pullRequest, _, err = gh.PullRequests.Create(ctx, p.githubPath[0], p.githubPath[1], &newPullRequest); err != nil {
575575
if mergeRequest.State == "closed" && strings.Contains(err.Error(), "No commits between") {
576576
logger.Debug("skipping closed merge request as the change is already present in trunk branch", "owner", p.githubPath[0], "repo", p.githubPath[1], "merge_request_id", mergeRequest.IID)
577-
return nil
577+
return false, nil
578578
}
579-
return fmt.Errorf("creating pull request: %v", err)
579+
return false, fmt.Errorf("creating pull request: %v", err)
580580
}
581581

582582
if mergeRequest.State == "closed" || mergeRequest.State == "merged" {
583583
logger.Debug("closing pull request", "owner", p.githubPath[0], "repo", p.githubPath[1], "pr_number", pullRequest.GetNumber())
584584

585585
pullRequest.State = pointer("closed")
586586
if pullRequest, _, err = gh.PullRequests.Edit(ctx, p.githubPath[0], p.githubPath[1], pullRequest.GetNumber(), pullRequest); err != nil {
587-
return fmt.Errorf("updating pull request: %v", err)
587+
return false, fmt.Errorf("updating pull request: %v", err)
588588
}
589589
}
590590

@@ -604,7 +604,7 @@ func (p *project) migrateMergeRequest(ctx context.Context, mergeRequest *gitlab.
604604
}
605605

606606
if pullRequest, _, err = gh.PullRequests.Edit(ctx, p.githubPath[0], p.githubPath[1], pullRequestState.GetNumber(), pullRequestState); err != nil {
607-
return fmt.Errorf("updating pull request state: %v", err)
607+
return false, fmt.Errorf("updating pull request state: %v", err)
608608
}
609609
}
610610

@@ -619,7 +619,7 @@ func (p *project) migrateMergeRequest(ctx context.Context, mergeRequest *gitlab.
619619
pullRequest.Draft = &mergeRequest.Draft
620620
pullRequest.MaintainerCanModify = nil
621621
if pullRequest, _, err = gh.PullRequests.Edit(ctx, p.githubPath[0], p.githubPath[1], pullRequest.GetNumber(), pullRequest); err != nil {
622-
return fmt.Errorf("updating pull request: %v", err)
622+
return false, fmt.Errorf("updating pull request: %v", err)
623623
}
624624
} else {
625625
logger.Trace("existing pull request is up-to-date", "owner", p.githubPath[0], "repo", p.githubPath[1], "pr_number", pullRequest.GetNumber())
@@ -636,7 +636,7 @@ func (p *project) migrateMergeRequest(ctx context.Context, mergeRequest *gitlab.
636636
for {
637637
result, resp, err := gl.Notes.ListMergeRequestNotes(p.project.ID, mergeRequest.IID, opts)
638638
if err != nil {
639-
return fmt.Errorf("listing merge request notes: %v", err)
639+
return false, fmt.Errorf("listing merge request notes: %v", err)
640640
}
641641

642642
comments = append(comments, result...)
@@ -664,7 +664,7 @@ func (p *project) migrateMergeRequest(ctx context.Context, mergeRequest *gitlab.
664664

665665
commentAuthor, err := getGitlabUser(comment.Author.Username)
666666
if err != nil {
667-
return fmt.Errorf("retrieving gitlab user: %v", err)
667+
return false, fmt.Errorf("retrieving gitlab user: %v", err)
668668
}
669669
if commentAuthor.WebsiteURL != "" {
670670
githubCommentAuthorName = "@" + strings.TrimPrefix(strings.ToLower(commentAuthor.WebsiteURL), "https://github.com/")
@@ -698,7 +698,7 @@ func (p *project) migrateMergeRequest(ctx context.Context, mergeRequest *gitlab.
698698
logger.Debug("updating pull request comment", "owner", p.githubPath[0], "repo", p.githubPath[1], "pr_number", pullRequest.GetNumber(), "comment_id", prComment.GetID())
699699
prComment.Body = &commentBody
700700
if _, _, err = gh.Issues.EditComment(ctx, p.githubPath[0], p.githubPath[1], prComment.GetID(), prComment); err != nil {
701-
return fmt.Errorf("updating pull request comments: %v", err)
701+
return false, fmt.Errorf("updating pull request comments: %v", err)
702702
}
703703
}
704704
} else {
@@ -712,11 +712,11 @@ func (p *project) migrateMergeRequest(ctx context.Context, mergeRequest *gitlab.
712712
Body: &commentBody,
713713
}
714714
if _, _, err = gh.Issues.CreateComment(ctx, p.githubPath[0], p.githubPath[1], pullRequest.GetNumber(), &newComment); err != nil {
715-
return fmt.Errorf("creating pull request comment: %v", err)
715+
return false, fmt.Errorf("creating pull request comment: %v", err)
716716
}
717717
}
718718
}
719719
}
720720

721-
return nil
721+
return true, nil
722722
}

0 commit comments

Comments
 (0)