-
Notifications
You must be signed in to change notification settings - Fork 0
/
AccountUsage.gs
174 lines (153 loc) · 4.29 KB
/
AccountUsage.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* Get high-level account information
*
* IMPORTANT
* Assumes the Google Sheet it is sending information to is named "Usage History"
*/
/**
* Makes a POST request to a GraphQL API endpoint.
*
* @param {string} url - The URL of the API endpoint.
* @param {string} query - The GraphQL query.
* @param {Object} variables - The variables for the GraphQL query.
* @returns {Object} The parsed JSON response.
*/
function callApi(url, query, variables) {
let graphql = JSON.stringify({
query: query,
variables: variables,
});
let params = {
method: "POST",
payload: graphql,
headers: {
"Content-Type": "application/json",
Authorization: "ApiKey " + PropertiesService.getScriptProperties().getProperty("apiKey"),
},
};
var responseText = UrlFetchApp.fetch(url, params).getContentText();
return JSON.parse(responseText);
}
/**
* Makes a request to the 'findings-view' API endpoint.
*
* @param {string} query - The GraphQL query.
* @param {Object} variables - The variables for the GraphQL query.
* @returns {Object} The filters data from the response.
*/
function getFindingsData(query, variables) {
const url = 'https://api.boostsecurity.io/findings-view/graphql';
let response = callApi(url, query, variables);
return response.data.findings.filters;
}
/**
* Fetches organization data from the API.
*
* @returns {Array} An array of organization data.
*/
function getOrganizationData() {
const url = 'https://api.boostsecurity.io/ci-provisioning/graphql';
const query = `query ($search: String) {
organizations(search: $search) {
edges {
node {
repositories {
totalCount
needAttentionCount
}
}
}
}
}`;
let response = callApi(url, query, { "search": "" });
let arrayOfOrganizations = response.data.organizations.edges.map(f => f.node);
return arrayOfOrganizations.map(org => ({
totalCount: org.repositories.totalCount,
needAttentionCount: org.repositories.needAttentionCount,
}));
}
/**
* Fetches finding data from the API.
*
* @returns {Object} An object containing findingsCount and violationsCount.
*/
function getFindingData() {
const query = `query (
$filters: FindingsFiltersSchema,
$first: Int,
) {
findings(
filters: $filters,
first: $first,
) {
filters {
isViolation { value count }
}
}
}`;
let filtersData = getFindingsData(query, {
"filters": {
"confidences": ["HIGH"],
"severities": ["CRITICAL"]
},
"first": 10
});
let isViolationData = filtersData.isViolation;
let findingsCount = isViolationData.filter(f => !f.value).map(f => f.count)[0];
let violationsCount = isViolationData.filter(f => f.value).map(f => f.count)[0];
return { findingsCount, violationsCount };
}
/**
* Fetches rule group data from the API.
*
* @returns {number} The rule group count.
*/
function getRuleGroupData() {
const query = `query (
$filters: FindingsFiltersSchema,
$first: Int,
) {
findings(
filters: $filters,
first: $first,
) {
filters {
ruleGroup { value }
}
}
}`;
let filtersData = getFindingsData(query, {
"filters": {
"confidences": ["HIGH"],
"severities": ["CRITICAL"]
},
"first": 10
});
let ruleGroupCount = filtersData.ruleGroup.length;
return ruleGroupCount;
}
/**
* Fetches data from various API endpoints and updates the 'Usage History' sheet.
*
* @param {string} companyName - The name of the company.
*/
function updateCustomerUsageRegister(companyName) {
let orgData = getOrganizationData();
let findingData = getFindingData();
let ruleGroupCount = getRuleGroupData();
let totalOrgCount = orgData.length;
let totalCountSum = '=SUM(' + orgData.map(d => d.totalCount).join(",") + ')';
let needAttentionCountSum = '=SUM(' + orgData.map(d => d.needAttentionCount).join(",") + ')';
let row = [
Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "yyyy-MM-dd"),
companyName,
totalOrgCount,
totalCountSum,
needAttentionCountSum,
ruleGroupCount,
findingData.findingsCount,
findingData.violationsCount
];
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Usage History");
sheet.appendRow(row);
}