Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commit checks from forks are now included #72

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
16 changes: 16 additions & 0 deletions test/getCommitChecks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const test = require('tape');

const { getCommitChecks } = require('../utils/getCommitChecks');
const mock = require('./mocks/getCommitChecks');

test('evaluateCommitStatus', (t) => {
t.equal(
getCommitChecks(mock.commitChecks),
mock.expected,
mock.description,
);

t.end();
});
203 changes: 203 additions & 0 deletions test/mocks/getCommitChecks.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions utils/buildQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ const pullRequestQuery = (name, owner, pr, sha) => `
commits(last: 1) {
nodes {
commit {
oid
author {
user {
url
}
}
statusCheckRollup {
state
contexts(last: 100) {
Expand Down Expand Up @@ -70,6 +76,13 @@ const commitStatusQuery = (name, owner, sha) => `
... on Commit {
statusCheckRollup {
state
commit {
author {
user {
url
}
}
}
contexts(last: 100) {
totalCount
pageInfo {
Expand Down
109 changes: 109 additions & 0 deletions utils/getCommitChecks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/* eslint-disable no-shadow */
/* eslint-disable no-unused-vars */

'use strict';

const { graphql } = require('@octokit/graphql');

let owner, name, token, repo, pr, sha, search;

const modifiedNodes = [];

const runCommitQuery = async ({ name, owner, sha, token }) => {
const q = `
repository(name: "${name}", owner: "${owner}") {
commit: object(expression: "${sha}") {
... on Commit {
statusCheckRollup {
state
contexts(last: 100) {
totalCount
pageInfo {
endCursor
hasNextPage
}
nodes {
__typename
... on CheckRun {
status
name
conclusion
}
... on StatusContext {
state
context
description
}
}
}
}
}
}
}
`;

try {
const response = await graphql(`{${q}}`, {
headers: {
authorization: `token ${token}`,
},
});
return response.repository.commit?.statusCheckRollup?.contexts?.nodes;
} catch (error) {
console.log(error);
return null;
}
Comment on lines +51 to +55
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return response.repository.commit?.statusCheckRollup?.contexts?.nodes;
} catch (error) {
console.log(error);
return null;
}
} catch (error) {
console.log(error);
return null;
}
return response.repository.commit?.statusCheckRollup?.contexts?.nodes;

};

const doNodes = async (ogNode) => {
const node = JSON.parse(JSON.stringify(ogNode));
// console.log(JSON.stringify(node,undefined,2));
Comment on lines +59 to +60
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than cloning here, maybe we could { ...ogNode, commit: { ...ogNode.commit, ...newCommit } }?

const { commit } = node;
const {
author: {
user: { url },
},
oid,
} = commit;

await runCommitQuery({
name,
owner: url.slice(url.lastIndexOf('/') + 1),
sha: oid,
token,
}).then((commitChecks) => {
const prchecks = commit.statusCheckRollup?.contexts.nodes ?? [];
const allChecks = prchecks.concat(commitChecks || []);
// modify the values only if checks are found
if (allChecks.length > 0) {
commit.statusCheckRollup.contexts.nodes = allChecks;
commit.statusCheckRollup.contexts.totalCount = allChecks.length;
}

});
modifiedNodes.push(node);
};

const getCommitChecks = async (response, data) => {
({ repo, pr, sha, token } = data);
[owner, name] = repo.split('/');
({ search } = response);

await search.edges.reduce(async (prev, edge) => {
await prev;
return edge.node.commits.nodes.reduce(
async (prev, node) => {
await prev;
return doNodes(node);
},
Promise.resolve(),
);
}, Promise.resolve());

return modifiedNodes;

};

module.exports = {
getCommitChecks,
};
6 changes: 4 additions & 2 deletions utils/resolveIndentifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

const exec = require('child_process').execSync;

// function to resolve the identifier to it's sha
module.exports = (identifier, short = false) => {
const command = short ? `git show ${identifier} -s --format="%h"` : `git show ${identifier} -s --format="%H"`;
try {
return String(exec(command));
const command = short ? `git show ${identifier} -s --format="%h"` : `git show ${identifier} -s --format="%H"`;
const stdout = String(exec(command));
return stdout;
Comment on lines +8 to +10
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const command = short ? `git show ${identifier} -s --format="%h"` : `git show ${identifier} -s --format="%H"`;
const stdout = String(exec(command));
return stdout;
const command = `git show ${identifier} -s --format="%${short ? 'h' : 'H'}"`;
return String(exec(command));

} catch (e) {
return null;
}
Expand Down
3 changes: 3 additions & 0 deletions utils/runQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const { graphql } = require('@octokit/graphql');
const buildQuery = require('./buildQuery');
const { getCommitChecks } = require('./getCommitChecks');

module.exports = async function runQuery({ commit, repo, pr, sha, token }) {
const [owner, name] = repo.split('/');
Expand All @@ -12,6 +13,8 @@ module.exports = async function runQuery({ commit, repo, pr, sha, token }) {
authorization: `token ${token}`,
},
});
const nodes = await getCommitChecks(response, { commit, repo, pr, sha, token });
response.search.edges[0].node.commits.nodes = nodes;
} catch (err) {
throw err.message;
}
Expand Down