|
| 1 | +import * as pageDetect from 'github-url-detection'; |
| 2 | +import {CachedFunction} from 'webext-storage-cache'; |
| 3 | +import {writable} from 'svelte/store'; |
| 4 | + |
| 5 | +import features from '../feature-manager.js'; |
| 6 | +import api from '../github-helpers/api.js'; |
| 7 | +import {cacheByRepo, buildRepoUrl} from '../github-helpers/index.js'; |
| 8 | +import fetchDom from '../helpers/fetch-dom.js'; |
| 9 | +import looseParseInt from '../helpers/loose-parse-int.js'; |
| 10 | +import {overrideTab} from '../components/extensible-nav-store.js'; |
| 11 | +import {expectTokenScope} from '../github-helpers/github-token.js'; |
| 12 | +import RepoCountInfo from './extend-repo-tabs.gql'; |
| 13 | + |
| 14 | +type RepoTabsCounts = { |
| 15 | + projects: number; |
| 16 | + actionRuns: number; |
| 17 | +}; |
| 18 | + |
| 19 | +const cacheOptions = { |
| 20 | + maxAge: {days: 1}, |
| 21 | + staleWhileRevalidate: {days: 10}, |
| 22 | + cacheKey: cacheByRepo, |
| 23 | +} as const; |
| 24 | + |
| 25 | +const repoTabsCounts = new CachedFunction('repo-tabs-counts', { |
| 26 | + async updater(): Promise<RepoTabsCounts> { |
| 27 | + await expectTokenScope('read:project'); |
| 28 | + const {repository} = await api.v4(RepoCountInfo); |
| 29 | + |
| 30 | + return { |
| 31 | + // Projects undefined if not enabled in the repo |
| 32 | + projects: repository.projectsV2?.totalCount ?? 0, |
| 33 | + actionRuns: repository.defaultBranchRef.target.checkSuites.totalCount, |
| 34 | + }; |
| 35 | + }, |
| 36 | + ...cacheOptions, |
| 37 | +}); |
| 38 | + |
| 39 | +const wikiPageCount = new CachedFunction('wiki-page-count', { |
| 40 | + async updater(): Promise<number> { |
| 41 | + // No v3/v4 API access at all |
| 42 | + const counter = await fetchDom(buildRepoUrl('wiki'), '#wiki-pages-box .Counter'); |
| 43 | + return looseParseInt(counter); |
| 44 | + }, |
| 45 | + ...cacheOptions, |
| 46 | +}); |
| 47 | + |
| 48 | +async function updateWikiTab(): Promise<void> { |
| 49 | + const count = await wikiPageCount.get(); |
| 50 | + if (count > 0) { |
| 51 | + overrideTab('wiki', {counter: writable(count)}); |
| 52 | + } else { |
| 53 | + overrideTab('wiki', {demoted: 'empty'}); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +async function updateActionsAndProjectsTabs(): Promise<void> { |
| 58 | + const {projects, actionRuns} = await repoTabsCounts.get(); |
| 59 | + |
| 60 | + if (actionRuns === 0) { |
| 61 | + overrideTab('actions', {demoted: 'unused'}); |
| 62 | + } |
| 63 | + |
| 64 | + if (projects > 0) { |
| 65 | + overrideTab('projects', {counter: writable(projects)}); |
| 66 | + } else { |
| 67 | + overrideTab('projects', {demoted: 'none'}); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +function init(): void { |
| 72 | + overrideTab('pull-requests', {label: 'Pulls'}); |
| 73 | + overrideTab('agents', {demoted: true}); |
| 74 | + overrideTab('security-and-quality', {demoted: true}); |
| 75 | + overrideTab('insights', {demoted: true}); |
| 76 | + |
| 77 | + void updateWikiTab(); |
| 78 | + void updateActionsAndProjectsTabs(); |
| 79 | +} |
| 80 | + |
| 81 | +void features.add(import.meta.url, { |
| 82 | + include: [ |
| 83 | + pageDetect.hasRepoHeader, |
| 84 | + ], |
| 85 | + // The feature partially works without a token |
| 86 | + // requiresToken: true, |
| 87 | + init, |
| 88 | +}); |
| 89 | + |
| 90 | +/* |
| 91 | +
|
| 92 | +Test URLs: |
| 93 | +
|
| 94 | +- Repo with 0 projects: https://github.com/babel/flavortown |
| 95 | +- Repo with some projects: https://github.com/github/docs/projects |
| 96 | +- Repo with 0 wiki: https://github.com/babel/babel-sublime-snippets |
| 97 | +- Repo with some wiki: https://github.com/refined-github/refined-github |
| 98 | +- Repo with 0 actions: https://github.com/babel/jade-babel |
| 99 | +- Repo with security alerts: https://github.com/babel/babel |
| 100 | +
|
| 101 | +*/ |
0 commit comments