@@ -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
1919module . 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.
453466class 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 ;
0 commit comments