fix: fix repository update

This commit is contained in:
tdurieux
2021-08-20 15:58:32 +02:00
parent 8dc84cbc19
commit 28075889a6
5 changed files with 73 additions and 55 deletions

View File

@@ -97,7 +97,7 @@
class="form-control" class="form-control"
id="branch" id="branch"
name="branch" name="branch"
ng-model="branch" ng-model="source.branch"
> >
<option <option
ng-repeat="b in branches" ng-repeat="b in branches"
@@ -128,7 +128,7 @@
class="form-control" class="form-control"
id="commit" id="commit"
name="commit" name="commit"
ng-model="commit" ng-model="source.commit"
/> />
<small class="form-text text-muted" <small class="form-text text-muted"
>The commit to anonymize</small >The commit to anonymize</small
@@ -333,7 +333,7 @@
class="form-control" class="form-control"
id="mode" id="mode"
name="mode" name="mode"
ng-model="options.mode" ng-model="source.type"
> >
<option value="GitHubStream" selected>Stream</option> <option value="GitHubStream" selected>Stream</option>
<option value="GitHubDownload">Download</option> <option value="GitHubDownload">Download</option>

View File

@@ -616,10 +616,12 @@ angular
$scope.repoId = ""; $scope.repoId = "";
$scope.terms = ""; $scope.terms = "";
$scope.defaultTerms = ""; $scope.defaultTerms = "";
$scope.branch = "";
$scope.branches = []; $scope.branches = [];
source = {
type: "GitHubDownload",
branch: "",
commit: "",
};
$scope.options = { $scope.options = {
expirationMode: "remove", expirationMode: "remove",
expirationDate: new Date(), expirationDate: new Date(),
@@ -628,7 +630,6 @@ angular
pdf: true, pdf: true,
notebook: true, notebook: true,
link: true, link: true,
mode: "GitHubDownload",
}; };
$scope.options.expirationDate.setDate( $scope.options.expirationDate.setDate(
$scope.options.expirationDate.getDate() + 90 $scope.options.expirationDate.getDate() + 90
@@ -662,8 +663,8 @@ angular
async (res) => { async (res) => {
$scope.repoUrl = "https://github.com/" + res.data.source.fullName; $scope.repoUrl = "https://github.com/" + res.data.source.fullName;
$scope.terms = res.data.options.terms.join("\n"); $scope.terms = res.data.options.terms.filter((f) => f).join("\n");
$scope.branch = res.data.source.branch.name; $scope.source = res.data.source;
$scope.options = res.data.options; $scope.options = res.data.options;
$scope.conference = res.data.conference; $scope.conference = res.data.conference;
if (res.data.options.expirationDate) { if (res.data.options.expirationDate) {
@@ -712,9 +713,13 @@ angular
$scope.getRepositories(); $scope.getRepositories();
$scope.repoSelected = async () => { $scope.repoSelected = async () => {
$scope.terms = $scope.defaultTerms; $scope.terms = $scope.defaultTerms.filter((f) => f);
$scope.repoId = ""; $scope.repoId = "";
$scope.branch = ""; $scope.source = {
type: "GitHubDownload",
branch: "",
commit: "",
};
$scope.anonymize_readme = ""; $scope.anonymize_readme = "";
$scope.readme = ""; $scope.readme = "";
@@ -739,19 +744,19 @@ angular
}; };
$('[data-toggle="tooltip"]').tooltip(); $('[data-toggle="tooltip"]').tooltip();
$scope.$watch("branch", async (v) => { $scope.$watch("source.branch", async (v) => {
const selected = $scope.branches.filter( const selected = $scope.branches.filter(
(f) => f.name == $scope.branch (f) => f.name == $scope.source.branch
)[0]; )[0];
if ($scope.details && $scope.details.hasPage) { if ($scope.details && $scope.details.hasPage) {
$scope.anonymize.page.$$element[0].disabled = false; $scope.anonymize.page.$$element[0].disabled = false;
if ($scope.details.pageSource.branch != $scope.branch) { if ($scope.details.pageSource.branch != $scope.source.branch) {
$scope.anonymize.page.$$element[0].disabled = true; $scope.anonymize.page.$$element[0].disabled = true;
} }
} }
if (selected) { if (selected) {
$scope.commit = selected.commit; $scope.source.commit = selected.commit;
$scope.readme = selected.readme; $scope.readme = selected.readme;
await getReadme(); await getReadme();
anonymize(); anonymize();
@@ -786,12 +791,14 @@ angular
{ params: { force: force === true ? "1" : "0" } } { params: { force: force === true ? "1" : "0" } }
); );
$scope.branches = branches.data; $scope.branches = branches.data;
if (!$scope.branch) { if (!$scope.source.branch) {
$scope.branch = $scope.details.defaultBranch; $scope.source.branch = $scope.details.defaultBranch;
} }
const selected = $scope.branches.filter((b) => b.name == $scope.branch); const selected = $scope.branches.filter(
(b) => b.name == $scope.source.branch
);
if (selected.length > 0) { if (selected.length > 0) {
$scope.commit = selected[0].commit; $scope.source.commit = selected[0].commit;
$scope.readme = selected[0].readme; $scope.readme = selected[0].readme;
} }
$scope.$apply(); $scope.$apply();
@@ -854,11 +861,17 @@ angular
} }
content = content.replace( content = content.replace(
new RegExp(`\\b${$scope.repoUrl}/blob/${$scope.branch}\\b`, "gi"), new RegExp(
`\\b${$scope.repoUrl}/blob/${$scope.source.branch}\\b`,
"gi"
),
`https://anonymous.4open.science/r/${$scope.repoId}` `https://anonymous.4open.science/r/${$scope.repoId}`
); );
content = content.replace( content = content.replace(
new RegExp(`\\b${$scope.repoUrl}/tree/${$scope.branch}\\b`, "gi"), new RegExp(
`\\b${$scope.repoUrl}/tree/${$scope.source.branch}\\b`,
"gi"
),
`https://anonymous.4open.science/r/${$scope.repoId}` `https://anonymous.4open.science/r/${$scope.repoId}`
); );
content = content.replace( content = content.replace(
@@ -934,12 +947,14 @@ angular
$scope.options.pageSource = $scope.details.pageSource; $scope.options.pageSource = $scope.details.pageSource;
return { return {
repoId: $scope.repoId, repoId: $scope.repoId,
terms: $scope.terms.trim().split("\n"), terms: $scope.terms
.trim()
.split("\n")
.filter((f) => f),
fullName: `${o.owner}/${o.repo}`, fullName: `${o.owner}/${o.repo}`,
repository: $scope.repoUrl, repository: $scope.repoUrl,
options: $scope.options, options: $scope.options,
branch: $scope.branch, source: $scope.source,
commit: $scope.commit,
conference: $scope.conference, conference: $scope.conference,
}; };
} }

View File

@@ -170,7 +170,7 @@ export default class Repository {
if (this._model.status == "ready") return; if (this._model.status == "ready") return;
await this.updateStatus("preparing"); await this.updateStatus("preparing");
await this.files(); await this.files();
await this.updateStatus("ready"); return this.updateStatus("ready");
} }
/** /**
@@ -179,7 +179,7 @@ export default class Repository {
async countView() { async countView() {
this._model.lastView = new Date(); this._model.lastView = new Date();
this._model.pageView = (this._model.pageView || 0) + 1; 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) { async updateStatus(status: RepositoryStatus, errorMessage?: string) {
this._model.status = status; this._model.status = status;
this._model.errorMessage = errorMessage; this._model.errorMessage = errorMessage;
await this._model.save(); return this._model.save();
} }
/** /**
* Expire the repository * Expire the repository
*/ */
async expire() { async expire() {
this.resetSate("expired"); return this.resetSate("expired");
} }
/** /**
* Remove the repository * Remove the repository
*/ */
async remove() { async remove() {
this.resetSate("removed"); return this.resetSate("removed");
} }
/** /**

View File

@@ -13,22 +13,6 @@ import Repository from "../Repository";
const router = express.Router(); 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 // user needs to be connected for all user API
router.use(ensureAuthenticated); router.use(ensureAuthenticated);
@@ -71,7 +55,7 @@ router.post("/claim", async (req: express.Request, res: express.Response) => {
} }
}); });
// refresh a repository // refresh repository
router.post( router.post(
"/:repoId/refresh", "/:repoId/refresh",
async (req: express.Request, res: express.Response) => { async (req: express.Request, res: express.Response) => {
@@ -183,23 +167,26 @@ function validateNewRepo(repoUpdate) {
) { ) {
throw new Error("invalid_repoId"); throw new Error("invalid_repoId");
} }
if (!repoUpdate.branch) { if (!repoUpdate.source.branch) {
throw new Error("branch_not_specified"); throw new Error("branch_not_specified");
} }
if (!repoUpdate.source.commit) {
throw new Error("commit_not_specified");
}
if (!repoUpdate.options) { if (!repoUpdate.options) {
throw new Error("options_not_provided"); throw new Error("options_not_provided");
} }
if (!Array.isArray(repoUpdate.terms)) { if (!Array.isArray(repoUpdate.terms)) {
throw new Error("invalid_terms_format"); 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"); throw new Error("invalid_commit_format");
} }
} }
function updateRepoModel(model: IAnonymizedRepositoryDocument, repoUpdate) { function updateRepoModel(model: IAnonymizedRepositoryDocument, repoUpdate) {
model.source.commit = repoUpdate.commit; model.source.commit = repoUpdate.source.commit;
model.source.branch = repoUpdate.branch; model.source.branch = repoUpdate.source.branch;
model.conference = repoUpdate.conference; model.conference = repoUpdate.conference;
model.options = { model.options = {
terms: repoUpdate.terms, terms: repoUpdate.terms,
@@ -216,6 +203,23 @@ function updateRepoModel(model: IAnonymizedRepositoryDocument, repoUpdate) {
pageSource: repoUpdate.options.pageSource, 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 // update a repository
router.post( router.post(
"/:repoId/", "/:repoId/",
@@ -233,17 +237,15 @@ router.post(
validateNewRepo(repoUpdate); validateNewRepo(repoUpdate);
if (repoUpdate.commit != repo.model.source.commit) { if (repoUpdate.source.commit != repo.model.source.commit) {
repo.model.anonymizeDate = new Date(); repo.model.anonymizeDate = new Date();
repo.model.source.commit = repoUpdate.commit; repo.model.source.commit = repoUpdate.source.commit;
await repo.remove(); await repo.remove();
} }
updateRepoModel(repo.model, repoUpdate); updateRepoModel(repo.model, repoUpdate);
await repo.updateStatus("preparing"); await repo.updateStatus("preparing");
await repo.model.save();
res.send("ok"); res.send("ok");
new Repository(repo.model).anonymize(); new Repository(repo.model).anonymize();
} catch (error) { } catch (error) {

View File

@@ -77,7 +77,8 @@ export default abstract class GitHubBase {
return { return {
type: this.type, type: this.type,
fullName: this.githubRepository.fullName?.toString(), fullName: this.githubRepository.fullName?.toString(),
branch: this.branch, branch: this.branch.name,
commit: this.branch.commit,
}; };
} }
} }