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-20 17:03:08 +02:00
|
|
|
|
|
|
|
async function run(): Promise<void> {
|
|
|
|
try {
|
2020-05-21 00:38:22 +02:00
|
|
|
const token = core.getInput('githubToken', {required: true})
|
2020-05-21 01:37:33 +02:00
|
|
|
const filterYaml = core.getInput('filters', {required: true})
|
2020-05-21 00:31:16 +02:00
|
|
|
const client = new github.GitHub(token)
|
|
|
|
|
|
|
|
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
|
|
|
|
const filter = new Filter(filterYaml)
|
|
|
|
const files = await getChangedFiles(client, pr)
|
2020-05-20 17:03:08 +02:00
|
|
|
|
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-21 00:31:16 +02:00
|
|
|
// Uses github REST api to get list of files changed in PR
|
|
|
|
async function getChangedFiles(
|
|
|
|
client: github.GitHub,
|
|
|
|
pullRequest: Webhooks.WebhookPayloadPullRequestPullRequest
|
|
|
|
): Promise<string[]> {
|
|
|
|
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()
|