generated from chiffre-io/template-library
-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc: Dynamic GHA status on landing page
- Loading branch information
Showing
4 changed files
with
130 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { cn } from '@/src/lib/utils' | ||
import React from 'react' | ||
import { z } from 'zod' | ||
|
||
export async function GitHubActionsStatus({ | ||
className, | ||
...props | ||
}: React.ComponentProps<'div'>) { | ||
const statuses = await getGitHubActionsStatus() | ||
return ( | ||
<div | ||
className={cn( | ||
'flex items-center space-x-[-12px] md:space-x-[-14px]', | ||
className | ||
)} | ||
aria-label="Last 5 GitHub Actions status" | ||
{...props} | ||
> | ||
{statuses.map(status => { | ||
const color = { | ||
SUCCESS: 'bg-green-500', | ||
FAILURE: 'bg-red-500', | ||
CANCELLED: 'bg-zinc-500', | ||
TIMED_OUT: 'bg-zinc-500', | ||
ACTION_REQUIRED: 'bg-purple-500', | ||
NEUTRAL: 'bg-zinc-500' | ||
}[status.checkSuite.conclusion] | ||
return ( | ||
<a key={status.id} href={status.url} className="rounded-full p-1"> | ||
<div | ||
aria-label={status.checkSuite.conclusion} | ||
className={cn( | ||
'h-4 w-4 rounded-full border-2 border-background bg-current md:h-5 md:w-5', | ||
color | ||
)} | ||
/> | ||
</a> | ||
) | ||
})} | ||
</div> | ||
) | ||
} | ||
|
||
const ghaStatusSchema = z.object({ | ||
id: z.string(), | ||
url: z.string().url(), | ||
createdAt: z.string().datetime(), | ||
checkSuite: z.object({ | ||
status: z.enum(['QUEUED', 'IN_PROGRESS', 'COMPLETED']), | ||
conclusion: z.enum([ | ||
'SUCCESS', | ||
'FAILURE', | ||
'CANCELLED', | ||
'TIMED_OUT', | ||
'ACTION_REQUIRED', | ||
'NEUTRAL' | ||
]) | ||
}) | ||
}) | ||
|
||
async function getGitHubActionsStatus() { | ||
const query = `query { | ||
node(id: "W_kwDOD6wJuM4EeKz5") { | ||
... on Workflow { | ||
runs(first: 5) { | ||
nodes { | ||
id | ||
url | ||
createdAt | ||
checkSuite { | ||
status | ||
conclusion | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}`.replace(/\s+/g, ' ') // Minify | ||
const json = await fetch(`https://api.github.com/graphql?repo=47ng/nuqs`, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: `bearer ${process.env.GITHUB_TOKEN}` | ||
}, | ||
body: JSON.stringify({ query }), | ||
next: { | ||
tags: ['github-actions-status'] | ||
} | ||
}).then(res => res.json()) | ||
return z.array(ghaStatusSchema).parse(json.data.node.runs.nodes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { revalidateTag } from 'next/cache' | ||
import { NextRequest, NextResponse } from 'next/server' | ||
|
||
const ACCEPTED_TAGS = ['github', 'github-actions-status'] | ||
|
||
export async function GET(req: NextRequest) { | ||
const token = req.nextUrl.searchParams.get('token') | ||
if (token !== process.env.ISR_TOKEN) { | ||
return NextResponse.json({ error: 'Invalid token' }, { status: 400 }) | ||
} | ||
const now = new Date() | ||
const tag = req.nextUrl.searchParams.get('tag') | ||
if (!tag || !ACCEPTED_TAGS.includes(tag)) { | ||
return NextResponse.json({ error: 'Invalid tag' }, { status: 400 }) | ||
} | ||
revalidateTag(tag) | ||
return NextResponse.json({ | ||
at: now.toISOString(), | ||
revalidated: tag | ||
}) | ||
} |