Skip to content

Commit 76517d3

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 8a167db + 3b7d7d0 commit 76517d3

1 file changed

Lines changed: 62 additions & 4 deletions

File tree

src/githubHelper.ts

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ import {Octokit as GitHubApi, RestEndpointMethodTypes} from '@octokit/rest';
55
import { IssuesListForRepoResponseData, PullsListResponseData } from "@octokit/types";
66
import GitlabHelper from './gitlabHelper';
77

8+
const gitHubLocation = 'https://github.com';
9+
810
export default class GithubHelper {
911
githubApi: GitHubApi;
12+
githubUrl: string;
1013
githubOwner: string;
1114
githubToken: string;
1215
githubRepo: string;
@@ -18,6 +21,9 @@ export default class GithubHelper {
1821

1922
constructor(githubApi: GitHubApi, githubSettings: GithubSettings, gitlabHelper: GitlabHelper) {
2023
this.githubApi = githubApi;
24+
this.githubUrl = githubSettings.baseUrl
25+
? githubSettings.baseUrl
26+
: gitHubLocation;
2127
this.githubOwner = githubSettings.owner;
2228
this.githubToken = githubSettings.token;
2329
this.githubRepo = githubSettings.repo;
@@ -766,17 +772,18 @@ export default class GithubHelper {
766772
*/
767773

768774
async convertIssuesAndComments(str: string, item: any) {
775+
const repoLink = `${this.githubUrl}/${this.githubOwner}/${this.githubRepo}`;
769776
if (
770777
(!settings.usermap || Object.keys(settings.usermap).length === 0) &&
771778
(!settings.projectmap || Object.keys(settings.projectmap).length === 0)
772779
) {
773-
return GithubHelper.addMigrationLine(str, item);
780+
return GithubHelper.addMigrationLine(str, item, repoLink);
774781
} else {
775782
// - Replace userids as defined in settings.usermap.
776783
// They all start with '@' in the issues but we have them without in usermap
777784
// - Replace cross-project issue references. They are matched on org/project# so 'matched' ends with '#'
778785
// They all have a '#' right after the project name in the issues but we have them without in projectmap
779-
let strWithMigLine = GithubHelper.addMigrationLine(str, item);
786+
let strWithMigLine = GithubHelper.addMigrationLine(str, item, repoLink);
780787

781788
strWithMigLine = strWithMigLine.replace(
782789
this.userProjectRegex,
@@ -811,7 +818,7 @@ export default class GithubHelper {
811818
* Adds a line of text at the beginning of a comment that indicates who, when
812819
* and from GitLab.
813820
*/
814-
static addMigrationLine(str: string, item: any): string {
821+
static addMigrationLine(str: string, item: any, repoLink: string): string {
815822
if (!item || !item.author || !item.author.username || !item.created_at) {
816823
return str;
817824
}
@@ -830,6 +837,57 @@ export default class GithubHelper {
830837
dateformatOptions
831838
);
832839

833-
return `In GitLab by @${item.author.username} on ${formattedDate}\n\n${str}`;
840+
const attribution = `In GitLab by @${item.author.username} on ${formattedDate}`;
841+
const lineRef =
842+
item && item.position
843+
? GithubHelper.createLineRef(item.position, repoLink)
844+
: '';
845+
const summary = attribution + (lineRef ? `\n\n${lineRef}` : '');
846+
847+
return `${summary}\n\n${str}`;
848+
}
849+
850+
/**
851+
* When migrating in-line comments to GitHub then creates a link to the
852+
* appropriate line of the diff.
853+
*/
854+
static createLineRef(position, repoLink) {
855+
if (
856+
!repoLink ||
857+
!repoLink.startsWith(gitHubLocation) ||
858+
!position ||
859+
!position.head_sha
860+
) {
861+
return '';
862+
}
863+
const base_sha = position.base_sha;
864+
const head_sha = position.head_sha;
865+
var path = '';
866+
var line = '';
867+
var slug = '';
868+
if (
869+
(position.new_line && position.new_path) ||
870+
(position.old_line && position.old_path)
871+
) {
872+
var side;
873+
if (!position.old_line || !position.old_path) {
874+
side = 'R';
875+
path = position.new_path;
876+
line = position.new_line;
877+
} else {
878+
side = 'L';
879+
path = position.old_path;
880+
line = position.old_line;
881+
}
882+
const crypto = require('crypto');
883+
const hash = crypto
884+
.createHash('md5')
885+
.update(path)
886+
.digest('hex');
887+
slug = `#diff-${hash}${side}${line}`;
888+
}
889+
// Mention the file and line number. If we can't get this for some reason then use the commit id instead.
890+
const ref = path && line ? `${path} line ${line}` : `${head_sha}`;
891+
return `Commented on [${ref}](${repoLink}/compare/${base_sha}..${head_sha}${slug})\n\n`;
834892
}
835893
}

0 commit comments

Comments
 (0)