-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(apps/prod/tekton/configs/triggers/tests): solve the api rate limi…
…t of GitHub (#1349) Changes: - we need to pass the `--token` option to provide the GitHub private token to solve the api rate limit. Signed-off-by: wuhuizuo <[email protected]>
- Loading branch information
Showing
1 changed file
with
31 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
#!/usr/bin/env deno run --allow-net | ||
|
||
import { parseArgs } from "https://deno.land/[email protected]/cli/parse_args.ts"; | ||
import { parseArgs } from "jsr:@std/cli@^1.0.1"; | ||
import { Octokit } from "https://esm.sh/[email protected]?dts"; | ||
|
||
interface CliParams { | ||
token: string; // github private token | ||
url: string; // repo url: https://github.com/pingcap/tidb.git | ||
ref?: string; // refs/heads/master | ||
eventUrl?: string; // https://example.com | ||
eventType?: string; // push | create | ||
} | ||
|
||
interface Payload { | ||
ref: string; | ||
|
@@ -21,27 +30,24 @@ async function getCommitSha( | |
owner: string, | ||
repo: string, | ||
ref: string, | ||
client: Octokit, | ||
): Promise<string> { | ||
const apiUrl = `https://api.github.com/repos/${owner}/${repo}/commits/${ref}`; | ||
const response = await fetch(apiUrl); | ||
if (!response.ok) { | ||
throw new Error(`status: ${response.status}`); | ||
} | ||
const commit = await response.json(); | ||
return commit.sha; | ||
const response = await client.rest.repos.getCommit({ owner, repo, ref }); | ||
return response.data.sha; | ||
} | ||
|
||
async function generatePushEventPayload( | ||
gitUrl: string, | ||
ref: string, | ||
client: Octokit, | ||
) { | ||
const url = new URL(gitUrl); | ||
const owner = url.pathname.split("/")[1]; | ||
const repoName = url.pathname.split("/").pop()?.replace(/\.git$/, ""); | ||
|
||
return { | ||
before: "0000000000000000000000000000000000000000", | ||
after: await getCommitSha(owner, repoName!, ref), | ||
after: await getCommitSha(owner, repoName!, ref, client), | ||
ref, | ||
repository: { | ||
name: repoName!, | ||
|
@@ -81,6 +87,7 @@ async function sendGithubEvent( | |
gitUrl: string, | ||
ref: string, | ||
eventUrl: string, | ||
client: Octokit, | ||
): Promise<void> { | ||
let eventPayload; | ||
switch (eventType) { | ||
|
@@ -95,6 +102,7 @@ async function sendGithubEvent( | |
eventPayload = await generatePushEventPayload( | ||
gitUrl, | ||
`refs/heads/${ref.replace("refs/heads/", "")}`, | ||
client, | ||
); | ||
break; | ||
default: | ||
|
@@ -115,21 +123,26 @@ async function sendGithubEvent( | |
await fetch(eventUrl, fetchInit); | ||
} | ||
|
||
async function main() { | ||
const args = parseArgs(Deno.args); | ||
async function main(args: CliParams) { | ||
const gitUrl = args.url; | ||
const ref = args.ref || "refs/heads/master"; | ||
const eventUrl = args.eventUrl || "https://example.com"; | ||
const eventType = args.eventType; | ||
const eventType = args.eventType || "push"; | ||
|
||
if (!gitUrl) { | ||
console.error( | ||
"Usage: deno run script.ts --url <git-url> --eventType push|create [--ref <ref>] [--eventUrl <event-url>]", | ||
); | ||
const usage = | ||
"Usage: deno run script.ts --token <github-token> --url <git-url> --eventType push|create [--ref <ref>] [--eventUrl <event-url>]"; | ||
if (!gitUrl || !args.token) { | ||
console.error(usage); | ||
Deno.exit(1); | ||
} | ||
|
||
await sendGithubEvent(eventType, gitUrl, ref, eventUrl); | ||
const octokit = new Octokit({ | ||
auth: args.token, | ||
}); | ||
|
||
await sendGithubEvent(eventType, gitUrl, ref, eventUrl, octokit); | ||
} | ||
|
||
await main(); | ||
const args = parseArgs(Deno.args) as CliParams; | ||
await main(args); | ||
Deno.exit(); |