-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinputs.ts
More file actions
149 lines (133 loc) · 3.9 KB
/
Copy pathinputs.ts
File metadata and controls
149 lines (133 loc) · 3.9 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import * as core from '@actions/core'
import * as fs from 'fs'
import path from 'path'
import { AllowedAction } from './enums.js'
import type { ActionInputs, Classroom } from './types.js'
/**
* Gets and validates the command-line inputs.
*
* Command format:
*
* `node dist/index.js <action>`
*
* @returns Action Inputs
*/
export function getInputs(): ActionInputs | undefined {
const action = process.argv[2]
// Check if the action is present.
if (!action) {
core.error('No Action Input Specified')
return undefined
}
const inputs: ActionInputs = {
action: action as AllowedAction
}
// Check if the action is valid.
if (
Object.values(AllowedAction).includes(action as AllowedAction) === false
) {
core.error(
`Invalid Action Input (Options: ${Object.values(AllowedAction).join(', ')})`
)
return undefined
}
// If the action is `add-user`, check if the handle is present.
if (
/* istanbul ignore next */
action === AllowedAction.ADD_USER ||
action === AllowedAction.REMOVE_USER ||
action === AllowedAction.ADD_ADMIN ||
action === AllowedAction.REMOVE_ADMIN
) {
const handle = process.argv[3]
if (!handle) {
core.error('No Handle Input Specified')
core.error(`Usage: node dist/index.js ${action} <handle>`)
return undefined
}
inputs.handle = handle
}
return inputs
}
/**
* Gets and validates the classroom JSON file.
*
* @returns Classroom JSON
*/
export function getClassroom(): Classroom | undefined {
// Check if the file exists.
if (!fs.existsSync(path.resolve(process.cwd(), 'classroom.json'))) {
core.error('Classroom File Not Found')
return undefined
}
// Read the file.
const classroomFile = fs.readFileSync(
path.resolve(process.cwd(), 'classroom.json'),
'utf8'
)
// Try to parse the file.
try {
const parsedFile = JSON.parse(classroomFile) as Classroom
// Check if the required fields are present.
if (parsedFile.organization === undefined) {
core.error('Classroom File Missing Organization Field')
return undefined
}
if (parsedFile.customerName === undefined) {
core.error('Classroom File Missing Customer Name Field')
return undefined
}
if (parsedFile.customerAbbr === undefined) {
core.error('Classroom File Missing Customer Abbreviation Field')
return undefined
}
if (parsedFile.administrators === undefined) {
core.error('Classroom File Missing Administrators Field')
return undefined
}
if (parsedFile.administrators.length === 0) {
core.error('Classroom File Missing Administrators')
return undefined
}
if (parsedFile.attendees === undefined) {
core.error('Classroom File Missing Attendees Field')
return undefined
}
// Validate the organization matches GitHub org slug conventions.
if (!/^[a-zA-Z0-9-_]+$/.test(parsedFile.organization)) {
core.error(
'Organization Field Invalid (Only Alphanumeric, Hyphen, Underscore)'
)
return undefined
}
return {
githubServer:
/* istanbul ignore next */ parsedFile.githubServer?.trim() ||
'github.com',
organization: parsedFile.organization.trim(),
customerName: parsedFile.customerName.trim(),
customerAbbr: parsedFile.customerAbbr.trim().toUpperCase(),
administrators: parsedFile.administrators.map((admin: string) =>
admin.trim()
),
attendees: parsedFile.attendees.map((attendee: string) => attendee.trim())
}
} catch (error) {
/* istanbul ignore next */
core.error('Classroom File Not Valid JSON')
}
/* istanbul ignore next */
return undefined
}
/**
* Updates the classroom JSON file.
*
* @param classroom Classroom
*/
export function updateClassroom(classroom: Classroom): void {
fs.writeFileSync(
path.resolve('../classroom.json'),
JSON.stringify(classroom, null, 2),
'utf8'
)
}