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
|
import React from 'dom-chef';
import cache from 'webext-storage-cache';
import select from 'select-dom';
import {BookIcon} from '@primer/octicons-react';
import elementReady from 'element-ready';
import * as pageDetect from 'github-url-detection';
import features from '.';
import * as api from '../github-helpers/api';
import {buildRepoURL, getRepo} from '../github-helpers';
const getCacheKey = (): string => `changelog:${getRepo()!.nameWithOwner}`;
const changelogNames = new Set(['changelog', 'news', 'changes', 'history', 'release', 'whatsnew']);
function findChangelogName(files: string[]): string | false {
for (const file of files) {
if (changelogNames.has(file.toLowerCase().split('.', 1)[0])) {
return file;
}
}
return false;
}
function parseFromDom(): false {
const files = select.all('[aria-labelledby="files"] .js-navigation-open[href*="/blob/"').map(file => file.title);
void cache.set(getCacheKey(), findChangelogName(files));
return false;
}
const getChangelogName = cache.function(async (): Promise<string | false> => {
const {repository} = await api.v4(`
repository() {
object(expression: "HEAD:") {
...on Tree {
entries {
name
type
}
}
}
}
`);
const files: string[] = [];
for (const entry of repository.object.entries) {
if (entry.type === 'blob') {
files.push(entry.name);
}
}
return findChangelogName(files);
}, {
cacheKey: getCacheKey
});
async function init(): Promise<void | false> {
const changelog = await getChangelogName();
if (!changelog) {
return false;
}
(await elementReady('.subnav div', {waitForChildren: false}))!.after(
<a
className="btn ml-3 tooltipped tooltipped-n"
aria-label={`View the ${changelog} file`}
href={buildRepoURL('blob', 'HEAD', changelog)}
style={{padding: '6px 16px'}}
role="button"
>
<BookIcon className="text-blue color-text-link mr-2"/>
<span>Changelog</span>
</a>
);
}
void features.add(__filebasename, {
include: [
pageDetect.isReleasesOrTags
],
awaitDomReady: false,
init
}, {
include: [
pageDetect.isRepoHome
],
init: parseFromDom
});
|