mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-02-13 10:52:53 +00:00
fix: fix repository update
This commit is contained in:
@@ -170,7 +170,7 @@ export default class Repository {
|
||||
if (this._model.status == "ready") return;
|
||||
await this.updateStatus("preparing");
|
||||
await this.files();
|
||||
await this.updateStatus("ready");
|
||||
return this.updateStatus("ready");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -179,7 +179,7 @@ export default class Repository {
|
||||
async countView() {
|
||||
this._model.lastView = new Date();
|
||||
this._model.pageView = (this._model.pageView || 0) + 1;
|
||||
await this._model.save();
|
||||
return this._model.save();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -190,21 +190,21 @@ export default class Repository {
|
||||
async updateStatus(status: RepositoryStatus, errorMessage?: string) {
|
||||
this._model.status = status;
|
||||
this._model.errorMessage = errorMessage;
|
||||
await this._model.save();
|
||||
return this._model.save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Expire the repository
|
||||
*/
|
||||
async expire() {
|
||||
this.resetSate("expired");
|
||||
return this.resetSate("expired");
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the repository
|
||||
*/
|
||||
async remove() {
|
||||
this.resetSate("removed");
|
||||
return this.resetSate("removed");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -13,22 +13,6 @@ import Repository from "../Repository";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// get repository information
|
||||
router.get("/:repoId/", async (req: express.Request, res: express.Response) => {
|
||||
const repo = await getRepo(req, res, { nocheck: false });
|
||||
if (!repo) return;
|
||||
|
||||
try {
|
||||
const user = await getUser(req);
|
||||
if (user.username != repo.model.owner) {
|
||||
return res.status(401).send({ error: "not_authorized" });
|
||||
}
|
||||
res.json((await db.getRepository(req.params.repoId)).toJSON());
|
||||
} catch (error) {
|
||||
handleError(error, res);
|
||||
}
|
||||
});
|
||||
|
||||
// user needs to be connected for all user API
|
||||
router.use(ensureAuthenticated);
|
||||
|
||||
@@ -71,7 +55,7 @@ router.post("/claim", async (req: express.Request, res: express.Response) => {
|
||||
}
|
||||
});
|
||||
|
||||
// refresh a repository
|
||||
// refresh repository
|
||||
router.post(
|
||||
"/:repoId/refresh",
|
||||
async (req: express.Request, res: express.Response) => {
|
||||
@@ -183,23 +167,26 @@ function validateNewRepo(repoUpdate) {
|
||||
) {
|
||||
throw new Error("invalid_repoId");
|
||||
}
|
||||
if (!repoUpdate.branch) {
|
||||
if (!repoUpdate.source.branch) {
|
||||
throw new Error("branch_not_specified");
|
||||
}
|
||||
if (!repoUpdate.source.commit) {
|
||||
throw new Error("commit_not_specified");
|
||||
}
|
||||
if (!repoUpdate.options) {
|
||||
throw new Error("options_not_provided");
|
||||
}
|
||||
if (!Array.isArray(repoUpdate.terms)) {
|
||||
throw new Error("invalid_terms_format");
|
||||
}
|
||||
if (!/^[a-f0-9]+$/.test(repoUpdate.commit)) {
|
||||
if (!/^[a-f0-9]+$/.test(repoUpdate.source.commit)) {
|
||||
throw new Error("invalid_commit_format");
|
||||
}
|
||||
}
|
||||
|
||||
function updateRepoModel(model: IAnonymizedRepositoryDocument, repoUpdate) {
|
||||
model.source.commit = repoUpdate.commit;
|
||||
model.source.branch = repoUpdate.branch;
|
||||
model.source.commit = repoUpdate.source.commit;
|
||||
model.source.branch = repoUpdate.source.branch;
|
||||
model.conference = repoUpdate.conference;
|
||||
model.options = {
|
||||
terms: repoUpdate.terms,
|
||||
@@ -216,6 +203,23 @@ function updateRepoModel(model: IAnonymizedRepositoryDocument, repoUpdate) {
|
||||
pageSource: repoUpdate.options.pageSource,
|
||||
};
|
||||
}
|
||||
|
||||
// get repository information
|
||||
router.get("/:repoId/", async (req: express.Request, res: express.Response) => {
|
||||
try {
|
||||
const repo = await getRepo(req, res, { nocheck: true });
|
||||
if (!repo) throw new Error("repo_not_found");
|
||||
|
||||
const user = await getUser(req);
|
||||
if (user.username != repo.model.owner) {
|
||||
return res.status(401).send({ error: "not_authorized" });
|
||||
}
|
||||
res.json((await db.getRepository(req.params.repoId)).toJSON());
|
||||
} catch (error) {
|
||||
handleError(error, res);
|
||||
}
|
||||
});
|
||||
|
||||
// update a repository
|
||||
router.post(
|
||||
"/:repoId/",
|
||||
@@ -233,17 +237,15 @@ router.post(
|
||||
|
||||
validateNewRepo(repoUpdate);
|
||||
|
||||
if (repoUpdate.commit != repo.model.source.commit) {
|
||||
if (repoUpdate.source.commit != repo.model.source.commit) {
|
||||
repo.model.anonymizeDate = new Date();
|
||||
repo.model.source.commit = repoUpdate.commit;
|
||||
repo.model.source.commit = repoUpdate.source.commit;
|
||||
await repo.remove();
|
||||
}
|
||||
|
||||
updateRepoModel(repo.model, repoUpdate);
|
||||
|
||||
await repo.updateStatus("preparing");
|
||||
|
||||
await repo.model.save();
|
||||
res.send("ok");
|
||||
new Repository(repo.model).anonymize();
|
||||
} catch (error) {
|
||||
|
||||
@@ -77,7 +77,8 @@ export default abstract class GitHubBase {
|
||||
return {
|
||||
type: this.type,
|
||||
fullName: this.githubRepository.fullName?.toString(),
|
||||
branch: this.branch,
|
||||
branch: this.branch.name,
|
||||
commit: this.branch.commit,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user