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-05-26 17:16:09 +02:00
|
|
|
import * as git from './git'
|
2020-05-20 17:03:08 +02:00
|
|
|
|
|
|
|
async function run(): Promise<void> {
|
|
|
|
try {
|
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
|
|
|
|
|
2020-05-21 00:31:16 +02:00
|
|
|
if (github.context.eventName !== 'pull_request') {
|
|
|
|
core.setFailed('This action can be triggered only by pull_request event')
|
|
|
|
return
|
|
|
|
}
|
2020-05-20 17:03:08 +02:00
|
|
|
|
2020-05-21 00:31:16 +02:00
|
|
|
const pr = github.context.payload.pull_request as Webhooks.WebhookPayloadPullRequestPullRequest
|
2020-05-24 22:50:33 +02:00
|
|
|
const filter = new Filter(filtersYaml)
|
2020-05-26 17:16:09 +02:00
|
|
|
const files = token ? await getChangedFilesFromApi(token, pr) : await getChangedFilesFromGit(pr)
|
|
|
|
|
2020-05-21 00:31:16 +02:00
|
|
|
const result = filter.match(files)
|
|
|
|
for (const key in result) {
|
|
|
|
core.setOutput(key, String(result[key]))
|
|
|
|
}
|
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-05-26 17:16:09 +02:00
|
|
|
// Fetch base branch and use `git diff` to determine changed files
|
|
|
|
async function getChangedFilesFromGit(pullRequest: Webhooks.WebhookPayloadPullRequestPullRequest): Promise<string[]> {
|
|
|
|
core.debug('Fetching base branch and using `git diff-index` to determine changed files')
|
|
|
|
const baseRef = pullRequest.base.ref
|
|
|
|
await git.fetchBranch(baseRef)
|
|
|
|
return await git.getChangedFiles(pullRequest.base.sha)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
): Promise<string[]> {
|
2020-05-26 17:16:09 +02:00
|
|
|
core.debug('Fetching list of modified files from Github API')
|
|
|
|
const client = new github.GitHub(token)
|
2020-05-21 00:31:16 +02:00
|
|
|
const pageSize = 100
|
|
|
|
const files: string[] = []
|
|
|
|
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) {
|
|
|
|
files.push(row.filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:03:08 +02:00
|
|
|
run()
|