Skip to content

Commit bf6bbd6

Browse files
authored
Merge pull request #28 from github/add-copilot-seat-api
Using Copilot Seat API to check if authors have license assigned
2 parents a749101 + 899b480 commit bf6bbd6

File tree

5 files changed

+65
-39
lines changed

5 files changed

+65
-39
lines changed

.env.example

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ LOG_LEVEL=debug
99
LANGUAGE_API_ENDPOINT=
1010
LANGUAGE_API_KEY=
1111
APPLICATIONINSIGHTS_CONNECTION_STRING=
12-
DATABASE_CONNECTION_STRING=
12+
DATABASE_CONNECTION_STRING=
13+
14+
# Set a value if you want to check whether a Copilot license has been assigned to a user before opening the survey issue. Leave empty if you don't.
15+
VALIDATE_SEAT_ASSIGNMENT=true

app.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ default_permissions:
7676
# https://developer.github.com/v3/apps/permissions/#metadata-permissions
7777
metadata: read
7878

79+
# Organization permissions for "GitHub Copilot Business"
80+
# https://docs.github.com/en/rest/authentication/permissions-required-for-github-apps?apiVersion=2022-11-28#organization-permissions-for-github-copilot-business
81+
organization_copilot_seat_management: write
82+
7983
# Retrieve Pages statuses, configuration, and builds, as well as create new builds.
8084
# https://developer.github.com/v3/apps/permissions/#permission-on-pages
8185
# pages: read

index.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const {
1313
TextAnalysisClient,
1414
AzureKeyCredential,
1515
} = require("@azure/ai-language-text");
16-
const { LANGUAGE_API_KEY, LANGUAGE_API_ENDPOINT, DATABASE_CONNECTION_STRING } =
16+
const { LANGUAGE_API_KEY, LANGUAGE_API_ENDPOINT, DATABASE_CONNECTION_STRING, VALIDATE_SEAT_ASSIGNMENT, APPLICATIONINSIGHTS_CONNECTION_STRING } =
1717
process.env;
1818

1919
module.exports = (app) => {
@@ -86,18 +86,31 @@ module.exports = (app) => {
8686
// display the body for the issue
8787
app.log.info(fileContent);
8888

89-
// create an issue using fileContent as body if pr_author is included in copilotSeats
90-
try {
91-
await context.octokit.issues.create({
92-
owner: organization_name,
93-
repo: context.payload.repository.name,
94-
title: "Copilot Usage - PR#" + pr_number.toString(),
95-
body: fileContent,
96-
assignee: context.payload.pull_request.user.login,
97-
});
98-
} catch (err) {
99-
app.log.error(err);
100-
appInsights.trackException({ exception: err });
89+
// create an issue using fileContent as body if pr_author is included in copilotSeats using Copilot Seat Billing api
90+
let copilotSeats = await context.octokit.request(
91+
"GET /orgs/{org}/copilot/billing/seats",
92+
{
93+
org: organization_name,
94+
headers: {
95+
'X-GitHub-Api-Version': '2022-11-28'
96+
}
97+
}
98+
);
99+
let copilotSeatUsers = copilotSeats.data.seats;
100+
101+
if ( (Boolean(VALIDATE_SEAT_ASSIGNMENT) && copilotSeatUsers.some(user => user.assignee.login == pr_author)) || !Boolean(VALIDATE_SEAT_ASSIGNMENT)) {
102+
try {
103+
await context.octokit.issues.create({
104+
owner: organization_name,
105+
repo: context.payload.repository.name,
106+
title: "Copilot Usage - PR#" + pr_number.toString(),
107+
body: fileContent,
108+
assignee: context.payload.pull_request.user.login,
109+
});
110+
} catch (err) {
111+
app.log.error(err);
112+
appInsights.trackException({ exception: err });
113+
}
101114
}
102115
});
103116

@@ -452,7 +465,7 @@ module.exports = (app) => {
452465
// Define class for app insights. If no instrumentation key is provided, then no app insights will be used.
453466
class AppInsights {
454467
constructor() {
455-
if (process.env.APPLICATIONINSIGHTS_CONNECTION_STRING) {
468+
if (APPLICATIONINSIGHTS_CONNECTION_STRING) {
456469
this.appInsights = require("applicationinsights");
457470
this.appInsights.setup().start();
458471
this.appIClient = this.appInsights.defaultClient;

package-lock.json

Lines changed: 23 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/index.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,13 @@ describe("My Probot app", () => {
8181
expect(body).toMatchObject(expected_issue);
8282
return true;
8383
})
84-
.reply(200);
84+
.reply(200)
85+
86+
// Mock the call to the seats billing API
87+
.get("/orgs/mageroni/copilot/billing/seats")
88+
.reply(200, {
89+
seats: [{"assignee":{"login": "mageroni"}}]
90+
});
8591

8692
// Receive a webhook event
8793
await probot.receive({ name: "pull_request", payload : payload_pr_closed });

0 commit comments

Comments
 (0)