Skip to content

Commit

Permalink
Merge pull request #26 from TimonVS/chore/refactor
Browse files Browse the repository at this point in the history
Refactor and simplify code
  • Loading branch information
TimonVS committed Oct 27, 2019
2 parents c1b7f0b + ffc7465 commit 286b16f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 45 deletions.
42 changes: 24 additions & 18 deletions __tests__/action.test.ts
Expand Up @@ -2,14 +2,14 @@ import nock from 'nock'
import fs from 'fs'
import path from 'path'
import action from '../src/action'
import { Context } from '@actions/github/lib/context'
import { WebhookPayload } from '@actions/github/lib/interfaces'

nock.disableNetConnect()

describe('pr-labeler-action', () => {
beforeEach(() => {
// configuration-path parameter is required
// parameters are exposed as environment variables: https://help.github.com/en/github/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#jobsjob_idstepswith
process.env['INPUT_CONFIGURATION-PATH'] = '.github/pr-labeler.yml'
setupEnvironmentVariables()
})

it('adds the "fix" label for "fix/510-logging" branch', async () => {
Expand All @@ -24,9 +24,7 @@ describe('pr-labeler-action', () => {
})
.reply(200)

await action({
payload: pullRequestOpenedFixture({ ref: 'fix/510-logging' })
})
await action(new MockContext(pullRequestOpenedFixture({ ref: 'fix/510-logging' })))
expect.assertions(1)
})

Expand All @@ -42,9 +40,7 @@ describe('pr-labeler-action', () => {
})
.reply(200)

await action({
payload: pullRequestOpenedFixture({ ref: 'feature/sign-in-page/101' })
})
await action(new MockContext(pullRequestOpenedFixture({ ref: 'feature/sign-in-page/101' })))
expect.assertions(1)
})

Expand All @@ -60,9 +56,7 @@ describe('pr-labeler-action', () => {
})
.reply(200)

await action({
payload: pullRequestOpenedFixture({ ref: 'release/2.0' })
})
await action(new MockContext(pullRequestOpenedFixture({ ref: 'release/2.0' })))
expect.assertions(1)
})

Expand All @@ -78,9 +72,7 @@ describe('pr-labeler-action', () => {
})
.reply(200)

await action({
payload: pullRequestOpenedFixture({ ref: 'fix/510-logging' })
})
await action(new MockContext(pullRequestOpenedFixture({ ref: 'fix/510-logging' })))
expect.assertions(1)
})

Expand All @@ -93,12 +85,17 @@ describe('pr-labeler-action', () => {
})
.reply(200)

await action({
payload: pullRequestOpenedFixture({ ref: 'hello_world' })
})
await action(new MockContext(pullRequestOpenedFixture({ ref: 'hello_world' })))
})
})

class MockContext extends Context {
constructor(payload: WebhookPayload) {
super()
this.payload = payload
}
}

function encodeContent(content: Buffer) {
return Buffer.from(content).toString('base64')
}
Expand Down Expand Up @@ -140,3 +137,12 @@ function pullRequestOpenedFixture({ ref }: { ref: string }) {
}
}
}

function setupEnvironmentVariables() {
// reset process.env otherwise `Context` will use those variables
process.env = {}

// configuration-path parameter is required
// parameters are exposed as environment variables: https://help.github.com/en/github/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#jobsjob_idstepswith
process.env['INPUT_CONFIGURATION-PATH'] = '.github/pr-labeler.yml'
}
48 changes: 23 additions & 25 deletions src/action.ts
Expand Up @@ -2,23 +2,18 @@ import * as core from '@actions/core'
import * as github from '@actions/github'
import { Context } from '@actions/github/lib/context'
import matcher from 'matcher'
import getConfig from './utils/config'
import { RepoInfo } from './utils/config'
import getConfig, { Config } from './utils/config'

const defaultConfig = {
feature: ['feature/*', 'feat/*'],
fix: 'fix/*',
chore: 'chore/*'
}

async function action(context: Pick<Context, 'payload'> = github.context) {
async function action(context: Context = github.context) {
try {
const GITHUB_TOKEN = process.env.GITHUB_TOKEN!
const octokit = new github.GitHub(GITHUB_TOKEN)
const repoInfo: RepoInfo = {
owner: context.payload.repository!.owner.login,
repo: context.payload.repository!.name
}
const configPath = core.getInput('configuration-path', { required: true })

if (!context.payload.pull_request) {
Expand All @@ -28,28 +23,14 @@ async function action(context: Pick<Context, 'payload'> = github.context) {
}

const ref: string = context.payload.pull_request.head.ref
const config = await getConfig(octokit, configPath, repoInfo, ref, defaultConfig)

const labelsToAdd = Object.entries(config).reduce(
(labels, [label, patterns]) => {
if (
Array.isArray(patterns)
? patterns.some(pattern => matcher.isMatch(ref, pattern))
: matcher.isMatch(ref, patterns)
) {
labels.push(label)
}

return labels
},
[] as string[]
)
const config = await getConfig(octokit, configPath, context.repo, ref, defaultConfig)
const labelsToAdd = getLabelsToAdd(config, ref)

if (labelsToAdd.length > 0) {
await octokit.issues.addLabels({
...context.repo,
number: context.payload.pull_request.number,
labels: labelsToAdd,
...repoInfo
labels: labelsToAdd
})
}
} catch (error) {
Expand All @@ -62,4 +43,21 @@ async function action(context: Pick<Context, 'payload'> = github.context) {
}
}

function getLabelsToAdd(config: Config, branchName: string): string[] {
return Object.entries(config).reduce(
(labels, [label, patterns]) => {
if (
Array.isArray(patterns)
? patterns.some(pattern => matcher.isMatch(branchName, pattern))
: matcher.isMatch(branchName, patterns)
) {
labels.push(label)
}

return labels
},
[] as string[]
)
}

export default action
4 changes: 2 additions & 2 deletions src/utils/config.ts
@@ -1,12 +1,12 @@
import yaml from 'js-yaml'
import { GitHub } from '@actions/github'

export interface RepoInfo {
interface RepoInfo {
owner: string
repo: string
}

interface Config {
export interface Config {
[k: string]: string | string[]
}

Expand Down

0 comments on commit 286b16f

Please sign in to comment.