generated from ActionsDesk/admin-support-issueops-actions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
promote-demote-action.js
47 lines (43 loc) · 1.57 KB
/
promote-demote-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
const Command = require('../command')
const Result = require('../result')
const ParameterRequiredError = require('../../exceptions/ParameterRequiredError')
class PromoteDemoteAction extends Command {
async validate () {
if (!this.params.username) {
throw new ParameterRequiredError('A valid username is required.')
}
if (!this.params.targetOrg) {
throw new ParameterRequiredError('A valid target organization name is required.')
}
if (!this.params.role || !['admin', 'member'].includes(this.params.role)) {
throw new ParameterRequiredError('A valid role is required: [admin|member]')
}
}
async execute () {
const org = this.params.targetOrg
const username = this.params.username
const role = this.params.role
try {
await this.api.v3.orgs.setMembershipForUser({
org,
username,
role
})
const outputMessage = `The role of ${username} has been successfully changed to: ${role} in ${org}`
return new Result('success', outputMessage)
} catch (e) {
console.error(`Failed to change the role of ${username} in ${org}`)
console.error(`Status code: ${e.status}`)
console.error(
'Possible reasons: \n',
'- Username is not a member of the organization \n',
'- Personal access token provided does not have sufficient privileges \n',
'- Organization does not exist \n',
'- You do not have admin privileges for the organization provided')
}
}
static getName () {
return 'promote_demote'
}
}
module.exports = PromoteDemoteAction