Skip to content

Commit cf9b376

Browse files
authored
Merge pull request piceaTech#72 from tpage-alfresco/useIssuesForAllMergeRequests
Add option to always migrate merge requests as issues.
2 parents 551e9b6 + b65bc89 commit cf9b376

File tree

4 files changed

+61
-43
lines changed

4 files changed

+61
-43
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ If this is set to true (default) then the migration process will automatically c
102102

103103
It would of course be better to find the cause for migration fails, so that no replacement issues would be needed. Finding the cause together with a retry-mechanism would be optimal, and will maybe come in the future - currently the replacement-issue-mechanism helps to keep things in order.
104104

105+
#### useIssuesForAllMergeRequests
106+
107+
If this is set to true (default is false) then all merge requests will be migrated as GitHub issues (rather than pull requests). This can be
108+
used to sidestep the problem where pull requests are rejected by GitHub if the feature branch no longer exists or has been merged.
109+
105110
#### skipMatchingComments
106111

107112
This is an array (empty per default) that may contain string values. Any note/comment in any issue, that contains one or more of those string values, will be skipped (meaining not migrated). Note that this is case insensitive, therefore the string value `foo` would also lead to skipping notes containing a (sub)string `FOO`.

sample_settings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export default {
3131
debug: false,
3232
usePlaceholderIssuesForMissingIssues: true,
3333
useReplacementIssuesForCreationFails: true,
34+
useIssuesForAllMergeRequests: false,
3435
skipMatchingComments: [],
3536
mergeRequests: {
3637
logFile: './merge-requests.json',

src/githubHelper.ts

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ export default class GithubHelper {
1818
userProjectRegex: RegExp;
1919
repoId?: number;
2020
delayInMs: number;
21+
useIssuesForAllMergeRequests: boolean;
2122

22-
constructor(githubApi: GitHubApi, githubSettings: GithubSettings, gitlabHelper: GitlabHelper) {
23+
constructor(githubApi: GitHubApi,
24+
githubSettings: GithubSettings,
25+
gitlabHelper: GitlabHelper,
26+
useIssuesForAllMergeRequests: boolean) {
2327
this.githubApi = githubApi;
2428
this.githubUrl = githubSettings.baseUrl
2529
? githubSettings.baseUrl
@@ -32,6 +36,7 @@ export default class GithubHelper {
3236
// regex for converting user from GitLab to GitHub
3337
this.userProjectRegex = utils.generateUserProjectRegex();
3438
this.delayInMs = 2000;
39+
this.useIssuesForAllMergeRequests = useIssuesForAllMergeRequests;
3540
}
3641

3742
/*
@@ -492,51 +497,55 @@ export default class GithubHelper {
492497
* @returns {Promise<Promise<{data: null}>|Promise<Github.Response<Github.PullsCreateResponse>>|Promise<{data: *}>>}
493498
*/
494499
async createPullRequest(pullRequest) {
495-
let canCreate = true;
500+
let canCreate = !this.useIssuesForAllMergeRequests;
496501

497-
// Check to see if the target branch exists in GitHub - if it does not exist, we cannot create a pull request
498-
try {
499-
await this.githubApi.repos.getBranch({
500-
owner: this.githubOwner,
501-
repo: this.githubRepo,
502-
branch: pullRequest.target_branch,
503-
});
504-
} catch (err) {
505-
let gitlabBranches = await this.gitlabHelper.getAllBranches();
506-
if (gitlabBranches.find(m => m.name === pullRequest.target_branch)) {
507-
// Need to move that branch over to GitHub!
508-
console.error(
509-
`The '${pullRequest.target_branch}' branch exists on GitLab but has not been migrated to GitHub. Please migrate the branch before migrating pull request #${pullRequest.iid}.`
510-
);
511-
return Promise.resolve({ data: null });
512-
} else {
513-
console.error(
514-
`Merge request ${pullRequest.iid} (target branch '${pullRequest.target_branch}' does not exist => cannot migrate pull request, creating an issue instead.`
515-
);
516-
canCreate = false;
502+
if (canCreate) {
503+
// Check to see if the target branch exists in GitHub - if it does not exist, we cannot create a pull request
504+
try {
505+
await this.githubApi.repos.getBranch({
506+
owner: this.githubOwner,
507+
repo: this.githubRepo,
508+
branch: pullRequest.target_branch,
509+
});
510+
} catch (err) {
511+
let gitlabBranches = await this.gitlabHelper.getAllBranches();
512+
if (gitlabBranches.find(m => m.name === pullRequest.target_branch)) {
513+
// Need to move that branch over to GitHub!
514+
console.error(
515+
`The '${pullRequest.target_branch}' branch exists on GitLab but has not been migrated to GitHub. Please migrate the branch before migrating pull request #${pullRequest.iid}.`
516+
);
517+
return Promise.resolve({ data: null });
518+
} else {
519+
console.error(
520+
`Merge request ${pullRequest.iid} (target branch '${pullRequest.target_branch}' does not exist => cannot migrate pull request, creating an issue instead.`
521+
);
522+
canCreate = false;
523+
}
517524
}
518525
}
519526

520-
// Check to see if the source branch exists in GitHub - if it does not exist, we cannot create a pull request
521-
try {
522-
await this.githubApi.repos.getBranch({
523-
owner: this.githubOwner,
524-
repo: this.githubRepo,
525-
branch: pullRequest.source_branch,
526-
});
527-
} catch (err) {
528-
let gitlabBranches = await this.gitlabHelper.getAllBranches();
529-
if (gitlabBranches.find(m => m.name === pullRequest.source_branch)) {
530-
// Need to move that branch over to GitHub!
531-
console.error(
532-
`The '${pullRequest.source_branch}' branch exists on GitLab but has not been migrated to GitHub. Please migrate the branch before migrating pull request #${pullRequest.iid}.`
533-
);
534-
return Promise.resolve({ data: null });
535-
} else {
536-
console.error(
537-
`Pull request #${pullRequest.iid} (source branch '${pullRequest.source_branch}' does not exist => cannot migrate pull request, creating an issue instead.`
538-
);
539-
canCreate = false;
527+
if (canCreate) {
528+
// Check to see if the source branch exists in GitHub - if it does not exist, we cannot create a pull request
529+
try {
530+
await this.githubApi.repos.getBranch({
531+
owner: this.githubOwner,
532+
repo: this.githubRepo,
533+
branch: pullRequest.source_branch,
534+
});
535+
} catch (err) {
536+
let gitlabBranches = await this.gitlabHelper.getAllBranches();
537+
if (gitlabBranches.find(m => m.name === pullRequest.source_branch)) {
538+
// Need to move that branch over to GitHub!
539+
console.error(
540+
`The '${pullRequest.source_branch}' branch exists on GitLab but has not been migrated to GitHub. Please migrate the branch before migrating pull request #${pullRequest.iid}.`
541+
);
542+
return Promise.resolve({ data: null });
543+
} else {
544+
console.error(
545+
`Pull request #${pullRequest.iid} (source branch '${pullRequest.source_branch}' does not exist => cannot migrate pull request, creating an issue instead.`
546+
);
547+
canCreate = false;
548+
}
540549
}
541550
}
542551

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ const githubApi = new GitHubApi({
6666
});
6767

6868
const gitlabHelper = new GitlabHelper(gitlabApi, settings.gitlab);
69-
const githubHelper = new GithubHelper(githubApi, settings.github, gitlabHelper);
69+
const githubHelper = new GithubHelper(githubApi,
70+
settings.github,
71+
gitlabHelper,
72+
settings.useIssuesForAllMergeRequests);
7073

7174
// If no project id is given in settings.js, just return
7275
// all of the projects that this user is associated with.

0 commit comments

Comments
 (0)