2020-05-24 22:50:33 +02:00
|
|
|
import * as fs from 'fs'
|
2020-05-20 17:03:08 +02:00
|
|
|
import * as core from '@actions/core'
|
2020-05-21 00:31:16 +02:00
|
|
|
import * as github from '@actions/github'
|
|
|
|
import {Webhooks} from '@octokit/webhooks'
|
|
|
|
|
|
|
|
import Filter from './filter'
|
2020-07-11 17:17:56 +02:00
|
|
|
import {File, ChangeStatus} from './file'
|
2020-05-26 17:16:09 +02:00
|
|
|
import * as git from './git'
|
2020-05-20 17:03:08 +02:00
|
|
|
|
2020-07-11 23:33:11 +02:00
|
|
|
interface FilterResults {
|
|
|
|
[key: string]: boolean
|
|
|
|
}
|
|
|
|
interface ActionOutput {
|
|
|
|
[key: string]: string[]
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:03:08 +02:00
|
|
|
async function run(): Promise<void> {
|
|
|
|
try {
|
2020-07-02 22:56:14 +02:00
|
|
|
const workingDirectory = core.getInput('working-directory', {required: false})
|
|
|
|
if (workingDirectory) {
|
|
|
|
process.chdir(workingDirectory)
|
|
|
|
}
|
|
|
|
|
2020-05-26 17:16:09 +02:00
|
|
|
const token = core.getInput('token', {required: false})
|
2020-05-24 22:50:33 +02:00
|
|
|
const filtersInput = core.getInput('filters', {required: true})
|
|
|
|
const filtersYaml = isPathInput(filtersInput) ? getConfigFileContent(filtersInput) : filtersInput
|
|
|
|
|
|
|
|
const filter = new Filter(filtersYaml)
|
2020-06-15 21:49:10 +02:00
|
|
|
const files = await getChangedFiles(token)
|
2020-07-11 23:33:11 +02:00
|
|
|
let results: FilterResults
|
2020-05-26 17:16:09 +02:00
|
|
|
|
2020-06-24 21:53:31 +02:00
|
|
|
if (files === null) {
|
|
|
|
// Change detection was not possible
|
2020-07-11 23:33:11 +02:00
|
|
|
core.info('All filters will be set to true.')
|
|
|
|
results = {}
|
|
|
|
for (const key of Object.keys(filter.rules)) {
|
|
|
|
results[key] = true
|
2020-06-24 21:53:31 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-11 23:33:11 +02:00
|
|
|
results = filter.match(files)
|
2020-05-21 00:31:16 +02:00
|
|
|
}
|
2020-07-11 23:33:11 +02:00
|
|
|
|
|
|
|
exportFiles(files ?? [])
|
|
|
|
exportResults(results)
|
2020-05-20 17:03:08 +02:00
|
|
|
} catch (error) {
|
|
|
|
core.setFailed(error.message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-24 22:50:33 +02:00
|
|
|
function isPathInput(text: string): boolean {
|
|
|
|
return !text.includes('\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
function getConfigFileContent(configPath: string): string {
|
|
|
|
if (!fs.existsSync(configPath)) {
|
|
|
|
throw new Error(`Configuration file '${configPath}' not found`)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fs.lstatSync(configPath).isFile()) {
|
|
|
|
throw new Error(`'${configPath}' is not a file.`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fs.readFileSync(configPath, {encoding: 'utf8'})
|
|
|
|
}
|
|
|
|
|
2020-07-11 17:17:56 +02:00
|
|
|
async function getChangedFiles(token: string): Promise<File[] | null> {
|
2020-08-13 12:44:57 -07:00
|
|
|
if (github.context.eventName === 'pull_request' || github.context.eventName === 'pull_request_target') {
|
2020-06-15 21:49:10 +02:00
|
|
|
const pr = github.context.payload.pull_request as Webhooks.WebhookPayloadPullRequestPullRequest
|
|
|
|
return token ? await getChangedFilesFromApi(token, pr) : await getChangedFilesFromGit(pr.base.sha)
|
|
|
|
} else if (github.context.eventName === 'push') {
|
2020-06-24 21:53:31 +02:00
|
|
|
return getChangedFilesFromPush()
|
2020-06-15 21:49:10 +02:00
|
|
|
} else {
|
|
|
|
throw new Error('This action can be triggered only by pull_request or push event')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-11 17:17:56 +02:00
|
|
|
async function getChangedFilesFromPush(): Promise<File[] | null> {
|
2020-06-24 21:53:31 +02:00
|
|
|
const push = github.context.payload as Webhooks.WebhookPayloadPush
|
|
|
|
|
|
|
|
// No change detection for pushed tags
|
2020-07-11 23:33:11 +02:00
|
|
|
if (git.isTagRef(push.ref)) {
|
|
|
|
core.info('Workflow is triggered by pushing of tag. Change detection will not run.')
|
|
|
|
return null
|
|
|
|
}
|
2020-06-24 21:53:31 +02:00
|
|
|
|
|
|
|
// Get base from input or use repo default branch.
|
|
|
|
// It it starts with 'refs/', it will be trimmed (git fetch refs/heads/<NAME> doesn't work)
|
|
|
|
const baseInput = git.trimRefs(core.getInput('base', {required: false}) || push.repository.default_branch)
|
|
|
|
|
|
|
|
// If base references same branch it was pushed to, we will do comparison against the previously pushed commit.
|
|
|
|
// Otherwise changes are detected against the base reference
|
|
|
|
const base = git.trimRefsHeads(baseInput) === git.trimRefsHeads(push.ref) ? push.before : baseInput
|
|
|
|
|
|
|
|
// There is no previous commit for comparison
|
|
|
|
// e.g. change detection against previous commit of just pushed new branch
|
2020-07-11 23:33:11 +02:00
|
|
|
if (base === git.NULL_SHA) {
|
|
|
|
core.info('There is no previous commit for comparison. Change detection will not run.')
|
|
|
|
return null
|
|
|
|
}
|
2020-06-24 21:53:31 +02:00
|
|
|
|
|
|
|
return await getChangedFilesFromGit(base)
|
|
|
|
}
|
|
|
|
|
2020-05-26 17:16:09 +02:00
|
|
|
// Fetch base branch and use `git diff` to determine changed files
|
2020-07-11 17:17:56 +02:00
|
|
|
async function getChangedFilesFromGit(ref: string): Promise<File[]> {
|
2020-07-11 23:33:11 +02:00
|
|
|
return core.group(`Fetching base and using \`git diff-index\` to determine changed files`, async () => {
|
|
|
|
await git.fetchCommit(ref)
|
|
|
|
// FETCH_HEAD will always point to the just fetched commit
|
|
|
|
// No matter if ref is SHA, branch or tag name or full git ref
|
|
|
|
return await git.getChangedFiles(git.FETCH_HEAD)
|
|
|
|
})
|
2020-05-26 17:16:09 +02:00
|
|
|
}
|
|
|
|
|
2020-05-21 00:31:16 +02:00
|
|
|
// Uses github REST api to get list of files changed in PR
|
2020-05-26 17:16:09 +02:00
|
|
|
async function getChangedFilesFromApi(
|
|
|
|
token: string,
|
2020-05-21 00:31:16 +02:00
|
|
|
pullRequest: Webhooks.WebhookPayloadPullRequestPullRequest
|
2020-07-11 17:17:56 +02:00
|
|
|
): Promise<File[]> {
|
2020-07-11 23:33:11 +02:00
|
|
|
core.info(`Fetching list of changed files for PR#${pullRequest.number} from Github API`)
|
2020-05-26 17:16:09 +02:00
|
|
|
const client = new github.GitHub(token)
|
2020-05-21 00:31:16 +02:00
|
|
|
const pageSize = 100
|
2020-07-11 17:17:56 +02:00
|
|
|
const files: File[] = []
|
2020-05-21 00:31:16 +02:00
|
|
|
for (let page = 0; page * pageSize < pullRequest.changed_files; page++) {
|
|
|
|
const response = await client.pulls.listFiles({
|
|
|
|
owner: github.context.repo.owner,
|
|
|
|
repo: github.context.repo.repo,
|
|
|
|
pull_number: pullRequest.number,
|
|
|
|
page,
|
|
|
|
per_page: pageSize
|
|
|
|
})
|
|
|
|
for (const row of response.data) {
|
2020-07-11 17:17:56 +02:00
|
|
|
// 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 {
|
|
|
|
files.push({
|
|
|
|
filename: row.filename,
|
|
|
|
status: row.status as ChangeStatus
|
|
|
|
})
|
|
|
|
}
|
2020-05-21 00:31:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
2020-07-11 23:33:11 +02:00
|
|
|
function exportFiles(files: File[]): void {
|
|
|
|
const output: ActionOutput = {}
|
|
|
|
output[ChangeStatus.Added] = []
|
|
|
|
output[ChangeStatus.Deleted] = []
|
|
|
|
output[ChangeStatus.Modified] = []
|
|
|
|
|
|
|
|
for (const file of files) {
|
|
|
|
const arr = output[file.status] ?? []
|
|
|
|
arr.push(file.filename)
|
|
|
|
output[file.status] = arr
|
|
|
|
}
|
|
|
|
core.setOutput('files', output)
|
|
|
|
|
|
|
|
// Files grouped by status
|
|
|
|
for (const [status, paths] of Object.entries(output)) {
|
|
|
|
core.startGroup(`${status.toUpperCase()} files:`)
|
|
|
|
for (const filename of paths) {
|
|
|
|
core.info(filename)
|
|
|
|
}
|
|
|
|
core.endGroup()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function exportResults(results: FilterResults): void {
|
|
|
|
core.startGroup('Filters results:')
|
|
|
|
for (const [key, value] of Object.entries(results)) {
|
|
|
|
core.info(`${key}: ${value}`)
|
|
|
|
core.setOutput(key, value)
|
|
|
|
}
|
|
|
|
core.endGroup()
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:03:08 +02:00
|
|
|
run()
|