5 Commits

Author SHA1 Message Date
Michal Dorner
d599443ba5 Fix change detection using Github API (#51) 2020-11-13 20:15:41 +01:00
Michal Dorner
eb8fe2c24b Update dist/index.js and docs after PR #50 2020-11-13 18:55:57 +01:00
Michal Dorner
dec8b8030e Merge pull request #50 from simllll/patch-1
fix: retrieve all changes via api
2020-11-13 18:48:29 +01:00
Simon Tretter
785a14adbe fix: add missing Octokit import 2020-11-13 17:30:27 +01:00
Simon Tretter
e84bc6af29 fix: retrieve all changes via api
the number returned by pullRequest.changed_files doesn't reflect the correct number of real changed files. Therefore lot of filters didn't work in my scenario, as it said nothing has changed. This especially happens if there are more than 100 files changed (first time I experienced it where over 1000 files have changed, and now when there were about 300 files that have changed). 

I changed the detection by querying the api as long as it returns new results. This ensures all pages have been retrieved. It's tested and used in production on our end, but please check again if I didn't miss anything.

Thanks a lot for this awesome github action :)
2020-11-13 17:28:22 +01:00
4 changed files with 23 additions and 6 deletions

View File

@@ -1,5 +1,9 @@
# Changelog
## v2.5.3
- [Fixed mapping of removed/deleted change status from github API](https://github.com/dorny/paths-filter/pull/51)
- [Fixed retrieval of all changes via Github API when there are 100+ changes](https://github.com/dorny/paths-filter/pull/50)
## v2.5.2
- [Add support for multiple patterns when using file status](https://github.com/dorny/paths-filter/pull/48)
- [Use picomatch directly instead of micromatch wrapper](https://github.com/dorny/paths-filter/pull/49)

View File

@@ -62,6 +62,7 @@ For more scenarios see [examples](#examples) section.
# What's New
- Fixed retrieval of all changes via Github API when there are 100+ changes
- Paths expressions are now evaluated using [picomatch](https://github.com/micromatch/picomatch) library
- Support workflows triggered by any event
- Fixed compatibility with older (<2.23) versions of git

12
dist/index.js vendored
View File

@@ -4729,11 +4729,13 @@ async function getChangedFilesFromGit(base, initialFetchDepth) {
}
// Uses github REST api to get list of files changed in PR
async function getChangedFilesFromApi(token, pullRequest) {
core.info(`Fetching list of changed files for PR#${pullRequest.number} from Github API`);
core.startGroup(`Fetching list of changed files for PR#${pullRequest.number} from Github API`);
core.info(`Number of changed_files is ${pullRequest.changed_files}`);
const client = new github.GitHub(token);
const pageSize = 100;
const files = [];
for (let page = 0; page * pageSize < pullRequest.changed_files; page++) {
for (let page = 1; (page - 1) * pageSize < pullRequest.changed_files; page++) {
core.info(`Invoking listFiles(pull_number: ${pullRequest.number}, page: ${page}, per_page: ${pageSize})`);
const response = await client.pulls.listFiles({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
@@ -4742,6 +4744,7 @@ async function getChangedFilesFromApi(token, pullRequest) {
per_page: pageSize
});
for (const row of response.data) {
core.info(`[${row.status}] ${row.filename}`);
// There's no obvious use-case for detection of renames
// Therefore we treat it as if rename detection in git diff was turned off.
// Rename is replaced by delete of original filename and add of new filename
@@ -4757,13 +4760,16 @@ async function getChangedFilesFromApi(token, pullRequest) {
});
}
else {
// Github status and git status variants are same except for deleted files
const status = row.status === 'removed' ? file_1.ChangeStatus.Deleted : row.status;
files.push({
filename: row.filename,
status: row.status
status
});
}
}
}
core.endGroup();
return files;
}
function exportResults(results, format) {

View File

@@ -123,11 +123,13 @@ async function getChangedFilesFromApi(
token: string,
pullRequest: Webhooks.WebhookPayloadPullRequestPullRequest
): Promise<File[]> {
core.info(`Fetching list of changed files for PR#${pullRequest.number} from Github API`)
core.startGroup(`Fetching list of changed files for PR#${pullRequest.number} from Github API`)
core.info(`Number of changed_files is ${pullRequest.changed_files}`)
const client = new github.GitHub(token)
const pageSize = 100
const files: File[] = []
for (let page = 0; page * pageSize < pullRequest.changed_files; page++) {
for (let page = 1; (page - 1) * pageSize < pullRequest.changed_files; page++) {
core.info(`Invoking listFiles(pull_number: ${pullRequest.number}, page: ${page}, per_page: ${pageSize})`)
const response = await client.pulls.listFiles({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
@@ -136,6 +138,7 @@ async function getChangedFilesFromApi(
per_page: pageSize
})
for (const row of response.data) {
core.info(`[${row.status}] ${row.filename}`)
// There's no obvious use-case for detection of renames
// Therefore we treat it as if rename detection in git diff was turned off.
// Rename is replaced by delete of original filename and add of new filename
@@ -150,14 +153,17 @@ async function getChangedFilesFromApi(
status: ChangeStatus.Deleted
})
} else {
// Github status and git status variants are same except for deleted files
const status = row.status === 'removed' ? ChangeStatus.Deleted : (row.status as ChangeStatus)
files.push({
filename: row.filename,
status: row.status as ChangeStatus
status
})
}
}
}
core.endGroup()
return files
}