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
|
import './latest-tag-button.css';
import React from 'dom-chef';
import cache from 'webext-storage-cache';
import TagIcon from 'octicon/tag.svg';
import DiffIcon from 'octicon/diff.svg';
import select from 'select-dom';
import * as pageDetect from 'github-url-detection';
import features from '.';
import * as api from '../github-helpers/api';
import pluralize from '../helpers/pluralize';
import GitHubURL from '../github-helpers/github-url';
import {groupButtons} from '../github-helpers/group-buttons';
import getDefaultBranch from '../github-helpers/get-default-branch';
import {buildRepoURL, getCurrentBranch, getLatestVersionTag, getRepo} from '../github-helpers';
interface RepoPublishState {
latestTag: string | false;
aheadBy?: number;
}
const getRepoPublishState = cache.function(async (): Promise<RepoPublishState> => {
const {repository} = await api.v4(`
repository() {
refs(first: 20, refPrefix: "refs/tags/", orderBy: {
field: TAG_COMMIT_DATE,
direction: DESC
}) {
nodes {
name
tag: target {
oid
... on Tag {
commit: target {
oid
}
}
}
}
}
defaultBranchRef {
target {
... on Commit {
history(first: 20) {
nodes {
oid
}
}
}
}
}
}
`);
if (repository.refs.nodes.length === 0) {
return {
latestTag: false
};
}
const tags = new Map<string, string>();
for (const node of repository.refs.nodes) {
tags.set(node.name, node.tag.commit?.oid ?? node.tag.oid);
}
const latestTag = getLatestVersionTag([...tags.keys()]);
const latestTagOid = tags.get(latestTag)!;
const aheadBy = repository.defaultBranchRef.target.history.nodes.findIndex((node: AnyObject) => node.oid === latestTagOid);
if (aheadBy < 0) {
return {latestTag};
}
return {latestTag, aheadBy};
}, {
maxAge: {hours: 1},
staleWhileRevalidate: {days: 2},
cacheKey: () => `tag-ahead-by:${getRepo()!.nameWithOwner}`
});
async function init(): Promise<false | void> {
const {latestTag, aheadBy} = await getRepoPublishState();
if (!latestTag) {
return false;
}
const currentBranch = getCurrentBranch()!;
const url = new GitHubURL(location.href);
url.assign({
route: url.route || 'tree', // If route is missing, it's a repo root
branch: latestTag
});
const link = (
<a className="btn btn-sm btn-outline ml-2 flex-self-center rgh-latest-tag-button" href={String(url)}>
<TagIcon/>
</a>
);
select('#branch-select-menu')!.parentElement!.after(link);
if (currentBranch !== latestTag) {
link.append(' ', <span className="css-truncate-target">{latestTag}</span>);
}
if (currentBranch === latestTag || aheadBy === 0) {
link.setAttribute('aria-label', 'You’re on the latest release');
link.classList.add('disabled', 'tooltipped', 'tooltipped-ne');
return;
}
const defaultBranch = await getDefaultBranch();
if (currentBranch === defaultBranch) {
link.append(<sup> +{aheadBy}</sup>);
link.setAttribute(
'aria-label',
aheadBy ?
`${defaultBranch} is ${pluralize(aheadBy, '1 commit', '$$ commits')} ahead of the latest release` :
`The HEAD of ${defaultBranch} isn’t tagged`
);
if (pageDetect.isRepoRoot()) {
const compareLink = (
<a
className="btn btn-sm btn-outline tooltipped tooltipped-ne"
href={buildRepoURL(`compare/${latestTag}...${defaultBranch}`)}
aria-label={`Compare ${latestTag}...${defaultBranch}`}
>
<DiffIcon/>
</a>
);
groupButtons([link, compareLink]).classList.add('flex-self-center', 'd-flex');
}
} else {
link.setAttribute('aria-label', 'Visit the latest release');
}
link.classList.add('tooltipped', 'tooltipped-ne');
}
void features.add(__filebasename, {
include: [
pageDetect.isRepoTree,
pageDetect.isSingleFile
],
awaitDomReady: false,
init
});
|