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

feat: Exclude archived repositories for top languages #1653

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/top-langs.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default async (req, res) => {
layout,
langs_count,
exclude_repo,
exclude_archived,
custom_title,
locale,
border_radius,
Expand All @@ -44,6 +45,7 @@ export default async (req, res) => {
const topLangs = await fetchTopLanguages(
username,
parseArray(exclude_repo),
parseBoolean(exclude_archived),
);

const cacheSeconds = clampValue(
Expand Down
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,16 @@ You can use the `&exclude_repo=repo1,repo2` parameter to exclude individual repo
[![Top Langs](https://github-readme-stats.vercel.app/api/top-langs/?username=anuraghazra&exclude_repo=github-readme-stats,anuraghazra.github.io)](https://github.com/anuraghazra/github-readme-stats)
```

### Exclude archived repositories

You can use `?exclude_archived=true` parameter to exclude archived repositories.

This feature is **disabled** by default.

```md
[![Top Langs](https://github-readme-stats.vercel.app/api/top-langs/?username=anuraghazra&exclude_archived=true)](https://github.com/anuraghazra/github-readme-stats)
```

### Hide individual languages

You can use `&hide=language1,language2` parameter to hide individual languages.
Expand Down
14 changes: 13 additions & 1 deletion src/fetchers/top-languages-fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const fetcher = (variables, token) => {
repositories(ownerAffiliations: OWNER, isFork: false, first: 100) {
nodes {
name
isArchived
languages(first: 10, orderBy: {field: SIZE, direction: DESC}) {
edges {
size
Expand Down Expand Up @@ -52,9 +53,10 @@ const fetcher = (variables, token) => {
*
* @param {string} username GitHub username.
* @param {string[]} exclude_repo List of repositories to exclude.
* @param {boolean} exclude_archived Whether or not to exclude archived.
* @returns {Promise<import("./types").TopLangData>} Top languages data.
*/
const fetchTopLanguages = async (username, exclude_repo = []) => {
const fetchTopLanguages = async (username, exclude_repo = [], exclude_archived = false) => {
if (!username) throw new MissingParamError(["username"]);

const res = await retryer(fetcher, { login: username });
Expand Down Expand Up @@ -86,8 +88,18 @@ const fetchTopLanguages = async (username, exclude_repo = []) => {
}

let repoNodes = res.data.data.user.repositories.nodes;

let repoToHide = {};

// populate repoToHide with archived repos
if (exclude_archived) {
repoNodes.forEach(({ name, isArchived }) => {
if (isArchived) {
repoToHide[name] = true;
}
});
}

// populate repoToHide map for quick lookup
// while filtering out
if (exclude_repo) {
Expand Down
44 changes: 41 additions & 3 deletions tests/fetchTopLanguages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ const data_langs = {
],
},
},
{
name: "test-archived-repo-4",
isArchived: true,
languages: {
edges: [
{
size: 100,
node: { color: "#0fff", name: "javascript" },
},
],
},
},
],
},
},
Expand Down Expand Up @@ -73,15 +85,18 @@ describe("FetchTopLanguages", () => {
javascript: {
color: "#0ff",
name: "javascript",
size: 200,
size: 300,
},
});
});

it("should fetch correct language data while excluding the 'test-repo-1' repository", async () => {
mock.onPost("https://api.github.com/graphql").reply(200, data_langs);

let repo = await fetchTopLanguages("anuraghazra", ["test-repo-1"]);
let repo = await fetchTopLanguages(
"anuraghazra",
(exclude_repo = ["test-repo-1"]),
);
expect(repo).toStrictEqual({
HTML: {
color: "#0f0",
Expand All @@ -91,7 +106,30 @@ describe("FetchTopLanguages", () => {
javascript: {
color: "#0ff",
name: "javascript",
size: 200,
size: 300,
},
});
});

it("should fetch correct language data while excluding archived repositories", async () => {
mock.onPost("https://api.github.com/graphql").reply(200, data_langs);

let repo = await fetchTopLanguages(
"anuraghazra",
(exclude_repo = ["test-repo-1"]),
(exclude_archived = true),
);

expect(repo).toStrictEqual({
HTML: {
color: "#0f0",
name: "HTML",
size: 100,
},
javascript: {
color: "#0ff",
name: "javascript",
size: 200,
},
});
});
Expand Down