feat: now token can be undefined

This commit is contained in:
monoid 2022-06-11 18:58:40 +09:00
parent d2cf8bdea4
commit 0fc7294905

View File

@ -14,9 +14,14 @@ import "https://deno.land/std@0.136.0/dotenv/load.ts";
* console.log(issues);
* ```
*/
export async function getIssues(repo: string, token: string): Promise<Issue[]> {
export async function getIssues(repo: string, token?: string): Promise<Issue[]> {
//check https://docs.github.com/en/rest/reference/issues#list-repository-issues
const res = await fetch(`https://api.github.com/repos/${repo}/issues?per_page=100&labels=feature&state=all`, {
const url = `https://api.github.com/repos/${repo}/issues?per_page=100`;
if(!token) {
const res = await fetch(url);
return await res.json();
}
const res = await fetch(url, {
headers: {
Accept: 'application/vnd.github.v3+json',
Authorization: `Token ${token}`
@ -28,15 +33,7 @@ export async function getIssues(repo: string, token: string): Promise<Issue[]> {
if (import.meta.main) {
const args = parse(Deno.args);
const token = args.token ?? Deno.env.get("GITHUB_TOKEN");
if(typeof token !== "string"){
console.error("invalid type: token must be string");
Deno.exit(1);
}
if(!token) {
console.error("GITHUB_TOKEN is not set");
Deno.exit(1);
}
let token = args.token ?? Deno.env.get("GITHUB_TOKEN");
const issues = await getIssues("vi117/scrap-yard", token);
issues.sort((a, b) => a.number - b.number);
const content = JSON.stringify(issues, null, 2);