generated from ActionsDesk/admin-support-issueops-actions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse-issue-action.js
93 lines (85 loc) · 2.71 KB
/
parse-issue-action.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const Command = require('../command')
const Result = require('../result')
const ParameterRequiredError = require('../../exceptions/ParameterRequiredError')
const ParameterValidationError = require('../../exceptions/ParameterValidationError')
const YAML = require('yaml')
const validate = require('validate.js')
class ParseIssueAction extends Command {
async validate () {
if (!this.params.org) {
throw new ParameterRequiredError('A valid organization name is required.')
}
if (!this.params.repository) {
throw new ParameterRequiredError('A valid repository name is required.')
}
if (!this.params.supportedOrgs) {
throw new ParameterRequiredError(
'Missing supportedOrgs in the configuration.'
)
}
if (!this.params.issueNumber) {
throw new ParameterRequiredError(
'A valid repository issue number is required.'
)
}
}
async execute () {
// get issue body
const owner = this.params.org
const repo = this.params.repository
const issueNumber = this.params.issueNumber
const issueContent = await this.api.v3.issues.get({
owner,
repo,
issue_number: issueNumber
})
// parse issue body
const info = YAML.parse(issueContent.data.body)
const ticket = this.findObjectKeyInsensitive(info, 'ticket')
const parsedValues = {
target_org: this.findObjectKeyInsensitive(info, 'organization'),
description: this.findObjectKeyInsensitive(info, 'description'),
ticket: Number.isInteger(ticket) ? ticket.toString() : ticket,
duration: this.findObjectKeyInsensitive(info, 'duration')
}
const orgRegex = /^[a-z\d]+(?:-?[a-z\d]+)*$/i
const constraints = {
target_org: {
type: 'string',
presence: { allowEmpty: false },
inclusion: this.params.supportedOrgs,
format: orgRegex
},
description: {
type: 'string',
presence: { allowEmpty: false }
},
ticket: {
type: 'string',
presence: { allowEmpty: false }
},
duration: {
type: 'number',
presence: { allowEmpty: false },
numericality: {
onlyInteger: true,
greaterThan: 0,
lessThanOrEqualTo: 8
}
}
}
const validation = validate(parsedValues, constraints)
if (!validate.isEmpty(validation)) {
throw new ParameterValidationError(JSON.stringify(validation))
}
// Output
return new Result('success', JSON.stringify(parsedValues))
}
findObjectKeyInsensitive (obj, searchKey) {
return obj[Object.keys(obj).find(key => key.toLowerCase() === searchKey.toLowerCase())]
}
static getName () {
return 'parse_issue'
}
}
module.exports = ParseIssueAction