-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteams.ts
More file actions
183 lines (165 loc) · 4.44 KB
/
Copy pathteams.ts
File metadata and controls
183 lines (165 loc) · 4.44 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import * as core from '@actions/core'
import { Octokit } from '@octokit/rest'
import type { Classroom } from '../types.js'
/**
* Generates the team name for this class.
*
* @param classroom Classroom
* @returns Team Name
*/
export function generateTeamName(classroom: Classroom): string {
return `gh-int-${classroom.customerAbbr.toLowerCase()}`
}
/**
* Checks if the team exists.
*
* @param octokit Octokit
* @param classroom Classroom
* @returns True if Team Exists
*/
export async function exists(
octokit: InstanceType<typeof Octokit>,
classroom: Classroom
): Promise<boolean> {
try {
await octokit.rest.teams.getByName({
org: classroom.organization,
team_slug: generateTeamName(classroom)
})
} catch (error: any) {
if (error.status === 404) return false
}
return true
}
/**
* Gets the team.
*
* @param octokit Octokit
* @param classroom Classroom
* @returns Team Information
*/
export async function get(
octokit: InstanceType<typeof Octokit>,
classroom: Classroom
): Promise<string> {
const response = await octokit.rest.teams.getByName({
org: classroom.organization,
team_slug: generateTeamName(classroom)
})
return response.data.slug
}
/**
* Creates the team for this class.
*
* @param octokit Octokit
* @param classroom Classroom
*/
export async function create(
octokit: InstanceType<typeof Octokit>,
classroom: Classroom
): Promise<void> {
core.info(`Creating Team: ${generateTeamName(classroom)}`)
// Create the team. Add the class administrators as maintainers.
await octokit.rest.teams.create({
org: classroom.organization,
name: generateTeamName(classroom)
})
// Add the attendees to the team.
for (const user of classroom.attendees)
await addUser(octokit, classroom, user, 'member')
// Add the administrators to the team.
for (const user of classroom.administrators)
await addUser(octokit, classroom, user, 'maintainer')
}
/**
* Adds a member to the team.
*
* @param octokit Octokit
* @param classroom Classroom
* @param hande GitHub Handle
* @param role Role
*/
export async function addUser(
octokit: InstanceType<typeof Octokit>,
classroom: Classroom,
handle: string,
role: 'member' | 'maintainer'
): Promise<void> {
core.info(`\tAdding User to Class Team: ${handle} (${role})`)
// Add the user to the team.
await octokit.rest.teams.addOrUpdateMembershipForUserInOrg({
org: classroom.organization,
team_slug: generateTeamName(classroom),
username: handle,
role
})
}
/**
* Removes a member from the team.
*
* @param octokit Octokit
* @param classroom Classroom
* @param handle GitHub Handle
*/
export async function removeUser(
octokit: InstanceType<typeof Octokit>,
classroom: Classroom,
handle: string
): Promise<void> {
core.info(`Removing User from Class Team: ${handle}`)
try {
// Check if the user is a member (they must have accepted the invitation).
const membership = await octokit.rest.teams.getMembershipForUserInOrg({
org: classroom.organization,
team_slug: generateTeamName(classroom),
username: handle
})
// Remove the user from the team.
if (membership.data.state === 'active')
await octokit.rest.teams.removeMembershipForUserInOrg({
org: classroom.organization,
team_slug: generateTeamName(classroom),
username: handle
})
} catch (error: any) {
// If the user could not be found, they're not in the organization.
/* istanbul ignore next */
if (error.status !== 404) throw error
}
}
/**
* Deletes the team.
*
* @param octokit Octokit
* @param classroom Classroom
*/
export async function deleteTeam(
octokit: InstanceType<typeof Octokit>,
classroom: Classroom
): Promise<void> {
core.info(`Deleting Team: ${generateTeamName(classroom)}`)
// If the team exists, delete it.
if (await exists(octokit, classroom))
await octokit.rest.teams.deleteInOrg({
org: classroom.organization,
team_slug: generateTeamName(classroom)
})
}
/**
* Gets the members of the team.
*
* @param octokit Octokit
* @param classroom Classroom
* @returns Team Member Handles
*/
export async function getMembers(
octokit: InstanceType<typeof Octokit>,
classroom: Classroom
): Promise<string[]> {
// Get the members of the team.
const response = await octokit.rest.teams.listMembersInOrg({
org: classroom.organization,
team_slug: generateTeamName(classroom)
})
return response.data.map((user) => user.login)
}