Implement fetching, filtering and tests

This commit is contained in:
Michal Dorner 2020-05-21 00:31:16 +02:00
parent d475d5da26
commit 4e726dd036
10 changed files with 31262 additions and 306 deletions

38
src/filter.ts Normal file
View file

@ -0,0 +1,38 @@
import * as jsyaml from 'js-yaml'
import * as minimatch from 'minimatch'
export default class Filter {
rules: {[key: string]: minimatch.IMinimatch[]} = {}
constructor(yaml: string) {
const doc = jsyaml.safeLoad(yaml)
if (typeof doc !== 'object') {
this.throwInvalidFormatError()
}
for (const name of Object.keys(doc)) {
const patterns = doc[name] as string[]
if (!Array.isArray(patterns)) {
this.throwInvalidFormatError()
}
if (!patterns.every(x => typeof x === 'string')) {
this.throwInvalidFormatError()
}
this.rules[name] = patterns.map(x => new minimatch.Minimatch(x))
}
}
// Returns dictionary with match result per rules group
match(paths: string[]): {[key: string]: boolean} {
const result: {[key: string]: boolean} = {}
for (const [key, patterns] of Object.entries(this.rules)) {
const match = paths.some(fileName => patterns.some(rule => rule.match(fileName)))
result[key] = match
}
return result
}
private throwInvalidFormatError(): never {
throw new Error('Invalid filter YAML format: Expected dictionary of string arrays')
}
}

View file

@ -1,19 +1,54 @@
import * as core from '@actions/core'
import {wait} from './wait'
import * as github from '@actions/github'
import {Webhooks} from '@octokit/webhooks'
import Filter from './filter'
async function run(): Promise<void> {
try {
const ms: string = core.getInput('milliseconds')
core.debug(`Waiting ${ms} milliseconds ...`)
const token = core.getInput('github-token', {required: true})
const filterYaml = core.getInput('filter', {required: true})
const client = new github.GitHub(token)
core.debug(new Date().toTimeString())
await wait(parseInt(ms, 10))
core.debug(new Date().toTimeString())
if (github.context.eventName !== 'pull_request') {
core.setFailed('This action can be triggered only by pull_request event')
return
}
core.setOutput('time', new Date().toTimeString())
const pr = github.context.payload.pull_request as Webhooks.WebhookPayloadPullRequestPullRequest
const filter = new Filter(filterYaml)
const files = await getChangedFiles(client, pr)
const result = filter.match(files)
for (const key in result) {
core.setOutput(key, String(result[key]))
}
} catch (error) {
core.setFailed(error.message)
}
}
// 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
}
run()

View file

@ -1,9 +0,0 @@
export async function wait(milliseconds: number): Promise<string> {
return new Promise(resolve => {
if (isNaN(milliseconds)) {
throw new Error('milliseconds not a number')
}
setTimeout(() => resolve('done!'), milliseconds)
})
}