SRS/tools/getIssue.ts

46 lines
1.4 KiB
TypeScript
Raw Permalink Normal View History

2022-04-19 18:26:47 +09:00
#! /usr/bin/env deno run --allow-net --allow-env
2022-04-19 19:53:00 +09:00
import {Issue} from "./githubType.ts";
2022-04-20 11:14:48 +09:00
import {parse} from "https://deno.land/std@0.135.0/flags/mod.ts";
2022-04-21 17:50:24 +09:00
import "https://deno.land/std@0.136.0/dotenv/load.ts";
2022-04-19 18:26:47 +09:00
/**
* get issue from github
* @param repo repository name with owner (e.g. denoland/deno)
* @param token your github token
* @returns up to 100 issue objects
* @example
* ```ts
* const issues = await getIssues('denoland/deno', 'your-token')
* console.log(issues);
* ```
*/
2022-06-11 18:58:40 +09:00
export async function getIssues(repo: string, token?: string): Promise<Issue[]> {
2022-04-19 19:53:00 +09:00
//check https://docs.github.com/en/rest/reference/issues#list-repository-issues
2022-06-11 19:38:24 +09:00
const url = `https://api.github.com/repos/${repo}/issues?per_page=100&labels=feature&state=all`;
2022-06-11 18:58:40 +09:00
if(!token) {
const res = await fetch(url);
return await res.json();
}
const res = await fetch(url, {
2022-04-19 18:26:47 +09:00
headers: {
Accept: 'application/vnd.github.v3+json',
Authorization: `Token ${token}`
}
});
return await res.json();
}
if (import.meta.main) {
2022-04-20 11:14:48 +09:00
const args = parse(Deno.args);
2022-06-11 18:58:40 +09:00
let token = args.token ?? Deno.env.get("GITHUB_TOKEN");
2022-04-19 18:26:47 +09:00
const issues = await getIssues("vi117/scrap-yard", token);
2022-04-19 18:30:41 +09:00
issues.sort((a, b) => a.number - b.number);
2022-04-20 17:11:45 +09:00
const content = JSON.stringify(issues, null, 2);
if(typeof args.path === "string"){
Deno.writeTextFileSync(args.path, content);
}
else{
console.log(content);
}
2022-04-19 18:26:47 +09:00
}