-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.ts
More file actions
61 lines (54 loc) · 1.59 KB
/
Copy pathusers.ts
File metadata and controls
61 lines (54 loc) · 1.59 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
import * as core from '@actions/core'
import { Octokit } from '@octokit/rest'
import type { Classroom } from '../types.js'
/**
* Checks if the user is a member of the organization.
*
* @param octokit Octokit
* @param classroom Classroom
* @param handle GitHub Handle
* @returns User Membership Status
*/
export async function isOrgMember(
octokit: InstanceType<typeof Octokit>,
classroom: Classroom,
handle: string
): Promise<'pending' | 'active' | undefined> {
core.info(`Checking if User is Org Member: ${handle}`)
try {
const response = await octokit.rest.orgs.getMembershipForUser({
org: classroom.organization,
username: handle
})
core.info(`User is Org Member: ${handle}`)
return response.data.state
} catch (error: any) {
if (error.status === 404) return undefined
}
}
/**
* Removes all users in this class from the organization (except for
* administrators).
*
* @param octokit Octokit
* @param classroom Classroom
*/
export async function removeUsers(
octokit: InstanceType<typeof Octokit>,
classroom: Classroom
): Promise<void> {
core.info(`Removing Users from the Organization: ${classroom.organization}`)
for (const handle of classroom.attendees) {
// Check if the user is an instructor.
if (classroom.administrators.includes(handle)) {
core.info(`\tUser is an Administrator: ${handle}`)
continue
}
core.info(`\tRemoving User: ${handle}`)
if (await isOrgMember(octokit, classroom, handle))
await octokit.rest.orgs.removeMember({
org: classroom.organization,
username: handle
})
}
}