refactor: rely more on the db instead of querying GH

This commit is contained in:
tdurieux
2024-04-06 15:15:08 +01:00
parent 968a59726c
commit 389030adc9
4 changed files with 15 additions and 9 deletions

View File

@@ -69,6 +69,7 @@ async function main() {
accessToken: inq.token, accessToken: inq.token,
owner: ghURL.owner, owner: ghURL.owner,
repo: ghURL.name, repo: ghURL.name,
force: true,
}); });
const branches = await ghRepo.branches({ const branches = await ghRepo.branches({
accessToken: inq.token, accessToken: inq.token,

View File

@@ -324,6 +324,7 @@ export default class Repository {
accessToken: await this.getToken(), accessToken: await this.getToken(),
owner: this.source.data.organization, owner: this.source.data.organization,
repo: this.source.data.repoName, repo: this.source.data.repoName,
force: true,
}); });
if (repository.size) { if (repository.size) {
if ( if (

View File

@@ -230,6 +230,7 @@ export async function getRepositoryFromGitHub(opt: {
owner: string; owner: string;
repo: string; repo: string;
accessToken: string; accessToken: string;
force?: boolean;
}) { }) {
const span = trace const span = trace
.getTracer("ano-file") .getTracer("ano-file")
@@ -240,6 +241,14 @@ export async function getRepositoryFromGitHub(opt: {
if (opt.repo.indexOf(".git") > -1) { if (opt.repo.indexOf(".git") > -1) {
opt.repo = opt.repo.replace(".git", ""); opt.repo = opt.repo.replace(".git", "");
} }
const dbModel = isConnected
? await RepositoryModel.findOne({
name: opt.owner + "/" + opt.repo,
})
: null;
if (dbModel && !opt.force) {
return new GitHubRepository(dbModel);
}
const oct = octokit(opt.accessToken); const oct = octokit(opt.accessToken);
let r: RestEndpointMethodTypes["repos"]["get"]["response"]["data"]; let r: RestEndpointMethodTypes["repos"]["get"]["response"]["data"];
try { try {
@@ -268,15 +277,7 @@ export async function getRepositoryFromGitHub(opt: {
repo: opt.repo, repo: opt.repo,
}, },
}); });
let model = new RepositoryModel({ externalId: "gh_" + r.id }); const model = dbModel || new RepositoryModel({ externalId: "gh_" + r.id });
if (isConnected) {
const dbModel = await RepositoryModel.findOne({
externalId: "gh_" + r.id,
});
if (dbModel) {
model = dbModel;
}
}
model.name = r.full_name; model.name = r.full_name;
model.url = r.html_url; model.url = r.html_url;
model.size = r.size; model.size = r.size;

View File

@@ -191,6 +191,7 @@ router.get(
owner: req.params.owner, owner: req.params.owner,
repo: req.params.repo, repo: req.params.repo,
accessToken: token, accessToken: token,
force: req.query.force == "1",
}); });
res.json(repo.toJSON()); res.json(repo.toJSON());
} catch (error) { } catch (error) {
@@ -212,6 +213,7 @@ router.get(
accessToken: token, accessToken: token,
owner: req.params.owner, owner: req.params.owner,
repo: req.params.repo, repo: req.params.repo,
force: req.query.force == "1",
}); });
return res.json( return res.json(
await repository.branches({ await repository.branches({
@@ -239,6 +241,7 @@ router.get(
owner: req.params.owner, owner: req.params.owner,
repo: req.params.repo, repo: req.params.repo,
accessToken: token, accessToken: token,
force: req.query.force == "1",
}); });
if (!repo) { if (!repo) {
throw new AnonymousError("repo_not_found", { throw new AnonymousError("repo_not_found", {