fix: fix authentification for PR download

This commit is contained in:
tdurieux
2023-04-20 13:44:35 +02:00
parent a07c8d4635
commit 0a021d6e61
+30 -11
View File
@@ -20,40 +20,59 @@ export default class PullRequest {
this.owner.model.isNew = false; this.owner.model.isNew = false;
} }
getToken() { async getToken() {
if (this.owner && this.owner.accessToken) { let owner = this.owner.model;
return this.owner.accessToken; if (owner && !owner.accessTokens.github) {
const temp = await UserModel.findById(owner._id);
if (temp) {
owner = temp;
}
}
if (owner && owner.accessTokens && owner.accessTokens.github) {
if (owner.accessTokens.github != this._model.source.accessToken) {
this._model.source.accessToken = owner.accessTokens.github;
}
return owner.accessTokens.github;
} }
if (this._model.source.accessToken) { if (this._model.source.accessToken) {
try { try {
return this._model.source.accessToken; return this._model.source.accessToken;
} catch (error) { } catch (error) {
console.debug("[ERROR] Token is invalid", this.pullRequestId); console.debug(
"[ERROR] Token is invalid",
this._model.source.pullRequestId
);
} }
} }
return config.GITHUB_TOKEN; return config.GITHUB_TOKEN;
} }
async download() { async download() {
console.debug("[INFO] Downloading pull request", this.pullRequestId); console.debug(
const auth = this.getToken(); "[INFO] Downloading pull request",
const octokit = new Octokit({ auth }); this._model.source.pullRequestId
);
const auth = await this.getToken();
const octokit = new Octokit({ auth: auth });
const [owner, repo] = this._model.source.repositoryFullName.split("/"); const [owner, repo] = this._model.source.repositoryFullName.split("/");
const pull_number = this._model.source.pullRequestId; const pull_number = this._model.source.pullRequestId;
const [prInfo, comments, diff] = await Promise.all([ const [prInfo, comments, diff] = await Promise.all([
octokit.rest.pulls.get({ octokit.rest.pulls.get({
owner, owner,
repo, repo,
pull_number, pull_number,
}), }),
octokit.rest.issues.listComments({ octokit.paginate(octokit.rest.issues.listComments, {
owner, owner: owner,
repo, repo: repo,
issue_number: pull_number, issue_number: pull_number,
per_page: 100, per_page: 100,
}), }),
got(`https://github.com/${owner}/${repo}/pull/${pull_number}.diff`), got(`https://github.com/${owner}/${repo}/pull/${pull_number}.diff`),
]); ]);
this._model.pullRequest = { this._model.pullRequest = {
diff: diff.body, diff: diff.body,
title: prInfo.data.title, title: prInfo.data.title,
@@ -68,7 +87,7 @@ export default class PullRequest {
state: prInfo.data.state, state: prInfo.data.state,
baseRepositoryFullName: prInfo.data.base.repo.full_name, baseRepositoryFullName: prInfo.data.base.repo.full_name,
headRepositoryFullName: prInfo.data.head.repo?.full_name, headRepositoryFullName: prInfo.data.head.repo?.full_name,
comments: comments.data.map((comment) => ({ comments: comments.map((comment) => ({
body: comment.body || "", body: comment.body || "",
creationDate: new Date(comment.created_at), creationDate: new Date(comment.created_at),
updatedDate: new Date(comment.updated_at), updatedDate: new Date(comment.updated_at),