Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ If this is set to true (default) then the migration process will automatically c

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.

#### useIssuesForAllMergeRequests

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
used to sidestep the problem where pull requests are rejected by GitHub if the feature branch no longer exists or has been merged.

#### skipMatchingComments

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`.
Expand Down
1 change: 1 addition & 0 deletions sample_settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default {
debug: false,
usePlaceholderIssuesForMissingIssues: true,
useReplacementIssuesForCreationFails: true,
useIssuesForAllMergeRequests: false,
skipMatchingComments: [],
mergeRequests: {
logFile: './merge-requests.json',
Expand Down
93 changes: 51 additions & 42 deletions src/githubHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ export default class GithubHelper {
userProjectRegex: RegExp;
repoId?: number;
delayInMs: number;
useIssuesForAllMergeRequests: boolean;

constructor(githubApi: GitHubApi, githubSettings: GithubSettings, gitlabHelper: GitlabHelper) {
constructor(githubApi: GitHubApi,
githubSettings: GithubSettings,
gitlabHelper: GitlabHelper,
useIssuesForAllMergeRequests: boolean) {
this.githubApi = githubApi;
this.githubUrl = githubSettings.baseUrl
? githubSettings.baseUrl
Expand All @@ -32,6 +36,7 @@ export default class GithubHelper {
// regex for converting user from GitLab to GitHub
this.userProjectRegex = utils.generateUserProjectRegex();
this.delayInMs = 2000;
this.useIssuesForAllMergeRequests = useIssuesForAllMergeRequests;
}

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

// Check to see if the target branch exists in GitHub - if it does not exist, we cannot create a pull request
try {
await this.githubApi.repos.getBranch({
owner: this.githubOwner,
repo: this.githubRepo,
branch: pullRequest.target_branch,
});
} catch (err) {
let gitlabBranches = await this.gitlabHelper.getAllBranches();
if (gitlabBranches.find(m => m.name === pullRequest.target_branch)) {
// Need to move that branch over to GitHub!
console.error(
`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}.`
);
return Promise.resolve({ data: null });
} else {
console.error(
`Merge request ${pullRequest.iid} (target branch '${pullRequest.target_branch}' does not exist => cannot migrate pull request, creating an issue instead.`
);
canCreate = false;
if (canCreate) {
// Check to see if the target branch exists in GitHub - if it does not exist, we cannot create a pull request
try {
await this.githubApi.repos.getBranch({
owner: this.githubOwner,
repo: this.githubRepo,
branch: pullRequest.target_branch,
});
} catch (err) {
let gitlabBranches = await this.gitlabHelper.getAllBranches();
if (gitlabBranches.find(m => m.name === pullRequest.target_branch)) {
// Need to move that branch over to GitHub!
console.error(
`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}.`
);
return Promise.resolve({ data: null });
} else {
console.error(
`Merge request ${pullRequest.iid} (target branch '${pullRequest.target_branch}' does not exist => cannot migrate pull request, creating an issue instead.`
);
canCreate = false;
}
}
}

// Check to see if the source branch exists in GitHub - if it does not exist, we cannot create a pull request
try {
await this.githubApi.repos.getBranch({
owner: this.githubOwner,
repo: this.githubRepo,
branch: pullRequest.source_branch,
});
} catch (err) {
let gitlabBranches = await this.gitlabHelper.getAllBranches();
if (gitlabBranches.find(m => m.name === pullRequest.source_branch)) {
// Need to move that branch over to GitHub!
console.error(
`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}.`
);
return Promise.resolve({ data: null });
} else {
console.error(
`Pull request #${pullRequest.iid} (source branch '${pullRequest.source_branch}' does not exist => cannot migrate pull request, creating an issue instead.`
);
canCreate = false;
if (canCreate) {
// Check to see if the source branch exists in GitHub - if it does not exist, we cannot create a pull request
try {
await this.githubApi.repos.getBranch({
owner: this.githubOwner,
repo: this.githubRepo,
branch: pullRequest.source_branch,
});
} catch (err) {
let gitlabBranches = await this.gitlabHelper.getAllBranches();
if (gitlabBranches.find(m => m.name === pullRequest.source_branch)) {
// Need to move that branch over to GitHub!
console.error(
`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}.`
);
return Promise.resolve({ data: null });
} else {
console.error(
`Pull request #${pullRequest.iid} (source branch '${pullRequest.source_branch}' does not exist => cannot migrate pull request, creating an issue instead.`
);
canCreate = false;
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ const githubApi = new GitHubApi({
});

const gitlabHelper = new GitlabHelper(gitlabApi, settings.gitlab);
const githubHelper = new GithubHelper(githubApi, settings.github, gitlabHelper);
const githubHelper = new GithubHelper(githubApi,
settings.github,
gitlabHelper,
settings.useIssuesForAllMergeRequests);

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