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
|
import React from 'dom-chef';
import {CachedFunction} from 'webext-storage-cache';
import * as pageDetect from 'github-url-detection';
import {TagIcon} from '@primer/octicons-react';
import features from '../feature-manager.js';
import observe from '../helpers/selector-observer.js';
import api from '../github-helpers/api.js';
import {addAfterBranchSelector, buildRepoURL, cacheByRepo, getLatestVersionTag} from '../github-helpers/index.js';
import isDefaultBranch from '../github-helpers/is-default-branch.js';
import getDefaultBranch from '../github-helpers/get-default-branch.js';
import pluralize from '../helpers/pluralize.js';
import {branchSelectorParent} from '../github-helpers/selectors.js';
type RepoPublishState = {
latestTag: string | false;
aheadBy: number;
};
type Tags = {
name: string;
tag: {
oid: string;
commit?: {
oid: string;
};
};
};
export const undeterminableAheadBy = Number.MAX_SAFE_INTEGER; // For when the branch is ahead by more than 20 commits #5505
export const repoPublishState = new CachedFunction('tag-ahead-by', {
async updater(): 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,
aheadBy: 0,
};
}
const tags = new Map<string, string>();
for (const node of repository.refs.nodes as Tags[]) {
tags.set(node.name, node.tag.commit?.oid ?? node.tag.oid);
}
// If this logic ever gets dropped or becomes simpler, consider using the native "compare" API
// https://github.com/refined-github/refined-github/issues/6094
const latestTag = getLatestVersionTag([...tags.keys()]);
const latestTagOid = tags.get(latestTag)!;
const aheadBy = repository.defaultBranchRef.target.history.nodes.findIndex((node: AnyObject) => node.oid === latestTagOid);
return {
latestTag,
aheadBy: aheadBy === -1 ? undeterminableAheadBy : aheadBy,
};
},
maxAge: {hours: 1},
staleWhileRevalidate: {days: 2},
cacheKey: cacheByRepo,
});
async function add(branchSelectorParent: HTMLDetailsElement): Promise<void> {
const {latestTag, aheadBy} = await repoPublishState.get();
const isAhead = aheadBy > 0;
if (!latestTag || !isAhead) {
return;
}
const commitCount
= aheadBy === undeterminableAheadBy
? 'more than 20 unreleased commits'
: pluralize(aheadBy, '$$ unreleased commit');
const label = `There are ${commitCount} since ${latestTag}`;
addAfterBranchSelector(
branchSelectorParent,
<a
className="btn px-2 tooltipped tooltipped-ne"
href={buildRepoURL('compare', `${latestTag}...${await getDefaultBranch()}`)}
aria-label={label}
>
<TagIcon className="v-align-middle"/>
{aheadBy === undeterminableAheadBy || <sup className="ml-n2"> +{aheadBy}</sup>}
</a>,
);
}
async function init(signal: AbortSignal): Promise<false | void> {
if (!await isDefaultBranch()) {
return false;
}
await api.expectToken();
observe(branchSelectorParent, add, {signal});
}
void features.add(import.meta.url, {
include: [
pageDetect.isRepoHome,
],
awaitDomReady: true, // DOM-based exclusions
init,
});
/*
Test URLs
Repo with no tags (no button)
https://github.com/refined-github/yolo
Repo with too many unreleased commits
https://github.com/refined-github/sandbox
Repo with some unreleased commits
https://github.com/refined-github/refined-github
*/
|