Skip to content

Commit a4f71a4

Browse files
authored
Get parent sha on PR diff view (OctoLinker#494)
* Get parent sha on PR diff view * Perform user login since GitHub return different html when logged-in
1 parent 6eae26a commit a4f71a4

File tree

6 files changed

+70
-11
lines changed

6 files changed

+70
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
node_modules
22
npm-debug.log
33
lerna-debug.log
4+
.env
45

56
dist/
67
out/

e2e/automated.test.js

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require('dotenv').config();
2+
13
const fixtures = require('./fixtures.json'); // eslint-disable-line import/no-unresolved
24
const diffFixtures = require('./diff-fixtures.json'); // eslint-disable-line import/no-unresolved
35

@@ -15,7 +17,39 @@ async function executeTest(url, targetUrl, selector) {
1517
await expect(page.url()).toEqual(expect.stringMatching(targetUrl));
1618
}
1719

20+
jest.setTimeout(20000);
21+
1822
describe('End to End tests', () => {
23+
beforeAll(async () => {
24+
if (!process.env.E2E_USER_NAME || !process.env.E2E_USER_PASSWORD) {
25+
console.log('Run E2E tests as an anonymous user'); // eslint-disable-line
26+
return;
27+
}
28+
29+
console.log('Perform login ...'); // eslint-disable-line
30+
31+
await page.goto('https://github.com/login');
32+
await expect(page).toFill('#login_field', process.env.E2E_USER_NAME);
33+
await expect(page).toFill('#password', process.env.E2E_USER_PASSWORD);
34+
await Promise.all([
35+
page.waitForNavigation(),
36+
expect(page).toClick('input[type=submit]'),
37+
]);
38+
39+
try {
40+
const authError = await page.$eval('#login .flash-error', el =>
41+
el.textContent.trim(),
42+
);
43+
throw new Error(authError);
44+
} catch (error) {
45+
if (!error.message.includes('failed to find element matching selector')) {
46+
await expect(error).toBeUndefined();
47+
}
48+
}
49+
50+
console.log('Run E2E tests with authenticated user'); // eslint-disable-line
51+
});
52+
1953
describe('single blob', () => {
2054
fixtures.forEach(({ url, content, lineNumber, targetUrl }) => {
2155
it(`resolves ${content} to ${targetUrl}`, async () => {
@@ -26,17 +60,13 @@ describe('End to End tests', () => {
2660

2761
describe('diff view', () => {
2862
diffFixtures.forEach(({ url, targetUrl }) => {
29-
it(
30-
`resolves ${url} to ${targetUrl}`,
31-
async () => {
32-
await executeTest(
33-
url,
34-
targetUrl,
35-
'.selected-line.blob-code .octolinker-link',
36-
);
37-
},
38-
10000,
39-
);
63+
it(`resolves ${url} to ${targetUrl}`, async () => {
64+
await executeTest(
65+
url,
66+
targetUrl,
67+
'.selected-line.blob-code .octolinker-link',
68+
);
69+
});
4070
});
4171
});
4272
});

e2e/diff-fixtures.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,13 @@
66
{
77
"url": "https://github.com/OctoLinker/OctoLinker/commit/b97dfbfdbf3dee5f4836426e6dac6d6f473461db?diff=split#diff-b9cfc7f2cdf78a7f4b91a753d10865a2L23",
88
"targetUrl": "https://github.com/eslint/eslint"
9+
},
10+
{
11+
"url": "https://github.com/OctoLinker/OctoLinker/pull/451/files#diff-b9cfc7f2cdf78a7f4b91a753d10865a2L59",
12+
"targetUrl": "https://github.com/webpack/webpack"
13+
},
14+
{
15+
"url": "https://github.com/OctoLinker/OctoLinker/pull/451/files#diff-b9cfc7f2cdf78a7f4b91a753d10865a2R59",
16+
"targetUrl": "https://github.com/webpack/webpack"
917
}
1018
]

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"codecov": "^3.0.4",
3535
"copy-webpack-plugin": "^4.4.2",
3636
"css-loader": "^0.28.7",
37+
"dotenv": "^6.0.0",
3738
"eslint": "^4.8.0",
3839
"eslint-config-airbnb": "^16.0.0",
3940
"eslint-config-prettier": "^2.6.0",

packages/blob-reader/helper.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,22 @@ function isGist() {
2828
}
2929

3030
function getParentSha() {
31+
// Pull request diff view
32+
const input = document.querySelector('[name="comparison_start_oid"]');
33+
34+
if (input && input.value) {
35+
return input.value;
36+
}
37+
38+
// Pull request diff for unauthenticated users
39+
const url = document.querySelector('.js-load-contents');
40+
if (url && url.dataset.contentsUrl) {
41+
return url.dataset.contentsUrl.match(/base_sha=([0-9a-z]+)/)[1];
42+
}
43+
44+
// Commit diff view
3145
const el = document.querySelector('.sha-block .sha[data-hotkey]');
46+
3247
return el ? el.textContent : null;
3348
}
3449

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3061,6 +3061,10 @@ dot-prop@^4.1.0:
30613061
dependencies:
30623062
is-obj "^1.0.0"
30633063

3064+
dotenv@^6.0.0:
3065+
version "6.0.0"
3066+
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-6.0.0.tgz#24e37c041741c5f4b25324958ebbc34bca965935"
3067+
30643068
dtrace-provider@~0.8:
30653069
version "0.8.7"
30663070
resolved "https://registry.yarnpkg.com/dtrace-provider/-/dtrace-provider-0.8.7.tgz#dc939b4d3e0620cfe0c1cd803d0d2d7ed04ffd04"

0 commit comments

Comments
 (0)