Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unreleased-commits feature #6576

Merged
merged 7 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ Thanks for contributing! 🦋🙌
- [](# "repo-avatars") [Adds the profile picture to the header of public repositories.](https://user-images.githubusercontent.com/44045911/177211845-c9b0fa37-c157-4449-890e-af2602c312e3.png)
- [](# "quick-new-issue") [Adds a link to create issues from anywhere in a repository.](https://user-images.githubusercontent.com/1402241/218251057-b94b62dd-a944-4763-b78a-fc233f7c9fd3.png)
- [](# "small-user-avatars") [Shows a small avatar next to the username in conversation lists.](https://user-images.githubusercontent.com/44045911/230960291-721f42cc-e1ac-4fdc-83ea-2430b062f9ce.png)
- [](# "unreleased-commits") 🔥 [Tells you whether you're looking at the latest version of a repository, or if there are any unreleased commits.](https://user-images.githubusercontent.com/1402241/234576563-1a0ca255-4c0d-45ae-883d-2b1aa2d7f4c1.png)

<!-- Refer to style guide above. Keep this message between sections. -->

Expand Down Expand Up @@ -317,7 +318,6 @@ Thanks for contributing! 🦋🙌
- [](# "releases-dropdown") [Adds a tags dropdown/search on tag/release pages.](https://user-images.githubusercontent.com/1402241/231678527-f0a96112-9c30-4b49-8205-efa472bd880e.png)
- [](# "create-release-shortcut") Adds a keyboard shortcut to create a new release while on the Releases page: <kbd>c</kbd>.
- [](# "tag-changes-link") 🔥 [Adds a link to changes since last tag/release for each tag/release.](https://user-images.githubusercontent.com/1402241/57081611-ad4a7180-6d27-11e9-9cb6-c54ec1ac18bb.png)
- [](# "latest-tag-button") [Adds a link to the latest version tag on directory listings and files.](https://user-images.githubusercontent.com/44045911/155496122-6267c45d-21d4-45c9-adf7-94c9e41cc652.png)
- [](# "convert-release-to-draft") [Adds a button to convert a release to draft.](https://user-images.githubusercontent.com/46634000/139236979-44533bfd-5c17-457d-bdc1-f9ec395f6a3a.png)
- [](# "link-to-changelog-file") [Adds a button to view the changelog file from the releases page.](https://user-images.githubusercontent.com/46634000/139236982-a1bce2a2-f3aa-40a9-bca4-8756bc941210.png)

Expand Down
13 changes: 0 additions & 13 deletions source/features/latest-tag-button.css

This file was deleted.

170 changes: 0 additions & 170 deletions source/features/latest-tag-button.tsx

This file was deleted.

151 changes: 151 additions & 0 deletions source/features/unreleased-commits.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import React from 'dom-chef';
import cache from 'webext-storage-cache';
import * as pageDetect from 'github-url-detection';
import {TagIcon} from '@primer/octicons-react';

import features from '../feature-manager';
import observe from '../helpers/selector-observer';
import * as api from '../github-helpers/api';
import {buildRepoURL, cacheByRepo, getCurrentCommittish, getLatestVersionTag} from '../github-helpers';
import getDefaultBranch from '../github-helpers/get-default-branch';
import pluralize from '../helpers/pluralize';

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 getRepoPublishState = cache.function('tag-ahead-by', 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,
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);
}

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(branchSelector: HTMLElement): Promise<void> {
const defaultBranch = await getDefaultBranch();
const currentBranch = getCurrentCommittish();

const onDefaultBranch = !currentBranch || currentBranch === defaultBranch; // `getCurrentCommittish` returns `undefined` when at the repo root on the default branch #5446
if (!onDefaultBranch) {
return;
}

const {latestTag, aheadBy} = await getRepoPublishState();
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}`;

// TODO: use .position-relative:has(> #branch-select-menu)
branchSelector.closest('.position-relative')!.after(
<a
className="btn ml-2 px-2 tooltipped tooltipped-ne"
href={buildRepoURL('compare', `${latestTag}...${defaultBranch}`)}
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> {
await api.expectToken();

observe('#branch-select-menu', 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

*/
2 changes: 1 addition & 1 deletion source/refined-github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ import './features/global-conversation-list-filters';
import './features/more-conversation-filters';
import './features/sort-conversations-by-update-time'; // Must be after global-conversation-list-filters and more-conversation-filters and conversation-links-on-repo-lists
import './features/pinned-issues-update-time';
import './features/latest-tag-button';
import './features/default-branch-button';
import './features/one-click-diff-options';
import './features/ci-link';
Expand Down Expand Up @@ -215,3 +214,4 @@ import './features/rgh-netiquette';
import './features/small-user-avatars';
import './features/releases-dropdown';
import './features/pr-base-commit';
import './features/unreleased-commits';