Skip to content

Commit

Permalink
chore: functions added by PRs are saved to a Supabase instance (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson authored Aug 10, 2024
1 parent 11f3767 commit e45ed04
Show file tree
Hide file tree
Showing 12 changed files with 2,415 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/register-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Register PR In Database

on:
pull_request_target:
branches: [main]
types: [opened, reopened, synchronize, closed]

jobs:
register-pr:
runs-on: ubuntu-latest
continue-on-error: true
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
cache: pnpm
- uses: actions/github-script@v6
env:
ALGOLIA_KEY: ${{ secrets.ALGOLIA_KEY }}
SUPABASE_KEY: ${{ secrets.SUPABASE_KEY }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const script = await import('${{ github.workspace }}/scripts/radashi-db/ci-register-pr.ts')
await script.run(context, github, console)
11 changes: 11 additions & 0 deletions scripts/radashi-db/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## ./scripts/radashi-db/

This folder contains scripts for managing the Radashi database (hosted on Supabase and Algolia). For example, when a pull request is updated, the `registerPullRequest` function from the `register-pr.ts` module is called to update the database with the latest information about the pull request. This data is then used on certain pages of the Radashi website and the Radashi VSCode extension.

### Environment Variables

To use these scripts, you may need these environment variables:

- `SUPABASE_KEY`: A private API key for Supabase
- `ALGOLIA_KEY`: A private API key for Algolia
- `GITHUB_TOKEN`: A GitHub token with access to the Radashi organization
137 changes: 137 additions & 0 deletions scripts/radashi-db/ci-register-pr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import type { Octokit } from '@octokit/rest'
import { registerPullRequest } from './src/register-pr'

export async function run(
context: Context,
github: Octokit,
console?: Pick<Console, 'log' | 'error'>,
): Promise<void> {
// https://docs.github.com/en/webhooks/webhook-events-and-payloads?actionType=synchronize#pull_request
const pr = context.payload.pull_request
if (!pr) {
throw new Error('No pull request found in context')
}

const owner = pr.head.repo.owner.login
const repo = pr.head.repo.name

const { data: files } = await github.pulls.listFiles({
owner,
repo,
pull_number: pr.number,
})
const { data: checkRuns } = await github.checks.listForRef({
owner,
repo,
ref: pr.head.sha,
})

await registerPullRequest(pr.number, {
status: pr.draft ? 'draft' : pr.merged_at ? 'merged' : pr.state,
sha: pr.head.sha,
repo,
owner,
ownerAvatarUrl: pr.user.avatar_url,
branch: pr.head.ref,
checksPassed: checkRuns.check_runs.every(
run => run.conclusion === 'success',
),
console,
files,
breaking: (pr.labels as { name: string }[]).some(
label => label.name === 'breaking',
),
getApprovalRating: async () => {
const { data: reactions } = await github.reactions.listForIssue({
owner: 'radashi-org',
repo: 'radashi',
issue_number: pr.number,
})
return reactions.filter(reaction => reaction.content === '+1').length
},
getCommit: async () => {
return {
sha: pr.head.sha,
author: pr.user.name,
date: pr.created_at,
}
},
getFileContent: async path => {
const { data } = await github.repos.getContent({
owner,
repo,
path,
ref: pr.head.sha,
})
if (!('content' in data)) {
throw new Error(`File ${path} has no content`)
}
return Buffer.from(data.content, 'base64').toString('utf-8')
},
getIssueBody: async () => {
return pr.body ?? null
},
})
}

/**
* @source https://github.com/actions/toolkit/blob/f003268/packages/github/src/context.ts
*/
interface Context {
payload: WebhookPayload
eventName: string
sha: string
ref: string
workflow: string
action: string
actor: string
job: string
runAttempt: number
runNumber: number
runId: number
apiUrl: string
serverUrl: string
graphqlUrl: string
}

interface PayloadRepository {
[key: string]: any
full_name?: string
name: string
owner: {
[key: string]: any
login: string
name?: string
}
html_url?: string
}

interface WebhookPayload {
[key: string]: any
repository?: PayloadRepository
issue?: {
[key: string]: any
number: number
html_url?: string
body?: string
}
pull_request?: {
[key: string]: any
number: number
html_url?: string
body?: string
}
sender?: {
[key: string]: any
type: string
}
action?: string
installation?: {
id: number
[key: string]: any
}
comment?: {
id: number
[key: string]: any
}
}
25 changes: 25 additions & 0 deletions scripts/radashi-db/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@radashi-org/radashi-db",
"private": true,
"type": "module",
"main": "./index.js",
"dependencies": {
"@octokit/rest": "^21.0.1",
"@supabase/supabase-js": "^2.45.0",
"algoliasearch": "^4.24.0",
"execa": "^9.3.0",
"fast-glob": "^3.3.2",
"markdown-it": "^14.1.0",
"markdown-it-front-matter": "^0.2.4",
"mri": "^1.2.0",
"radashi": "12.2.0-beta.83909af",
"sucrase": "^3.35.0",
"tsx": "^4.17.0",
"ultrahtml": "^1.5.3",
"yaml": "^2.5.0"
},
"devDependencies": {
"@types/markdown-it": "^14.1.2",
"@types/node": "^22.0.0"
}
}
Loading

0 comments on commit e45ed04

Please sign in to comment.