Merge branch 'master'

This commit is contained in:
Michal Dorner 2024-02-15 09:13:51 +01:00
commit 5da0e4c086
No known key found for this signature in database
GPG key ID: 7325B8B59CA1B65C
10 changed files with 40910 additions and 47835 deletions

View file

@ -1,15 +1,14 @@
import * as fs from 'fs'
import * as core from '@actions/core'
import * as github from '@actions/github'
import type {Octokit} from '@octokit/rest'
import {Webhooks} from '@octokit/webhooks'
import {GetResponseDataTypeFromEndpointMethod} from '@octokit/types'
import {PushEvent, PullRequestEvent} from '@octokit/webhooks-types'
import {Filter, FilterResults} from './filter'
import {File, ChangeStatus} from './file'
import * as git from './git'
import {backslashEscape, shellEscape} from './list-format/shell-escape'
import {csvEscape} from './list-format/csv-escape'
import {getChanges} from './git'
type ExportFormat = 'none' | 'csv' | 'json' | 'shell' | 'escape'
@ -39,7 +38,7 @@ async function run(): Promise<void> {
const results = filter.match(files)
exportResults(results, listFiles)
} catch (error) {
core.setFailed(error.message)
core.setFailed(getErrorMessage(error))
}
}
@ -77,7 +76,7 @@ async function getChangedFiles(token: string, base: string, ref: string, initial
if (base) {
core.warning(`'base' input parameter is ignored when action is triggered by pull request event`)
}
const pr = github.context.payload.pull_request as Webhooks.WebhookPayloadPullRequestPullRequest
const pr = github.context.payload.pull_request as PullRequestEvent
if (token) {
return await getChangedFilesFromApi(token, pr)
}
@ -87,7 +86,7 @@ async function getChangedFiles(token: string, base: string, ref: string, initial
// At the same time we don't want to fetch any code from forked repository
throw new Error(`'token' input parameter is required if action is triggered by 'pull_request_target' event`)
}
core.info('Github token is not available - changes will be detected from PRs merge commit')
core.info('Github token is not available - changes will be detected using git diff')
const baseSha = github.context.payload.pull_request?.base.sha
const defaultBranch = github.context.payload.repository?.default_branch
const currentRef = await git.getCurrentRef()
@ -100,8 +99,7 @@ async function getChangedFiles(token: string, base: string, ref: string, initial
async function getChangedFilesFromGit(base: string, head: string, initialFetchDepth: number): Promise<File[]> {
const defaultBranch = github.context.payload.repository?.default_branch
const beforeSha =
github.context.eventName === 'push' ? (github.context.payload as Webhooks.WebhookPayloadPush).before : null
const beforeSha = github.context.eventName === 'push' ? (github.context.payload as PushEvent).before : null
const currentRef = await git.getCurrentRef()
@ -161,31 +159,28 @@ async function getChangedFilesFromGit(base: string, head: string, initialFetchDe
}
// Uses github REST api to get list of files changed in PR
async function getChangedFilesFromApi(
token: string,
prNumber: Webhooks.WebhookPayloadPullRequestPullRequest
): Promise<File[]> {
core.startGroup(`Fetching list of changed files for PR#${prNumber.number} from Github API`)
async function getChangedFilesFromApi(token: string, pullRequest: PullRequestEvent): Promise<File[]> {
core.startGroup(`Fetching list of changed files for PR#${pullRequest.number} from Github API`)
try {
const client = new github.GitHub(token)
const client = github.getOctokit(token)
const per_page = 100
const files: File[] = []
core.info(`Invoking listFiles(pull_number: ${prNumber.number}, per_page: ${per_page})`)
core.info(`Invoking listFiles(pull_number: ${pullRequest.number}, per_page: ${per_page})`)
for await (const response of client.paginate.iterator(
client.pulls.listFiles.endpoint.merge({
client.rest.pulls.listFiles.endpoint.merge({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: prNumber.number,
pull_number: pullRequest.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) {
for (const row of response.data as GetResponseDataTypeFromEndpointMethod<typeof client.rest.pulls.listFiles>) {
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.
@ -271,4 +266,9 @@ function isExportFormat(value: string): value is ExportFormat {
return ['none', 'csv', 'shell', 'json', 'escape'].includes(value)
}
function getErrorMessage(error: unknown): string {
if (error instanceof Error) return error.message
return String(error)
}
run()