Skip to content

Commit

Permalink
fix(apps/prod/tekton/configs/triggers/tests): solve the api rate limi…
Browse files Browse the repository at this point in the history
…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
wuhuizuo authored Nov 20, 2024
1 parent 2498cf1 commit 8a40735
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions apps/prod/tekton/configs/triggers/tests/test-github-events.ts
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;
Expand All @@ -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!,
Expand Down Expand Up @@ -81,6 +87,7 @@ async function sendGithubEvent(
gitUrl: string,
ref: string,
eventUrl: string,
client: Octokit,
): Promise<void> {
let eventPayload;
switch (eventType) {
Expand All @@ -95,6 +102,7 @@ async function sendGithubEvent(
eventPayload = await generatePushEventPayload(
gitUrl,
`refs/heads/${ref.replace("refs/heads/", "")}`,
client,
);
break;
default:
Expand All @@ -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();

0 comments on commit 8a40735

Please sign in to comment.