Skip to content
This repository was archived by the owner on Sep 1, 2022. It is now read-only.

Commit d8de9da

Browse files
committed
Implement additional workflows for obtaining diff
1 parent c040433 commit d8de9da

File tree

1 file changed

+94
-28
lines changed
  • codeql-learninglab-check/package/src

1 file changed

+94
-28
lines changed

codeql-learninglab-check/package/src/index.ts

Lines changed: 94 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -118,40 +118,106 @@ function isConfig(config: any): config is Config {
118118
* There are a few different ways in which we may determine which queries
119119
* are currently interesting to the user, in decreasing usefulness:
120120
*
121-
* * If the user just pushed to a branch that currently has an open pull
122-
* request, the interesting queries are those that are changed in the pull
123-
* request (and not just those changed in the most recent push).
124-
* * If there's no active pull request, then what's probably most interesting
125-
* are the queries that have changed in the last push (i.e. between the
126-
* previous head and the new head)
127-
* * If that's not possible (e.g. the push could have created the branch for
128-
* the first time, and so there is no "previous ref"), then comparing this
129-
* branch to the default branch of a repo will probably give the most
130-
* accurate results.
131-
* * Finally, if all else fails (e.g. the push was the initial push to the
132-
* default branch of the repo), then we should just run every query we
133-
* recognize (as if RUN_ALL was true). We do this by setting
134-
* unableToGetChangedQueries to true.
121+
* 1. If the user just pushed to a branch that currently has an open pull
122+
* request, the interesting queries are those that are changed in the pull
123+
* request (and not just those changed in the most recent push).
124+
* 2. If there's no active pull request, then what's probably most
125+
* interesting are the queries that have changed in the last push (i.e.
126+
* between the previous head and the new head)
127+
* 3. If that's not possible (e.g. the push could have created the branch for
128+
* the first time, and so there is no "previous ref"), then comparing this
129+
* branch to the default branch of a repo will probably give the most
130+
* accurate results.
131+
* 4. Finally, if all else fails (e.g. the push was the initial push to the
132+
* default branch of the repo), then we should just run every query we
133+
* recognize (as if RUN_ALL was true). We do this by setting
134+
* unableToGetChangedQueries to true.
135135
*/
136136

137-
const {stdout: filesChangedRaw} = await execFile('git', ['diff', '--name-only', `${event.before}..${event.after}`]);
138-
filesChangedRaw.split('\n')
137+
/**
138+
* The output from a successful call to `git diff --name-only`
139+
*/
140+
let filesChangedRaw: string | null = null;
141+
142+
// Try (1) - find any PR associated with the branch of this push
143+
144+
// Get branch name
145+
// This is expected to fail if e.g. the push was to a tag not a branch
146+
const branch = /^refs\/heads\/(.*)$/.exec(event.ref)?.[1];
147+
148+
if (branch) {
149+
try {
150+
const pulls = await api.pulls.list({
151+
owner: event.repository.owner.login,
152+
repo: event.repository.name,
153+
head: `${event.repository.owner.login}:${branch}`
154+
});
155+
if (pulls && pulls.data.length > 0) {
156+
// Just use first PR
157+
const pr = pulls.data[0];
158+
const baseBranch = pr.base.ref;
159+
// Ensure we have the commits from that ref
160+
await execFile('git', ['fetch', 'origin', baseBranch]);
161+
filesChangedRaw = (await execFile('git', ['diff', '--name-only', `origin/${baseBranch}..${event.after}`])).stdout;
162+
} else {
163+
console.log('No pull requests associated with the current push');
164+
}
165+
} catch (err) {
166+
console.warn(err);
167+
console.log(`Failed to use PRs to calculate changed files branch ${branch}.`);
168+
}
169+
} else {
170+
console.log(
171+
'Push was not for a branch, calculating changed files differently'
172+
);
173+
}
174+
175+
// Try (2) - see what files have changed in the last push
176+
177+
if (filesChangedRaw === null) {
178+
const result = await execFile('git', ['diff', '--name-only', `${event.before}..${event.after}`])
179+
.catch(err => {
180+
console.warn(err);
181+
console.log('Failed to get diff for push');
182+
});
183+
if (result)
184+
filesChangedRaw = (await execFile('git', ['diff', '--name-only', `${event.before}..${event.after}`])).stdout;
185+
}
186+
187+
// Try (3) - see how the current HEAD differs from the default branch
188+
189+
if (filesChangedRaw === null) {
190+
const result = await execFile('git', ['diff', '--name-only', `refs/remotes/origin/HEAD..${event.after}`])
191+
.catch(err => {
192+
console.warn(err);
193+
console.log('Failed to diff against default branch');
194+
});
195+
if (result)
196+
filesChangedRaw = (await execFile('git', ['diff', '--name-only', `${event.before}..${event.after}`])).stdout;
197+
}
198+
199+
if (filesChangedRaw === null) {
200+
unableToGetChangedQueries = true;
201+
} else {
202+
// We have successfully obtained the diff for this push
203+
filesChangedRaw.split('\n')
139204
.map(s => s.trim())
140205
.filter(s => s.endsWith('.ql'))
141206
.forEach(s => queriesChanged.add(s));
142-
console.log(`${pluralize(queriesChanged.size, 'query')} updated in this push`);
143-
comment += `${pluralize(queriesChanged.size, 'query')} changed `
144-
comment += `[between \`${event.before.substr(0, 7)}\` and \`${event.after.substr(0, 7)}\`]`
145-
comment += `(${event.repository.html_url}/compare/${event.before}...${event.after}) after push to \`${event.ref}\``;
146-
if (queriesChanged.size > 0) {
147-
comment += ':\n';
148-
for (const query of queriesChanged) {
149-
console.log(`- ${query}`);
150-
const exists = await access(query, fs.constants.R_OK).then(() => true, () => false);
151-
comment += `* \`${query}\`${exists ? '' : ' *(deleted)*'}\n`;
207+
console.log(`${pluralize(queriesChanged.size, 'query')} updated in this push`);
208+
comment += `${pluralize(queriesChanged.size, 'query')} changed `
209+
comment += `[between \`${event.before.substr(0, 7)}\` and \`${event.after.substr(0, 7)}\`]`
210+
comment += `(${event.repository.html_url}/compare/${event.before}...${event.after}) after push to \`${event.ref}\``;
211+
if (queriesChanged.size > 0) {
212+
comment += ':\n';
213+
for (const query of queriesChanged) {
214+
console.log(`- ${query}`);
215+
const exists = await access(query, fs.constants.R_OK).then(() => true, () => false);
216+
comment += `* \`${query}\`${exists ? '' : ' *(deleted)*'}\n`;
217+
}
218+
} else {
219+
comment += '\n';
152220
}
153-
} else {
154-
comment += '\n';
155221
}
156222
}
157223

0 commit comments

Comments
 (0)