Skip to content

Commit 7572017

Browse files
authored
Merge pull request #93 from john-forrest/fix-422-existing-branch-issue
Handle scenario where new PR branch has been merged
2 parents 0df6923 + d65ac3f commit 7572017

1 file changed

Lines changed: 36 additions & 24 deletions

File tree

src/githubHelper.ts

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -569,32 +569,44 @@ export default class GithubHelper {
569569

570570
await utils.sleep(this.delayInMs);
571571

572-
// create the GitHub pull request from the GitLab issue
573-
return this.githubApi.pulls.create(props);
574-
} else {
575-
// Create an issue with a descriptive title
576-
let mergeStr =
577-
'_Merges ' +
578-
pullRequest.source_branch +
579-
' -> ' +
580-
pullRequest.target_branch +
581-
'_\n\n';
582-
let bodyConverted = await this.convertIssuesAndComments(
583-
mergeStr + pullRequest.description,
584-
pullRequest
585-
);
586-
let props = {
587-
owner: this.githubOwner,
588-
repo: this.githubRepo,
589-
title: pullRequest.title.trim() + ' - [' + pullRequest.state + ']',
590-
body: bodyConverted,
591-
};
572+
try {
573+
// try to create the GitHub pull request from the GitLab issue
574+
await this.githubApi.pulls.create(props);
575+
return Promise.resolve({ data: null }); // need to return null promise for parent to wait on
576+
} catch (err) {
577+
if(err.status === 422) {
578+
console.error(
579+
`Pull request #${pullRequest.iid} - attempt to create has failed, assume '${pullRequest.source_branch}' has already been merged => cannot migrate pull request, creating an issue instead.`
580+
);
581+
// fall through to next section
582+
} else {
583+
throw (err);
584+
}
585+
}
586+
}
587+
588+
// Failing all else, create an issue with a descriptive title
589+
let mergeStr =
590+
'_Merges ' +
591+
pullRequest.source_branch +
592+
' -> ' +
593+
pullRequest.target_branch +
594+
'_\n\n';
595+
let bodyConverted = await this.convertIssuesAndComments(
596+
mergeStr + pullRequest.description,
597+
pullRequest
598+
);
599+
let props = {
600+
owner: this.githubOwner,
601+
repo: this.githubRepo,
602+
title: pullRequest.title.trim() + ' - [' + pullRequest.state + ']',
603+
body: bodyConverted,
604+
};
592605

593-
// Add a label to indicate the issue is a merge request
594-
pullRequest.labels.push('gitlab merge request');
606+
// Add a label to indicate the issue is a merge request
607+
pullRequest.labels.push('gitlab merge request');
595608

596-
return this.githubApi.issues.create(props);
597-
}
609+
return this.githubApi.issues.create(props);
598610
}
599611

600612
// ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)