Skip to content

Commit 840100f

Browse files
authored
Merge pull request #94 from gaurav5430/master
feat(releases): migrate releases from gitlab to github
2 parents fe06007 + 83ba519 commit 840100f

5 files changed

Lines changed: 126 additions & 0 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ If this is set to true (default) then the migration process will transfer issues
128128

129129
If this is set to true (default) then the migration process will transfer merge requests.
130130

131+
#### transfer.releases
132+
133+
If this is set to true (default) then the migration process will transfer releases.
134+
Note that github api for releases is limited and hence this will only transfer the title and description of the releases
135+
and add them to github in chronological order, but it would not preserve the original release dates, nor transfer artefacts or assets.
136+
131137
### debug
132138

133139
As default it is set to false. Doesn't fire the requests to github api and only does the work on the gitlab side to test for wonky cases before using up api-calls

sample_settings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export default {
3535
labels: true,
3636
issues: true,
3737
mergeRequests: true,
38+
releases: true,
3839
},
3940
debug: false,
4041
useIssueImportAPI: true,

src/githubHelper.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,62 @@ export class GithubHelper {
186186

187187
// ----------------------------------------------------------------------------
188188

189+
190+
/**
191+
* Gets a release by tag name
192+
* @param tag {string} - the tag name to search a release for
193+
* @returns
194+
*/
195+
async getReleaseByTag(tag) {
196+
try {
197+
await utils.sleep(this.delayInMs);
198+
// get an existing release by tag name in github
199+
let result = await this.githubApi.repos.getReleaseByTag({
200+
owner: this.githubOwner,
201+
repo: this.githubRepo,
202+
tag: tag
203+
});
204+
205+
return result;
206+
} catch (err) {
207+
console.error('No existing release for this tag on github');
208+
return null;
209+
}
210+
}
211+
212+
// ----------------------------------------------------------------------------
213+
214+
/**
215+
* Creates a new release on github
216+
* @param tag_name {string} - the tag name
217+
* @param name {string} - title of the release
218+
* @param body {string} - description for the release
219+
*/
220+
async createRelease(
221+
tag_name: string,
222+
name: string,
223+
body: string) {
224+
try {
225+
await utils.sleep(this.delayInMs);
226+
// get an array of GitHub labels for the new repo
227+
let result = await this.githubApi.repos.createRelease({
228+
owner: this.githubOwner,
229+
repo: this.githubRepo,
230+
tag_name,
231+
name,
232+
body,
233+
});
234+
235+
return result;
236+
} catch (err) {
237+
console.error('Could not create release on github');
238+
console.error(err);
239+
return null;
240+
}
241+
}
242+
243+
// ----------------------------------------------------------------------------
244+
189245
/**
190246
* Get a list of all the current GitHub pull requests.
191247
* This uses a while loop to make sure that each page of issues is received.

src/index.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,13 @@ async function migrate() {
161161
await transferLabels(true, settings.conversion.useLowerCaseLabels);
162162
}
163163

164+
165+
// transfer GitLab releases to GitHub
166+
if (settings.transfer.releases) {
167+
await transferReleases();
168+
}
169+
170+
164171
// Transfer issues with their comments; do this before transferring the merge requests
165172
if (settings.transfer.issues) {
166173
await transferIssues();
@@ -477,6 +484,61 @@ async function transferMergeRequests() {
477484
}
478485
}
479486

487+
/**
488+
* Transfer any releases that exist in GitLab that do not exist in GitHub
489+
* Please note that due to github api restrictions, this only transfers the
490+
* name, description and tag name of the release. It sorts the releases chronologically
491+
* and creates them on github one by one
492+
* @returns {Promise<void>}
493+
*/
494+
async function transferReleases() {
495+
inform('Transferring Releases');
496+
497+
// Get a list of all releases associated with this project
498+
let releases = await gitlabApi.Releases.all(settings.gitlab.projectId) as any;
499+
500+
// Sort releases in ascending order of their release date
501+
releases = releases.sort((a, b) => {
502+
return (new Date(a.released_at) as any) - (new Date(b.released_at) as any);
503+
});
504+
505+
console.log(
506+
'Transferring ' + releases.length.toString() + ' releases'
507+
);
508+
509+
//
510+
// Create GitHub release for each GitLab release
511+
//
512+
513+
// if a GitLab release does not exist in GitHub repo, create it
514+
for (let release of releases) {
515+
// Try to find an existing github release that already exists for this GitLab
516+
// release
517+
let githubRelease = await githubHelper.getReleaseByTag(release.tag_name);
518+
519+
if (!githubRelease) {
520+
console.log(
521+
'Creating release: !' + release.name + ' - ' + release.tag_name
522+
);
523+
try {
524+
// process asynchronous code in sequence
525+
await githubHelper.createRelease(release.tag_name, release.name, release.description);
526+
} catch (err) {
527+
console.error(
528+
'Could not create release: !' +
529+
release.name + ' - ' + release.tag_name
530+
);
531+
console.error(err);
532+
}
533+
} else {
534+
console.log(
535+
'Gitlab release already exists (as github release): ' +
536+
githubRelease.data.name + ' - ' + githubRelease.data.tag_name
537+
);
538+
}
539+
}
540+
}
541+
480542
//-----------------------------------------------------------------------------
481543

482544
/**

src/settings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default interface Settings {
1616
labels: boolean;
1717
issues: boolean;
1818
mergeRequests: boolean;
19+
releases: boolean;
1920
};
2021
useIssueImportAPI: boolean;
2122
usePlaceholderIssuesForMissingIssues: boolean;

0 commit comments

Comments
 (0)