diff --git a/api/top-langs.js b/api/top-langs.js index d183d3b455ca0..7de0365c0ec84 100644 --- a/api/top-langs.js +++ b/api/top-langs.js @@ -25,6 +25,7 @@ export default async (req, res) => { layout, langs_count, exclude_repo, + exclude_archived, custom_title, locale, border_radius, @@ -44,6 +45,7 @@ export default async (req, res) => { const topLangs = await fetchTopLanguages( username, parseArray(exclude_repo), + parseBoolean(exclude_archived), ); const cacheSeconds = clampValue( diff --git a/readme.md b/readme.md index bfe042fcc2032..1fc146ff99874 100644 --- a/readme.md +++ b/readme.md @@ -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. diff --git a/src/fetchers/top-languages-fetcher.js b/src/fetchers/top-languages-fetcher.js index 86d794435be08..0e71acde9fce1 100644 --- a/src/fetchers/top-languages-fetcher.js +++ b/src/fetchers/top-languages-fetcher.js @@ -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 @@ -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} 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 }); @@ -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) { diff --git a/tests/fetchTopLanguages.test.js b/tests/fetchTopLanguages.test.js index 24416cd294525..971a1d247ca02 100644 --- a/tests/fetchTopLanguages.test.js +++ b/tests/fetchTopLanguages.test.js @@ -42,6 +42,18 @@ const data_langs = { ], }, }, + { + name: "test-archived-repo-4", + isArchived: true, + languages: { + edges: [ + { + size: 100, + node: { color: "#0fff", name: "javascript" }, + }, + ], + }, + }, ], }, }, @@ -73,7 +85,7 @@ describe("FetchTopLanguages", () => { javascript: { color: "#0ff", name: "javascript", - size: 200, + size: 300, }, }); }); @@ -81,7 +93,10 @@ describe("FetchTopLanguages", () => { 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", @@ -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, }, }); });