Filters input accepts path to external yaml file (#8)

* Filters input accepts path to external yaml file

* Fix formatting and eslint issues

* Fix file ext of filters yaml file

* Update documentation in README file
This commit is contained in:
Michal Dorner 2020-05-24 22:50:33 +02:00 committed by GitHub
parent 0612377665
commit a2e5f9f7bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 48 additions and 8 deletions

View file

@ -1,3 +1,4 @@
import * as fs from 'fs'
import * as core from '@actions/core'
import * as github from '@actions/github'
import {Webhooks} from '@octokit/webhooks'
@ -7,7 +8,10 @@ import Filter from './filter'
async function run(): Promise<void> {
try {
const token = core.getInput('githubToken', {required: true})
const filterYaml = core.getInput('filters', {required: true})
const filtersInput = core.getInput('filters', {required: true})
const filtersYaml = isPathInput(filtersInput) ? getConfigFileContent(filtersInput) : filtersInput
const client = new github.GitHub(token)
if (github.context.eventName !== 'pull_request') {
@ -16,9 +20,8 @@ async function run(): Promise<void> {
}
const pr = github.context.payload.pull_request as Webhooks.WebhookPayloadPullRequestPullRequest
const filter = new Filter(filterYaml)
const filter = new Filter(filtersYaml)
const files = await getChangedFiles(client, pr)
const result = filter.match(files)
for (const key in result) {
core.setOutput(key, String(result[key]))
@ -28,6 +31,22 @@ async function run(): Promise<void> {
}
}
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'})
}
// Uses github REST api to get list of files changed in PR
async function getChangedFiles(
client: github.GitHub,