Skip to content

Commit d599443

Browse files
authored
Fix change detection using Github API (#51)
1 parent eb8fe2c commit d599443

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22

33
## v2.5.3
4+
- [Fixed mapping of removed/deleted change status from github API](https://github.com/dorny/paths-filter/pull/51)
45
- [Fixed retrieval of all changes via Github API when there are 100+ changes](https://github.com/dorny/paths-filter/pull/50)
56

67
## v2.5.2

dist/index.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4729,22 +4729,22 @@ async function getChangedFilesFromGit(base, initialFetchDepth) {
47294729
}
47304730
// Uses github REST api to get list of files changed in PR
47314731
async function getChangedFilesFromApi(token, pullRequest) {
4732-
var _a;
4733-
core.info(`Fetching list of changed files for PR#${pullRequest.number} from Github API`);
4732+
core.startGroup(`Fetching list of changed files for PR#${pullRequest.number} from Github API`);
4733+
core.info(`Number of changed_files is ${pullRequest.changed_files}`);
47344734
const client = new github.GitHub(token);
47354735
const pageSize = 100;
47364736
const files = [];
4737-
let response;
4738-
let page = 0;
4739-
do {
4740-
response = await client.pulls.listFiles({
4737+
for (let page = 1; (page - 1) * pageSize < pullRequest.changed_files; page++) {
4738+
core.info(`Invoking listFiles(pull_number: ${pullRequest.number}, page: ${page}, per_page: ${pageSize})`);
4739+
const response = await client.pulls.listFiles({
47414740
owner: github.context.repo.owner,
47424741
repo: github.context.repo.repo,
47434742
pull_number: pullRequest.number,
47444743
page,
47454744
per_page: pageSize
47464745
});
47474746
for (const row of response.data) {
4747+
core.info(`[${row.status}] ${row.filename}`);
47484748
// There's no obvious use-case for detection of renames
47494749
// Therefore we treat it as if rename detection in git diff was turned off.
47504750
// Rename is replaced by delete of original filename and add of new filename
@@ -4760,14 +4760,16 @@ async function getChangedFilesFromApi(token, pullRequest) {
47604760
});
47614761
}
47624762
else {
4763+
// Github status and git status variants are same except for deleted files
4764+
const status = row.status === 'removed' ? file_1.ChangeStatus.Deleted : row.status;
47634765
files.push({
47644766
filename: row.filename,
4765-
status: row.status
4767+
status
47664768
});
47674769
}
47684770
}
4769-
page++;
4770-
} while (((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.length) > 0);
4771+
}
4772+
core.endGroup();
47714773
return files;
47724774
}
47734775
function exportResults(results, format) {

src/main.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as fs from 'fs'
22
import * as core from '@actions/core'
33
import * as github from '@actions/github'
44
import {Webhooks} from '@octokit/webhooks'
5-
import type {Octokit} from '@octokit/rest'
65

76
import {Filter, FilterResults} from './filter'
87
import {File, ChangeStatus} from './file'
@@ -124,21 +123,22 @@ async function getChangedFilesFromApi(
124123
token: string,
125124
pullRequest: Webhooks.WebhookPayloadPullRequestPullRequest
126125
): Promise<File[]> {
127-
core.info(`Fetching list of changed files for PR#${pullRequest.number} from Github API`)
126+
core.startGroup(`Fetching list of changed files for PR#${pullRequest.number} from Github API`)
127+
core.info(`Number of changed_files is ${pullRequest.changed_files}`)
128128
const client = new github.GitHub(token)
129129
const pageSize = 100
130130
const files: File[] = []
131-
let response: Octokit.Response<Octokit.PullsListFilesResponse>
132-
let page = 0
133-
do {
134-
response = await client.pulls.listFiles({
131+
for (let page = 1; (page - 1) * pageSize < pullRequest.changed_files; page++) {
132+
core.info(`Invoking listFiles(pull_number: ${pullRequest.number}, page: ${page}, per_page: ${pageSize})`)
133+
const response = await client.pulls.listFiles({
135134
owner: github.context.repo.owner,
136135
repo: github.context.repo.repo,
137136
pull_number: pullRequest.number,
138137
page,
139138
per_page: pageSize
140139
})
141140
for (const row of response.data) {
141+
core.info(`[${row.status}] ${row.filename}`)
142142
// There's no obvious use-case for detection of renames
143143
// Therefore we treat it as if rename detection in git diff was turned off.
144144
// Rename is replaced by delete of original filename and add of new filename
@@ -153,15 +153,17 @@ async function getChangedFilesFromApi(
153153
status: ChangeStatus.Deleted
154154
})
155155
} else {
156+
// Github status and git status variants are same except for deleted files
157+
const status = row.status === 'removed' ? ChangeStatus.Deleted : (row.status as ChangeStatus)
156158
files.push({
157159
filename: row.filename,
158-
status: row.status as ChangeStatus
160+
status
159161
})
160162
}
161163
}
162-
page++
163-
} while (response?.data?.length > 0)
164+
}
164165

166+
core.endGroup()
165167
return files
166168
}
167169

0 commit comments

Comments
 (0)