|
1 | 1 | // TypeScript
|
2 | 2 | import axios from "axios";
|
3 | 3 | import { Seat } from "../model/Seat";
|
| 4 | +import config from '../config'; |
4 | 5 |
|
5 | 6 | import organizationMockedResponse_seats from '../assets/organization_response_sample_seats.json';
|
6 | 7 | import enterpriseMockedResponse_seats from '../assets/enterprise_response_sample_seats.json';
|
7 | 8 |
|
8 | 9 | export const getSeatsApi = async (): Promise<Seat[]> => {
|
9 | 10 | const perPage = 50;
|
10 | 11 | let page = 1;
|
11 |
| - let seatUrl = `https://api.github.com/`; |
12 | 12 | let seatsData: Seat[] = [];
|
13 | 13 |
|
14 | 14 | let response;
|
15 | 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 |
| - } |
28 |
| - seatsData = seatsData.concat(response.seats.map((item: any) => new Seat(item))); |
29 |
| - return seatsData; |
| 16 | + if (config.scope.type !== "organization") { |
| 17 | + // when the scope is not organization, return seatsData,by default it will return empty array |
| 18 | + return seatsData; |
30 | 19 | }
|
31 | 20 | 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`; |
| 21 | + if (config.mockedData) { |
| 22 | + response = organizationMockedResponse_seats; |
| 23 | + seatsData = seatsData.concat(response.seats.map((item: any) => new Seat(item))); |
42 | 24 | }
|
43 | 25 | else {
|
44 |
| - throw new Error(`Invalid VUE_APP_SCOPE value: ${process.env.VUE_APP_SCOPE}. Expected "organization" or "enterprise".`); |
45 |
| - return seatsData; |
46 |
| - } |
47 |
| - |
48 |
| - // Fetch the first page to get the total number of 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 | + // Fetch the first page to get the total number of seats |
| 27 | + response = await axios.get(`${config.github.apiUrl}/copilot/billing/seats`, { |
69 | 28 | headers: {
|
70 | 29 | Accept: "application/vnd.github+json",
|
71 |
| - Authorization: `Bearer ${process.env.VUE_APP_GITHUB_TOKEN}`, |
| 30 | + Authorization: `Bearer ${config.github.token}`, |
72 | 31 | "X-GitHub-Api-Version": "2022-11-28",
|
73 | 32 | },
|
74 | 33 | params: {
|
75 | 34 | per_page: perPage,
|
76 | 35 | page: page
|
77 | 36 | }
|
78 | 37 | });
|
| 38 | + |
79 | 39 | seatsData = seatsData.concat(response.data.seats.map((item: any) => new Seat(item)));
|
| 40 | + |
| 41 | + // Calculate the total pages |
| 42 | + const totalSeats = response.data.total_seats; |
| 43 | + const totalPages = Math.ceil(totalSeats / perPage); |
| 44 | + |
| 45 | + // Fetch the remaining pages |
| 46 | + for (page = 2; page <= totalPages; page++) { |
| 47 | + response = await axios.get(`${config.github.apiUrl}/copilot/billing/seats`, { |
| 48 | + headers: { |
| 49 | + Accept: "application/vnd.github+json", |
| 50 | + Authorization: `Bearer ${config.github.token}`, |
| 51 | + "X-GitHub-Api-Version": "2022-11-28", |
| 52 | + }, |
| 53 | + params: { |
| 54 | + per_page: perPage, |
| 55 | + page: page |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + seatsData = seatsData.concat(response.data.seats.map((item: any) => new Seat(item))); |
| 60 | + } |
80 | 61 | }
|
81 | 62 | return seatsData;
|
82 |
| - } |
83 | 63 | }
|
| 64 | +} |
0 commit comments