@@ -53,6 +53,13 @@ func newProject(slugs []string) (*project, error) {
5353 return nil , fmt .Errorf ("no matching GitLab project found: %s" , slugs [0 ])
5454 }
5555
56+ p .defaultBranch = "main"
57+ if renameTrunkBranch != "" {
58+ p .defaultBranch = renameTrunkBranch
59+ } else if ! renameMasterToMain && p .project .DefaultBranch != "" {
60+ p .defaultBranch = p .project .DefaultBranch
61+ }
62+
5663 return p , nil
5764}
5865
@@ -64,6 +71,29 @@ type project struct {
6471 githubPath []string
6572}
6673
74+ func (p * project ) createRepo (ctx context.Context , homepage string , repoDeleted bool ) error {
75+ if repoDeleted {
76+ logger .Warn ("recreating GitHub repository" , "owner" , p .githubPath [0 ], "repo" , p .githubPath [1 ])
77+ } else {
78+ logger .Debug ("repository not found on GitHub, proceeding to create" , "owner" , p .githubPath [0 ], "repo" , p .githubPath [1 ])
79+ }
80+ newRepo := github.Repository {
81+ Name : pointer (p .githubPath [1 ]),
82+ Description : & p .project .Description ,
83+ Homepage : & homepage ,
84+ DefaultBranch : & p .defaultBranch ,
85+ Private : pointer (true ),
86+ HasIssues : pointer (true ),
87+ HasProjects : pointer (true ),
88+ HasWiki : pointer (true ),
89+ }
90+ if _ , _ , err := gh .Repositories .Create (ctx , p .githubPath [0 ], & newRepo ); err != nil {
91+ return fmt .Errorf ("creating github repo: %v" , err )
92+ }
93+
94+ return nil
95+ }
96+
6797func (p * project ) migrate (ctx context.Context ) error {
6898 cloneUrl , err := url .Parse (p .project .HTTPURLToRepo )
6999 if err != nil {
@@ -72,18 +102,6 @@ func (p *project) migrate(ctx context.Context) error {
72102
73103 logger .Info ("mirroring repository from GitLab to GitHub" , "name" , p .gitlabPath [1 ], "group" , p .gitlabPath [0 ], "github_org" , p .githubPath [0 ], "github_repo" , p .githubPath [1 ])
74104
75- user , err := getGithubUser (ctx , p .githubPath [0 ])
76- if err != nil {
77- return fmt .Errorf ("retrieving github user: %v" , err )
78- }
79-
80- var org string
81- if strings .EqualFold (* user .Type , "organization" ) {
82- org = p .githubPath [0 ]
83- } else if ! strings .EqualFold (* user .Type , "user" ) || ! strings .EqualFold (* user .Login , p .githubPath [0 ]) {
84- return fmt .Errorf ("configured owner is neither an organization nor the current user: %s" , p .githubPath [0 ])
85- }
86-
87105 logger .Debug ("checking for existing repository on GitHub" , "owner" , p .githubPath [0 ], "repo" , p .githubPath [1 ])
88106 _ , _ , err = gh .Repositories .Get (ctx , p .githubPath [0 ], p .githubPath [1 ])
89107
@@ -92,46 +110,21 @@ func (p *project) migrate(ctx context.Context) error {
92110 return fmt .Errorf ("retrieving github repo: %v" , err )
93111 }
94112
95- var createRepo , repoDeleted bool
113+ homepage := fmt .Sprintf ("https://%s/%s/%s" , gitlabDomain , p .gitlabPath [0 ], p .gitlabPath [1 ])
114+
96115 if err != nil {
97- createRepo = true
116+ // Repository not found
117+ if err = p .createRepo (ctx , homepage , false ); err != nil {
118+ return err
119+ }
98120 } else if deleteExistingRepos {
99121 logger .Warn ("existing repository was found on GitHub, proceeding to delete" , "owner" , p .githubPath [0 ], "repo" , p .githubPath [1 ])
100122 if _ , err = gh .Repositories .Delete (ctx , p .githubPath [0 ], p .githubPath [1 ]); err != nil {
101123 return fmt .Errorf ("deleting existing github repo: %v" , err )
102124 }
103125
104- createRepo = true
105- repoDeleted = true
106- }
107-
108- defaultBranch := "main"
109- if renameTrunkBranch != "" {
110- defaultBranch = renameTrunkBranch
111- } else if ! renameMasterToMain && p .project .DefaultBranch != "" {
112- defaultBranch = p .project .DefaultBranch
113- }
114-
115- homepage := fmt .Sprintf ("https://%s/%s/%s" , gitlabDomain , p .gitlabPath [0 ], p .gitlabPath [1 ])
116-
117- if createRepo {
118- if repoDeleted {
119- logger .Warn ("recreating GitHub repository" , "owner" , p .githubPath [0 ], "repo" , p .githubPath [1 ])
120- } else {
121- logger .Debug ("repository not found on GitHub, proceeding to create" , "owner" , p .githubPath [0 ], "repo" , p .githubPath [1 ])
122- }
123- newRepo := github.Repository {
124- Name : pointer (p .githubPath [1 ]),
125- Description : & p .project .Description ,
126- Homepage : & homepage ,
127- DefaultBranch : & defaultBranch ,
128- Private : pointer (true ),
129- HasIssues : pointer (true ),
130- HasProjects : pointer (true ),
131- HasWiki : pointer (true ),
132- }
133- if _ , _ , err = gh .Repositories .Create (ctx , org , & newRepo ); err != nil {
134- return fmt .Errorf ("creating github repo: %v" , err )
126+ if err = p .createRepo (ctx , homepage , true ); err != nil {
127+ return err
135128 }
136129 }
137130
@@ -168,12 +161,12 @@ func (p *project) migrate(ctx context.Context) error {
168161 return fmt .Errorf ("cloning gitlab repo: %v" , err )
169162 }
170163
171- if defaultBranch != p .project .DefaultBranch {
164+ if p . defaultBranch != p .project .DefaultBranch {
172165 if gitlabTrunk , err := p .repo .Reference (plumbing .NewBranchReferenceName (p .project .DefaultBranch ), false ); err == nil {
173- logger .Info ("renaming trunk branch prior to push" , "name" , p .gitlabPath [1 ], "group" , p .gitlabPath [0 ], "gitlab_trunk" , p .project .DefaultBranch , "github_trunk" , defaultBranch , "sha" , gitlabTrunk .Hash ())
166+ logger .Info ("renaming trunk branch prior to push" , "name" , p .gitlabPath [1 ], "group" , p .gitlabPath [0 ], "gitlab_trunk" , p .project .DefaultBranch , "github_trunk" , p . defaultBranch , "sha" , gitlabTrunk .Hash ())
174167
175- logger .Debug ("creating new trunk branch" , "name" , p .gitlabPath [1 ], "group" , p .gitlabPath [0 ], "github_trunk" , defaultBranch , "sha" , gitlabTrunk .Hash ())
176- githubTrunk := plumbing .NewHashReference (plumbing .NewBranchReferenceName (defaultBranch ), gitlabTrunk .Hash ())
168+ logger .Debug ("creating new trunk branch" , "name" , p .gitlabPath [1 ], "group" , p .gitlabPath [0 ], "github_trunk" , p . defaultBranch , "sha" , gitlabTrunk .Hash ())
169+ githubTrunk := plumbing .NewHashReference (plumbing .NewBranchReferenceName (p . defaultBranch ), gitlabTrunk .Hash ())
177170 if err = p .repo .Storer .SetReference (githubTrunk ); err != nil {
178171 return fmt .Errorf ("creating trunk branch: %v" , err )
179172 }
@@ -238,9 +231,9 @@ func (p *project) migrate(ctx context.Context) error {
238231 }
239232 }
240233
241- logger .Debug ("setting default repository branch" , "owner" , p .githubPath [0 ], "repo" , p .githubPath [1 ], "branch_name" , defaultBranch )
234+ logger .Debug ("setting default repository branch" , "owner" , p .githubPath [0 ], "repo" , p .githubPath [1 ], "branch_name" , p . defaultBranch )
242235 updateRepo = github.Repository {
243- DefaultBranch : & defaultBranch ,
236+ DefaultBranch : & p . defaultBranch ,
244237 }
245238 if _ , _ , err = gh .Repositories .Edit (ctx , p .githubPath [0 ], p .githubPath [1 ], & updateRepo ); err != nil {
246239 return fmt .Errorf ("setting default branch: %v" , err )
0 commit comments