Skip to content

Commit 22ebdb5

Browse files
committed
Log warning when MAX_PAGE is reached
This always logs a warning if we fetched page MAX_PAGE regardless whether there would be more pages to fetch or not. No effort is done to determine whether the list of modified files is complete. Also the modified files are now collected in a Set to avoid duplicate entries.
1 parent 25c2479 commit 22ebdb5

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/util.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ const os = require('os');
77
const fs = require('fs').promises;
88
const path = require('path');
99

10+
// Load at most MAX_PAGE pages when determining modified files.
11+
// This is used both for pull/{pull_number}/files as well as for
12+
// repos/compareCommits API calls.
13+
const MAX_PAGE = 10;
14+
1015
const downloadPmd = async function(version, token) {
1116
let pmdVersion = version;
1217
let cachedPmdPath = tc.find('pmd', version);
@@ -104,10 +109,11 @@ const determineModifiedFiles = async function(token, sourcePath) {
104109
if (context.eventName === 'pull_request') {
105110
core.debug(`Pull request ${eventData.number}: ${eventData.pull_request.html_url}`);
106111

107-
let modifiedFilenames = [];
112+
let modifiedFilenames = new Set();
108113

109114
// maximum of 300 files are loaded (page size is 30, max 10 pages)
110-
for(let page = 1; page < 10; page++) {
115+
let page;
116+
for(page = 1; page <= MAX_PAGE; page++) {
111117
const listFilesResponse = await octokit.rest.pulls.listFiles({
112118
...context.repo,
113119
pull_number: eventData.number,
@@ -119,17 +125,21 @@ const determineModifiedFiles = async function(token, sourcePath) {
119125
break;
120126
}
121127
const filenames = extractFilenames(allFiles, page, sourcePath);
122-
modifiedFilenames.push(...filenames);
128+
filenames.forEach(f => modifiedFilenames.add(f));
129+
}
130+
if (page >= MAX_PAGE) {
131+
core.warning(`The pull request ${eventData.number} is too big - not all changed files will be analyzed!`);
123132
}
124133

125-
return modifiedFilenames;
134+
return [...modifiedFilenames];
126135
} else if (context.eventName === 'push') {
127136
core.debug(`Push on ${eventData.ref}: ${eventData.before}...${eventData.after}`);
128137

129-
let modifiedFilenames = [];
138+
let modifiedFilenames = new Set();
130139

131140
// maximum of 300 files are loaded (page size is 30, max 10 pages)
132-
for(let page = 1; page < 10; page++) {
141+
let page;
142+
for(page = 1; page <= MAX_PAGE; page++) {
133143
const compareResponse = await octokit.rest.repos.compareCommitsWithBasehead({
134144
...context.repo,
135145
basehead: `${eventData.before}...${eventData.after}`,
@@ -141,10 +151,13 @@ const determineModifiedFiles = async function(token, sourcePath) {
141151
break;
142152
}
143153
const filenames = extractFilenames(allFiles, page, sourcePath);
144-
modifiedFilenames.push(...filenames);
154+
filenames.forEach(f => modifiedFilenames.add(f));
155+
}
156+
if (page >= MAX_PAGE) {
157+
core.warning(`The push on ${eventData.ref} is too big - not all changed files will be analyzed!`);
145158
}
146159

147-
return modifiedFilenames;
160+
return [...modifiedFilenames];
148161
} else {
149162
core.warning(`Unsupported github action event '${context.eventName}' - cannot determine modified files. All files will be analyzed.`);
150163
return undefined;

tests/util.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,24 @@ describe('pmd-github-action-util', function () {
218218
.map(f => path.normalize(f)));
219219
})
220220

221+
test('can determine modified files from pull request with max page', async () => {
222+
// disable ACTIONS_STEP_DEBUG
223+
delete process.env['RUNNER_DEBUG'];
224+
process.env['GITHUB_REPOSITORY'] = 'pmd/pmd-github-action-tests'
225+
process.env['GITHUB_EVENT_NAME'] = 'pull_request';
226+
process.env['GITHUB_EVENT_PATH'] = __dirname + '/data/pull-request-event-data.json';
227+
for (let page = 1; page <= 10; page++) {
228+
nock('https://api.github.com')
229+
.get(`/repos/pmd/pmd-github-action-tests/pulls/1/files?per_page=30&page=${page}`)
230+
.replyWithFile(200, __dirname + '/data/pull-request-files.json', {
231+
'Content-Type': 'application/json',
232+
});
233+
}
234+
let fileList = await util.determineModifiedFiles('my_test_token', path.normalize('src/main/java'));
235+
expect(fileList).toStrictEqual(['src/main/java/AvoidCatchingThrowableSample.java', 'src/main/java/NewFile.java', 'src/main/java/ChangedFile.java']
236+
.map(f => path.normalize(f)));
237+
})
238+
221239
test('return undefined for unsupported event', async () => {
222240
process.env['GITHUB_REPOSITORY'] = 'pmd/pmd-github-action-tests'
223241
process.env['GITHUB_EVENT_NAME'] = 'workflow_dispatch';
@@ -289,6 +307,24 @@ describe('pmd-github-action-util', function () {
289307
expect(fileList).toStrictEqual(['src/main/java/AvoidCatchingThrowableSample.java', 'src/main/java/NewFile.java', 'src/main/java/ChangedFile.java']
290308
.map(f => path.normalize(f)));
291309
})
310+
311+
test('can determine modified files from push event with max page', async () => {
312+
// disable ACTIONS_STEP_DEBUG
313+
delete process.env['RUNNER_DEBUG'];
314+
process.env['GITHUB_REPOSITORY'] = 'pmd/pmd-github-action-tests'
315+
process.env['GITHUB_EVENT_NAME'] = 'push';
316+
process.env['GITHUB_EVENT_PATH'] = __dirname + '/data/push-event-data.json';
317+
for (let page = 1; page <= 10; page++) {
318+
nock('https://api.github.com')
319+
.get(`/repos/pmd/pmd-github-action-tests/compare/44c1557134c7dbaf46ecdf796fb871c8df8989e4...8a7a25638d8ca5207cc824dea9571325b243c6a1?per_page=30&page=${page}`)
320+
.replyWithFile(200, __dirname + '/data/compare-files-page1.json', {
321+
'Content-Type': 'application/json',
322+
});
323+
}
324+
let fileList = await util.determineModifiedFiles('my_test_token', path.normalize('src/main/java'));
325+
expect(fileList).toStrictEqual(['src/main/java/AvoidCatchingThrowableSample.java', 'src/main/java/NewFile.java', 'src/main/java/ChangedFile.java']
326+
.map(f => path.normalize(f)));
327+
})
292328
});
293329

294330
function setGlobal(key, value) {

0 commit comments

Comments
 (0)