Skip to content

Commit ad9cf8d

Browse files
committed
Convert to typescript
1 parent 4a930f9 commit ad9cf8d

18 files changed

+10674
-25866
lines changed

.eslintignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dist/
2+
lib/
3+
node_modules/

.eslintrc.json

+18-16
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
{
2-
"env": {
3-
"commonjs": true,
4-
"es6": true,
5-
"node": true
6-
},
7-
"extends": "eslint:recommended",
8-
"globals": {
9-
"Atomics": "readonly",
10-
"SharedArrayBuffer": "readonly"
11-
},
12-
"parserOptions": {
13-
"ecmaVersion": 2018
14-
},
15-
"rules": {
16-
}
17-
}
2+
"env": { "node": true, "jest": true },
3+
"parser": "@typescript-eslint/parser",
4+
"parserOptions": { "ecmaVersion": 9, "sourceType": "module" },
5+
"extends": [
6+
"eslint:recommended",
7+
"plugin:@typescript-eslint/eslint-recommended",
8+
"plugin:@typescript-eslint/recommended",
9+
"plugin:import/errors",
10+
"plugin:import/warnings",
11+
"plugin:import/typescript",
12+
"plugin:prettier/recommended",
13+
"prettier/@typescript-eslint"
14+
],
15+
"plugins": ["@typescript-eslint"],
16+
"rules": {
17+
"@typescript-eslint/camelcase": "off"
18+
}
19+
}

.github/workflows/ci.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ jobs:
1919
with:
2020
node-version: 12.x
2121
- run: npm ci
22+
- run: npm run build
23+
- run: npm run format-check
24+
- run: npm run lint
2225
- run: npm run test
23-
- run: npm run package
2426
- uses: actions/upload-artifact@v2
2527
with:
2628
name: dist

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
node_modules
1+
lib/
2+
node_modules/

.prettierignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dist/
2+
lib/
3+
node_modules/

.prettierrc.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"printWidth": 80,
3+
"tabWidth": 2,
4+
"useTabs": false,
5+
"semi": false,
6+
"singleQuote": true,
7+
"trailingComma": "none",
8+
"bracketSpacing": false,
9+
"arrowParens": "avoid",
10+
"parser": "typescript"
11+
}

__test__/command-helper.test.ts

