Skip to content

Commit

Permalink
Refactor and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
crazy-max committed Aug 20, 2020
1 parent a084826 commit 78c615e
Show file tree
Hide file tree
Showing 12 changed files with 4,748 additions and 829 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: test

on:
push:
branches:
- master
- releases/v*
paths-ignore:
- '**.md'
pull_request:
paths-ignore:
- '**.md'

jobs:
test:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2.3.2
-
name: Install
run: yarn install
-
name: Test
run: yarn run test
-
name: Upload coverage
uses: codecov/codecov-action@v1.0.13
if: success()
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage/clover.xml
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 3.0.0 (2020/08/20)

* Move `GITHUB_TOKEN` env var to `github-token` input
* Rename `yaml_file` input to `yaml-file`
* Rename `skip_delete` input to `skip-delete`
* Rename `dry_run` input to `dry-run`
* Refactor and add tests

## 2.1.0 (2020/07/22)

* Handle [@actions/github](https://github.com/actions/toolkit/tree/main/packages/github) v4
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[![GitHub release](https://img.shields.io/github/release/crazy-max/ghaction-github-labeler.svg?style=flat-square)](https://github.com/crazy-max/ghaction-github-labeler/releases/latest)
[![GitHub marketplace](https://img.shields.io/badge/marketplace-github--labeler-blue?logo=github&style=flat-square)](https://github.com/marketplace/actions/github-labeler)
[![CI workflow](https://img.shields.io/github/workflow/status/crazy-max/ghaction-github-labeler/ci?label=ci&logo=github&style=flat-square)](https://github.com/crazy-max/ghaction-github-labeler/actions?workflow=ci)
[![Test workflow](https://img.shields.io/github/workflow/status/crazy-max/ghaction-github-labeler/test?label=test&logo=github&style=flat-square)](https://github.com/crazy-max/ghaction-github-labeler/actions?workflow=test)
[![Codecov](https://img.shields.io/codecov/c/github/crazy-max/ghaction-github-labeler?logo=codecov&style=flat-square)](https://codecov.io/gh/crazy-max/ghaction-github-labeler)
[![Become a sponsor](https://img.shields.io/badge/sponsor-crazy--max-181717.svg?logo=github&style=flat-square)](https://github.com/sponsors/crazy-max)
[![Paypal Donate](https://img.shields.io/badge/donate-paypal-00457c.svg?logo=paypal&style=flat-square)](https://www.paypal.me/crazyws)

Expand Down Expand Up @@ -103,7 +104,7 @@ Following inputs can be used as `step.with` keys
| `yaml-file` | String | `.github/labels.yml` | Path to YAML file containing labels definitions |
| `skip-delete` | Bool | `false` | If enabled, labels will not be deleted if not found in YAML file |
| `dry-run` | Bool | `false` | If enabled, changes will not be applied |
| `exclude` | List | | Comma or newline delimited list of labels pattern(s)/matcher to exclude |
| `exclude` | List | | Newline delimited list of labels pattern(s)/matcher to exclude |

## Keep up-to-date with GitHub Dependabot

Expand Down
47 changes: 47 additions & 0 deletions __tests__/context.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as context from '../src/context';

describe('getInputList', () => {
it('handles single line correctly', async () => {
await setInput('foo', 'bar');
const res = await context.getInputList('foo');
console.log(res);
expect(res).toEqual(['bar']);
});

it('handles multiple lines correctly', async () => {
setInput('foo', 'bar\nbaz');
const res = await context.getInputList('foo');
console.log(res);
expect(res).toEqual(['bar', 'baz']);
});

it('handles comma correctly', async () => {
setInput('foo', 'bar,baz');
const res = await context.getInputList('foo');
console.log(res);
expect(res).toEqual(['bar', 'baz']);
});

it('handles different new lines correctly', async () => {
setInput('foo', 'bar\r\nbaz');
const res = await context.getInputList('foo');
console.log(res);
expect(res).toEqual(['bar', 'baz']);
});

it('handles different new lines and comma correctly', async () => {
setInput('foo', 'bar\r\nbaz,bat');
const res = await context.getInputList('foo');
console.log(res);
expect(res).toEqual(['bar', 'baz', 'bat']);
});
});

// See: https://github.com/actions/toolkit/blob/master/packages/core/src/core.ts#L67
function getInputName(name: string): string {
return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`;
}

function setInput(name: string, value: string): void {
process.env[getInputName(name)] = value;
}
116 changes: 116 additions & 0 deletions __tests__/labeler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import {Inputs} from '../src/context';
import {Labeler, LabelStatus} from '../src/labeler';

const cases = [
[
'labels.update.yml',
{
githubToken: process.env.GITHUB_TOKEN || '',
yamlFile: '.res/labels.update.yml',
skipDelete: true,
dryRun: true,
exclude: []
},
{
skip: 13,
exclude: 0,
create: 0,
update: 2,
rename: 1,
delete: 3,
error: 0
}
],
[
'labels.exclude1.yml',
{
githubToken: process.env.GITHUB_TOKEN || '',
yamlFile: '.res/labels.exclude1.yml',
skipDelete: true,
dryRun: true,
exclude: ['* d*', '*enhancement', '*fix']
},
{
skip: 12,
exclude: 5,
create: 0,
update: 1,
rename: 0,
delete: 1,
error: 0
}
],
[
'labels.exclude2.yml',
{
githubToken: process.env.GITHUB_TOKEN || '',
yamlFile: '.res/labels.exclude2.yml',
skipDelete: true,
dryRun: true,
exclude: ['*fix']
},
{
skip: 17,
exclude: 1,
create: 0,
update: 0,
rename: 0,
delete: 1,
error: 0
}
]
];

describe('run', () => {
test.each(cases)('given %p', async (name, inputs, expected) => {
const labeler = new Labeler(inputs as Inputs);
console.log(
(await labeler.labels).map(label => {
return label.ghaction_log;
})
);

const res = {
skip: 0,
exclude: 0,
create: 0,
update: 0,
rename: 0,
delete: 0,
error: 0
};
for (const label of await labeler.labels) {
switch (label.ghaction_status) {
case LabelStatus.Exclude: {
res.exclude++;
break;
}
case LabelStatus.Create: {
res.create++;
break;
}
case LabelStatus.Update: {
res.update++;
break;
}
case LabelStatus.Rename: {
res.rename++;
break;
}
case LabelStatus.Delete: {
res.delete++;
break;
}
case LabelStatus.Skip: {
res.skip++;
break;
}
case LabelStatus.Error: {
res.error++;
break;
}
}
}
expect(res).toEqual(expected);
});
});

0 comments on commit 78c615e

Please sign in to comment.