-
Notifications
You must be signed in to change notification settings - Fork 4
/
content.js
187 lines (174 loc) · 5.14 KB
/
content.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
let port = null;
const currentYear = (new Date()).getFullYear();
const buildidPattern = new RegExp("^[0-9]{14}$");
let currentBuildid = "";
function getBuildid() {
const sel = window.getSelection();
const str = sel.toString().trim();
if (str.length > 14) {
return "";
}
const range = sel.getRangeAt(0);
const container = range.startContainer;
if (container == range.endContainer) {
const text = container.textContent;
const pos = range.startOffset;
if (pos + 14 < text.length) {
const s = text.slice(pos, pos + 14);
if (checkBuildid(s)) {
return s;
}
}
}
if (str.length == 14 && checkBuildid(str)) {
return str;
}
return "";
}
function selectionHandler() {
const bid = getBuildid();
if (bid.length != 0) {
if (bid !== currentBuildid) {
port.postMessage("pushlog-clean");
currentBuildid = bid;
getBuildInfo(bid).then(response => response.json())
.then(json => {
port.postMessage({"pushlog-data": extract(json, bid)});
})
.catch(e => {
console.log(e);
});
}
} else {
currentBuildid = "";
port.postMessage("pushlog-clean");
}
}
function init() {
document.addEventListener("selectionchange", selectionHandler);
port = browser.runtime.connect({name: "pushlog-port"});
}
function getMajor(version) {
return parseInt(version.split(".")[0], 10);
}
function checkBuildid(s) {
if (s.match(buildidPattern)) {
let n = parseInt(s, 10);
[59, 59, 23, 31, 12].forEach(x => {
if ((n % 100) > x) {
return false;
}
n = Math.trunc(n / 100);
});
if (n < 1999 || n > currentYear) {
return false;
}
return true;
}
return false;
}
function extract(data, baseBuildid) {
const results = {};
data.aggregations.products.buckets.forEach(i => {
const product = i.key;
i.channels.buckets.forEach(j => {
const channel = j.key;
const buckets = j.buildids.buckets;
if (buckets.length == 2 && buckets[0].key === baseBuildid) {
const lastRevision = buckets[0].revisions.buckets[0].key.slice(0, 12);
const prevRevision = buckets[1].revisions.buckets[0].key.slice(0, 12);
const id = product + "-" + channel;
if (channel === "esr") {
const major = getMajor(buckets[0].versions.buckets[0].key);
results[id] = getPushlogUrl(lastRevision, prevRevision, "esr" + major);
} else {
results[id] = getPushlogUrl(lastRevision, prevRevision, channel);
}
}
});
});
return results;
}
function getBaseUrl(channel) {
if (channel === "beta" || channel === "aurora") {
return "https://hg.mozilla.org/releases/mozilla-beta/pushloghtml?fromchange=";
} else if (channel === "release") {
return "https://hg.mozilla.org/releases/mozilla-release/pushloghtml?fromchange=";
} else if (channel.startsWith("esr")) {
return "https://hg.mozilla.org/releases/mozilla-" + channel + "/pushloghtml?fromchange=";
}
return "https://hg.mozilla.org/mozilla-central/pushloghtml?fromchange=";
}
function getPushlogUrl(last, prev, channel) {
return getBaseUrl(channel) + prev + "&tochange=" + last;
}
async function getBuildInfo(buildid) {
const url = "https://buildhub.moz.tools/api/search";
const query = {
"aggs": {
"products": {
"terms": {
"field": "source.product",
"size": 3
},
"aggs": {
"channels": {
"terms": {
"field": "target.channel",
"size": 5
},
"aggs": {
"buildids": {
"terms": {
"field": "build.id",
"size": 2,
"order": {
"_term": "desc"
}
},
"aggs": {
"revisions": {
"terms": {
"field": "source.revision",
"size": 1
}
},
"versions": {
"terms": {
"field": "target.version",
"size": 1
}
}
}
}
}
}
}
}
},
"query": {
"bool": {
"filter": [
{"terms": {"target.channel": ["release", "beta", "aurora", "nightly", "esr"]}},
{"terms": {"source.product": ["devedition", "firefox", "fennec"]}},
{"range": {"build.id": {"lte": buildid}}},
{"regexp": {"target.version": "[0-9]+\.[0-9]+([ab\.][0-9]+)?(esr)?"}}
]
}
},
"size": 0};
const settings = {
"method": "POST",
"headers": {
"Accept": "application/json",
"Content-Type": "application/json",
},
"body": JSON.stringify(query)
};
return await fetch(url, settings);
}
init();