+231
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
import {
2+
Inputs,
3+
Command,
4+
SlashCommandPayload,
5+
commandDefaults,
6+
getCommandsConfigFromInputs,
7+
getCommandsConfigFromJson,
8+
actorHasPermission,
9+
configIsValid,
10+
getSlashCommandPayload
11+
} from '../lib/command-helper'
12+
13+
describe('command-helper tests', () => {
14+
test('building config with required inputs only', async () => {
15+
const inputs: Inputs = {
16+
token: '',
17+
reactionToken: '',
18+
reactions: true,
19+
commands: 'list, of, slash, commands',
20+
permission: 'write',
21+
issueType: 'both',
22+
allowEdits: false,
23+
// Should be process.env.GITHUB_REPOSITORY, but '' for tests
24+
repository: '',
25+
eventTypeSuffix: '-command',
26+
namedArgs: false,
27+
config: '',
28+
configFromFile: ''
29+
}
30+
const commands = inputs.commands.replace(/\s+/g, '').split(',')
31+
const config = getCommandsConfigFromInputs(inputs)
32+
expect(config.length).toEqual(4)
33+
for (var i = 0; i < config.length; i++) {
34+
expect(config[i].command).toEqual(commands[i])
35+
expect(config[i].permission).toEqual(commandDefaults.permission)
36+
expect(config[i].issue_type).toEqual(commandDefaults.issue_type)
37+
expect(config[i].allow_edits).toEqual(commandDefaults.allow_edits)
38+
expect(config[i].repository).toEqual(commandDefaults.repository)
39+
expect(config[i].event_type_suffix).toEqual(
40+
commandDefaults.event_type_suffix
41+
)
42+
expect(config[i].named_args).toEqual(commandDefaults.named_args)
43+
}
44+
})
45+
46+
test('building config with optional inputs', async () => {
47+
const inputs: Inputs = {
48+
token: '',
49+
reactionToken: '',
50+
reactions: true,
51+
commands: 'list, of, slash, commands',
52+
permission: 'admin',
53+
issueType: 'pull-request',
54+
allowEdits: true,
55+
repository: 'owner/repo',
56+
eventTypeSuffix: '-cmd',
57+
namedArgs: true,
58+
config: '',
59+
configFromFile: ''
60+
}
61+
const commands = inputs.commands.replace(/\s+/g, '').split(',')
62+
const config = getCommandsConfigFromInputs(inputs)
63+
expect(config.length).toEqual(4)
64+
for (var i = 0; i < config.length; i++) {
65+
expect(config[i].command).toEqual(commands[i])
66+
expect(config[i].permission).toEqual(inputs.permission)
67+
expect(config[i].issue_type).toEqual(inputs.issueType)
68+
expect(config[i].allow_edits).toEqual(inputs.allowEdits)
69+
expect(config[i].repository).toEqual(inputs.repository)
70+
expect(config[i].event_type_suffix).toEqual(inputs.eventTypeSuffix)
71+
expect(config[i].named_args).toEqual(inputs.namedArgs)
72+
}
73+
})
74+
75+
test('building config with required JSON only', async () => {
76+
const json = `[
77+
{
78+
"command": "do-stuff"
79+
},
80+
{
81+
"command": "test-all-the-things"
82+
}
83+
]`
84+
const commands = ['do-stuff', 'test-all-the-things']
85+
const config = getCommandsConfigFromJson(json)
86+
expect(config.length).toEqual(2)
87+
for (var i = 0; i < config.length; i++) {
88+
expect(config[i].command).toEqual(commands[i])
89+
expect(config[i].permission).toEqual(commandDefaults.permission)
90+
expect(config[i].issue_type).toEqual(commandDefaults.issue_type)
91+
expect(config[i].allow_edits).toEqual(commandDefaults.allow_edits)
92+
expect(config[i].repository).toEqual(commandDefaults.repository)
93+
expect(config[i].event_type_suffix).toEqual(
94+
commandDefaults.event_type_suffix
95+
)
96+
expect(config[i].named_args).toEqual(commandDefaults.named_args)
97+
}
98+
})
99+
100+
test('building config with optional JSON properties', async () => {
101+
const json = `[
102+
{
103+
"command": "do-stuff",
104+
"permission": "admin",
105+
"issue_type": "pull-request",
106+
"allow_edits": true,
107+
"repository": "owner/repo",
108+
"event_type_suffix": "-cmd",
109+
"named_args": true
110+
},
111+
{
112+
"command": "test-all-the-things",
113+
"permission": "read"
114+
}
115+
]`
116+
const commands = ['do-stuff', 'test-all-the-things']
117+
const config = getCommandsConfigFromJson(json)
118+
expect(config.length).toEqual(2)
119+
expect(config[0].command).toEqual(commands[0])
120+
expect(config[0].permission).toEqual('admin')
121+
expect(config[0].issue_type).toEqual('pull-request')
122+
expect(config[0].allow_edits).toBeTruthy()
123+
expect(config[0].repository).toEqual('owner/repo')
124+
expect(config[0].event_type_suffix).toEqual('-cmd')
125+
expect(config[0].named_args).toBeTruthy()
126+
expect(config[1].command).toEqual(commands[1])
127+
expect(config[1].permission).toEqual('read')
128+
expect(config[1].issue_type).toEqual(commandDefaults.issue_type)
129+
})
130+
131+
test('valid config', async () => {
132+
const config: Command[] = [
133+
{
134+
command: 'test',
135+
permission: 'write',
136+
issue_type: 'both',
137+
allow_edits: false,
138+
repository: 'peter-evans/slash-command-dispatch',
139+
event_type_suffix: '-command',
140+
named_args: false
141+
}
142+
]
143+
expect(configIsValid(config)).toBeTruthy()
144+
})
145+
146+
test('invalid permission level in config', async () => {
147+
const config: Command[] = [
148+
{
149+
command: 'test',
150+
permission: 'test-case-invalid-permission',
151+
issue_type: 'both',
152+
allow_edits: false,
153+
repository: 'peter-evans/slash-command-dispatch',
154+
event_type_suffix: '-command',
155+
named_args: false
156+
}
157+
]
158+
expect(configIsValid(config)).toBeFalsy()
159+
})
160+
161+
test('invalid issue type in config', async () => {
162+
const config: Command[] = [
163+
{
164+
command: 'test',
165+
permission: 'write',
166+
issue_type: 'test-case-invalid-issue-type',
167+
allow_edits: false,
168+
repository: 'peter-evans/slash-command-dispatch',
169+
event_type_suffix: '-command',
170+
named_args: false
171+
}
172+
]
173+
expect(configIsValid(config)).toBeFalsy()
174+
})
175+
176+
test('actor does not have permission', async () => {
177+
expect(actorHasPermission('none', 'read')).toBeFalsy()
178+
expect(actorHasPermission('read', 'write')).toBeFalsy()
179+
expect(actorHasPermission('write', 'admin')).toBeFalsy()
180+
})
181+
182+
test('actor has permission', async () => {
183+
expect(actorHasPermission('read', 'none')).toBeTruthy()
184+
expect(actorHasPermission('write', 'read')).toBeTruthy()
185+
expect(actorHasPermission('admin', 'write')).toBeTruthy()
186+
expect(actorHasPermission('write', 'write')).toBeTruthy()
187+
})
188+
189+
test('slash command payload', async () => {
190+
const commandWords = ['test', 'arg1', 'arg2', 'arg3']
191+
const namedArgs = false
192+
const payload: SlashCommandPayload = {
193+
command: 'test',
194+
args: 'arg1 arg2 arg3',
195+
arg1: 'arg1',
196+
arg2: 'arg2',
197+
arg3: 'arg3'
198+
}
199+
expect(getSlashCommandPayload(commandWords, namedArgs)).toEqual(payload)
200+
})
201+
202+
test('slash command payload with named args', async () => {
203+
const commandWords = ['test', 'branch=master', 'arg1', 'env=prod', 'arg2']
204+
const namedArgs = true
205+
const payload: SlashCommandPayload = {
206+
command: 'test',
207+
args: 'branch=master arg1 env=prod arg2',
208+
unnamed_args: 'arg1 arg2',
209+
branch: 'master',
210+
env: 'prod',
211+
arg1: 'arg1',
212+
arg2: 'arg2'
213+
}
214+
expect(getSlashCommandPayload(commandWords, namedArgs)).toEqual(payload)
215+
})
216+
217+
test('slash command payload with malformed named args', async () => {
218+
const commandWords = ['test', 'branch=', 'arg1', 'e-nv=prod', 'arg2']
219+
const namedArgs = true
220+
const payload: SlashCommandPayload = {
221+
command: 'test',
222+
args: 'branch= arg1 e-nv=prod arg2',
223+
unnamed_args: 'branch= arg1 e-nv=prod arg2',
224+
arg1: 'branch=',
225+
arg2: 'arg1',
226+
arg3: 'e-nv=prod',
227+
arg4: 'arg2'
228+
}
229+
expect(getSlashCommandPayload(commandWords, namedArgs)).toEqual(payload)
230+
})
231+
})

action.yml

+7
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,28 @@ inputs:
99
default: ${{ github.token }}
1010
reactions:
1111
description: 'Add reactions to comments containing commands.'
12+
default: true
1213
commands:
1314
description: 'A comma separated list of commands.'
1415
required: true
1516
permission:
1617
description: 'The repository permission level required by the user to dispatch commands.'
18+
default: write
1719
issue-type:
1820
description: 'The issue type required for commands.'
21+
default: both
1922
allow-edits:
2023
description: 'Allow edited comments to trigger command dispatches.'
24+
default: false
2125
repository:
2226
description: 'The full name of the repository to send the dispatch events.'
27+
default: ${{ github.repository }}
2328
event-type-suffix:
2429
description: 'The repository dispatch event type suffix for the commands.'
30+
default: -command
2531
named-args:
2632
description: 'Parse named arguments and add them to the command payload.'
33+
default: false
2734
config:
2835
description: 'JSON configuration for commands.'
2936
config-from-file:

0 commit comments

Comments
 (0)