-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtravis.js
153 lines (132 loc) · 4.19 KB
/
travis.js
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
const travisLogoUrl = 'https://travis-ci.com/images/logos/TravisCI-Full-Color.png';
const branchIconUrl = 'https://upload.wikimedia.org/wikipedia/commons/thumb/e/ed/Octicons-git-branch.svg/480px-Octicons-git-branch.svg.png';
const pullRequestIconUrl = 'https://upload.wikimedia.org/wikipedia/commons/thumb/8/87/Octicons-git-pull-request.svg/768px-Octicons-git-pull-request.svg.png';
const travisLogoRequest = new Request(travisLogoUrl);
const travisLogo = await travisLogoRequest.loadImage();
const branchIconRequest = new Request(branchIconUrl);
const branchIcon = await branchIconRequest.loadImage();
const prIconRequest = new Request(pullRequestIconUrl);
const prIcon = await prIconRequest.loadImage();
const requestLimits = {
small: 5,
medium: 8,
large: 20,
};
const buildStatus = {
passed: '🟢',
cancelled: '⚪️',
failed: '🔴',
unknown: '❓'
};
const widget = new ListWidget()
try {
const params = JSON.parse(args.widgetParameter);
if (checkRequiredParams(params)) {
await buildWidget(params)
} else {
displayMessage('Missing config data, repository and token required.');
}
} catch (e) {
displayMessage(`Invalid config: ${e.message}`);
}
if (!config.runsInWidget) {
switch (config.widgetFamily) {
case 'small':
await widget.presentSmall();
break;
case 'medium':
await widget.presentMedium();
break;
case 'large':
await widget.presentLarge();
break;
default:
await widget.presentLarge();
}
}
Script.setWidget(widget)
Script.complete()
async function buildWidget(params) {
widget.setPadding(10,10,10,10)
widget.spacing = 3;
await displayHeader(params.repository);
const buildData = await fetchBuilds(params.repository, params.token);
if (buildData) {
const buildSummary = listBuilds(buildData);
for (const build of buildSummary) {
displayBuildInfo(build);
}
} else {
displayMessage('No data!');
}
}
function listBuilds(jsonData) {
const { builds } = jsonData;
if (!builds) {
return [];
} else {
return builds.reduce((acc, curr) => {
const { state, pull_request_number: prId, repository, commit, branch } = curr;
const { slug: repoSlug } = repository;
const { message: commitMessage } = commit;
const { name: branchName } = branch;
acc.push({ state, repoSlug, commitMessage, branchName, prId});
return acc;
}, []);
}
}
async function fetchBuilds(repo, accessToken) {
const uriEncodedRepo = encodeURIComponent(repo);
const baseUrl = "https://api.travis-ci.com";
const buildsUrl = `${baseUrl}/repo/${uriEncodedRepo}/builds?limit=${requestLimits[config.widgetFamily] || 5}`;
const req = new Request(buildsUrl);
req.headers = {
'Travis-API-Version': '3',
'User-Agent': 'ci-widget',
'Authorization': `token ${accessToken}`
};
return await req.loadJSON();
}
async function displayHeader(repoName) {
const titleFontSize = 12;
const header = widget.addStack();
if (repoName) {
const repoText = header.addText(repoName);
repoText.font = Font.mediumRoundedSystemFont(titleFontSize);
}
header.addSpacer();
const logoImage = header.addImage(travisLogo)
logoImage.imageSize = new Size(40, 12)
}
function displayBuildInfo(build) {
const branchFontSize = 10;
const iconSize = 8;
const infoStack = widget.addStack();
let typeIcon;
if (build.prId) {
typeIcon = infoStack.addImage(prIcon);
} else {
typeIcon = infoStack.addImage(branchIcon);
}
typeIcon.imageSize = new Size(12, 12)
const textWidget = infoStack.addText(build.branchName);
textWidget.font = Font.mediumRoundedSystemFont(branchFontSize);
infoStack.addSpacer();
const buildState = buildStatus[build.state] || buildStatus.unknown;
const statusWidget = infoStack.addText(buildState);
statusWidget.font = Font.mediumRoundedSystemFont(iconSize);
}
function displayMessage(messageText) {
const messageFontSize = 10;
const textWidget = widget.addText(messageText);
textWidget.font = Font.mediumRoundedSystemFont(messageFontSize);
}
function checkRequiredParams(params) {
if (!params) {
return false;
}
if ('token' in params && 'repository' in params) {
return true;
}
return false;
}