Skip to content

Commit ffa98c1

Browse files
authored
Merge pull request #11 from manicminer/support-arbitrary-trunk-rename
support renaming trunk branch in GitLab repo to any arbitrary branch name on the GitHub repo
2 parents a325c26 + 9f54ce4 commit ffa98c1

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Written in Go, this is a cross-platform CLI utility that accepts the following r
3838
-github-repo string
3939
the GitHub repository to migrate to
4040
-github-user string
41-
specifies the GitHub user to use, who will author any migrated PRs. can also be sourced from GITHUB_USER environment variable (required)
41+
specifies the GitHub user to use, who will author any migrated PRs (required)
4242
-gitlab-domain string
4343
specifies the GitLab domain to use (default "gitlab.com")
4444
-gitlab-project string
@@ -52,8 +52,11 @@ Written in Go, this is a cross-platform CLI utility that accepts the following r
5252
-projects-csv string
5353
specifies the path to a CSV file describing projects to migrate (incompatible with -gitlab-project and -github-repo)
5454
-rename-master-to-main
55-
rename master branch to main and update pull requests
56-
```
55+
rename master branch to main and update pull requests (incompatible with -rename-trunk-branch)
56+
-rename-trunk-branch string
57+
specifies the new trunk branch name (incompatible with -rename-master-to-main)
58+
-report
59+
report on primitives to be migrated instead of beginning migration```
5760
5861
Use the `-github-user` argument to specify the GitHub username for whom the authentication token was issued (mandatory). You can also specify this with the `GITHUB_USER` environment variable.
5962
@@ -75,7 +78,7 @@ Note: If the destination repository does not exist, this tool will attempt to cr
7578

7679
Specify the location of a self-hosted instance of GitLab with the `-gitlab-domain` argument, or a GitHub Enterprise instance with the `-github-domain` argument.
7780

78-
As a bonus, this tool can transparently rename the `master` branch on your GitLab repository, to `main` on the migrated GitHub repository - enable with the `-rename-master-to-main` argument.
81+
As a bonus, this tool can transparently rename the trunk branch on your GitHub repository - enable with the `-rename-trunk-branch` argument. This will also work for any open merge requests as they are translated to pull requests.
7982

8083
By default, 4 workers will be spawned to migrate up to 4 projects in parallel. You can increase or decrease this with the `-max-concurrency` argument. Note that due to GitHub API rate-limiting, you may not experience any significant speed-up. See [GitHub API docs](https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api) for details.
8184

