The commit view *within* a PR doesn't get injected (#282)

* The commit view *within* a PR doesn't get injected
Fixes #280

* Add specific return for consistency

* Better test

* Add e2e test

* Typo
This commit is contained in:
Pierre Jacquier
2023-07-13 06:49:55 -04:00
committed by GitHub
parent 5aa3aa4ee6
commit ae3149e3cb
6 changed files with 76 additions and 0 deletions

View File

@ -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

View File

@ -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 =

View File

@ -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

View File

@ -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,

Binary file not shown.

After

Width:  |  Height:  |  Size: 100 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB