-
Notifications
You must be signed in to change notification settings - Fork 5
/
scripts.js
150 lines (136 loc) · 5 KB
/
scripts.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
const deforganization = "project-pokemon";
const defproject = "NHSE";
const defprojurl = "https://github.com/kwsch/NHSE";
const oneMinute = 60 * 1000;
const oneHour = 60 * oneMinute;
const oneDay = 24 * oneHour;
var timedOut = true;
// make this a little more portable
var organization = getParameterByName('org');
var project = getParameterByName('proj');
var projurl = getParameterByName('projurl');
var artName = getParameterByName('proj');
var query = window.location.href;
var endq = query.split('?');
if (endq.length === 2)
{
if (endq[1].toLowerCase() == 'sysbot') //sb.ac
{
organization = 'project-pokemon';
project = 'SysBot.AnimalCrossing';
projurl = 'https://github.com/kwsch/SysBot.AnimalCrossing';
artName = 'SysBot-ACNH';
}
if (endq[1].toLowerCase() == 'sysbotextra') //sb.ac (mine)
{
organization = 'berichan';
project = 'SysBot.AnimalCrossing';
projurl = 'https://github.com/berichan/SysBot.AnimalCrossing';
artName = 'SysBot-ACNH-Extra';
}
if (endq[1].toLowerCase() == 'sysbotorders') //sb.ac-orders (mine)
{
organization = 'berichan';
project = 'SysBot.ACNHOrders';
projurl = 'https://github.com/berichan/SysBot.ACNHOrders';
artName = 'SysBot-ACNH-Orders';
}
if (endq[1].toLowerCase() == 'sv') //sb.psv
{
organization = 'berichan';
project = 'SysBot.PokemonScarletViolet';
projurl = 'https://github.com/berichan/SysBot.PokemonScarletViolet';
artName = 'SysBot-x64';
}
}
else if (organization===null || project ===null || projurl ===null)
{
organization = deforganization;
project = defproject;
projurl = defprojurl;
artName = defproject;
}
setTimeout(function() {
if (timedOut)
{
var msgString = 'The request to Azure DevOps API timed out. This may be because of a script blocker, unsupported browser, or no network connection. Please disable any blockers and try again.'
document.getElementById("loader").innerHTML = msgString;
alert(msgString);
}
}, 4000);
document.getElementById("title").innerHTML += `${project}`;
document.getElementById("loader").innerHTML += `${project}...`;
document.getElementById("errorhelp").innerHTML = `Go to the <a href="${projurl}">${project} source here.</a><br>Go directly to the <a href="https://dev.azure.com/${organization}/${project}/_build?view=runs">Pipelines here</a>.`;
try {
var request = new XMLHttpRequest();
var azureUri = `https://dev.azure.com/${organization}/${project}/_apis/build/builds?api-version=6.0`;
request.open('GET', azureUri, true);
request.onload = function () {
if (IsJsonString(this.response))
{
try
{
var data = JSON.parse(this.response);
if (request.status >= 200 && request.status < 400) {
const id = data.value[0].id;
const pc = data.value[0].definition.project.id;
const timestamp = new Date(data.value[0].finishTime);
const timediff = getBuildTimeDifferenceString(timestamp);
document.getElementById("buildTime").innerHTML =`Build no. ${id} built ${timediff}.<br>${timestamp}`;
document.getElementById("loader").innerHTML =`Click below to download the latest version of ${project}.`;
document.getElementById("getDownload").innerHTML =`<a href="https://dev.azure.com/${organization}/${pc}/_apis/build/builds/${id}/artifacts?artifactName=${artName}&api-version=7.0&%24format=zip">Download Latest ${project} Version</a>`;
timedOut = false;
} else {
document.getElementById("loader").innerHTML ='A request error occured';
timedOut = false;
}
}
catch (err) {
document.getElementById("loader").innerHTML = err.message;
timedOut = false;
}
}
else
{
document.getElementById("loader").innerHTML = 'No REST json returned. Make sure your parameters are correct!';
timedOut = false;
}
}
request.send();
}
catch(err) {
document.getElementById("loader").innerHTML = err.message;
timedOut = false;
}
function getBuildTimeDifferenceString(timestamp) {
const timestampToday = new Date();
const diffDays = Math.floor(Math.abs((timestampToday - timestamp) / oneDay));
const diffHours = Math.floor(Math.abs((timestampToday - timestamp) / oneHour)) - (diffDays * 24);
if (diffDays===0 && diffHours===0)
{
const diffMinutes = Math.floor(Math.abs((timestampToday - timestamp) / oneMinute)) - (diffDays * 24) - (diffHours * 60);
const pluralMins = diffMinutes === 1 ? '' : 's';
return `${diffMinutes} minute${pluralMins} ago`;
}
const pluralDays = diffDays === 1 ? '' : 's';
const pluralHours = diffHours === 1 ? '' : 's';
const dayString = diffDays < 1 ? '' : `${diffDays} day` + pluralDays + ', ';
return `${dayString}${diffHours} hour${pluralHours} ago`
}
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, '\\$&');
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
function IsJsonString(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}