main.go

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const (
4444

4545
var loop, report bool
4646
var deleteExistingRepos, enablePullRequests, renameMasterToMain bool
47-
var githubDomain, githubRepo, githubToken, githubUser, gitlabDomain, gitlabProject, gitlabToken, projectsCsvPath string
47+
var githubDomain, githubRepo, githubToken, githubUser, gitlabDomain, gitlabProject, gitlabToken, projectsCsvPath, renameTrunkBranch string
4848

4949
var (
5050
cache *objectCache
@@ -150,14 +150,15 @@ func main() {
150150

151151
flag.BoolVar(&deleteExistingRepos, "delete-existing-repos", false, "whether existing repositories should be deleted before migrating")
152152
flag.BoolVar(&enablePullRequests, "migrate-pull-requests", false, "whether pull requests should be migrated")
153-
flag.BoolVar(&renameMasterToMain, "rename-master-to-main", false, "rename master branch to main and update pull requests")
153+
flag.BoolVar(&renameMasterToMain, "rename-master-to-main", false, "rename master branch to main and update pull requests (incompatible with -rename-trunk-branch)")
154154

155155
flag.StringVar(&githubDomain, "github-domain", defaultGithubDomain, "specifies the GitHub domain to use")
156156
flag.StringVar(&githubRepo, "github-repo", "", "the GitHub repository to migrate to")
157157
flag.StringVar(&githubUser, "github-user", "", "specifies the GitHub user to use, who will author any migrated PRs (required)")
158158
flag.StringVar(&gitlabDomain, "gitlab-domain", defaultGitlabDomain, "specifies the GitLab domain to use")
159159
flag.StringVar(&gitlabProject, "gitlab-project", "", "the GitLab project to migrate")
160160
flag.StringVar(&projectsCsvPath, "projects-csv", "", "specifies the path to a CSV file describing projects to migrate (incompatible with -gitlab-project and -github-repo)")
161+
flag.StringVar(&renameTrunkBranch, "rename-trunk-branch", "", "specifies the new trunk branch name (incompatible with -rename-master-to-main)")
161162

162163
flag.IntVar(&maxConcurrency, "max-concurrency", 4, "how many projects to migrate in parallel")
163164

@@ -182,6 +183,11 @@ func main() {
182183
os.Exit(1)
183184
}
184185

186+
if renameMasterToMain && renameTrunkBranch != "" {
187+
logger.Error("cannot specify -rename-master-to-main and -rename-trunk-branch together")
188+
os.Exit(1)
189+
}
190+
185191
retryClient := &retryablehttp.Client{
186192
HTTPClient: cleanhttp.DefaultPooledClient(),
187193
Logger: nil,
@@ -584,7 +590,9 @@ func migrateProject(ctx context.Context, proj []string) error {
584590
}
585591

586592
defaultBranch := "main"
587-
if !renameMasterToMain && project.DefaultBranch != "" {
593+
if renameTrunkBranch != "" {
594+
defaultBranch = renameTrunkBranch
595+
} else if !renameMasterToMain && project.DefaultBranch != "" {
588596
defaultBranch = project.DefaultBranch
589597
}
590598

@@ -644,19 +652,19 @@ func migrateProject(ctx context.Context, proj []string) error {
644652
return fmt.Errorf("cloning gitlab repo: %v", err)
645653
}
646654

647-
if renameMasterToMain {
648-
if masterBranch, err := repo.Reference(plumbing.NewBranchReferenceName("master"), false); err == nil {
649-
logger.Info("renaming master branch to main prior to push", "name", gitlabPath[1], "group", gitlabPath[0], "sha", masterBranch.Hash())
655+
if defaultBranch != project.DefaultBranch {
656+
if gitlabTrunk, err := repo.Reference(plumbing.NewBranchReferenceName(project.DefaultBranch), false); err == nil {
657+
logger.Info("renaming trunk branch prior to push", "name", gitlabPath[1], "group", gitlabPath[0], "gitlab_trunk", project.DefaultBranch, "github_trunk", defaultBranch, "sha", gitlabTrunk.Hash())
650658

651-
logger.Debug("creating main branch", "name", gitlabPath[1], "group", gitlabPath[0], "sha", masterBranch.Hash())
652-
mainBranch := plumbing.NewHashReference(plumbing.NewBranchReferenceName("main"), masterBranch.Hash())
653-
if err = repo.Storer.SetReference(mainBranch); err != nil {
654-
return fmt.Errorf("creating main branch: %v", err)
659+
logger.Debug("creating new trunk branch", "name", gitlabPath[1], "group", gitlabPath[0], "github_trunk", defaultBranch, "sha", gitlabTrunk.Hash())
660+
githubTrunk := plumbing.NewHashReference(plumbing.NewBranchReferenceName(defaultBranch), gitlabTrunk.Hash())
661+
if err = repo.Storer.SetReference(githubTrunk); err != nil {
662+
return fmt.Errorf("creating trunk branch: %v", err)
655663
}
656664

657-
logger.Debug("deleting master branch", "name", gitlabPath[1], "group", gitlabPath[0], "sha", masterBranch.Hash())
658-
if err = repo.Storer.RemoveReference(masterBranch.Name()); err != nil {
659-
return fmt.Errorf("deleting master branch: %v", err)
665+
logger.Debug("deleting old trunk branch", "name", gitlabPath[1], "group", gitlabPath[0], "gitlab_trunk", project.DefaultBranch, "sha", gitlabTrunk.Hash())
666+
if err = repo.Storer.RemoveReference(gitlabTrunk.Name()); err != nil {
667+
return fmt.Errorf("deleting old trunk branch: %v", err)
660668
}
661669
}
662670
}
@@ -708,13 +716,13 @@ func migrateProject(ctx context.Context, proj []string) error {
708716
}
709717

710718
if enablePullRequests {
711-
migratePullRequests(ctx, githubPath, gitlabPath, project, repo)
719+
migratePullRequests(ctx, githubPath, gitlabPath, defaultBranch, project, repo)
712720
}
713721

714722
return nil
715723
}
716724

717-
func migratePullRequests(ctx context.Context, githubPath, gitlabPath []string, project *gitlab.Project, repo *git.Repository) {
725+
func migratePullRequests(ctx context.Context, githubPath, gitlabPath []string, defaultBranch string, project *gitlab.Project, repo *git.Repository) {
718726
var mergeRequests []*gitlab.MergeRequest
719727

720728
opts := &gitlab.ListProjectMergeRequestsOptions{
@@ -940,9 +948,9 @@ func migratePullRequests(ctx context.Context, githubPath, gitlabPath []string, p
940948
cleanUpBranch = true
941949
}
942950

943-
if renameMasterToMain && mergeRequest.TargetBranch == "master" {
944-
logger.Trace("changing target branch from master to main", "name", gitlabPath[1], "group", gitlabPath[0], "project_id", project.ID, "merge_request_id", mergeRequest.IID)
945-
mergeRequest.TargetBranch = "main"
951+
if defaultBranch != project.DefaultBranch && mergeRequest.TargetBranch == project.DefaultBranch {
952+
logger.Trace("changing target trunk branch", "name", gitlabPath[1], "group", gitlabPath[0], "project_id", project.ID, "merge_request_id", mergeRequest.IID, "old_trunk", project.DefaultBranch, "new_trunk", defaultBranch)
953+
mergeRequest.TargetBranch = defaultBranch
946954
}
947955

948956
githubAuthorName := mergeRequest.Author.Name

0 commit comments

Comments
 (0)