async function getChangedFilesFromApi()

in paths-filter/src/main.ts [160:214]


async function getChangedFilesFromApi(
  token: string,
  prNumber: Webhooks.WebhookPayloadPullRequestPullRequest
): Promise<File[]> {
  core.startGroup(`Fetching list of changed files for PR#${prNumber.number} from Github API`)
  try {
    const client = new github.GitHub(token)
    const per_page = 100
    const files: File[] = []

    core.info(`Invoking listFiles(pull_number: ${prNumber.number}, per_page: ${per_page})`)
    for await (const response of client.paginate.iterator(
      client.pulls.listFiles.endpoint.merge({
        owner: github.context.repo.owner,
        repo: github.context.repo.repo,
        pull_number: prNumber.number,
        per_page
      })
    ) as AsyncIterableIterator<Octokit.Response<Octokit.PullsListFilesResponse>>) {
      if (response.status !== 200) {
        throw new Error(`Fetching list of changed files from GitHub API failed with error code ${response.status}`)
      }
      core.info(`Received ${response.data.length} items`)

      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
        if (row.status === ChangeStatus.Renamed) {
          files.push({
            filename: row.filename,
            status: ChangeStatus.Added
          })
          files.push({
            // 'previous_filename' for some unknown reason isn't in the type definition or documentation
            filename: (<any>row).previous_filename as string,
            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
          })
        }
      }
    }

    return files
  } finally {
    core.endGroup()
  }
}