Skip to content

Commit ece6f62

Browse files
committed
Some types added. Cosmetic changes.
1 parent fef67be commit ece6f62

5 files changed

Lines changed: 41 additions & 49 deletions

File tree

src/githubHelper.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ interface CommentImport {
2020
interface IssueImport {
2121
title: string;
2222
body: string;
23-
created_at: string;
2423
closed: boolean;
2524
assignee?: string;
25+
created_at?: string;
2626
updated_at?: string;
2727
milestone?: number;
28-
labels?: Array<string>;
28+
labels?: string[];
2929
}
3030

3131
export interface MilestoneImport {
@@ -200,7 +200,7 @@ export class GithubHelper {
200200
* @param tag {string} - the tag name to search a release for
201201
* @returns
202202
*/
203-
async getReleaseByTag(tag) {
203+
async getReleaseByTag(tag: string) {
204204
try {
205205
await utils.sleep(this.delayInMs);
206206
// get an existing release by tag name in github
@@ -604,9 +604,7 @@ export class GithubHelper {
604604
let nrOfMigratedNotes = 0;
605605
for (let note of notes) {
606606
const gotMigrated = await this.processNote(note, githubIssue);
607-
if (gotMigrated) {
608-
nrOfMigratedNotes++;
609-
}
607+
if (gotMigrated) nrOfMigratedNotes++;
610608
}
611609

612610
console.log(
@@ -625,7 +623,7 @@ export class GithubHelper {
625623
* Note that this is case insensitive!
626624
*
627625
*/
628-
checkIfNoteCanBeSkipped(noteBody) {
626+
checkIfNoteCanBeSkipped(noteBody: string) {
629627
const stateChange =
630628
(/Status changed to .*/i.test(noteBody) &&
631629
!/Status changed to closed by commit.*/i.test(noteBody)) ||
@@ -734,7 +732,7 @@ export class GithubHelper {
734732

735733
await utils.sleep(this.delayInMs);
736734

737-
if (settings.debug) return Promise.resolve();
735+
if (settings.debug) return Promise.resolve({ number: -1, title: 'DEBUG' });
738736

739737
const created = await this.githubApi.issues.createMilestone(
740738
githubMilestone
@@ -754,7 +752,7 @@ export class GithubHelper {
754752
owner: this.githubOwner,
755753
repo: this.githubRepo,
756754
name: label.name,
757-
color: label.color.substr(1), // remove leading "#" because gitlab returns it but github wants the color without it
755+
color: label.color.substring(1), // remove leading "#" because gitlab returns it but github wants the color without it
758756
};
759757

760758
await utils.sleep(this.delayInMs);
@@ -1249,7 +1247,7 @@ export class GithubHelper {
12491247
* When migrating in-line comments to GitHub then creates a link to the
12501248
* appropriate line of the diff.
12511249
*/
1252-
static createLineRef(position, repoLink) {
1250+
static createLineRef(position, repoLink: string): string {
12531251
if (
12541252
!repoLink ||
12551253
!repoLink.startsWith(gitHubLocation) ||

src/gitlabHelper.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ export class GitlabHelper {
2525
this.gitlabToken = gitlabSettings.token;
2626
this.gitlabProjectId = gitlabSettings.projectId;
2727
this.host = gitlabSettings.url ? gitlabSettings.url : 'http://gitlab.com';
28+
this.host = this.host.endsWith('/')
29+
? this.host.substring(0, this.host.length - 1)
30+
: this.host;
2831
this.sessionCookie = gitlabSettings.sessionCookie;
2932
this.allBranches = null;
3033
}
@@ -37,13 +40,13 @@ export class GitlabHelper {
3740
const projects = await this.gitlabApi.Projects.all({ membership: true });
3841

3942
// print each project with info
40-
for (let i = 0; i < projects.length; i++) {
43+
for (let project of projects) {
4144
console.log(
42-
projects[i].id.toString(),
45+
project.id.toString(),
4346
'\t',
44-
projects[i].name,
47+
project.name,
4548
'\t--\t',
46-
projects[i]['description']
49+
project['description']
4750
);
4851
}
4952

@@ -93,8 +96,7 @@ export class GitlabHelper {
9396
*/
9497
async getAttachment(relurl: string) {
9598
try {
96-
const host = this.host.endsWith('/') ? this.host : this.host + '/';
97-
const attachmentUrl = host + this.projectPath + relurl;
99+
const attachmentUrl = this.host + '/' + this.projectPath + relurl;
98100
const data = (
99101
await axios.get(attachmentUrl, {
100102
responseType: 'arraybuffer',

src/index.ts

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ if (!settings.gitlab.projectId) {
102102
*/
103103
function createPlaceholderMilestone(expectedIdx: number): MilestoneImport {
104104
return {
105+
id: -1, // dummy
105106
iid: expectedIdx,
106107
title: `[PLACEHOLDER] - for milestone #${expectedIdx}`,
107108
description:
@@ -155,35 +156,30 @@ async function migrate() {
155156
//
156157

157158
try {
158-
let milestoneMap: Map<number, number>;
159-
160159
await githubHelper.registerRepoId();
161160
await gitlabHelper.registerProjectPath(settings.gitlab.projectId);
162161

163-
// transfer GitLab milestones to GitHub
164162
if (settings.transfer.milestones) {
165-
milestoneMap = await transferMilestones(
163+
await transferMilestones(
166164
settings.usePlaceholderMilestonesForMissingMilestones
167165
);
168166
}
169167

170-
// transfer GitLab labels to GitHub
171168
if (settings.transfer.labels) {
172169
await transferLabels(true, settings.conversion.useLowerCaseLabels);
173170
}
174171

175-
// transfer GitLab releases to GitHub
176172
if (settings.transfer.releases) {
177173
await transferReleases();
178174
}
179175

180-
// Transfer issues with their comments; do this before transferring the merge requests
176+
// Important: do this before transferring the merge requests
181177
if (settings.transfer.issues) {
182178
await transferIssues();
183179
}
180+
184181
if (settings.transfer.mergeRequests) {
185182
if (settings.mergeRequests.log) {
186-
// log merge requests
187183
await logMergeRequests(settings.mergeRequests.logFile);
188184
} else {
189185
await transferMergeRequests();
@@ -299,9 +295,7 @@ async function transferLabels(attachmentLabel = true, useLowerCase = true) {
299295
// if a GitLab label does not exist in GitHub repo, create it.
300296
for (let label of labels) {
301297
// GitHub prefers lowercase label names
302-
if (useLowerCase) {
303-
label.name = label.name.toLowerCase();
304-
}
298+
if (useLowerCase) label.name = label.name.toLowerCase();
305299

306300
if (!githubLabels.find(l => l === label.name)) {
307301
console.log('Creating: ' + label.name);
@@ -536,9 +530,7 @@ async function transferReleases() {
536530
inform('Transferring Releases');
537531

538532
// Get a list of all releases associated with this project
539-
let releases = (await gitlabApi.Releases.all(
540-
settings.gitlab.projectId
541-
)) as any;
533+
let releases = await gitlabApi.Releases.all(settings.gitlab.projectId);
542534

543535
// Sort releases in ascending order of their release date
544536
releases = releases.sort((a, b) => {
@@ -593,44 +585,40 @@ async function transferReleases() {
593585
/**
594586
* logs merge requests that exist in GitLab to a file.
595587
*/
596-
async function logMergeRequests(logFile) {
588+
async function logMergeRequests(logFile: string) {
597589
inform('Logging Merge Requests');
598590

599591
// get a list of all GitLab merge requests associated with this project
600592
// TODO return all MRs via pagination
601-
let mergeRequests = (await gitlabApi.MergeRequests.all({
593+
let mergeRequests = await gitlabApi.MergeRequests.all({
602594
projectId: settings.gitlab.projectId,
603595
labels: settings.filterByLabel,
604-
})) as any;
596+
});
605597

606598
// sort MRs in ascending order of when they were created (by id)
607599
mergeRequests = mergeRequests.sort((a, b) => a.id - b.id);
608600

609601
console.log('Logging ' + mergeRequests.length.toString() + ' merge requests');
610602

611-
for (let mergeRequest of mergeRequests) {
603+
for (let mr of mergeRequests) {
612604
let mergeRequestDiscussions = await gitlabApi.MergeRequestDiscussions.all(
613605
settings.gitlab.projectId,
614-
mergeRequest.iid
606+
mr.iid
615607
);
616608
let mergeRequestNotes = await gitlabApi.MergeRequestNotes.all(
617609
settings.gitlab.projectId,
618-
mergeRequest.iid,
610+
mr.iid,
619611
{}
620612
);
621613

622-
mergeRequest.discussions = mergeRequestDiscussions
623-
? mergeRequestDiscussions
624-
: [];
625-
mergeRequest.notes = mergeRequestNotes ? mergeRequestNotes : [];
614+
mr.discussions = mergeRequestDiscussions ? mergeRequestDiscussions : [];
615+
mr.notes = mergeRequestNotes ? mergeRequestNotes : [];
626616
}
627617

628618
//
629619
// Log the merge requests to a file
630620
//
631-
const output = {
632-
mergeRequests: mergeRequests,
633-
};
621+
const output = { mergeRequests: mergeRequests };
634622

635623
fs.writeFileSync(logFile, JSON.stringify(output, null, 2));
636624
}
@@ -640,7 +628,7 @@ async function logMergeRequests(logFile) {
640628
/**
641629
* Print out a section heading to let the user know what is happening
642630
*/
643-
function inform(msg) {
631+
function inform(msg: string) {
644632
console.log('==================================');
645633
console.log(msg);
646634
console.log('==================================');

src/settings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default interface Settings {
2323
usePlaceholderIssuesForMissingIssues: boolean;
2424
useReplacementIssuesForCreationFails: boolean;
2525
useIssuesForAllMergeRequests: boolean;
26-
filterByLabel: string | null;
26+
filterByLabel?: string;
2727
skipMergeRequestStates: string[];
2828
skipMatchingComments: string[];
2929
mergeRequests: {

src/utils.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const generateUserProjectRegex = () => {
3636
export const migrateAttachments = async (
3737
body: string,
3838
githubRepoId: number | undefined,
39-
s3: S3Settings,
39+
s3: S3Settings | undefined,
4040
gitlabHelper: GitlabHelper
4141
) => {
4242
const regexp = /(!?)\[([^\]]+)\]\((\/uploads[^)]+)\)/g;
@@ -79,7 +79,7 @@ export const migrateAttachments = async (
7979
const params: S3.PutObjectRequest = {
8080
Key: relativePath,
8181
Body: attachmentBuffer,
82-
ContentType: mimeType === false ? null : mimeType,
82+
ContentType: mimeType === false ? undefined : mimeType,
8383
Bucket: s3.bucket,
8484
};
8585

@@ -94,14 +94,18 @@ export const migrateAttachments = async (
9494
});
9595

9696
// Add the new URL to the map
97-
offsetToAttachment[match.index] = `${prefix}[${name}](${s3url})`;
97+
offsetToAttachment[
98+
match.index as number
99+
] = `${prefix}[${name}](${s3url})`;
98100
} else {
99101
// Not using S3: default to old URL, adding absolute path
100102
const host = gitlabHelper.host.endsWith('/')
101103
? gitlabHelper.host
102104
: gitlabHelper.host + '/';
103105
const attachmentUrl = host + gitlabHelper.projectPath + url;
104-
offsetToAttachment[match.index] = `${prefix}[${name}](${attachmentUrl})`;
106+
offsetToAttachment[
107+
match.index as number
108+
] = `${prefix}[${name}](${attachmentUrl})`;
105109
}
106110
}
107111

0 commit comments

Comments
 (0)