Last active
May 3, 2021 01:01
-
-
Save gtrabanco/7bea0d777e318d2f310f06576b23eab5 to your computer and use it in GitHub Desktop.
Get using deno the number of Pull Request that are open in a GitHub repository
This file contains hidden or 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
| #!/usr/bin/env -S deno run --allow-env --allow-net | |
| import { parse } from "https://deno.land/[email protected]/flags/mod.ts"; | |
| import { red, green } from "https://deno.land/[email protected]/fmt/colors.ts"; | |
| const enum PullRequestState { | |
| open = "open", | |
| close = "close" | |
| } | |
| const enum HttpMethod { | |
| GET = "GET", | |
| POST = "POST", | |
| PUT = "PUT", | |
| PATCH = "PATCH", | |
| DELETE = "DELETE", | |
| } | |
| interface PullRequestParameters { | |
| user: string, | |
| repository: string | |
| } | |
| interface PullRequestStateParameters extends PullRequestParameters { | |
| state: PullRequestState, | |
| } | |
| interface GitHubApiRequestParameters { | |
| apiUrl: string, | |
| method: HttpMethod | |
| } | |
| const object2UrlParameters = (objectValues:object): string => { | |
| let returnedParams:string = "" | |
| Object.getOwnPropertyNames(objectValues).forEach((paramName) => { | |
| let key = paramName as keyof object //Avoiding Index Signature | |
| let value = objectValues[key] | |
| returnedParams = `${returnedParams}&${paramName}=${value}` | |
| }) | |
| return returnedParams.slice(1) | |
| } | |
| const GitHubApiUrl = { | |
| baseUrl:() => "https://api.github.com", | |
| getUrl:(queryPath:Array<string>, params:object={}): string => { | |
| let paramsAsString:string="" | |
| if (params) { | |
| paramsAsString=`?${object2UrlParameters(params)}` | |
| } | |
| return `${GitHubApiUrl.baseUrl()}/${queryPath.join("/")}${paramsAsString}` | |
| }, | |
| pullRequestFor: ({user, repository}:PullRequestParameters): string => | |
| GitHubApiUrl.getUrl(["repos", user, repository, "pulls"]), | |
| pullRequestStateFor: ({user, repository, state}: PullRequestStateParameters): string => | |
| GitHubApiUrl.getUrl(["repos", user, repository, "pulls"], {state}) | |
| } | |
| async function GitHubApiRequest(apiUrl: string): Promise<any> { | |
| let headers: Headers = new Headers({ | |
| Accept: "application/vnd.github.v3.star+json" | |
| }); | |
| const GITHUB_TOKEN = Deno.env.get("GITHUB_TOKEN")? Deno.env.get("GITHUB_TOKEN"): null; | |
| if (GITHUB_TOKEN !== null) { | |
| headers.append("Authorization", `token ${GITHUB_TOKEN}`) | |
| } | |
| const response = await fetch(apiUrl, { | |
| method: HttpMethod.GET, | |
| headers, | |
| redirect: "follow" | |
| }) | |
| return await response.json() //body | |
| } | |
| const parsedArgs = parse(Deno.args, { | |
| string: ["user", "organization"], | |
| alias: { | |
| user: ["u", "o", "organization"], | |
| repository: ["r"] | |
| }, | |
| default: { | |
| user: "codelytv", | |
| repository: "dotly" | |
| } | |
| }) | |
| const jsonRepository = await GitHubApiRequest(GitHubApiUrl.pullRequestStateFor({ | |
| user: parsedArgs.user, | |
| repository: parsedArgs.repository, | |
| state: PullRequestState.open | |
| })) | |
| if (jsonRepository == "Not Found") { | |
| console.warn(red("Repositorio o usuario no encontrado")) | |
| } else { | |
| console.log(green("El repositorio de GitHub %s/%s, tiene %d PRs abiertas"), | |
| parsedArgs.user, | |
| parsedArgs.repository, | |
| jsonRepository.length | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment