From d599443ba55e103985817f072734ef381431aeb4 Mon Sep 17 00:00:00 2001 From: Michal Dorner Date: Fri, 13 Nov 2020 20:15:41 +0100 Subject: [PATCH] Fix change detection using Github API (#51) --- CHANGELOG.md | 1 + dist/index.js | 20 +++++++++++--------- src/main.ts | 20 +++++++++++--------- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1cb3d9..8f6e799 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # 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 diff --git a/dist/index.js b/dist/index.js index 842497e..76ddc47 100644 --- a/dist/index.js +++ b/dist/index.js @@ -4729,15 +4729,14 @@ async function getChangedFilesFromGit(base, initialFetchDepth) { } // Uses github REST api to get list of files changed in PR async function getChangedFilesFromApi(token, pullRequest) { - var _a; - 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 = []; - let response; - let page = 0; - do { - response = await client.pulls.listFiles({ + 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, pull_number: pullRequest.number, @@ -4745,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 @@ -4760,14 +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 }); } } - page++; - } while (((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.length) > 0); + } + core.endGroup(); return files; } function exportResults(results, format) { diff --git a/src/main.ts b/src/main.ts index e295ac0..56718f2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,7 +2,6 @@ import * as fs from 'fs' import * as core from '@actions/core' import * as github from '@actions/github' import {Webhooks} from '@octokit/webhooks' -import type {Octokit} from '@octokit/rest' import {Filter, FilterResults} from './filter' import {File, ChangeStatus} from './file' @@ -124,14 +123,14 @@ async function getChangedFilesFromApi( token: string, pullRequest: Webhooks.WebhookPayloadPullRequestPullRequest ): Promise { - 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[] = [] - let response: Octokit.Response - let page = 0 - do { - response = await client.pulls.listFiles({ + 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, pull_number: pullRequest.number, @@ -139,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 @@ -153,15 +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 }) } } - page++ - } while (response?.data?.length > 0) + } + core.endGroup() return files }