-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathgithub_copilot_license_management.yml
More file actions
559 lines (507 loc) · 24.5 KB
/
github_copilot_license_management.yml
File metadata and controls
559 lines (507 loc) · 24.5 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
name: GitHub Copilot License Management
# Limit the default GITHUB_TOKEN permissions to the minimum required
permissions:
contents: read
issues: write
actions: read
# This workflow is triggered when an issue is labeled
on:
issues:
types:
- labeled
# Prevent concurrent runs for the same issue
concurrency:
group: copilot-license-${{ github.event.issue.number }}
cancel-in-progress: true
# This workflow contains two jobs: assign_github_copilot_license and remove_github_copilot_license
jobs:
assign_github_copilot_license:
name: Assign GitHub Copilot License
runs-on: ubuntu-latest
if: github.event.label.name == 'copilot-request'
steps:
# Checkout repository to access local composite actions
- name: Checkout repository
uses: actions/checkout@v4
# This step gets a GitHub App installation access token
- name: Get Token
id: get_workflow_token
uses: actions/create-github-app-token@v1
with:
app-id: ${{ secrets.APPLICATION_ID }}
private-key: ${{ secrets.APPLICATION_PRIVATE_KEY }}
# Validate that the requester is an organization member
- name: Validate organization membership
id: validate_membership
uses: ./.github/actions/validate-org-membership
with:
github-token: ${{ steps.get_workflow_token.outputs.token }}
username: ${{ github.event.sender.login }}
org: ${{ github.repository_owner }}
# Comment if user is not an org member
- name: Comment on invalid membership for license assignment
if: steps.validate_membership.outputs.status == 'invalid'
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: '❌ You must be a member of the **${{ github.repository_owner }}** organization to request a GitHub Copilot license.\n\nPlease contact the organization administrator if you believe this is an error.'
});
- name: Remove copilot-request label after invalid membership
if: steps.validate_membership.outputs.status == 'invalid'
uses: ./.github/actions/remove-label
with:
github-token: ${{ github.token }}
label-name: 'copilot-request'
issue-number: ${{ github.event.issue.number }}
owner: ${{ github.repository_owner }}
repo: ${{ github.event.repository.name }}
- name: Close issue after invalid membership
if: steps.validate_membership.outputs.status == 'invalid'
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
state: 'closed',
state_reason: 'not_planned'
});
# Comment if validation encountered an internal error
- name: Comment on validation error for license assignment
if: steps.validate_membership.outputs.status == 'error'
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: '⚠️ Copilot license request validation failed due to a workflow configuration error.\n\nPlease contact a repository administrator to investigate.\n\nThis issue will remain open for administrator review.'
});
# This step tries to assign a license and handles specific errors
- name: Assign GitHub Copilot license
if: steps.validate_membership.outputs.status == 'valid'
id: assign_license
uses: actions/github-script@v7
with:
github-token: ${{ steps.get_workflow_token.outputs.token }}
result-encoding: string
script: |
const username = context.payload.sender.login;
console.log(`Attempting to assign Copilot license to: ${username}`);
try {
await github.rest.copilot.addCopilotSeatsForUsers({
org: context.repo.owner,
selected_usernames: [username]
});
console.log(`Successfully assigned license to ${username}`);
core.summary.addRaw(`✅ Successfully assigned GitHub Copilot license to @${username}`, true);
await core.summary.write();
return 'success';
} catch (error) {
console.error(`Error assigning license to ${username}:`, error.message);
if (error.status === 422) {
if (error.message.includes('User already has Copilot access')) {
core.summary.addRaw(`ℹ️ User @${username} already has an active Copilot license`, true);
await core.summary.write();
return 'already_exists';
} else if (error.message.includes('seat limit')) {
core.summary.addRaw(`⚠️ Organization has reached seat limit. Cannot assign to @${username}`, true);
await core.summary.write();
return 'seat_limit';
}
} else if (error.status === 404) {
// 404 can mean the user is not eligible for Copilot or otherwise inaccessible, not necessarily missing from the org
return 'not_eligible_or_not_found';
} else if (error.status === 403) {
return 'insufficient_permissions';
}
// Sanitize error message to prevent injection attacks using robust escaping
const rawError = String(error.message || 'Unknown error');
const sanitizedError = JSON.stringify(rawError)
// Remove surrounding quotes added by JSON.stringify to keep a plain string
.replace(/^"|"$/g, '')
.substring(0, 500);
core.setOutput('error_message', sanitizedError);
return 'failure';
}
# This step runs if the user ALREADY has a license
- name: Comment on existing license and close issue
if: steps.assign_license.outputs.result == 'already_exists'
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: 'It appears you already have an active GitHub Copilot license. No action was needed. Closing this request. ✅'
});
- name: Remove copilot-request label after existing license
if: steps.assign_license.outputs.result == 'already_exists'
uses: ./.github/actions/remove-label
with:
github-token: ${{ github.token }}
label-name: 'copilot-request'
issue-number: ${{ github.event.issue.number }}
owner: ${{ github.repository_owner }}
repo: ${{ github.event.repository.name }}
- name: Close issue after existing license
if: steps.assign_license.outputs.result == 'already_exists'
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
state: 'closed'
});
# This step runs on successful assignment
- name: Comment and close GitHub Issue on success
if: steps.assign_license.outputs.result == 'success'
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: 'Successfully assigned the GitHub Copilot license.\n\nHere are some resources to help you get started with GitHub Copilot:\n- [GitHub Copilot Fundamentals MS Learn Path](https://learn.microsoft.com/training/paths/copilot)\n- [Essentials of GitHub Copilot](https://resources.github.com/learn/pathways/copilot/essentials/essentials-of-github-copilot)\n- [Introduction to GitHub Copilot](https://www.youtube.com/playlist?list=PLlrxD0HtieHgr23PS05FIncnih4dH9Na5)\n- [Mastering GitHub Copilot for AI Paired Programming](https://github.com/microsoft/Mastering-GitHub-Copilot-for-Paired-Programming)'
});
- name: Remove copilot-request label after successful assignment
if: steps.assign_license.outputs.result == 'success'
uses: ./.github/actions/remove-label
with:
github-token: ${{ github.token }}
label-name: 'copilot-request'
issue-number: ${{ github.event.issue.number }}
owner: ${{ github.repository_owner }}
repo: ${{ github.event.repository.name }}
- name: Close issue after successful assignment
if: steps.assign_license.outputs.result == 'success'
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
state: 'closed'
});
# Handle seat limit reached
- name: Comment on seat limit
if: steps.assign_license.outputs.result == 'seat_limit'
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: '⚠️ Unable to assign GitHub Copilot license: The organization has reached its seat limit.\n\nPlease contact the organization administrator to:\n- Increase the seat count, or\n- Free up existing seats\n\nThis issue will remain open for administrator review.'
});
- name: Remove copilot-request label after seat limit
if: steps.assign_license.outputs.result == 'seat_limit'
uses: ./.github/actions/remove-label
with:
github-token: ${{ github.token }}
label-name: 'copilot-request'
issue-number: ${{ github.event.issue.number }}
owner: ${{ github.repository_owner }}
repo: ${{ github.event.repository.name }}
# Handle user not found
- name: Comment on user not found
if: steps.assign_license.outputs.result == 'user_not_found'
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: '❌ User not found in the organization. Please ensure you are a member of the organization before requesting a Copilot license.'
});
- name: Remove copilot-request label after user not found
if: steps.assign_license.outputs.result == 'user_not_found'
uses: ./.github/actions/remove-label
with:
github-token: ${{ github.token }}
label-name: 'copilot-request'
issue-number: ${{ github.event.issue.number }}
owner: ${{ github.repository_owner }}
repo: ${{ github.event.repository.name }}
- name: Close issue after user not found
if: steps.assign_license.outputs.result == 'user_not_found'
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
state: 'closed',
state_reason: 'not_planned'
});
# Handle insufficient permissions
- name: Comment on insufficient permissions
if: steps.assign_license.outputs.result == 'insufficient_permissions'
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: '🔒 Insufficient permissions to assign Copilot license.\n\nThe GitHub App requires the following permissions:\n- `members: read`\n- `copilot_business: write`\n\nPlease contact the organization administrator to configure the GitHub App correctly.\n\nThis issue will remain open for administrator review.'
});
- name: Remove copilot-request label after insufficient permissions
if: steps.assign_license.outputs.result == 'insufficient_permissions'
uses: ./.github/actions/remove-label
with:
github-token: ${{ github.token }}
label-name: 'copilot-request'
issue-number: ${{ github.event.issue.number }}
owner: ${{ github.repository_owner }}
repo: ${{ github.event.repository.name }}
# This step runs on any other failure
- name: Comment GitHub Issue on failure
if: steps.assign_license.outputs.result == 'failure'
uses: actions/github-script@v7
with:
script: |
const rawErrorMessage = '${{ steps.assign_license.outputs.error_message }}';
const safeErrorMessage = rawErrorMessage || 'Unknown error';
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: `❌ Failed to assign the GitHub Copilot license.\n\n**Error:** ${safeErrorMessage}\n\nPlease contact the organization administrator for assistance.\n\nThis issue will remain open for administrator review.`
});
- name: Remove copilot-request label after failure
if: steps.assign_license.outputs.result == 'failure'
uses: ./.github/actions/remove-label
with:
github-token: ${{ github.token }}
label-name: 'copilot-request'
issue-number: ${{ github.event.issue.number }}
owner: ${{ github.repository_owner }}
repo: ${{ github.event.repository.name }}
remove_github_copilot_license:
name: Remove GitHub Copilot License
runs-on: ubuntu-latest
if: github.event.label.name == 'copilot-remove'
steps:
# Checkout repository to access local composite actions
- name: Checkout repository
uses: actions/checkout@v4
- name: Get Token
id: get_workflow_token
uses: actions/create-github-app-token@v1
with:
app-id: ${{ secrets.APPLICATION_ID }}
private-key: ${{ secrets.APPLICATION_PRIVATE_KEY }}
# Validate that the requester is an organization member
- name: Validate organization membership
id: validate_membership
uses: ./.github/actions/validate-org-membership
with:
github-token: ${{ steps.get_workflow_token.outputs.token }}
username: ${{ github.event.sender.login }}
org: ${{ github.repository_owner }}
# Comment if user is not an org member
- name: Comment on invalid membership for license removal
if: steps.validate_membership.outputs.status == 'invalid'
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: '❌ You must be a member of the **${{ github.repository_owner }}** organization to request removal of a GitHub Copilot license.\n\nPlease contact the organization administrator if you believe this is an error.'
});
- name: Remove copilot-remove label after invalid membership
if: steps.validate_membership.outputs.status == 'invalid'
uses: ./.github/actions/remove-label
with:
github-token: ${{ github.token }}
label-name: 'copilot-remove'
issue-number: ${{ github.event.issue.number }}
owner: ${{ github.repository_owner }}
repo: ${{ github.event.repository.name }}
- name: Close issue after invalid membership for removal
if: steps.validate_membership.outputs.status == 'invalid'
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
state: 'closed',
state_reason: 'not_planned'
});
# Comment if validation encountered an internal error
- name: Comment on validation error for license removal
if: steps.validate_membership.outputs.status == 'error'
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: '⚠️ Copilot license removal validation failed due to a workflow configuration error.\n\nPlease contact a repository administrator to investigate.\n\nThis issue will remain open for administrator review.'
});
- name: Remove GitHub Copilot license
if: steps.validate_membership.outputs.status == 'valid'
id: remove_license
uses: actions/github-script@v7
with:
github-token: ${{ steps.get_workflow_token.outputs.token }}
result-encoding: string
script: |
const username = context.payload.sender.login;
console.log(`Attempting to remove Copilot license from: ${username}`);
try {
await github.rest.copilot.removeCopilotSeatsForUsers({
org: context.repo.owner,
selected_usernames: [username]
});
console.log(`Successfully removed license from ${username}`);
core.summary.addRaw(`✅ Successfully removed GitHub Copilot license from @${username}`, true);
await core.summary.write();
return 'success';
} catch (error) {
console.error(`Error removing license from ${username}:`, error.message);
if (error.status === 404) {
core.summary.addRaw(`ℹ️ User @${username} does not have an active Copilot license`, true);
await core.summary.write();
return 'no_license';
} else if (error.status === 403) {
return 'insufficient_permissions';
}
// Sanitize error message to prevent injection attacks (use the same JSON.stringify-based approach as elsewhere)
const rawMessage = String(error && error.message ? error.message : 'Unknown error');
const sanitizedError = JSON.stringify(rawMessage)
.slice(1, -1) // remove surrounding quotes added by JSON.stringify
.substring(0, 500);
core.setOutput('error_message', sanitizedError);
return 'failure';
}
- name: Comment and close GitHub Issue on success
if: steps.remove_license.outputs.result == 'success'
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: '✅ Successfully removed the GitHub Copilot license.'
});
- name: Remove copilot-remove label after successful removal
if: steps.remove_license.outputs.result == 'success'
uses: ./.github/actions/remove-label
with:
github-token: ${{ github.token }}
label-name: 'copilot-remove'
issue-number: ${{ github.event.issue.number }}
owner: ${{ github.repository_owner }}
repo: ${{ github.event.repository.name }}
- name: Close issue after successful removal
if: steps.remove_license.outputs.result == 'success'
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
state: 'closed'
});
# Handle user with no license
- name: Comment on no license
if: steps.remove_license.outputs.result == 'no_license'
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: 'ℹ️ You do not currently have a GitHub Copilot license assigned. No action was needed.'
});
- name: Remove copilot-remove label after no license
if: steps.remove_license.outputs.result == 'no_license'
uses: ./.github/actions/remove-label
with:
github-token: ${{ github.token }}
label-name: 'copilot-remove'
issue-number: ${{ github.event.issue.number }}
owner: ${{ github.repository_owner }}
repo: ${{ github.event.repository.name }}
- name: Close issue after no license
if: steps.remove_license.outputs.result == 'no_license'
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
state: 'closed'
});
# Handle insufficient permissions
- name: Comment on insufficient permissions for removal
if: steps.remove_license.outputs.result == 'insufficient_permissions'
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: '🔒 Insufficient permissions to remove Copilot license.\n\nThe GitHub App requires the following permissions:\n- `members: read`\n- `copilot_business: write`\n\nPlease contact the organization administrator to configure the GitHub App correctly.\n\nThis issue will remain open for administrator review.'
});
- name: Remove copilot-remove label after insufficient permissions
if: steps.remove_license.outputs.result == 'insufficient_permissions'
uses: ./.github/actions/remove-label
with:
github-token: ${{ github.token }}
label-name: 'copilot-remove'
issue-number: ${{ github.event.issue.number }}
owner: ${{ github.repository_owner }}
repo: ${{ github.event.repository.name }}
- name: Comment GitHub Issue on failure
if: steps.remove_license.outputs.result == 'failure'
uses: actions/github-script@v7
with:
script: |
function escapeMarkdown(text) {
if (!text) return '';
return String(text).replace(/[\\`*_{}\[\]()#+\-.!$]/g, '\\$&');
}
const rawErrorMessage = '${{ steps.remove_license.outputs.error_message }}';
const safeErrorMessage = escapeMarkdown(rawErrorMessage) || 'Unknown error';
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: `❌ Failed to remove the GitHub Copilot license.\n\n**Error:** ${safeErrorMessage}\n\nPlease contact the organization administrator for assistance.\n\nThis issue will remain open for administrator review.`
});
- name: Remove copilot-remove label after failure
if: steps.remove_license.outputs.result == 'failure'
uses: ./.github/actions/remove-label
with:
github-token: ${{ github.token }}
label-name: 'copilot-remove'
issue-number: ${{ github.event.issue.number }}
owner: ${{ github.repository_owner }}
repo: ${{ github.event.repository.name }}