feat: fix and update /github/checks examples

pull/579/head
Amio 2023-03-18 13:09:19 +08:00
rodzic df9eb896f0
commit 48fe8911db
2 zmienionych plików z 25 dodań i 28 usunięć

Wyświetl plik

@ -4,14 +4,14 @@ import { BadgenError } from './create-badgen-handler'
const rand = <T>(arr: T[]): T => arr[Math.floor(Math.random() * arr.length)]
// request github api (rest)
export function restGithub<T = any>(path: string) {
export function restGithub<T = any>(path: string, searchParams?: Record<string, string>) {
const headers = {
authorization: `Bearer ${pickGithubToken()}`,
accept: `application/vnd.github+json`,
'X-GitHub-Api-Version': '2022-11-28',
}
const prefixUrl = process.env.GITHUB_API || 'https://api.github.com/'
return got.get(path, { prefixUrl, headers }).json<T>()
return got.get(path, { prefixUrl, headers, searchParams }).json<T>()
}
// request github api (graphql)

Wyświetl plik

@ -10,18 +10,22 @@ type DependentsType = 'REPOSITORY' | 'PACKAGE'
export default createBadgenHandler({
title: 'GitHub',
examples: {
'/github/license/micromatch/micromatch': 'license',
'/github/watchers/micromatch/micromatch': 'watchers',
'/github/branches/micromatch/micromatch': 'branches',
'/github/releases/micromatch/micromatch': 'releases',
'/github/tags/micromatch/micromatch': 'tags',
'/github/tag/micromatch/micromatch': 'latest tag',
'/github/contributors/micromatch/micromatch': 'contributors',
'/github/release/babel/babel': 'latest release',
'/github/release/babel/babel/stable': 'latest stable release',
'/github/tag/micromatch/micromatch': 'latest tag',
'/github/watchers/micromatch/micromatch': 'watchers',
'/github/checks/tunnckoCore/opensource': 'combined checks (default branch)',
'/github/checks/node-formidable/node-formidable': 'combined checks (default branch)',
'/github/checks/node-formidable/node-formidable/master/lint': 'single checks (lint job)',
'/github/checks/node-formidable/node-formidable/master/test': 'single checks (test job)',
'/github/checks/node-formidable/node-formidable/master/ubuntu?label=linux': 'single checks (linux)',
'/github/checks/node-formidable/node-formidable/master/windows': 'single checks (windows)',
'/github/checks/node-formidable/node-formidable/master/macos': 'single checks (macos)',
'/github/checks/styfle/packagephobia/main': 'combined checks (branch)',
'/github/checks/nodejs/node': 'combined checks conclusion (default branch)',
'/github/checks/nodejs/node/canary-base': 'combined checks conclusion (specified branch)',
'/github/checks/nodejs/node/v18.0.0': 'combined checks conclusion (specified tag)',
'/github/checks/nodejs/node/main/lint-cpp': 'single check (by job name)',
'/github/checks/nodejs/node/main/lint-cpp?label=Lint%20CPP': 'single check (lint job)',
'/github/checks/nodejs/node/main/test-linux': 'single check (test job)',
'/github/checks/nodejs/node/main/build-windows%20(windows-2022)': 'single check (by job name)',
'/github/stars/micromatch/micromatch': 'stars',
'/github/forks/micromatch/micromatch': 'forks',
'/github/issues/micromatch/micromatch': 'issues',
@ -41,11 +45,6 @@ export default createBadgenHandler({
'/github/last-commit/micromatch/micromatch': 'last commit',
'/github/last-commit/micromatch/micromatch/gh-pages': 'last commit (branch ref)',
'/github/last-commit/micromatch/micromatch/4.0.1': 'last commit (tag ref)',
'/github/branches/micromatch/micromatch': 'branches',
'/github/releases/micromatch/micromatch': 'releases',
'/github/tags/micromatch/micromatch': 'tags',
'/github/license/micromatch/micromatch': 'license',
'/github/contributors/micromatch/micromatch': 'contributors',
'/github/assets-dl/electron/electron': 'assets downloads for latest release',
'/github/assets-dl/electron/electron/v7.0.0': 'assets downloads for a tag',
'/github/dependents-repo/micromatch/micromatch': 'repository dependents',
@ -61,7 +60,7 @@ export default createBadgenHandler({
'/github/:topic<dt|assets-dl>/:owner/:repo/:tag?': downloads, // `dt` is deprecated
'/github/release/:owner/:repo/:channel?': release,
'/github/checks/:owner/:repo/:ref?': checks,
'/github/checks/:owner/:repo/:ref/:context+': checks,
'/github/checks/:owner/:repo/:ref/:check_name+': checks,
'/github/contributors/:owner/:repo': contributors,
'/github/milestones/:owner/:repo/:milestone_number': milestones,
'/github/dependents-repo/:owner/:repo': dependents('REPOSITORY'),
@ -99,21 +98,19 @@ function combined (states: Array<any>, stateKey: string = 'state') {
throw new Error(`Unknown states: ${states.map(x => x[stateKey]).join()}`)
}
async function checks ({ owner, repo, ref, context}: PathArgs) {
async function checks ({owner, repo, ref, check_name}: PathArgs) {
if (!ref) {
const resp = await restGithub(`repos/${owner}/${repo}`)
ref = resp!.default_branch
}
const resp = await restGithub(`repos/${owner}/${repo}/commits/${ref}/check-runs`)
let state = typeof context === 'string'
? resp!.check_runs.filter(check => {
const checkName = check.name.toLowerCase().includes(context.toLowerCase())
const appName = check.app.slug.toLowerCase().includes(context.toLowerCase())
const queryUrl = `repos/${owner}/${repo}/commits/${ref}/check-runs`
const searchParams = { check_name }
const resp = await restGithub(queryUrl, searchParams)
return checkName || appName
})
: resp!.check_runs
// console.log('check_runs', searchParams, resp.check_runs.length, resp.check_runs)
let state = resp.check_runs
if (Array.isArray(state)) {
state = combined(state, 'conclusion')
@ -121,7 +118,7 @@ async function checks ({ owner, repo, ref, context}: PathArgs) {
if (state) {
return {
subject: context || 'checks',
subject: check_name || 'checks',
status: state,
color: statesColor[state]
}