forked from JetBrains/toolbox-browser-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitbucket-server.js
304 lines (257 loc) · 8.88 KB
/
bitbucket-server.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import 'whatwg-fetch';
import {observe} from 'selector-observer';
import parseBitbucketUrl from 'parse-bitbucket-url';
import {
SUPPORTED_LANGUAGES,
SUPPORTED_TOOLS,
DEFAULT_LANGUAGE,
CLONE_PROTOCOLS
} from './constants';
import {
getToolboxURN,
getToolboxNavURN,
callToolbox
} from './api/toolbox';
const OPEN_BUTTON_JS_CSS_CLASS = 'js-toolbox-open-button';
const fetchMetadata = () => new Promise((resolve, reject) => {
const parsedStashUrl = document.querySelector('meta[name=application-name][content=Bitbucket]') &&
parseBitbucketUrl(window.location.toString());
if (!parsedStashUrl) {
reject();
return;
}
// normalize metadata
const metadata = {
// eslint-disable-next-line camelcase
api_url: `${location.origin}/rest/api/latest/projects/${parsedStashUrl.owner}/repos/${parsedStashUrl.name}`,
branch: parsedStashUrl.branch,
repo: parsedStashUrl.name,
user: parsedStashUrl.owner
};
fetch(metadata.api_url).
then(response => response.json()).
then(parsedResponse => {
metadata.links = {
clone: parsedResponse.links.clone
};
const httpLink = metadata.links.clone.find(l => l.name === 'http');
if (httpLink) {
// normalize name
httpLink.name = 'https';
}
resolve(metadata);
}).
catch(() => {
reject();
});
});
const fetchLanguages = () => new Promise(resolve => {
// we don't know how to obtain repo languages in stash
resolve(DEFAULT_LANGUAGE);
});
const selectTools = language => new Promise(resolve => {
// All languages in Bitbucket match the common list with an exception of HTML
const normalizedLanguage = language === 'html/css' ? 'html' : language;
const toolIds = normalizedLanguage && SUPPORTED_LANGUAGES[normalizedLanguage.toLowerCase()];
const normalizedToolIds = toolIds && toolIds.length > 0
? toolIds
: SUPPORTED_LANGUAGES[DEFAULT_LANGUAGE];
const tools = normalizedToolIds.
sort().
map(toolId => SUPPORTED_TOOLS[toolId]);
resolve(tools);
});
const fetchTools = bitbucketMetadata => fetchLanguages(bitbucketMetadata).then(selectTools);
const renderPageAction = bitbucketMetadata => new Promise(resolve => {
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
switch (message.type) {
case 'get-tools':
fetchTools(bitbucketMetadata).then(sendResponse);
return true;
case 'perform-action':
const toolboxAction = getToolboxURN(message.toolTag, message.cloneUrl);
callToolbox(toolboxAction);
break;
// no default
}
return undefined;
});
resolve();
});
const getCloneUrl = (links, which) => {
const link = links.clone.find(l => l.name === which);
return link ? link.href : '';
};
const getHttpsCloneUrl = links => getCloneUrl(links, 'https');
const getSshCloneUrl = links => getCloneUrl(links, 'ssh');
const addCloneButtonEventHandler = (btn, bitbucketMetadata) => {
btn.addEventListener('click', e => {
e.preventDefault();
const {toolTag} = e.currentTarget.dataset;
chrome.runtime.sendMessage({type: 'get-protocol'}, ({protocol}) => {
const cloneUrl = protocol === CLONE_PROTOCOLS.HTTPS
? getHttpsCloneUrl(bitbucketMetadata.links)
: getSshCloneUrl(bitbucketMetadata.links);
const action = getToolboxURN(toolTag, cloneUrl);
callToolbox(action);
});
});
};
const createCloneButton = (tool, bitbucketMetadata) => {
const title = `Clone in ${tool.name}`;
const button = document.createElement('a');
button.setAttribute('class', 'aui-nav-item');
button.setAttribute('href', '#');
button.setAttribute('original-title', title);
button.dataset.toolTag = tool.tag;
const buttonIcon = document.createElement('span');
buttonIcon.setAttribute('class', 'aui-icon toolbox-aui-icon');
buttonIcon.setAttribute('style', `background-image:url(${tool.icon});background-size:contain`);
const buttonLabel = document.createElement('span');
buttonLabel.setAttribute('class', 'aui-nav-item-label');
buttonLabel.textContent = title;
button.appendChild(buttonIcon);
button.appendChild(buttonLabel);
addCloneButtonEventHandler(button, bitbucketMetadata);
return button;
};
const renderCloneButtons = bitbucketMetadata => {
const cloneElement = document.querySelector('.clone-repo');
if (!cloneElement) {
return;
}
fetchTools(bitbucketMetadata).
then(tools => {
tools.forEach(tool => {
const buttonContainer = document.createElement('li');
buttonContainer.setAttribute('class', 'js-toolbox-clone-repo');
const button = createCloneButton(tool, bitbucketMetadata);
buttonContainer.appendChild(button);
cloneElement.insertAdjacentElement('beforebegin', buttonContainer);
});
}).catch(() => {
// do nothing
});
};
const removeCloneButtons = () => {
document.querySelectorAll('.js-toolbox-clone-repo').forEach(buttonContainer => {
buttonContainer.remove();
});
};
const addOpenButtonEventHandler = (domElement, tool, bitbucketMetadata) => {
domElement.addEventListener('click', e => {
e.preventDefault();
const filePathIndex = 6;
const filePath = location.pathname.split('/').splice(filePathIndex).join('/');
let lineNumber = location.hash.replace('#', '');
if (lineNumber === '') {
lineNumber = null;
}
callToolbox(getToolboxNavURN(tool.tag, bitbucketMetadata.repo, filePath, lineNumber));
});
};
const createOpenButton = (tool, bitbucketMetadata) => {
const buttonContainer = document.createElement('div');
buttonContainer.setAttribute('class', `aui-buttons ${OPEN_BUTTON_JS_CSS_CLASS}`);
const button = document.createElement('button');
button.setAttribute('class', 'aui-button');
button.setAttribute('original-title', `Open this file in ${tool.name}`);
const buttonIcon = document.createElement('img');
buttonIcon.setAttribute('alt', tool.name);
buttonIcon.setAttribute('src', tool.icon);
buttonIcon.setAttribute('width', '16');
buttonIcon.setAttribute('height', '16');
buttonIcon.setAttribute('style', 'vertical-align:text-bottom');
button.appendChild(buttonIcon);
buttonContainer.append(button);
addOpenButtonEventHandler(button, tool, bitbucketMetadata);
return buttonContainer;
};
const setOpenButtonTooltips = () => {
const tooltipScript = document.createElement('script');
tooltipScript.textContent = `jQuery('.${OPEN_BUTTON_JS_CSS_CLASS} > .aui-button:first-child').tipsy();`;
document.body.appendChild(tooltipScript);
};
const openButtonsRendered = () => document.getElementsByClassName(OPEN_BUTTON_JS_CSS_CLASS).length > 0;
const renderOpenButtons = bitbucketMetadata => {
if (openButtonsRendered()) {
return;
}
const anchorElement = document.querySelector('.file-toolbar > .secondary > .aui-buttons:first-child');
if (anchorElement) {
fetchTools(bitbucketMetadata).
then(tools => {
tools.forEach(tool => {
const action = createOpenButton(tool, bitbucketMetadata);
anchorElement.insertAdjacentElement('beforebegin', action);
});
setOpenButtonTooltips();
}).
catch(() => {
// do nothing
});
}
};
const removeOpenButtons = () => {
document.querySelectorAll(`.${OPEN_BUTTON_JS_CSS_CLASS}`).forEach(button => {
button.remove();
});
};
const startTrackingDOMChanges = bitbucketMetadata =>
observe('#file-content > .file-toolbar > .secondary > .aui-buttons > .file-blame', {
add(/*el*/) {
renderOpenButtons(bitbucketMetadata);
},
remove() {
removeOpenButtons();
}
});
const stopTrackingDOMChanges = observer => {
if (observer) {
observer.abort();
}
};
const enablePageAction = bitbucketMetadata => {
chrome.runtime.sendMessage({
type: 'enable-page-action',
project: bitbucketMetadata.repo,
https: getHttpsCloneUrl(bitbucketMetadata.links),
ssh: getSshCloneUrl(bitbucketMetadata.links)
});
};
const disablePageAction = () => {
chrome.runtime.sendMessage({type: 'disable-page-action'});
};
const toolboxify = () => {
fetchMetadata().
then(metadata => {
renderPageAction(metadata).then(() => {
enablePageAction(metadata);
});
chrome.runtime.sendMessage({type: 'get-modify-pages'}, data => {
let DOMObserver = null;
if (data.allow) {
renderCloneButtons(metadata);
DOMObserver = startTrackingDOMChanges(metadata);
}
chrome.runtime.onMessage.addListener(message => {
switch (message.type) {
case 'modify-pages-changed':
if (message.newValue) {
renderCloneButtons(metadata);
DOMObserver = startTrackingDOMChanges(metadata);
} else {
removeCloneButtons();
stopTrackingDOMChanges(DOMObserver);
}
break;
// no default
}
});
});
}).
catch(() => {
disablePageAction();
});
};
export default toolboxify;