@@ -8,21 +8,64 @@ import enterpriseMockedResponse_seats from '../assets/enterprise_response_sample
8
8
export const getSeatsApi = async ( ) : Promise < Seat [ ] > => {
9
9
const perPage = 50 ;
10
10
let page = 1 ;
11
+ let seatUrl = `https://api.github.com/` ;
11
12
let seatsData : Seat [ ] = [ ] ;
12
13
13
14
let response ;
14
- if ( process . env . VUE_APP_SCOPE !== "organization" ) {
15
- // when the scope is not organization, return seatsData,by default it will return empty array
16
- return seatsData ;
17
- }
18
- else {
19
- if ( process . env . VUE_APP_MOCKED_DATA === "true" ) {
20
- response = organizationMockedResponse_seats ;
15
+
16
+
17
+ if ( process . env . VUE_APP_MOCKED_DATA === "true" ) {
18
+ console . log ( "Using mock data. Check VUE_APP_MOCKED_DATA variable." ) ;
19
+ if ( process . env . VUE_APP_SCOPE === "organization" ) {
20
+ response = organizationMockedResponse_seats ;
21
+ }
22
+ else if ( process . env . VUE_APP_SCOPE === "enterprise" ) {
23
+ response = enterpriseMockedResponse_seats ;
24
+ }
25
+ else {
26
+ throw new Error ( `Invalid VUE_APP_SCOPE value: ${ process . env . VUE_APP_SCOPE } . Expected "organization" or "enterprise".` ) ;
27
+ }
21
28
seatsData = seatsData . concat ( response . seats . map ( ( item : any ) => new Seat ( item ) ) ) ;
22
- }
23
- else if ( process . env . VUE_APP_MOCKED_DATA === "false" ) {
29
+ return seatsData ;
30
+ }
31
+ else {
32
+ // if VUE_APP_GITHUB_TOKEN is not set, throw an error
33
+ if ( ! process . env . VUE_APP_GITHUB_TOKEN ) {
34
+ throw new Error ( "VUE_APP_GITHUB_TOKEN environment variable is not set." ) ;
35
+ return seatsData ;
36
+ }
37
+ else if ( process . env . VUE_APP_SCOPE === "organization" ) {
38
+ seatUrl = seatUrl + `orgs/${ process . env . VUE_APP_GITHUB_ORG } /copilot/billing/seats` ;
39
+ }
40
+ else if ( process . env . VUE_APP_SCOPE === "enterprise" ) {
41
+ seatUrl = seatUrl + `enterprises/${ process . env . VUE_APP_GITHUB_ENT } /copilot/billing/seats` ;
42
+ }
43
+ else {
44
+ throw new Error ( `Invalid VUE_APP_SCOPE value: ${ process . env . VUE_APP_SCOPE } . Expected "organization" or "enterprise".` ) ;
45
+ return seatsData ;
46
+ }
47
+
24
48
// Fetch the first page to get the total number of seats
25
- response = await axios . get ( `https://api.github.com/orgs/${ process . env . VUE_APP_GITHUB_ORG } /copilot/billing/seats` , {
49
+ response = await axios . get ( seatUrl , {
50
+ headers : {
51
+ Accept : "application/vnd.github+json" ,
52
+ Authorization : `Bearer ${ process . env . VUE_APP_GITHUB_TOKEN } ` ,
53
+ "X-GitHub-Api-Version" : "2022-11-28" ,
54
+ } ,
55
+ params : {
56
+ per_page : perPage ,
57
+ page : page
58
+ }
59
+ } ) ;
60
+
61
+ seatsData = seatsData . concat ( response . data . seats . map ( ( item : any ) => new Seat ( item ) ) ) ;
62
+ // Calculate the total pages
63
+ const totalSeats = response . data . total_seats ;
64
+ const totalPages = Math . ceil ( totalSeats / perPage ) ;
65
+
66
+ // Fetch the remaining pages
67
+ for ( page = 2 ; page <= totalPages ; page ++ ) {
68
+ response = await axios . get ( seatUrl , {
26
69
headers : {
27
70
Accept : "application/vnd.github+json" ,
28
71
Authorization : `Bearer ${ process . env . VUE_APP_GITHUB_TOKEN } ` ,
@@ -33,29 +76,8 @@ export const getSeatsApi = async (): Promise<Seat[]> => {
33
76
page : page
34
77
}
35
78
} ) ;
36
-
37
79
seatsData = seatsData . concat ( response . data . seats . map ( ( item : any ) => new Seat ( item ) ) ) ;
38
- // Calculate the total pages
39
- const totalSeats = response . data . total_seats ;
40
- const totalPages = Math . ceil ( totalSeats / perPage ) ;
41
-
42
- // Fetch the remaining pages
43
- for ( page = 2 ; page <= totalPages ; page ++ ) {
44
- response = await axios . get ( `https://api.github.com/orgs/${ process . env . VUE_APP_GITHUB_ORG } /copilot/billing/seats` , {
45
- headers : {
46
- Accept : "application/vnd.github+json" ,
47
- Authorization : `Bearer ${ process . env . VUE_APP_GITHUB_TOKEN } ` ,
48
- "X-GitHub-Api-Version" : "2022-11-28" ,
49
- } ,
50
- params : {
51
- per_page : perPage ,
52
- page : page
53
- }
54
- } ) ;
55
-
56
- seatsData = seatsData . concat ( response . data . seats . map ( ( item : any ) => new Seat ( item ) ) ) ;
57
- } //end of else if (process.env.VUE_APP_MOCKED_DATA === "false")
58
- } //end of else if (process.env.VUE_APP_SCOPE !== "organization")
59
- return seatsData ;
60
- }
61
- }
80
+ }
81
+ return seatsData ;
82
+ }
83
+ }
0 commit comments