Skip to content

Commit

Permalink
Get parent sha on PR diff view (#494)
Browse files Browse the repository at this point in the history
* Get parent sha on PR diff view

* Perform user login since GitHub return different html when logged-in
  • Loading branch information
stefanbuck authored Aug 13, 2018
1 parent 6eae26a commit a4f71a4
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
node_modules
npm-debug.log
lerna-debug.log
.env

dist/
out/
Expand Down
52 changes: 41 additions & 11 deletions e2e/automated.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require('dotenv').config();

const fixtures = require('./fixtures.json'); // eslint-disable-line import/no-unresolved
const diffFixtures = require('./diff-fixtures.json'); // eslint-disable-line import/no-unresolved

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

jest.setTimeout(20000);

describe('End to End tests', () => {
beforeAll(async () => {
if (!process.env.E2E_USER_NAME || !process.env.E2E_USER_PASSWORD) {
console.log('Run E2E tests as an anonymous user'); // eslint-disable-line
return;
}

console.log('Perform login ...'); // eslint-disable-line

await page.goto('https://github.com/login');
await expect(page).toFill('#login_field', process.env.E2E_USER_NAME);
await expect(page).toFill('#password', process.env.E2E_USER_PASSWORD);
await Promise.all([
page.waitForNavigation(),
expect(page).toClick('input[type=submit]'),
]);

try {
const authError = await page.$eval('#login .flash-error', el =>
el.textContent.trim(),
);
throw new Error(authError);
} catch (error) {
if (!error.message.includes('failed to find element matching selector')) {
await expect(error).toBeUndefined();
}
}

console.log('Run E2E tests with authenticated user'); // eslint-disable-line
});

describe('single blob', () => {
fixtures.forEach(({ url, content, lineNumber, targetUrl }) => {
it(`resolves ${content} to ${targetUrl}`, async () => {
Expand All @@ -26,17 +60,13 @@ describe('End to End tests', () => {

describe('diff view', () => {
diffFixtures.forEach(({ url, targetUrl }) => {
it(
`resolves ${url} to ${targetUrl}`,
async () => {
await executeTest(
url,
targetUrl,
'.selected-line.blob-code .octolinker-link',
);
},
10000,
);
it(`resolves ${url} to ${targetUrl}`, async () => {
await executeTest(
url,
targetUrl,
'.selected-line.blob-code .octolinker-link',
);
});
});
});
});
8 changes: 8 additions & 0 deletions e2e/diff-fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,13 @@
{
"url": "https://github.com/OctoLinker/OctoLinker/commit/b97dfbfdbf3dee5f4836426e6dac6d6f473461db?diff=split#diff-b9cfc7f2cdf78a7f4b91a753d10865a2L23",
"targetUrl": "https://github.com/eslint/eslint"
},
{
"url": "https://github.com/OctoLinker/OctoLinker/pull/451/files#diff-b9cfc7f2cdf78a7f4b91a753d10865a2L59",
"targetUrl": "https://github.com/webpack/webpack"
},
{
"url": "https://github.com/OctoLinker/OctoLinker/pull/451/files#diff-b9cfc7f2cdf78a7f4b91a753d10865a2R59",
"targetUrl": "https://github.com/webpack/webpack"
}
]
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"codecov": "^3.0.4",
"copy-webpack-plugin": "^4.4.2",
"css-loader": "^0.28.7",
"dotenv": "^6.0.0",
"eslint": "^4.8.0",
"eslint-config-airbnb": "^16.0.0",
"eslint-config-prettier": "^2.6.0",
Expand Down
15 changes: 15 additions & 0 deletions packages/blob-reader/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,22 @@ function isGist() {
}

function getParentSha() {
// Pull request diff view
const input = document.querySelector('[name="comparison_start_oid"]');

if (input && input.value) {
return input.value;
}

// Pull request diff for unauthenticated users
const url = document.querySelector('.js-load-contents');
if (url && url.dataset.contentsUrl) {
return url.dataset.contentsUrl.match(/base_sha=([0-9a-z]+)/)[1];
}

// Commit diff view
const el = document.querySelector('.sha-block .sha[data-hotkey]');

return el ? el.textContent : null;
}

Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3061,6 +3061,10 @@ dot-prop@^4.1.0:
dependencies:
is-obj "^1.0.0"

dotenv@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-6.0.0.tgz#24e37c041741c5f4b25324958ebbc34bca965935"

dtrace-provider@~0.8:
version "0.8.7"
resolved "https://registry.yarnpkg.com/dtrace-provider/-/dtrace-provider-0.8.7.tgz#dc939b4d3e0620cfe0c1cd803d0d2d7ed04ffd04"
Expand Down

0 comments on commit a4f71a4

Please sign in to comment.