Skip to content

Commit 39a6a3b

Browse files
committed
#292 : implement GitHub calls for repo metadata
1 parent 5fdec5f commit 39a6a3b

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
};

api/src/core/adapters/hal/getHalSoftwareExternalData.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { SILL } from "../../../types/SILL";
77
import { HAL } from "./types/HAL";
88
import { repoAnalyser, RepoType } from "../../../tools/repoAnalyser";
99
import { projectGitLabApiMaker } from "../GitLab/api/project";
10+
import { repoGitHubEndpointMaker } from "../GitHub/api/repo";
1011

1112
const buildParentOrganizationTree = async (
1213
structureIdArray: number[] | string[] | undefined
@@ -239,7 +240,30 @@ export const getHalSoftwareExternalData: GetSoftwareExternalData = memoize(
239240
}
240241
};
241242
case "GitHub":
242-
return undefined;
243+
const gitHubApi = repoGitHubEndpointMaker(halRawSoftware?.softCodeRepository_s?.[0]);
244+
if (!gitHubApi) {
245+
console.error("Bad URL string");
246+
return undefined;
247+
}
248+
249+
const lastGHCommit = await gitHubApi.commits.getLastCommit();
250+
const lastGHCloseIssue = await gitHubApi.issues.getLastClosedIssue();
251+
const lastGHClosedPull = await gitHubApi.mergeRequests.getLast();
252+
253+
return {
254+
healthCheck: {
255+
lastCommit: lastGHCommit?.commit?.author?.date
256+
? new Date(lastGHCommit.commit.author.date).valueOf()
257+
: undefined,
258+
lastClosedIssue: lastGHCloseIssue?.closed_at
259+
? new Date(lastGHCloseIssue.closed_at).valueOf()
260+
: undefined,
261+
lastClosedIssuePullRequest: lastGHClosedPull?.closed_at
262+
? new Date(lastGHClosedPull.closed_at).valueOf()
263+
: undefined
264+
}
265+
};
266+
243267
case undefined:
244268
return undefined;
245269
default:

0 commit comments

Comments
 (0)