diff --git a/src/chrome/content.ts b/src/chrome/content.ts index e0b463c..ffcd5e9 100644 --- a/src/chrome/content.ts +++ b/src/chrome/content.ts @@ -8,6 +8,7 @@ import { getGithubCommitUrlParams, createReactRoot, getGithubBlobUrlParams, + getGithubCommitWithinPullUrlParams, } from './web' import gitHubInjection from 'github-injection' @@ -128,6 +129,15 @@ async function run() { return } + const commitWithinPullParams = getGithubCommitWithinPullUrlParams(url) + if (commitWithinPullParams) { + const { owner, repo, pull, sha } = commitWithinPullParams + console.log('Found commit diff within pull: ', owner, repo, pull, sha) + // TODO: understand if more things are needed here for this special case + await injectCommitDiff(owner, repo, sha, window.document) + return + } + const blobParams = getGithubBlobUrlParams(url) if (blobParams) { const { owner, repo, sha, filename } = blobParams diff --git a/src/chrome/web.test.ts b/src/chrome/web.test.ts index e17f77d..82534d0 100644 --- a/src/chrome/web.test.ts +++ b/src/chrome/web.test.ts @@ -8,6 +8,7 @@ import { getSupportedWebDiffElements, createReactRoot, getGithubBlobUrlParams, + getGithubCommitWithinPullUrlParams, } from './web' const githubPullHtmlSnippet = ` @@ -147,6 +148,29 @@ describe('Function getGithubCommitUrlParams', () => { }) }) +describe('Function getGithubCommitWithinPullUrlParams', () => { + it('gets params out of a valid github commit link within a PR', () => { + const url = + 'https://github.com/KittyCAD/diff-samples/pull/2/commits/1dc0d43a94dba95279fcfc112bb5dd4dfaac01ae' + const params = getGithubCommitWithinPullUrlParams(url) + expect(params).toBeDefined() + const { owner, repo, pull, sha } = params! + expect(owner).toEqual('KittyCAD') + expect(repo).toEqual('diff-samples') + expect(pull).toEqual(2) + expect(sha).toEqual('1dc0d43a94dba95279fcfc112bb5dd4dfaac01ae') + }) + + it("doesn't match other URLs", () => { + expect(getGithubCommitWithinPullUrlParams('http://google.com')).toBeUndefined() + expect( + getGithubCommitWithinPullUrlParams( + 'https://github.com/KittyCAD/litterbox/commit/4ddf899550addf41d6bf1b790ce79e46501411b3' + ) + ).toBeUndefined() + }) +}) + describe('Function getGithubBlobUrlParams', () => { it('gets params out of a valid github blob link', () => { const url = diff --git a/src/chrome/web.ts b/src/chrome/web.ts index c41cec6..ab00aa2 100644 --- a/src/chrome/web.ts +++ b/src/chrome/web.ts @@ -44,6 +44,34 @@ export function getGithubCommitUrlParams( return { owner, repo, sha } } +export type GithubCommitWithinPullUrlParams = { + owner: string + repo: string + pull: number + sha: string +} + +export function getGithubCommitWithinPullUrlParams( + url: string +): GithubCommitWithinPullUrlParams | undefined { + const pullRe = + /https:\/\/github\.com\/([a-zA-Z0-9_.-]+)\/([a-zA-Z0-9_.-]+)\/pull\/(\d+)\/commits\/(\w+)/ + const result = pullRe.exec(url) + if (!result) { + return undefined + } + + const [, owner, repo, pull, sha] = result + console.log( + 'Found a supported Github Commit witin Pull URL:', + owner, + repo, + pull, + sha + ) + return { owner, repo, sha, pull: parseInt(pull) } +} + export type GithubBlobUrlParams = { owner: string repo: string diff --git a/tests/extension.spec.ts b/tests/extension.spec.ts index 6c654b4..6000fb8 100644 --- a/tests/extension.spec.ts +++ b/tests/extension.spec.ts @@ -68,6 +68,20 @@ test('pull request diff with a modified .obj file', async ({ expect(screenshot2).toMatchSnapshot() }) +test('commit diff within pull request with a modified .stl file', async ({ + page, + authorizedBackground, +}) => { + const url = 'https://github.com/KittyCAD/diff-samples/pull/2/commits/1dc0d43a94dba95279fcfc112bb5dd4dfaac01ae' + const element = await getFirstDiffElement(page, url, 'stl') + const screenshot = await element.screenshot() + expect(screenshot).toMatchSnapshot() + + await enableCombined(page, element) + const screenshot2 = await element.screenshot() + expect(screenshot2).toMatchSnapshot() +}) + test('pull request diff with a modified .step file', async ({ page, authorizedBackground, diff --git a/tests/extension.spec.ts-snapshots/commit-diff-within-pull-request-with-a-modified-stl-file-1-chromium-linux.png b/tests/extension.spec.ts-snapshots/commit-diff-within-pull-request-with-a-modified-stl-file-1-chromium-linux.png new file mode 100644 index 0000000..b6644f4 Binary files /dev/null and b/tests/extension.spec.ts-snapshots/commit-diff-within-pull-request-with-a-modified-stl-file-1-chromium-linux.png differ diff --git a/tests/extension.spec.ts-snapshots/commit-diff-within-pull-request-with-a-modified-stl-file-2-chromium-linux.png b/tests/extension.spec.ts-snapshots/commit-diff-within-pull-request-with-a-modified-stl-file-2-chromium-linux.png new file mode 100644 index 0000000..99dcc3d Binary files /dev/null and b/tests/extension.spec.ts-snapshots/commit-diff-within-pull-request-with-a-modified-stl-file-2-chromium-linux.png differ