This repository has been archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from JupiterOne/INT-2374-commitWebLink
INT-2374 - Add support for `commitWebLink` on `gitlab_merge_request`
- Loading branch information
Showing
6 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { | ||
buildCommitWebLinkFromCommitSha, | ||
getBaseRepositoryUrlFromWebUrl, | ||
getCommitWebLinkFromMergeRequest, | ||
} from './mergeRequest'; | ||
|
||
describe('#getBaseRepositoryUrlFromWebUrl', () => { | ||
test('should return base repository URL if valid input supplied', () => { | ||
expect( | ||
getBaseRepositoryUrlFromWebUrl( | ||
'https://gitlab.com/jupiterone-dev/test-proj/-/merge_requests/1', | ||
), | ||
).toEqual('https://gitlab.com/jupiterone-dev/test-proj'); | ||
}); | ||
|
||
test('should return undefined if invalid input supplied', () => { | ||
expect(getBaseRepositoryUrlFromWebUrl('')).toEqual(undefined); | ||
}); | ||
}); | ||
|
||
describe('#buildCommitWebLinkFromCommitSha', () => { | ||
test('should return base repository URL if valid input supplied', () => { | ||
expect( | ||
buildCommitWebLinkFromCommitSha( | ||
'https://gitlab.com/jupiterone-dev/test-proj', | ||
'abcdef', | ||
), | ||
).toEqual('https://gitlab.com/jupiterone-dev/test-proj/-/commit/abcdef'); | ||
}); | ||
}); | ||
|
||
describe('#getCommitWebLinkFromMergeRequest', () => { | ||
test('should build commit web link from merge request when both merge commit sha and squash commit sha present', () => { | ||
expect( | ||
getCommitWebLinkFromMergeRequest({ | ||
web_url: | ||
'https://gitlab.com/jupiterone-dev/test-proj/-/merge_requests/1', | ||
merge_commit_sha: 'abc123', | ||
squash_commit_sha: 'abc456', | ||
}), | ||
).toEqual('https://gitlab.com/jupiterone-dev/test-proj/-/commit/abc123'); | ||
}); | ||
|
||
test('should build commit web link from merge commit sha if no squash commit present', () => { | ||
expect( | ||
getCommitWebLinkFromMergeRequest({ | ||
web_url: | ||
'https://gitlab.com/jupiterone-dev/test-proj/-/merge_requests/1', | ||
merge_commit_sha: 'abc123', | ||
}), | ||
).toEqual('https://gitlab.com/jupiterone-dev/test-proj/-/commit/abc123'); | ||
}); | ||
|
||
test('should build commit web link from squash commit sha if no merge commit sha present', () => { | ||
expect( | ||
getCommitWebLinkFromMergeRequest({ | ||
web_url: | ||
'https://gitlab.com/jupiterone-dev/test-proj/-/merge_requests/1', | ||
squash_commit_sha: 'abc456', | ||
}), | ||
).toEqual('https://gitlab.com/jupiterone-dev/test-proj/-/commit/abc456'); | ||
}); | ||
|
||
test('should return undefined if neither merge commit or squash commit sha is present', () => { | ||
expect( | ||
getCommitWebLinkFromMergeRequest({ | ||
web_url: | ||
'https://gitlab.com/jupiterone-dev/test-proj/-/merge_requests/1', | ||
}), | ||
).toEqual(undefined); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { GitLabMergeRequest } from '../provider/types'; | ||
|
||
/** | ||
* Example input: https://gitlab.com/jupiterone-dev/test-proj/-/merge_requests/1 | ||
* Example output: https://gitlab.com/jupiterone-dev/test-proj | ||
*/ | ||
export function getBaseRepositoryUrlFromWebUrl( | ||
webUrl: string, | ||
): string | undefined { | ||
// ['https://gitlab.com/jupiterone-dev/test-proj', 'merge_requests/1'] | ||
const expectedSplitPartsSize = 2; | ||
const parts = webUrl.split('/-/'); | ||
if (parts.length !== expectedSplitPartsSize) return undefined; | ||
return parts[0]; | ||
} | ||
|
||
export function buildCommitWebLinkFromCommitSha( | ||
baseUrl: string, | ||
commitSha: string, | ||
) { | ||
return `${baseUrl}/-/commit/${commitSha}`; | ||
} | ||
|
||
export function getCommitWebLinkFromMergeRequest( | ||
mergeRequest: GitLabMergeRequest, | ||
): string | undefined { | ||
const sha = mergeRequest.merge_commit_sha || mergeRequest.squash_commit_sha; | ||
if (!sha) return; | ||
|
||
const baseUrl = getBaseRepositoryUrlFromWebUrl(mergeRequest.web_url); | ||
return baseUrl && buildCommitWebLinkFromCommitSha(baseUrl, sha); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters