Skip to content

Commit

Permalink
#292 : implement GitHub calls for repo metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
guillermau committed Mar 6, 2025
1 parent 96a4a17 commit e2bec66
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
78 changes: 78 additions & 0 deletions api/src/core/adapters/GitHub/api/repo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Octokit } from "@octokit/rest";

import { env } from "../../../../env";

export const repoGitHubEndpointMaker = (repoUrl: string | URL) => {
const octokit = new Octokit({
auth: env.githubPersonalAccessTokenForApiRateLimit
});
let repoUrlObj = typeof repoUrl === "string" ? URL.parse(repoUrl) : repoUrl;
if (!repoUrlObj) return undefined;

// Case .git at the end
if (repoUrlObj.pathname.endsWith("/")) repoUrlObj.pathname = repoUrlObj.pathname.slice(0, -1);
if (repoUrlObj.pathname.endsWith(".git")) repoUrlObj.pathname = repoUrlObj.pathname.slice(0, -4);

const parsed = repoUrlObj.pathname.split("/").filter(text => text);

const repo = parsed[1];
const owner = parsed[0];

return {
issues: {
getLastClosedIssue: async () => {
try {
const resIssues = await octokit.request("GET /repos/{owner}/{repo}/issues", {
owner,
repo,
headers: {
"X-GitHub-Api-Version": "2022-11-28"
},
direction: "desc",
state: "closed"
});

return resIssues.data[0];
} catch (error) {
return undefined;
}
}
},
commits: {
getLastCommit: async () => {
try {
const resCommit = await octokit.request("GET /repos/{owner}/{repo}/commits", {
owner,
repo,
headers: {
"X-GitHub-Api-Version": "2022-11-28"
},
direction: "desc"
});
return resCommit.data[0];
} catch (error) {
return undefined;
}
}
},
mergeRequests: {
getLast: async () => {
try {
const resPull = await octokit.request("GET /repos/{owner}/{repo}/pulls", {
owner,
repo,
headers: {
"X-GitHub-Api-Version": "2022-11-28"
},
direction: "desc",
state: "closed"
});

return resPull.data[0];
} catch (error) {
return undefined;
}
}
}
};
};
26 changes: 25 additions & 1 deletion api/src/core/adapters/hal/getHalSoftwareExternalData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { SILL } from "../../../types/SILL";
import { HAL } from "./types/HAL";
import { repoAnalyser, RepoType } from "../../../tools/repoAnalyser";
import { projectGitLabApiMaker } from "../GitLab/api/project";
import { repoGitHubEndpointMaker } from "../GitHub/api/repo";

const buildParentOrganizationTree = async (
structureIdArray: number[] | string[] | undefined
Expand Down Expand Up @@ -239,7 +240,30 @@ export const getHalSoftwareExternalData: GetSoftwareExternalData = memoize(
}
};
case "GitHub":
return undefined;
const gitHubApi = repoGitHubEndpointMaker(halRawSoftware?.softCodeRepository_s?.[0]);
if (!gitHubApi) {
console.error("Bad URL string");
return undefined;
}

const lastGHCommit = await gitHubApi.commits.getLastCommit();
const lastGHCloseIssue = await gitHubApi.issues.getLastClosedIssue();
const lastGHClosedPull = await gitHubApi.mergeRequests.getLast();

return {
healthCheck: {
lastCommit: lastGHCommit?.commit?.author?.date
? new Date(lastGHCommit.commit.author.date).valueOf()
: undefined,
lastClosedIssue: lastGHCloseIssue?.closed_at
? new Date(lastGHCloseIssue.closed_at).valueOf()
: undefined,
lastClosedIssuePullRequest: lastGHClosedPull?.closed_at
? new Date(lastGHClosedPull.closed_at).valueOf()
: undefined
}
};

case undefined:
return undefined;
default:
Expand Down

0 comments on commit e2bec66

Please sign in to comment.