Skip to content

Commit 347e459

Browse files
authored
GitLab: Additional places to encode, handle first commit and empty ta… (#27)
1 parent eeafaa0 commit 347e459

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

src/scm.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ class Gitlab extends BaseScmAdapter {
514514
const namespace = encodeURIComponent(
515515
`${commitInfo.owner}/${commitInfo.repo}`,
516516
);
517-
517+
518518
// get project id
519519
const commitUrl = `${this.getApiUrl()}/projects/${namespace}/repository/commits/${commitInfo.commitHash}`;
520520

@@ -564,9 +564,14 @@ class Gitlab extends BaseScmAdapter {
564564
throw new Error('Unable to retrieve modified files from commitData.');
565565
}
566566

567-
const baseApiUrl = `${this.getApiUrl()}/projects/${commitInfo.owner}%2F${
568-
commitInfo.repo
569-
}/repository/files`;
567+
const namespace = encodeURIComponent(
568+
`${commitInfo.owner}/${commitInfo.repo}`,
569+
);
570+
const baseApiUrl = `${this.getApiUrl()}/projects/${namespace}/repository/files`;
571+
572+
// commitData.parents[0] is probably empty if this is the first commit
573+
const shaOld = commitData.parents[0]?.sha || commitData.sha;
574+
const shaNew = commitData.sha;
570575
const modifiedFiles = commitData.files.map((file) => {
571576
return {
572577
filename: file.filename,
@@ -576,16 +581,16 @@ class Gitlab extends BaseScmAdapter {
576581
renamed: file.renamed,
577582
additions: file.additions,
578583
deletions: file.deletions,
579-
shaOld: commitData.parents[0].sha,
580-
shaNew: commitData.sha,
584+
shaOld,
585+
shaNew,
581586
download: {
582587
type: 'raw' as const,
583588
old: `${baseApiUrl}/${file.filenameOld.replace(
584589
/\//g,
585590
'%2f',
586-
)}/raw?ref=${commitData.parents[0].sha}`,
591+
)}/raw?ref=${shaOld}`,
587592
new: `${baseApiUrl}/${file.filename.replace(/\//g, '%2f')}/raw?ref=${
588-
commitData.sha
593+
shaNew
589594
}`,
590595
},
591596
};
@@ -604,10 +609,9 @@ class Gitlab extends BaseScmAdapter {
604609
};
605610
files: CommonChange[];
606611
}> {
612+
const namespace = encodeURIComponent(`${pullInfo.owner}/${pullInfo.repo}`);
607613
const response = await fetch(
608-
`${this.getApiUrl()}/projects/${pullInfo.owner}%2f${
609-
pullInfo.repo
610-
}/merge_requests/${pullInfo.pullNumber}/changes`,
614+
`${this.getApiUrl()}/projects/${namespace}/merge_requests/${pullInfo.pullNumber}/changes`,
611615
{ headers: this.createHeaders(token) },
612616
);
613617
if (!response.ok) {
@@ -632,10 +636,13 @@ class Gitlab extends BaseScmAdapter {
632636
token: string,
633637
): Promise<ModifiedFile[]> {
634638
const pullData = await this.getPullDetails(pullInfo, token);
635-
const baseApiUrl = `${this.getApiUrl()}/projects/${pullInfo.owner}%2F${
636-
pullInfo.repo
637-
}/repository/files`;
639+
const namespace = encodeURIComponent(`${pullInfo.owner}/${pullInfo.repo}`);
640+
const baseApiUrl = `${this.getApiUrl()}/projects/${namespace}/repository/files`;
638641

642+
643+
// pullData.info.base.sha is probably not set if target branch has no commit yet
644+
const shaOld = pullData.info.base.sha || pullData.info.head.sha;
645+
const shaNew = pullData.info.head.sha;
639646
const modifiedFiles = pullData.files.map((file) => {
640647
return {
641648
filename: file.filename,
@@ -645,16 +652,16 @@ class Gitlab extends BaseScmAdapter {
645652
renamed: file.renamed,
646653
additions: file.additions,
647654
deletions: file.deletions,
648-
shaOld: pullData.info.base.sha,
649-
shaNew: pullData.info.head.sha,
655+
shaOld,
656+
shaNew,
650657
download: {
651658
type: 'raw' as const,
652659
old: `${baseApiUrl}/${file.filenameOld.replace(
653660
/\//g,
654661
'%2f',
655-
)}/raw?ref=${pullData.info.base.sha}`,
662+
)}/raw?ref=${shaOld}`,
656663
new: `${baseApiUrl}/${file.filename.replace(/\//g, '%2f')}/raw?ref=${
657-
pullData.info.head.sha
664+
shaNew
658665
}`,
659666
},
660667
};

0 commit comments

Comments
 (0)