Skip to content

Commit

Permalink
Add link-to-changelog-file feature (#3958)
Browse files Browse the repository at this point in the history
Co-authored-by: Federico <me@fregante.com>
  • Loading branch information
yakov116 and fregante committed Feb 12, 2021
1 parent df4c06f commit 64344f9
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ Thanks for contributing! 🦋🙌
- [](# "tag-changelog-link") 🔥 [Adds a link to an automatic changelog 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/1402241/74594998-71df2080-5077-11ea-927c-b484ca656e88.png)
- [](# "convert-release-to-draft") [Adds a button to convert a release to draft.](https://user-images.githubusercontent.com/16872793/90017455-8e03f900-dc79-11ea-95c5-377e0a82d4ea.png)
- [](# "link-to-changelog-file") [Adds a button to view the changelog file from the releases page.](https://user-images.githubusercontent.com/16872793/107520428-684fa200-6b7f-11eb-8f7c-59172db71160.png)

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

Expand Down
77 changes: 77 additions & 0 deletions source/features/link-to-changelog-file.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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}`;

function parseFromDom(): false {
void cache.set(getCacheKey(), select('.js-navigation-item [title^="changelog" i]')?.textContent ?? false);
return false;
}

const getChangelogName = cache.function(async (): Promise<string | false> => {
const {repository} = await api.v4(`
repository() {
object(expression: "HEAD:") {
...on Tree {
entries {
name
}
}
}
}
`);

for (const file of repository.object.entries) {
if (file.name.toLowerCase().startsWith('changelog')) {
return file.name;
}
}

return false;
}, {
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 mr-2"/>
<span>Changelog</span>
</a>
);
}

void features.add(__filebasename, {
include: [
pageDetect.isReleasesOrTags
],
awaitDomReady: false,
init
}, {
include: [
pageDetect.isRepoHome
],
exclude: [
pageDetect.isEmptyRepoRoot
],
init: parseFromDom
});
1 change: 1 addition & 0 deletions source/refined-github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ import './features/quick-repo-deletion';
import './features/clean-repo-sidebar';
import './features/rgh-feature-descriptions';
import './features/useful-forks';
import './features/link-to-changelog-file';

// Add global for easier debugging
(window as any).select = select;

0 comments on commit 64344f9

Please sign in to comment.