|
| 1 | +import { Octokit } from "@octokit/rest"; |
| 2 | + |
| 3 | +import { env } from "../../../../env"; |
| 4 | + |
| 5 | +export const repoGitHubEndpointMaker = (repoUrl: string | URL) => { |
| 6 | + const octokit = new Octokit({ |
| 7 | + auth: env.githubPersonalAccessTokenForApiRateLimit |
| 8 | + }); |
| 9 | + let repoUrlObj = typeof repoUrl === "string" ? URL.parse(repoUrl) : repoUrl; |
| 10 | + if (!repoUrlObj) return undefined; |
| 11 | + |
| 12 | + // Case .git at the end |
| 13 | + if (repoUrlObj.pathname.endsWith("/")) repoUrlObj.pathname = repoUrlObj.pathname.slice(0, -1); |
| 14 | + if (repoUrlObj.pathname.endsWith(".git")) repoUrlObj.pathname = repoUrlObj.pathname.slice(0, -4); |
| 15 | + |
| 16 | + const parsed = repoUrlObj.pathname.split("/").filter(text => text); |
| 17 | + |
| 18 | + const repo = parsed[1]; |
| 19 | + const owner = parsed[0]; |
| 20 | + |
| 21 | + return { |
| 22 | + issues: { |
| 23 | + getLastClosedIssue: async () => { |
| 24 | + try { |
| 25 | + const resIssues = await octokit.request("GET /repos/{owner}/{repo}/issues", { |
| 26 | + owner, |
| 27 | + repo, |
| 28 | + headers: { |
| 29 | + "X-GitHub-Api-Version": "2022-11-28" |
| 30 | + }, |
| 31 | + direction: "desc", |
| 32 | + state: "closed" |
| 33 | + }); |
| 34 | + |
| 35 | + return resIssues.data[0]; |
| 36 | + } catch (error) { |
| 37 | + return undefined; |
| 38 | + } |
| 39 | + } |
| 40 | + }, |
| 41 | + commits: { |
| 42 | + getLastCommit: async () => { |
| 43 | + try { |
| 44 | + const resCommit = await octokit.request("GET /repos/{owner}/{repo}/commits", { |
| 45 | + owner, |
| 46 | + repo, |
| 47 | + headers: { |
| 48 | + "X-GitHub-Api-Version": "2022-11-28" |
| 49 | + }, |
| 50 | + direction: "desc" |
| 51 | + }); |
| 52 | + return resCommit.data[0]; |
| 53 | + } catch (error) { |
| 54 | + return undefined; |
| 55 | + } |
| 56 | + } |
| 57 | + }, |
| 58 | + mergeRequests: { |
| 59 | + getLast: async () => { |
| 60 | + try { |
| 61 | + const resPull = await octokit.request("GET /repos/{owner}/{repo}/pulls", { |
| 62 | + owner, |
| 63 | + repo, |
| 64 | + headers: { |
| 65 | + "X-GitHub-Api-Version": "2022-11-28" |
| 66 | + }, |
| 67 | + direction: "desc", |
| 68 | + state: "closed" |
| 69 | + }); |
| 70 | + |
| 71 | + return resPull.data[0]; |
| 72 | + } catch (error) { |
| 73 | + return undefined; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + }; |
| 78 | +}; |
0 commit comments