-
Notifications
You must be signed in to change notification settings - Fork 5
180 lines (177 loc) · 8.33 KB
/
github_copilot_license_management.yml
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
name: GitHub Copilot License Management
# This workflow is triggered when an issue is labeled
on:
issues:
types:
- labeled
# This workflow uses the GitHub Token to comment on the issues and close them
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# This workflow contains two jobs: assign_github_copilot_license and remove_github_copilot_license
jobs:
assign_github_copilot_license:
name: Assign GitHub Copilot License
# This job runs on the latest version of Ubuntu
runs-on: ubuntu-latest
# This job only runs if the label added to the issue is 'copilot-request'
if: github.event.label.name == 'copilot-request'
steps:
# This step sets up Node.js version 20
- uses: actions/setup-node@v4
with:
node-version: 20
# This step installs the @octokit/action npm package
- run: npm install @octokit/action
# This step gets a GitHub App installation access token
- name: Get Token
id: get_workflow_token
uses: peter-murray/workflow-application-token-action@v3
with:
application_id: ${{ secrets.APPLICATION_ID }}
application_private_key: ${{ secrets.APPLICATION_PRIVATE_KEY }}
# This step assigns a GitHub Copilot license to the user who created the issue
- name: Assign GitHub Copilot license
id: assign_license
continue-on-error: true
uses: actions/github-script@v7
env:
GITHUB_TOKEN: ${{ steps.get_workflow_token.outputs.token }}
with:
script: |
const { Octokit } = require("@octokit/action");
const octokit = new Octokit();
const response = await octokit.request('POST /orgs/{org}/copilot/billing/selected_users', {
org: context.repo.owner,
selected_usernames: [context.payload.sender.login],
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
})
return response.status >= 200 && response.status < 300 ? 'success' : 'failure'
# If the previous step was successful, this step comments on the issue and closes it
- name: Comment and close GitHub Issue on success
if: steps.assign_license.outcome == 'success'
uses: actions/github-script@v7
with:
script: |
const { Octokit } = require("@octokit/action");
const octokit = new Octokit();
await octokit.request('POST /repos/{owner}/{repo}/issues/{issue_number}/comments', {
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Successfully assigned the GitHub Copilot license. <br> <br> Here are some resources to help you get started with GitHub Copilot: <br><ul><li><a href="https://learn.microsoft.com/training/paths/copilot">GitHub Copilot Fundamentals MS Learn Path</a></li><li><a href="https://resources.github.com/learn/pathways/copilot/essentials/essentials-of-github-copilot">Essentials of GitHub Copilot</a></li><li><a href="https://www.youtube.com/playlist?list=PLlrxD0HtieHgr23PS05FIncnih4dH9Na5">Introduction to GitHub Copilot</a></li><li><a href="https://github.com/microsoft/Mastering-GitHub-Copilot-for-Paired-Programming">Mastering GitHub Copilot for AI Paired Programming</a></li></ul>',
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
});
await octokit.request('PATCH /repos/{owner}/{repo}/issues/{issue_number}', {
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
state: 'closed',
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
});
# If the previous step failed, this step comments on the issue to notify about the failure
- name: Comment GitHub Issue on failure
if: steps.assign_license.outcome == 'failure'
uses: actions/github-script@v7
with:
script: |
const { Octokit } = require("@octokit/action");
const octokit = new Octokit();
await octokit.request('POST /repos/{owner}/{repo}/issues/{issue_number}/comments', {
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Failed assigning the GitHub Copilot license. Please contact the organization administrator.',
content: 'confused',
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
});
remove_github_copilot_license:
name: Remove GitHub Copilot License
# This job runs on the latest version of Ubuntu
runs-on: ubuntu-latest
# This job only runs if the label added to the issue is 'copilot-remove'
if: github.event.label.name == 'copilot-remove'
steps:
# This step sets up Node.js version 20
- uses: actions/setup-node@v4
with:
node-version: 20
# This step installs the @octokit/action npm package
- run: npm install @octokit/action
# This step gets a GitHub App installation access token
- name: Get Token
id: get_workflow_token
uses: peter-murray/workflow-application-token-action@v3
with:
application_id: ${{ secrets.APPLICATION_ID }}
application_private_key: ${{ secrets.APPLICATION_PRIVATE_KEY }}
# This step assigns a GitHub Copilot license to the user who created the issue
- name: Remove GitHub Copilot license
id: remove_license
continue-on-error: true
uses: actions/github-script@v7
env:
GITHUB_TOKEN: ${{ steps.get_workflow_token.outputs.token }}
with:
script: |
const { Octokit } = require("@octokit/action");
const octokit = new Octokit();
const response = await octokit.request('DELETE /orgs/{org}/copilot/billing/selected_users', {
org: context.repo.owner,
selected_usernames: [context.payload.sender.login],
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
})
return response.status >= 200 && response.status < 300 ? 'success' : 'failure'
# If the previous step was successful, this step comments on the issue and closes it
- name: Comment and close GitHub Issue on success
if: steps.remove_license.outcome == 'success'
uses: actions/github-script@v7
with:
script: |
const { Octokit } = require("@octokit/action");
const octokit = new Octokit();
await octokit.request('POST /repos/{owner}/{repo}/issues/{issue_number}/comments', {
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Successfully removed the GitHub Copilot license.',
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
});
await octokit.request('PATCH /repos/{owner}/{repo}/issues/{issue_number}', {
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
state: 'closed',
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
});
# If the previous step failed, this step comments on the issue to notify about the failure
- name: Comment GitHub Issue on failure
if: steps.remove_license.outcome == 'failure'
uses: actions/github-script@v7
with:
script: |
const { Octokit } = require("@octokit/action");
const octokit = new Octokit();
await octokit.request('POST /repos/{owner}/{repo}/issues/{issue_number}/comments', {
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Failed removing the GitHub Copilot license. Please contact the organization administrator.',
content: 'confused',
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
});