Skip to content

Commit

Permalink
Merge pull request #28 from github/add-copilot-seat-api
Browse files Browse the repository at this point in the history
Using Copilot Seat API to check if authors have license assigned
  • Loading branch information
mageroni committed Apr 5, 2024
2 parents a749101 + 899b480 commit bf6bbd6
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 39 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ LOG_LEVEL=debug
LANGUAGE_API_ENDPOINT=
LANGUAGE_API_KEY=
APPLICATIONINSIGHTS_CONNECTION_STRING=
DATABASE_CONNECTION_STRING=
DATABASE_CONNECTION_STRING=

# 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.
VALIDATE_SEAT_ASSIGNMENT=true
4 changes: 4 additions & 0 deletions app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ default_permissions:
# https://developer.github.com/v3/apps/permissions/#metadata-permissions
metadata: read

# Organization permissions for "GitHub Copilot Business"
# https://docs.github.com/en/rest/authentication/permissions-required-for-github-apps?apiVersion=2022-11-28#organization-permissions-for-github-copilot-business
organization_copilot_seat_management: write

# Retrieve Pages statuses, configuration, and builds, as well as create new builds.
# https://developer.github.com/v3/apps/permissions/#permission-on-pages
# pages: read
Expand Down
41 changes: 27 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const {
TextAnalysisClient,
AzureKeyCredential,
} = require("@azure/ai-language-text");
const { LANGUAGE_API_KEY, LANGUAGE_API_ENDPOINT, DATABASE_CONNECTION_STRING } =
const { LANGUAGE_API_KEY, LANGUAGE_API_ENDPOINT, DATABASE_CONNECTION_STRING, VALIDATE_SEAT_ASSIGNMENT, APPLICATIONINSIGHTS_CONNECTION_STRING } =
process.env;

module.exports = (app) => {
Expand Down Expand Up @@ -86,18 +86,31 @@ module.exports = (app) => {
// display the body for the issue
app.log.info(fileContent);

// create an issue using fileContent as body if pr_author is included in copilotSeats
try {
await context.octokit.issues.create({
owner: organization_name,
repo: context.payload.repository.name,
title: "Copilot Usage - PR#" + pr_number.toString(),
body: fileContent,
assignee: context.payload.pull_request.user.login,
});
} catch (err) {
app.log.error(err);
appInsights.trackException({ exception: err });
// create an issue using fileContent as body if pr_author is included in copilotSeats using Copilot Seat Billing api
let copilotSeats = await context.octokit.request(
"GET /orgs/{org}/copilot/billing/seats",
{
org: organization_name,
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
}
);
let copilotSeatUsers = copilotSeats.data.seats;

if ( (Boolean(VALIDATE_SEAT_ASSIGNMENT) && copilotSeatUsers.some(user => user.assignee.login == pr_author)) || !Boolean(VALIDATE_SEAT_ASSIGNMENT)) {
try {
await context.octokit.issues.create({
owner: organization_name,
repo: context.payload.repository.name,
title: "Copilot Usage - PR#" + pr_number.toString(),
body: fileContent,
assignee: context.payload.pull_request.user.login,
});
} catch (err) {
app.log.error(err);
appInsights.trackException({ exception: err });
}
}
});

Expand Down Expand Up @@ -452,7 +465,7 @@ module.exports = (app) => {
// Define class for app insights. If no instrumentation key is provided, then no app insights will be used.
class AppInsights {
constructor() {
if (process.env.APPLICATIONINSIGHTS_CONNECTION_STRING) {
if (APPLICATIONINSIGHTS_CONNECTION_STRING) {
this.appInsights = require("applicationinsights");
this.appInsights.setup().start();
this.appIClient = this.appInsights.defaultClient;
Expand Down
46 changes: 23 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,13 @@ describe("My Probot app", () => {
expect(body).toMatchObject(expected_issue);
return true;
})
.reply(200);
.reply(200)

// Mock the call to the seats billing API
.get("/orgs/mageroni/copilot/billing/seats")
.reply(200, {
seats: [{"assignee":{"login": "mageroni"}}]
});

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

0 comments on commit bf6bbd6

Please sign in to comment.