-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtriggerLighthouse.js
More file actions
189 lines (163 loc) · 4.77 KB
/
Copy pathtriggerLighthouse.js
File metadata and controls
189 lines (163 loc) · 4.77 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import get from 'lodash.get';
import fetch from './fetch';
import LighthouseCheckError from './LighthouseCheckError';
import {
API_PAGES_PATH,
API_QUEUE_ITEMS_PATH,
API_URL,
DEFAULT_TAG,
NAME,
SOURCE_GITHUB_ACTION,
SOURCE_CIRCLECI_ORB,
SUCCESS_CODE_GENERIC,
TRIGGER_TYPE
} from './constants';
import {
ERROR_GENERIC,
ERROR_NO_RESULTS,
ERROR_NO_URLS,
ERROR_UNAUTHORIZED,
ERROR_QUEUE_MAX_USED_DAY
} from './errorCodes';
export default async ({
apiToken,
device,
isGitHubAction,
isOrb,
tag,
urls = [],
verbose = true
}) => {
try {
let apiTokens = urls;
// if urls was not provided - run on all pages
if (!Array.isArray(urls) || !urls.length) {
if (verbose) {
console.log(`${NAME}:`, 'Fetching URLs from account.');
}
const pagesResponse = await fetch(`${API_URL}${API_PAGES_PATH}`, {
method: 'get',
headers: {
Authorization: apiToken,
'Content-Type': 'application/json'
}
});
const pagesJson = await pagesResponse.json();
if (pagesJson.status >= 400) {
const errorMessage = `Account wasn't found for the provided API token.`;
if (verbose) {
console.log(`${NAME}:`, errorMessage);
}
throw new LighthouseCheckError(errorMessage, {
code: ERROR_UNAUTHORIZED
});
}
const pages = get(pagesJson, 'data.page', []);
if (!pages.length) {
const errorMessage = 'No URLs were found for this account.';
if (verbose) {
console.log(`${NAME}:`, errorMessage);
}
throw new LighthouseCheckError(errorMessage, {
code: ERROR_NO_URLS
});
}
apiTokens = pages.map(current => current.apiToken);
}
if (verbose) {
console.log(`${NAME}:`, 'Enqueueing Lighthouse audits.');
}
// pass in the source of the queued item for tracking
let source = 'api';
if (isGitHubAction) {
source = SOURCE_GITHUB_ACTION;
} else if (isOrb) {
source = SOURCE_CIRCLECI_ORB;
}
// enqueue urls for Lighthouse audits
const queueItemsResponse = await fetch(
`${API_URL}${API_QUEUE_ITEMS_PATH}`,
{
method: 'post',
headers: {
Authorization: apiToken,
'Content-Type': 'application/json'
},
body: JSON.stringify({
device,
tag: tag || DEFAULT_TAG,
pages: apiTokens.join(),
source,
type: TRIGGER_TYPE
})
}
);
const queueItemsJson = await queueItemsResponse.json();
const queue = get(queueItemsJson, 'data.queue');
// if no results
if (!queue.results.length) {
const errorMessage = 'No results.';
if (verbose) {
console.log(`${NAME}:`, errorMessage);
}
throw new LighthouseCheckError(errorMessage, {
code: ERROR_NO_RESULTS
});
}
// if the API responded with error/s, set a message to be used later
let apiErrorMessage;
if (queue.errors) {
apiErrorMessage = `${NAME}: Below is the API response for attempted URLs as an array.`;
}
// if all urls failed to be enqueued...
if (queue.errors === queue.results.length) {
const errorCode = queue.results[0].code;
const errorMessage =
errorCode === ERROR_QUEUE_MAX_USED_DAY
? queue.results[0].message
: 'All URLs failed to be enqueued.';
if (verbose) {
console.log(`${NAME}:`, errorMessage);
if (apiErrorMessage) {
console.log(apiErrorMessage, queue.results);
}
}
throw new LighthouseCheckError(errorMessage, {
code: errorCode,
data: queue.results
});
}
// if only some urls succeeded to be enqueued...
const successResultLength = queue.results.length - queue.errors;
const message =
successResultLength < queue.results.length
? `Only ${
successResultLength > 1 ? 'some' : 'one'
} of your account URLs were enqueued. Typically this occurs when daily limit has been met for a given URL. Check your account limits online.`
: `${queue.results.length} ${
queue.results.length > 1 ? 'URLs' : 'URL'
} successfully enqueued for Lighthouse. Visit dashboard for results.`;
if (verbose) {
console.log(`${NAME}:`, message);
if (apiErrorMessage) {
console.log(apiErrorMessage, queue.results);
}
}
// success
return {
code: SUCCESS_CODE_GENERIC,
data: queue.results,
message
};
} catch (error) {
const result = {
code: error.code || ERROR_GENERIC,
error
};
// if an error occurred but we still have data (typically if only some URLs failed)
if (error.data) {
result.data = error.data;
}
return result;
}
};