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

Commit be65aee

Browse files
committed
Update the comments to print accurate shas for diff calculation
1 parent d8de9da commit be65aee

File tree

1 file changed

+45
-22
lines changed
  • codeql-learninglab-check/package/src

1 file changed

+45
-22
lines changed

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

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,10 @@ function isConfig(config: any): config is Config {
137137
/**
138138
* The output from a successful call to `git diff --name-only`
139139
*/
140-
let filesChangedRaw: string | null = null;
140+
let diff: {
141+
baseSha: string;
142+
filesChangedRaw: string;
143+
} | null = null;
141144

142145
// Try (1) - find any PR associated with the branch of this push
143146

@@ -158,7 +161,12 @@ function isConfig(config: any): config is Config {
158161
const baseBranch = pr.base.ref;
159162
// Ensure we have the commits from that ref
160163
await execFile('git', ['fetch', 'origin', baseBranch]);
161-
filesChangedRaw = (await execFile('git', ['diff', '--name-only', `origin/${baseBranch}..${event.after}`])).stdout;
164+
diff = {
165+
baseSha: baseBranch,
166+
filesChangedRaw: (await execFile(
167+
'git', ['diff', '--name-only', `origin/${baseBranch}..${event.after}`]
168+
)).stdout
169+
}
162170
} else {
163171
console.log('No pull requests associated with the current push');
164172
}
@@ -174,40 +182,55 @@ function isConfig(config: any): config is Config {
174182

175183
// Try (2) - see what files have changed in the last push
176184

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+
if (!diff) {
186+
try {
187+
const result = await execFile(
188+
'git', ['diff', '--name-only', `${event.before}..${event.after}`]
189+
);
190+
if (result)
191+
diff = {
192+
baseSha: event.before,
193+
filesChangedRaw: result.stdout
194+
};
195+
} catch (err) {
196+
console.warn(err);
197+
console.log('Failed to get diff for push');
198+
}
185199
}
186200

187201
// Try (3) - see how the current HEAD differs from the default branch
188202

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;
203+
if (!diff) {
204+
try {
205+
const defaultBranchSha = await (await execFile(
206+
'git', ['rev-parse', 'refs/remotes/origin/HEAD']
207+
)).stdout.trim();
208+
const result = await execFile(
209+
'git', ['diff', '--name-only', `${defaultBranchSha}..${event.after}`]
210+
);
211+
if (result)
212+
diff = {
213+
baseSha: defaultBranchSha,
214+
filesChangedRaw: result.stdout
215+
}
216+
} catch (err) {
217+
console.warn(err);
218+
console.log('Failed to diff against default branch');
219+
}
197220
}
198221

199-
if (filesChangedRaw === null) {
222+
if (!diff) {
200223
unableToGetChangedQueries = true;
201224
} else {
202225
// We have successfully obtained the diff for this push
203-
filesChangedRaw.split('\n')
226+
diff.filesChangedRaw.split('\n')
204227
.map(s => s.trim())
205228
.filter(s => s.endsWith('.ql'))
206229
.forEach(s => queriesChanged.add(s));
207230
console.log(`${pluralize(queriesChanged.size, 'query')} updated in this push`);
208231
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}\``;
232+
comment += `[between \`${diff.baseSha.substr(0, 7)}\` and \`${event.after.substr(0, 7)}\`]`
233+
comment += `(${event.repository.html_url}/compare/${diff.baseSha}...${event.after}) after push to \`${event.ref}\``;
211234
if (queriesChanged.size > 0) {
212235
comment += ':\n';
213236
for (const query of queriesChanged) {

0 commit comments

Comments
 (0)