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
|
import './branch-buttons.css';
import React from 'dom-chef';
import select from 'select-dom';
import compareVersions from 'tiny-version-compare';
import * as api from '../libs/api';
import * as icons from '../libs/icons';
import features from '../libs/features';
import {isRepoRoot} from '../libs/page-detect';
import {groupSiblings} from '../libs/group-buttons';
import getDefaultBranch from '../libs/get-default-branch';
import {getRepoURL, getCurrentBranch, replaceBranch, getRepoGQL} from '../libs/utils';
async function getTagLink(): Promise<'' | HTMLAnchorElement> {
const {repository} = await api.v4(`
repository(${getRepoGQL()}) {
refs(first: 20, refPrefix: "refs/tags/", orderBy: {
field: TAG_COMMIT_DATE,
direction: DESC
}) {
nodes {
name
}
}
}
`);
const tags: string[] = repository.refs.nodes.map((tag: {name: string}) => tag.name);
if (tags.length === 0) {
return '';
}
// If all tags are plain versions, parse them,
// otherwise just use the latest.
let latestRelease: string;
if (tags.every(tag => /^[vr]?\d/.test(tag))) {
latestRelease = tags.sort(compareVersions).pop()!;
} else {
latestRelease = tags[0];
}
const link = <a className="btn btn-sm btn-outline tooltipped tooltipped-ne">{icons.tag()}</a> as unknown as HTMLAnchorElement;
const currentBranch = getCurrentBranch();
if (currentBranch === latestRelease) {
link.classList.add('disabled');
link.setAttribute('aria-label', 'You’re on the latest release');
} else {
if (isRepoRoot()) {
link.href = `/${getRepoURL()}/tree/${latestRelease}`;
} else {
link.href = replaceBranch(currentBranch, latestRelease);
}
link.setAttribute('aria-label', 'Visit the latest release');
link.append(' ', <span className="css-truncate-target">{latestRelease}</span>);
}
return link;
}
async function getDefaultBranchLink(): Promise<HTMLElement | undefined> {
const defaultBranch = await getDefaultBranch();
const currentBranch = getCurrentBranch();
// Don't show the button if we’re already on the default branch
if (defaultBranch === undefined || defaultBranch === currentBranch) {
return;
}
let url;
if (isRepoRoot()) {
url = `/${getRepoURL()}`;
} else {
url = replaceBranch(currentBranch, defaultBranch);
}
return (
<a
className="btn btn-sm btn-outline tooltipped tooltipped-ne"
href={url}
aria-label="Visit the default branch">
{icons.branch()}
{' '}
{defaultBranch}
</a>
);
}
async function init(): Promise<false | void> {
const breadcrumbs = select('.breadcrumb');
if (!breadcrumbs) {
return false;
}
const [defaultLink = '', tagLink = ''] = await Promise.all([
getDefaultBranchLink(),
getTagLink()
]);
const wrapper = (
<div className="rgh-branch-buttons ml-2">
{defaultLink}
{tagLink}
</div>
);
if (wrapper.children.length > 0) {
breadcrumbs.before(wrapper);
}
if (wrapper.children.length > 1) {
groupSiblings(wrapper.firstElementChild!);
}
}
features.add({
id: __featureName__,
description: 'Adds links to the default branch and to the latest version tag.',
screenshot: 'https://user-images.githubusercontent.com/1402241/38107328-ccb3fb46-33bb-11e8-9654-23a6410943cc.png',
include: [
features.isRepoTree,
features.isSingleFile
],
load: features.onAjaxedPages,
init
});
|