Skip to content

Commit bd7fddc

Browse files
committed
Add support for placeholder merge requests
1 parent a562e12 commit bd7fddc

File tree

5 files changed

+67
-4
lines changed

5 files changed

+67
-4
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,18 @@ Set to `true` (default) to enable using the [GitHub preview API for importing is
206206

207207
### usePlaceholderIssuesForMissingIssues
208208

209+
NOTE: This setting requires `usePlaceholderIssuesForMissingMergeRequests` to be set to `false` (default).
210+
209211
If this is set to `true` (default) then the migration process will automatically create empty dummy issues for every 'missing' GitLab issue (if you deleted a GitLab issue for example). Those issues will be closed on Github and they ensure that the issue ids stay the same on both GitLab and Github.
210212

213+
### usePlaceholderIssuesForMissingMergeRequests
214+
215+
NOTE: GitLab has distinct numbers for issues and merge requests whereas GitHub treats pull requests as issues.
216+
Only use this setting if your GitLab repository does not have issues or you do not intend on migrating them.
217+
This setting therefore requires `usePlaceholderIssuesForMissingIssues` to be set to `false`.
218+
219+
If this is set to `true` then the migration process will automatically create empty dummy issues for every 'missing' GitLab merge request (if you deleted a GitLab merge request for example). Those issues will be closed on Github and they ensure that the issue ids stay the same on both GitLab and Github.
220+
211221
#### usePlaceholderMilestonesForMissingMilestones
212222

213223
If this is set to `true` (default) then the migration process will automatically create empty dummy milestones for every 'missing' GitLab milestone (if you deleted a GitLab milestone for example). Those milestones will be closed on Github and they ensure that the milestone ids stay the same on both GitLab and Github.

sample_settings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export default {
4949
useIssueImportAPI: true,
5050
usePlaceholderMilestonesForMissingMilestones: true,
5151
usePlaceholderIssuesForMissingIssues: true,
52+
usePlaceholderIssuesForMissingMergeRequests: false,
5253
useReplacementIssuesForCreationFails: true,
5354
useIssuesForAllMergeRequests: false,
5455
filterByLabel: undefined,

src/githubHelper.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,15 @@ export class GithubHelper {
900900
async createPullRequestAndComments(
901901
mergeRequest: GitLabMergeRequest
902902
): Promise<void> {
903+
// Use the issue creation for placeholder issues
904+
if (mergeRequest.isPlaceholder) {
905+
let issue = mergeRequest as unknown;
906+
await this.createIssueAndComments(issue as GitLabIssue);
907+
console.log(`Created placeholder issue for placeholder merge request #${mergeRequest.iid}`);
908+
909+
return;
910+
}
911+
903912
let pullRequestData = await this.createPullRequest(mergeRequest);
904913

905914
// createPullRequest() returns an issue number if a PR could not be created and

src/index.ts

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
SimpleLabel,
55
SimpleMilestone,
66
} from './githubHelper';
7-
import { GitlabHelper, GitLabIssue, GitLabMilestone } from './gitlabHelper';
7+
import { GitlabHelper, GitLabIssue, GitLabMergeRequest, GitLabMilestone } from './gitlabHelper';
88
import settings from '../settings';
99

1010
import { Octokit as GitHubApi } from '@octokit/rest';
@@ -156,6 +156,23 @@ function createPlaceholderIssue(expectedIdx: number): Partial<GitLabIssue> {
156156
};
157157
}
158158

159+
/**
160+
* Creates dummy data for a placeholder issue
161+
*
162+
* @param expectedIdx Number of the GitLab issue
163+
* @returns Data for the issue
164+
*/
165+
function createPlaceholderMergeRequest(expectedIdx: number): Partial<GitLabMergeRequest> {
166+
return {
167+
iid: expectedIdx,
168+
title: `[PLACEHOLDER] - for merge request #${expectedIdx}`,
169+
description:
170+
'This is to ensure that merge request numbers in GitLab and GitHub are the same',
171+
state: 'closed',
172+
isPlaceholder: true,
173+
};
174+
}
175+
159176
// ----------------------------------------------------------------------------
160177

161178
/**
@@ -549,6 +566,25 @@ async function transferMergeRequests() {
549566
// Issues are sometimes created from Gitlab merge requests. Avoid creating duplicates.
550567
let githubIssues = await githubHelper.getAllGithubIssues();
551568

569+
if (settings.usePlaceholderIssuesForMissingMergeRequests) {
570+
for (let i = 0; i < mergeRequests.length; i++) {
571+
// GitLab issue internal Id (iid)
572+
let expectedIdx = i + 1;
573+
574+
// is there a gap in the GitLab merge requests?
575+
// Create placeholder issues so that new GitHub issues will have the same
576+
// issue number as in GitLab. If a placeholder is used it is because there
577+
// was a gap in GitLab merge requests -- likely caused by a deleted GitLab merge request.
578+
if (mergeRequests[i].iid !== expectedIdx) {
579+
mergeRequests.splice(i, 0, createPlaceholderMergeRequest(expectedIdx) as GitLabMergeRequest); // HACK: remove type coercion
580+
counters.nrOfPlaceholderIssues++;
581+
console.log(
582+
`Added placeholder issue for GitLab merge request #${expectedIdx}.`
583+
);
584+
}
585+
}
586+
}
587+
552588
console.log(
553589
'Transferring ' + mergeRequests.length.toString() + ' merge requests'
554590
);
@@ -568,11 +604,17 @@ async function transferMergeRequests() {
568604
let githubIssue = githubIssues.find(
569605
// allow for issues titled "Original Issue Name - [merged|closed]"
570606
i => {
571-
// regex needs escaping in case merge request title contains special characters
572-
const regex = new RegExp(escapeRegExp(mr.title.trim()) + ' - \\[(merged|closed)\\]');
573-
return regex.test(i.title.trim()) && i.body.includes(mr.web_url);
607+
if (!mr.isPlaceholder) {
608+
// regex needs escaping in case merge request title contains special characters
609+
const regex = new RegExp(escapeRegExp(mr.title.trim()) + ' - \\[(merged|closed)\\]');
610+
return regex.test(i.title.trim()) && i.body.includes(mr.web_url);
611+
}
612+
else {
613+
return i.title.trim() === mr.title.trim();
614+
}
574615
}
575616
);
617+
576618
if (!githubRequest && !githubIssue) {
577619
if (settings.skipMergeRequestStates.includes(mr.state)) {
578620
console.log(

src/settings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default interface Settings {
2424
useIssueImportAPI: boolean;
2525
usePlaceholderMilestonesForMissingMilestones: boolean;
2626
usePlaceholderIssuesForMissingIssues: boolean;
27+
usePlaceholderIssuesForMissingMergeRequests: boolean;
2728
useReplacementIssuesForCreationFails: boolean;
2829
useIssuesForAllMergeRequests: boolean;
2930
filterByLabel?: string;

0 commit comments

Comments
 (0)