-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathgithub.js
355 lines (311 loc) · 12.3 KB
/
github.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// When Github page loads at repo path e.g. https://github.com/jquery/jquery, the HTML tree has
// <main id="js-repo-pjax-container"> to contain server-rendered HTML in response of pjax.
// However, that <main> element doesn't have "id" attribute if the Github page loads at specific
// File e.g. https://github.com/jquery/jquery/blob/master/.editorconfig.
// Therefore, the below selector uses many path but only points to the same <main> element
const GH_PJAX_CONTAINER_SEL =
'#js-repo-pjax-container, div[itemtype="http://schema.org/SoftwareSourceCode"] main, [data-pjax-container]';
const GH_CONTAINERS = '.container, .container-lg, .container-responsive';
const GH_MAX_HUGE_REPOS_SIZE = 50;
const GH_HIDDEN_RESPONSIVE_CLASS = '.d-none';
const GH_RESPONSIVE_BREAKPOINT = 1010;
class GitHub extends PjaxAdapter {
constructor() {
super(GH_PJAX_CONTAINER_SEL);
}
// @override
init($sidebar) {
super.init($sidebar);
// Fix #151 by detecting when page layout is updated.
// In this case, split-diff page has a wider layout, so need to recompute margin.
// Note that couldn't do this in response to URL change, since new DOM via pjax might not be ready.
const diffModeObserver = new window.MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (~mutation.oldValue.indexOf('split-diff') || ~mutation.target.className.indexOf('split-diff')) {
return $(document).trigger(EVENT.LAYOUT_CHANGE);
}
});
});
diffModeObserver.observe(document.body, {
attributes: true,
attributeFilter: ['class'],
attributeOldValue: true
});
}
// @override
getCssClass() {
return 'octotree-github-sidebar';
}
// @override
async shouldLoadEntireTree(repo) {
const isLoadingPr = await extStore.get(STORE.PR) && repo.pullNumber;
if (isLoadingPr) {
return true;
}
const isGlobalLazyLoad = await extStore.get(STORE.LAZYLOAD);
if (isGlobalLazyLoad) {
return false;
}
// Else, return true only if it isn't in a huge repo list, which we must lazy load
const key = `${repo.username}/${repo.reponame}`;
const hugeRepos = await extStore.get(STORE.HUGE_REPOS);
if (hugeRepos[key] && isValidTimeStamp(hugeRepos[key])) {
// Update the last load time of the repo
hugeRepos[key] = new Date().getTime();
await extStore.set(STORE.HUGE_REPOS, hugeRepos);
}
return !hugeRepos[key];
}
// @override
getCreateTokenUrl() {
return (
`${location.protocol}//${location.host}/settings/tokens/new?` +
'scopes=repo&description=Octotree%20browser%20extension'
);
}
// @override
updateLayout(sidebarPinned, sidebarVisible, sidebarWidth) {
const SPACING = 20;
const $containers =
$('html').width() <= GH_RESPONSIVE_BREAKPOINT
? $(GH_CONTAINERS).not(GH_HIDDEN_RESPONSIVE_CLASS)
: $(GH_CONTAINERS);
const shouldPushEverything = sidebarPinned && sidebarVisible;
if (shouldPushEverything) {
$('html').css('margin-left', sidebarWidth);
const autoMarginLeft = ($(document).width() - $containers.width()) / 2;
const marginLeft = Math.max(SPACING, autoMarginLeft - sidebarWidth);
$containers.each(function () {
const $container = $(this);
const paddingLeft = ($container.innerWidth() - $container.width()) / 2;
$container.css('margin-left', marginLeft - paddingLeft);
})
} else {
$('html').css('margin-left', '');
$containers.css('margin-left', '');
}
}
// @override
async getRepoFromPath(currentRepo, token, cb) {
if (!await octotree.shouldShowOctotree()) {
return cb();
}
// (username)/(reponame)[/(type)][/(typeId)]
const match = window.location.pathname.match(/([^\/]+)\/([^\/]+)(?:\/([^\/]+))?(?:\/([^\/]+))?/);
const username = match[1];
const reponame = match[2];
const type = match[3];
const typeId = match[4];
const isPR = type === 'pull';
// Not a repository, skip
if (~GH_RESERVED_USER_NAMES.indexOf(username) || ~GH_RESERVED_REPO_NAMES.indexOf(reponame)) {
return cb();
}
// Get branch by inspecting URL or DOM, quite fragile so provide multiple fallbacks.
// TODO would be great if there's a more robust way to do this
/**
* Github renders the branch name in one of below structure depending on the length
* of branch name. We're using this for default code page or tree/blob.
*
* Option 1: when the length is short enough
* <summary title="Switch branches or tags">
* <span class="css-truncate-target">feature/1/2/3</span>
* </summary>
*
* Option 2: when the length is too long
* <summary title="feature/1/2/3/4/5/6/7/8">
* <span class="css-truncate-target">feature/1/2/3...</span>
* </summary>
*/
const branchDropdownMenuSummary = $('.branch-select-menu summary');
const branchNameInTitle = branchDropdownMenuSummary.attr('title');
const branchNameInSpan = branchDropdownMenuSummary.find('span').text();
const branchFromSummary =
branchNameInTitle && branchNameInTitle.toLowerCase().startsWith('switch branches')
? branchNameInSpan
: branchNameInTitle;
const branch =
// Use the commit ID when showing a particular commit
(type === 'commit' && typeId) ||
// Use 'master' when viewing repo's releases or tags
((type === 'releases' || type === 'tags') && 'master') ||
// Get commit ID or branch name from the DOM
branchFromSummary ||
($('.overall-summary .numbers-summary .commits a').attr('href') || '').replace(
`/${username}/${reponame}/commits/`,
''
) ||
// The above should work for tree|blob, but if DOM changes, fallback to use ID from URL
((type === 'tree' || type === 'blob') && typeId) ||
// Use target branch in a PR page
(isPR ? ($('.commit-ref').not('.head-ref').attr('title') || ':').match(/:(.*)/)[1] : null) ||
// Reuse last selected branch if exist
(currentRepo.username === username && currentRepo.reponame === reponame && currentRepo.branch) ||
// Get default branch from cache
this._defaultBranch[username + '/' + reponame];
const showOnlyChangedInPR = await extStore.get(STORE.PR);
const pullNumber = isPR && showOnlyChangedInPR ? typeId : null;
const pullHead = isPR ? ($('.commit-ref.head-ref').attr('title') || ':').match(/:(.*)/)[1] : null;
const displayBranch = isPR && pullHead ? `${branch} < ${pullHead}` : null;
const repo = {username, reponame, branch, displayBranch, pullNumber};
if (repo.branch) {
cb(null, repo);
} else {
// Still no luck, get default branch for real
this._get(null, {repo, token}, (err, data) => {
if (err) return cb(err);
repo.branch = this._defaultBranch[username + '/' + reponame] = data.default_branch || 'master';
cb(null, repo);
});
}
}
// @override
loadCodeTree(opts, cb) {
opts.encodedBranch = encodeURIComponent(decodeURIComponent(opts.repo.branch));
opts.path = (opts.node && (opts.node.sha || opts.encodedBranch)) || opts.encodedBranch + '?recursive=1';
this._loadCodeTreeInternal(opts, null, cb);
}
get isOnPRPage() {
const match = window.location.pathname.match(/([^\/]+)\/([^\/]+)(?:\/([^\/]+))?(?:\/([^\/]+))?/);
if (!match) return false;
const type = match[3];
return type === 'pull';
}
// @override
_getTree(path, opts, cb) {
if (opts.repo.pullNumber) {
this._getPatch(opts, cb);
} else {
this._get(`/git/trees/${path}`, opts, (err, res) => {
if (err) cb(err);
else cb(null, res.tree);
});
}
}
/**
* Get files that were patched in Pull Request.
* The diff map that is returned contains changed files, as well as the parents of the changed files.
* This allows the tree to be filtered for only folders that contain files with diffs.
* @param {Object} opts: {
* path: the starting path to load the tree,
* repo: the current repository,
* node (optional): the selected node (null to load entire tree),
* token (optional): the personal access token
* }
* @param {Function} cb(err: error, diffMap: Object)
*/
_getPatch(opts, cb) {
const {pullNumber} = opts.repo;
this._get(`/pulls/${pullNumber}/files?per_page=300`, opts, (err, res) => {
if (err) cb(err);
else {
const diffMap = {};
res.forEach((file, index) => {
// Record file patch info
diffMap[file.filename] = {
type: 'blob',
diffId: index,
action: file.status,
additions: file.additions,
blob_url: file.blob_url,
deletions: file.deletions,
filename: file.filename,
path: file.path,
sha: file.sha
};
// Record ancestor folders
const folderPath = file.filename
.split('/')
.slice(0, -1)
.join('/');
const split = folderPath.split('/');
// Aggregate metadata for ancestor folders
split.reduce((path, curr) => {
if (path.length) path = `${path}/${curr}`;
else path = `${curr}`;
if (diffMap[path] == null) {
diffMap[path] = {
type: 'tree',
filename: path,
filesChanged: 1,
additions: file.additions,
deletions: file.deletions
};
} else {
diffMap[path].additions += file.additions;
diffMap[path].deletions += file.deletions;
diffMap[path].filesChanged++;
}
return path;
}, '');
});
// Transform to emulate response from get `tree`
const tree = Object.keys(diffMap).map((fileName) => {
const patch = diffMap[fileName];
return {
patch,
path: fileName,
sha: patch.sha,
type: patch.type,
url: patch.blob_url
};
});
// Sort by path, needs to be alphabetical order (so parent folders come before children)
// Note: this is still part of the above transform to mimic the behavior of get tree
tree.sort((a, b) => a.path.localeCompare(b.path));
cb(null, tree);
}
});
}
// @override
_getSubmodules(tree, opts, cb) {
const item = tree.filter((item) => /^\.gitmodules$/i.test(item.path))[0];
if (!item) return cb();
this._get(`/git/blobs/${item.sha}`, opts, (err, res) => {
if (err) return cb(err);
const data = atob(res.content.replace(/\n/g, ''));
cb(null, parseGitmodules(data));
});
}
_get(path, opts, cb) {
let url;
if (path && path.startsWith('http')) {
url = path;
} else {
const host =
location.protocol + '//' + (location.host === 'github.com' ? 'api.github.com' : location.host + '/api/v3');
url = `${host}/repos/${opts.repo.username}/${opts.repo.reponame}${path || ''}`;
}
const cfg = {url, method: 'GET', cache: false};
if (opts.token) {
cfg.headers = {Authorization: 'token ' + opts.token};
}
$.ajax(cfg)
.done((data, textStatus, jqXHR) => {
(async () => {
if (path && path.indexOf('/git/trees') === 0 && data.truncated) {
try {
const hugeRepos = await extStore.get(STORE.HUGE_REPOS);
const repo = `${opts.repo.username}/${opts.repo.reponame}`;
const repos = Object.keys(hugeRepos).filter((hugeRepoKey) => isValidTimeStamp(hugeRepos[hugeRepoKey]));
if (!hugeRepos[repo]) {
// If there are too many repos memoized, delete the oldest one
if (repos.length >= GH_MAX_HUGE_REPOS_SIZE) {
const oldestRepo = repos.reduce((min, p) => (hugeRepos[p] < hugeRepos[min] ? p : min));
delete hugeRepos[oldestRepo];
}
hugeRepos[repo] = new Date().getTime();
await extStore.set(STORE.HUGE_REPOS, hugeRepos);
}
} catch (ignored) {
} finally {
await this._handleError(cfg, {status: 206}, cb);
}
} else {
cb(null, data, jqXHR);
}
})();
})
.fail((jqXHR) => this._handleError(cfg, jqXHR, cb));
}
}