From 6410220e91f77064cb6b6983f1245390dac81e6c Mon Sep 17 00:00:00 2001 From: Amio Date: Sun, 2 Jul 2023 09:14:25 +0800 Subject: [PATCH] feat: use default branch for GitHub commits when appropriate #575 --- pages/api/github.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/pages/api/github.ts b/pages/api/github.ts index 58c60bd..11ef456 100644 --- a/pages/api/github.ts +++ b/pages/api/github.ts @@ -279,7 +279,11 @@ const makeRepoQuery = (topic, owner, repo, restArgs) => { break case 'commits': queryBody = ` - branch: ref(qualifiedName: "${restArgs.ref || 'master'}") { + ${ + restArgs.ref + ? `branch: ref(qualifiedName: "${restArgs.ref}")` + : 'defaultBranchRef' + } { target { ... on Commit { history(first: 0) { @@ -292,7 +296,11 @@ const makeRepoQuery = (topic, owner, repo, restArgs) => { break case 'last-commit': queryBody = ` - branch: ref(qualifiedName: "${restArgs.ref || 'master'}") { + ${ + restArgs.ref ? + `branch: ref(qualifiedName: "${restArgs.ref}")` : + 'defaultBranchRef' + } { target { ... on Commit { history(first: 1) { @@ -442,7 +450,11 @@ async function repoStats ({topic, owner, repo, ...restArgs}: PathArgs) { case 'commits': return { subject: topic, - status: millify(result.branch.target.history.totalCount), + status: millify( + result.branch ? + result.branch.target.history.totalCount : + result.defaultBranchRef.target.history.totalCount + ), color: 'blue' } case 'tag': @@ -461,7 +473,8 @@ async function repoStats ({topic, owner, repo, ...restArgs}: PathArgs) { // color: li ? 'blue' : 'grey' // } case 'last-commit': - const commits = result.branch.target.history.nodes + const branch = result.branch || result.defaultBranchRef + const commits = branch.target.history.nodes const lastDate = commits.length && new Date(commits[0].committedDate) const fromNow = lastDate && distanceToNow(lastDate, { addSuffix: true }) return {