-
Notifications
You must be signed in to change notification settings - Fork 18
/
buildid_changeset.js
235 lines (205 loc) · 5.74 KB
/
buildid_changeset.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
function parseBuildID(buildID) {
return {
year: Number(buildID.substring(0, 4)),
month: Number(buildID.substring(4, 6)),
day: Number(buildID.substring(6, 8)),
hour: Number(buildID.substring(8, 10)),
minute: Number(buildID.substring(10, 12)),
second: Number(buildID.substring(12, 14)),
};
}
function compareBuildIDs(buildID1, buildID2) {
let buildObj1 = parseBuildID(buildID1);
let buildObj2 = parseBuildID(buildID2);
if (buildObj1.year > buildObj2.year) {
return 1;
} else if (buildObj1.year < buildObj2.year) {
return -1;
}
if (buildObj1.month > buildObj2.month) {
return 1;
} else if (buildObj1.month < buildObj2.month) {
return -1;
}
if (buildObj1.day > buildObj2.day) {
return 1;
} else if (buildObj1.day < buildObj2.day) {
return -1;
}
if (buildObj1.hour > buildObj2.hour) {
return 1;
} else if (buildObj1.hour < buildObj2.hour) {
return -1;
}
if (buildObj1.minute > buildObj2.minute) {
return 1;
} else if (buildObj1.minute < buildObj2.minute) {
return -1;
}
if (buildObj1.second > buildObj2.second) {
return 1;
} else if (buildObj1.second < buildObj2.second) {
return -1;
}
return 0;
}
function toTwoDigits(num) {
if (num < 10) {
return "0" + num;
}
return num;
}
function fromBuildIDtoChangeset(buildID, channel = "nightly") {
let buildObj = parseBuildID(buildID);
let dirEnd;
if (channel === "nightly") {
dirEnd = "central";
}
let directory =
"https://ftp.mozilla.org/pub/firefox/nightly/" +
buildObj.year +
"/" +
toTwoDigits(buildObj.month) +
"/" +
buildObj.year +
"-" +
toTwoDigits(buildObj.month) +
"-" +
toTwoDigits(buildObj.day) +
"-" +
toTwoDigits(buildObj.hour) +
"-" +
toTwoDigits(buildObj.minute) +
"-" +
toTwoDigits(buildObj.second) +
"-mozilla-" +
dirEnd +
"/";
return fetch(directory)
.then((response) => response.text())
.then((data) => {
let file = data.match(/firefox-\d+.0a[12].en-US.win32.txt/);
if (!file) {
file = data.match(/firefox-\d+.0a[12].en-US.linux-x86_64.txt/);
}
if (file && file.length == 1) {
return file[0];
} else {
throw new Error(
"Couldn't find *.win32.txt or *.linux-x86_64.txt file."
);
}
})
.then((file) => fetch(directory + file))
.then((response) => response.text())
.then((data) => {
let lines = data.split("\n");
lines = lines.map((line) => line.replace("\r", ""));
let buildIDfromFile = lines[0];
if (buildID != buildIDfromFile) {
throw new Error("Unexpected error: wrong build ID in directory.");
}
return lines[1];
});
}
function getRevFromChangeset(changeset, channel = "nightly") {
let re;
if (channel === "nightly") {
re = /https:\/\/hg.mozilla.org\/mozilla-central\/rev\/([A-Za-z0-9]+)/;
}
let result = re.exec(changeset);
return result[1];
}
function getChangesetDate(changeset, channel = "nightly") {
let baseURL;
if (channel === "nightly") {
baseURL = "https://hg.mozilla.org/mozilla-central";
}
return fetch(baseURL + "/json-rev/" + changeset)
.then((response) => response.json())
.then((json_rev) => new Date(json_rev["pushdate"][0] * 1000));
}
function findFirstBuildIDInSet(buildIDs, date, i = 0) {
return fromBuildIDtoChangeset(buildIDs[i])
.then((buildChangeset) =>
getChangesetDate(getRevFromChangeset(buildChangeset))
)
.then((buildDate) => {
if (buildDate >= date) {
return buildIDs[i];
} else {
return findFirstBuildIDInSet(buildIDs, date, i + 1);
}
});
}
function findFirstBuildID(date, year = null, month = null) {
// Who knows where Firefox is built, so give the build ID some slack.
let possibleDate = new Date(date.getTime());
possibleDate.setUTCHours(possibleDate.getUTCHours() - 9);
let possibleBuildID =
"" +
possibleDate.getUTCFullYear() +
toTwoDigits(possibleDate.getUTCMonth() + 1) +
toTwoDigits(possibleDate.getUTCDate()) +
toTwoDigits(possibleDate.getUTCHours()) +
toTwoDigits(possibleDate.getUTCMinutes()) +
toTwoDigits(possibleDate.getUTCSeconds());
if (year == null) {
year = possibleDate.getUTCFullYear();
month = possibleDate.getUTCMonth() + 1;
}
return fetch(
"https://ftp.mozilla.org/pub/firefox/nightly/" +
year +
"/" +
toTwoDigits(month) +
"/"
)
.then((response) => response.text())
.then((data) => {
let re = />(\d+)-(\d\d)-(\d\d)-(\d\d)-(\d\d)-(\d\d)-mozilla-central\/</g;
let results;
let buildIDs = [];
while ((results = re.exec(data)) !== null) {
let buildID =
results[1] +
results[2] +
results[3] +
results[4] +
results[5] +
results[6];
// Check if this build ID is 'newer' than date.
if (compareBuildIDs(possibleBuildID, buildID) != 1) {
buildIDs.push(buildID);
}
}
return buildIDs;
})
.then((buildIDs) => {
if (buildIDs.length === 0) {
return null;
}
return findFirstBuildIDInSet(buildIDs, date);
})
.then((rev) => {
if (!rev) {
let nextYear = month + 1 == 13 ? year + 1 : year;
let nextMonth = month + 1;
if (nextMonth == 13) {
nextMonth = 1;
}
if (
nextYear > new Date().getUTCFullYear() ||
(nextYear == new Date().getUTCFullYear() &&
nextMonth > new Date().getUTCMonth() + 1)
) {
return null;
}
return findFirstBuildID(date, nextYear, nextMonth);
}
return rev;
});
}
function getBuildID(changeset) {
return getChangesetDate(changeset).then((date) => findFirstBuildID(date));
}