Skip to content

Commit 78c615e

Browse files
committed
Refactor and add tests
1 parent a084826 commit 78c615e

File tree

12 files changed

+4748
-829
lines changed

12 files changed

+4748
-829
lines changed

.github/workflows/test.yml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: test
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
- releases/v*
8+
paths-ignore:
9+
- '**.md'
10+
pull_request:
11+
paths-ignore:
12+
- '**.md'
13+
14+
jobs:
15+
test:
16+
runs-on: ubuntu-latest
17+
steps:
18+
-
19+
name: Checkout
20+
uses: actions/[email protected]
21+
-
22+
name: Install
23+
run: yarn install
24+
-
25+
name: Test
26+
run: yarn run test
27+
-
28+
name: Upload coverage
29+
uses: codecov/[email protected]
30+
if: success()
31+
with:
32+
token: ${{ secrets.CODECOV_TOKEN }}
33+
file: ./coverage/clover.xml

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## 3.0.0 (2020/08/20)
4+
5+
* Move `GITHUB_TOKEN` env var to `github-token` input
6+
* Rename `yaml_file` input to `yaml-file`
7+
* Rename `skip_delete` input to `skip-delete`
8+
* Rename `dry_run` input to `dry-run`
9+
* Refactor and add tests
10+
311
## 2.1.0 (2020/07/22)
412

513
* Handle [@actions/github](https://github.com/actions/toolkit/tree/main/packages/github) v4

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[![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)
22
[![GitHub marketplace](https://img.shields.io/badge/marketplace-github--labeler-blue?logo=github&style=flat-square)](https://github.com/marketplace/actions/github-labeler)
3-
[![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)
3+
[![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)
4+
[![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)
45
[![Become a sponsor](https://img.shields.io/badge/sponsor-crazy--max-181717.svg?logo=github&style=flat-square)](https://github.com/sponsors/crazy-max)
56
[![Paypal Donate](https://img.shields.io/badge/donate-paypal-00457c.svg?logo=paypal&style=flat-square)](https://www.paypal.me/crazyws)
67

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

108109
## Keep up-to-date with GitHub Dependabot
109110

__tests__/context.test.ts

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import * as context from '../src/context';
2+
3+
describe('getInputList', () => {
4+
it('handles single line correctly', async () => {
5+
await setInput('foo', 'bar');
6+
const res = await context.getInputList('foo');
7+
console.log(res);
8+
expect(res).toEqual(['bar']);
9+
});
10+
11+
it('handles multiple lines correctly', async () => {
12+
setInput('foo', 'bar\nbaz');
13+
const res = await context.getInputList('foo');
14+
console.log(res);
15+
expect(res).toEqual(['bar', 'baz']);
16+
});
17+
18+
it('handles comma correctly', async () => {
19+
setInput('foo', 'bar,baz');
20+
const res = await context.getInputList('foo');
21+
console.log(res);
22+
expect(res).toEqual(['bar', 'baz']);
23+
});
24+
25+
it('handles different new lines correctly', async () => {
26+
setInput('foo', 'bar\r\nbaz');
27+
const res = await context.getInputList('foo');
28+
console.log(res);
29+
expect(res).toEqual(['bar', 'baz']);
30+
});
31+
32+
it('handles different new lines and comma correctly', async () => {
33+
setInput('foo', 'bar\r\nbaz,bat');
34+
const res = await context.getInputList('foo');
35+
console.log(res);
36+
expect(res).toEqual(['bar', 'baz', 'bat']);
37+
});
38+
});
39+
40+
// See: https://github.com/actions/toolkit/blob/master/packages/core/src/core.ts#L67
41+
function getInputName(name: string): string {
42+
return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`;
43+
}
44+
45+
function setInput(name: string, value: string): void {
46+
process.env[getInputName(name)] = value;
47+
}

__tests__/labeler.test.ts

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import {Inputs} from '../src/context';
2+
import {Labeler, LabelStatus} from '../src/labeler';
3+
4+
const cases = [
5+
[
6+
'labels.update.yml',
7+
{
8+
githubToken: process.env.GITHUB_TOKEN || '',
9+
yamlFile: '.res/labels.update.yml',
10+
skipDelete: true,
11+
dryRun: true,
12+
exclude: []
13+
},
14+
{
15+
skip: 13,
16+
exclude: 0,
17+
create: 0,
18+
update: 2,
19+
rename: 1,
20+
delete: 3,
21+
error: 0
22+
}
23+
],
24+
[
25+
'labels.exclude1.yml',
26+
{
27+
githubToken: process.env.GITHUB_TOKEN || '',
28+
yamlFile: '.res/labels.exclude1.yml',
29+
skipDelete: true,
30+
dryRun: true,
31+
exclude: ['* d*', '*enhancement', '*fix']
32+
},
33+
{
34+
skip: 12,
35+
exclude: 5,
36+
create: 0,
37+
update: 1,
38+
rename: 0,
39+
delete: 1,
40+
error: 0
41+
}
42+
],
43+
[
44+
'labels.exclude2.yml',
45+
{
46+
githubToken: process.env.GITHUB_TOKEN || '',
47+
yamlFile: '.res/labels.exclude2.yml',
48+
skipDelete: true,
49+
dryRun: true,
50+
exclude: ['*fix']
51+
},
52+
{
53+
skip: 17,
54+
exclude: 1,
55+
create: 0,
56+
update: 0,
57+
rename: 0,
58+
delete: 1,
59+
error: 0
60+
}
61+
]
62+
];
63+
64+
describe('run', () => {
65+
test.each(cases)('given %p', async (name, inputs, expected) => {
66+
const labeler = new Labeler(inputs as Inputs);
67+
console.log(
68+
(await labeler.labels).map(label => {
69+
return label.ghaction_log;
70+
})
71+
);
72+
73+
const res = {
74+
skip: 0,
75+
exclude: 0,
76+
create: 0,
77+
update: 0,
78+
rename: 0,
79+
delete: 0,
80+
error: 0
81+
};
82+
for (const label of await labeler.labels) {
83+
switch (label.ghaction_status) {
84+
case LabelStatus.Exclude: {
85+
res.exclude++;
86+
break;
87+
}
88+
case LabelStatus.Create: {
89+
res.create++;
90+
break;
91+
}
92+
case LabelStatus.Update: {
93+
res.update++;
94+
break;
95+
}
96+
case LabelStatus.Rename: {
97+
res.rename++;
98+
break;
99+
}
100+
case LabelStatus.Delete: {
101+
res.delete++;
102+
break;
103+
}
104+
case LabelStatus.Skip: {
105+
res.skip++;
106+
break;
107+
}
108+
case LabelStatus.Error: {
109+
res.error++;
110+
break;
111+
}
112+
}
113+
}
114+
expect(res).toEqual(expected);
115+
});
116+
});

0 commit comments

Comments
 (0)