2023-02-11 17:59:00 +03:00
|
|
|
#!/usr/bin/env tea
|
2022-12-21 00:54:26 +03:00
|
|
|
|
|
|
|
/*---
|
|
|
|
args:
|
|
|
|
- deno
|
|
|
|
- run
|
|
|
|
- --allow-net
|
|
|
|
- --allow-env
|
|
|
|
- --allow-write=./artifacts.tgz
|
|
|
|
---*/
|
|
|
|
|
|
|
|
/// Test
|
|
|
|
/// ./scripts/fetch-pr-artifacts.ts e582b03fe6efedde80f9569403555f4513dbec91
|
|
|
|
|
2023-01-12 01:00:05 +03:00
|
|
|
import { S3 } from "s3";
|
|
|
|
import { panic, undent } from "utils";
|
2022-12-21 00:54:26 +03:00
|
|
|
|
|
|
|
/// Main
|
|
|
|
/// -------------------------------------------------------------------------------
|
|
|
|
|
2023-01-12 01:00:05 +03:00
|
|
|
if (import.meta.main) {
|
|
|
|
const usage = "usage: fetch-pr-artifacts.ts {REPO} {SHA} {platform+arch}"
|
|
|
|
const repo = Deno.args[0] ?? panic(usage)
|
|
|
|
const ref = Deno.args[1] ?? panic(usage)
|
|
|
|
const flavor = Deno.args[2] ?? panic(usage)
|
2022-12-21 00:54:26 +03:00
|
|
|
|
2023-01-12 01:00:05 +03:00
|
|
|
const pr = await find_pr(repo, ref)
|
2022-12-21 00:54:26 +03:00
|
|
|
|
2023-01-12 01:00:05 +03:00
|
|
|
if (!pr) throw new Error(`No PR found for commit ${ref} in ${repo}`)
|
2022-12-21 00:54:26 +03:00
|
|
|
|
2023-01-12 01:00:05 +03:00
|
|
|
const s3 = new S3({
|
|
|
|
accessKeyID: Deno.env.get("AWS_ACCESS_KEY_ID")!,
|
|
|
|
secretKey: Deno.env.get("AWS_SECRET_ACCESS_KEY")!,
|
|
|
|
region: "us-east-1",
|
|
|
|
})
|
|
|
|
const bucket = s3.getBucket(Deno.env.get("AWS_S3_CACHE")!)
|
2022-12-21 00:54:26 +03:00
|
|
|
|
2023-01-12 01:00:05 +03:00
|
|
|
const key = `pull-request/${repo.split("/")[1]}/${pr}/${flavor}`
|
|
|
|
const artifacts = (await bucket.getObject(key)) ?? panic("No artifacts found")
|
2022-12-21 00:54:26 +03:00
|
|
|
|
2023-01-12 01:00:05 +03:00
|
|
|
const file = await Deno.open("artifacts.tgz", { create: true, write: true })
|
|
|
|
await artifacts.body.pipeTo(file.writable)
|
2022-12-21 00:54:26 +03:00
|
|
|
|
2023-01-12 01:00:05 +03:00
|
|
|
Deno.stdout.write(new TextEncoder().encode(`PR=${pr}`))
|
|
|
|
}
|
2023-01-04 02:18:49 +03:00
|
|
|
|
2022-12-21 00:54:26 +03:00
|
|
|
/// Functions
|
|
|
|
/// -------------------------------------------------------------------------------
|
|
|
|
|
2023-01-12 01:00:05 +03:00
|
|
|
export async function find_pr(repo: string, ref: string): Promise<number | undefined> {
|
|
|
|
const res = await queryGraphQL<CommitQuery>(prQuery(repo))
|
|
|
|
|
|
|
|
const node = res.repository?.ref?.target?.history?.edges.find(n => n.node.oid === ref)
|
2023-01-12 02:50:57 +03:00
|
|
|
const nodes = node?.node.associatedPullRequests.nodes
|
|
|
|
if (!nodes || nodes.length === 0) return
|
|
|
|
return nodes[0].number
|
2023-01-12 01:00:05 +03:00
|
|
|
}
|
|
|
|
|
2022-12-21 00:54:26 +03:00
|
|
|
async function queryGraphQL<T>(query: string): Promise<T> {
|
|
|
|
const headers: HeadersInit = {}
|
|
|
|
const token = Deno.env.get("GITHUB_TOKEN") ?? panic("GitHub GraphQL requires you set $GITHUB_TOKEN")
|
|
|
|
if (token) headers['Authorization'] = `bearer ${token}`
|
|
|
|
|
|
|
|
const rsp = await fetch('https://api.github.com/graphql', {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({ query }),
|
|
|
|
headers
|
|
|
|
})
|
|
|
|
const json = await rsp.json()
|
|
|
|
|
|
|
|
if (!rsp.ok) {
|
|
|
|
console.error({ rsp, json })
|
|
|
|
throw new Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.data as T ?? panic("No `data` returns from GraphQL endpoint")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Types
|
|
|
|
/// -------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type CommitQuery = {
|
|
|
|
repository: {
|
|
|
|
ref: {
|
|
|
|
target: {
|
|
|
|
history: {
|
|
|
|
edges: Node[]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Node = {
|
|
|
|
node: {
|
|
|
|
url: URL
|
|
|
|
oid: string
|
|
|
|
associatedPullRequests: { nodes: PullRequest[] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type PullRequest = {
|
|
|
|
number: number
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Queries
|
|
|
|
/// -------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function prQuery(repo: string): string {
|
|
|
|
const [owner, name] = repo.split("/")
|
|
|
|
return undent`
|
|
|
|
query {
|
|
|
|
repository(name: "${name}", owner: "${owner}") {
|
|
|
|
ref(qualifiedName: "main") {
|
|
|
|
target {
|
|
|
|
... on Commit {
|
|
|
|
history(first: 100) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
url
|
|
|
|
oid
|
|
|
|
associatedPullRequests(first: 1) {
|
|
|
|
nodes {
|
|
|
|
number
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
}
|