mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-03-31 19:02:37 +02:00
feat: add support for pull requests (#156)
This commit is contained in:
311
src/PullRequest.ts
Normal file
311
src/PullRequest.ts
Normal file
@@ -0,0 +1,311 @@
|
||||
import { RepositoryStatus, Source, Tree, TreeElement, TreeFile } from "./types";
|
||||
import User from "./User";
|
||||
import { anonymizeContent, anonymizePath } from "./anonymize-utils";
|
||||
import UserModel from "./database/users/users.model";
|
||||
import Conference from "./Conference";
|
||||
import ConferenceModel from "./database/conference/conferences.model";
|
||||
import AnonymousError from "./AnonymousError";
|
||||
import { IAnonymizedPullRequestDocument } from "./database/anonymizedPullRequests/anonymizedPullRequests.types";
|
||||
import config from "../config";
|
||||
import { Octokit } from "@octokit/rest";
|
||||
import got from "got";
|
||||
|
||||
export default class PullRequest {
|
||||
private _model: IAnonymizedPullRequestDocument;
|
||||
owner: User;
|
||||
|
||||
constructor(data: IAnonymizedPullRequestDocument) {
|
||||
this._model = data;
|
||||
this.owner = new User(new UserModel({ _id: data.owner }));
|
||||
}
|
||||
|
||||
getToken() {
|
||||
if (this.owner && this.owner.accessToken) {
|
||||
return this.owner.accessToken;
|
||||
}
|
||||
if (this._model.source.accessToken) {
|
||||
try {
|
||||
return this._model.source.accessToken;
|
||||
} catch (error) {
|
||||
console.debug("[ERROR] Token is invalid", this.pullRequestId);
|
||||
}
|
||||
}
|
||||
return config.GITHUB_TOKEN;
|
||||
}
|
||||
|
||||
async download() {
|
||||
console.debug("[INFO] Downloading pull request", this.pullRequestId);
|
||||
const auth = this.getToken();
|
||||
const octokit = new Octokit({ auth });
|
||||
const [owner, repo] = this._model.source.repositoryFullName.split("/");
|
||||
const pull_number = this._model.source.pullRequestId;
|
||||
const prInfo = await octokit.rest.pulls.get({
|
||||
owner,
|
||||
repo,
|
||||
pull_number,
|
||||
});
|
||||
|
||||
prInfo.data.updated_at;
|
||||
prInfo.data.draft;
|
||||
prInfo.data.merged;
|
||||
prInfo.data.merged_at;
|
||||
prInfo.data.state;
|
||||
prInfo.data.base.repo.full_name;
|
||||
prInfo.data.head.repo.full_name;
|
||||
const comments = await octokit.rest.issues.listComments({
|
||||
owner,
|
||||
repo,
|
||||
issue_number: pull_number,
|
||||
per_page: 100,
|
||||
});
|
||||
// const commits = await octokit.rest.pulls.listCommits({
|
||||
// owner,
|
||||
// repo,
|
||||
// pull_number,
|
||||
// per_page: 100,
|
||||
// });
|
||||
// const files = await octokit.rest.pulls.listFiles({
|
||||
// owner,
|
||||
// repo,
|
||||
// pull_number,
|
||||
// per_page: 100,
|
||||
// });
|
||||
const diff = await got(prInfo.data.diff_url);
|
||||
this._model.pullRequest = {
|
||||
diff: diff.body,
|
||||
title: prInfo.data.title,
|
||||
body: prInfo.data.body,
|
||||
creationDate: new Date(prInfo.data.created_at),
|
||||
updatedDate: new Date(prInfo.data.updated_at),
|
||||
draft: prInfo.data.draft,
|
||||
merged: prInfo.data.merged,
|
||||
mergedDate: prInfo.data.merged_at
|
||||
? new Date(prInfo.data.merged_at)
|
||||
: null,
|
||||
state: prInfo.data.state,
|
||||
baseRepositoryFullName: prInfo.data.base.repo.full_name,
|
||||
headRepositoryFullName: prInfo.data.head.repo.full_name,
|
||||
comments: comments.data.map((comment) => ({
|
||||
body: comment.body,
|
||||
creationDate: new Date(comment.created_at),
|
||||
updatedDate: new Date(comment.updated_at),
|
||||
author: comment.user.login,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the status of the pullRequest
|
||||
*/
|
||||
check() {
|
||||
if (
|
||||
this._model.options.expirationMode !== "never" &&
|
||||
this.status == "ready"
|
||||
) {
|
||||
if (this._model.options.expirationDate <= new Date()) {
|
||||
this.expire();
|
||||
}
|
||||
}
|
||||
if (
|
||||
this.status == "expired" ||
|
||||
this.status == "expiring" ||
|
||||
this.status == "removing" ||
|
||||
this.status == "removed"
|
||||
) {
|
||||
throw new AnonymousError("pullRequest_expired", {
|
||||
object: this,
|
||||
httpStatus: 410,
|
||||
});
|
||||
}
|
||||
const fiveMinuteAgo = new Date();
|
||||
fiveMinuteAgo.setMinutes(fiveMinuteAgo.getMinutes() - 5);
|
||||
|
||||
if (
|
||||
this.status == "preparing" ||
|
||||
(this.status == "download" && this._model.statusDate > fiveMinuteAgo)
|
||||
) {
|
||||
throw new AnonymousError("pullRequest_not_ready", {
|
||||
object: this,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the pullRequest if a new commit exists
|
||||
*
|
||||
* @returns void
|
||||
*/
|
||||
async updateIfNeeded(opt?: { force: boolean }): Promise<void> {
|
||||
const yesterday = new Date();
|
||||
yesterday.setDate(yesterday.getDate() - 1);
|
||||
if (
|
||||
opt?.force ||
|
||||
(this._model.options.update && this._model.lastView < yesterday)
|
||||
) {
|
||||
await this.download();
|
||||
this._model.lastView = new Date();
|
||||
await this._model.save();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Download the require state for the pullRequest to work
|
||||
*
|
||||
* @returns void
|
||||
*/
|
||||
async anonymize() {
|
||||
if (this.status == "ready") return;
|
||||
await this.updateStatus("preparing");
|
||||
await this.updateIfNeeded({ force: true });
|
||||
return this.updateStatus("ready");
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the last view and view count
|
||||
*/
|
||||
async countView() {
|
||||
this._model.lastView = new Date();
|
||||
this._model.pageView = (this._model.pageView || 0) + 1;
|
||||
return this._model.save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the status of the pullRequest
|
||||
* @param status the new status
|
||||
* @param errorMessage a potential error message to display
|
||||
*/
|
||||
async updateStatus(status: RepositoryStatus, statusMessage?: string) {
|
||||
this._model.status = status;
|
||||
this._model.statusDate = new Date();
|
||||
this._model.statusMessage = statusMessage;
|
||||
return this._model.save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Expire the pullRequest
|
||||
*/
|
||||
async expire() {
|
||||
await this.updateStatus("expiring");
|
||||
await this.resetSate();
|
||||
await this.updateStatus("expired");
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the pullRequest
|
||||
*/
|
||||
async remove() {
|
||||
await this.updateStatus("removing");
|
||||
await this.resetSate();
|
||||
await this.updateStatus("removed");
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset/delete the state of the pullRequest
|
||||
*/
|
||||
async resetSate(status?: RepositoryStatus, statusMessage?: string) {
|
||||
if (status) this._model.status = status;
|
||||
if (statusMessage) this._model.statusMessage = statusMessage;
|
||||
// remove cache
|
||||
this._model.pullRequest = null;
|
||||
return Promise.all([this._model.save()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the conference of the pullRequest
|
||||
*
|
||||
* @returns conference of the pullRequest
|
||||
*/
|
||||
async conference(): Promise<Conference | null> {
|
||||
if (!this._model.conference) {
|
||||
return null;
|
||||
}
|
||||
const conference = await ConferenceModel.findOne({
|
||||
conferenceID: this._model.conference,
|
||||
});
|
||||
if (conference) return new Conference(conference);
|
||||
return null;
|
||||
}
|
||||
|
||||
/***** Getters ********/
|
||||
|
||||
get pullRequestId() {
|
||||
return this._model.pullRequestId;
|
||||
}
|
||||
|
||||
get options() {
|
||||
return this._model.options;
|
||||
}
|
||||
|
||||
get source() {
|
||||
return this._model.source;
|
||||
}
|
||||
|
||||
get model() {
|
||||
return this._model;
|
||||
}
|
||||
|
||||
get status() {
|
||||
return this._model.status;
|
||||
}
|
||||
|
||||
content() {
|
||||
const output: any = {
|
||||
anonymizeDate: this._model.anonymizeDate,
|
||||
merged: this._model.pullRequest.merged,
|
||||
mergedDate: this._model.pullRequest.mergedDate,
|
||||
state: this._model.pullRequest.state,
|
||||
draft: this._model.pullRequest.draft,
|
||||
};
|
||||
if (this.options.title) {
|
||||
output.title = anonymizeContent(this._model.pullRequest.title, this);
|
||||
}
|
||||
if (this.options.body) {
|
||||
output.body = anonymizeContent(this._model.pullRequest.body, this);
|
||||
}
|
||||
if (this.options.comments) {
|
||||
output.comments = this._model.pullRequest.comments.map((comment) => {
|
||||
const o: any = {};
|
||||
if (this.options.body) o.body = anonymizeContent(comment.body, this);
|
||||
if (this.options.username)
|
||||
o.author = anonymizeContent(comment.author, this);
|
||||
if (this.options.date) {
|
||||
o.updatedDate = comment.updatedDate;
|
||||
o.creationDate = comment.creationDate;
|
||||
}
|
||||
return o;
|
||||
});
|
||||
}
|
||||
if (this.options.diff) {
|
||||
output.diff = anonymizeContent(this._model.pullRequest.diff, this);
|
||||
}
|
||||
if (this.options.origin) {
|
||||
output.baseRepositoryFullName =
|
||||
this._model.pullRequest.baseRepositoryFullName;
|
||||
}
|
||||
if (this.options.date) {
|
||||
output.updatedDate = this.model.pullRequest.updatedDate;
|
||||
output.creationDate = this.model.pullRequest.creationDate;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
pullRequestId: this._model.pullRequestId,
|
||||
options: this._model.options,
|
||||
conference: this._model.conference,
|
||||
anonymizeDate: this._model.anonymizeDate,
|
||||
status: this._model.status,
|
||||
state: this.model.pullRequest.state,
|
||||
merged: this.model.pullRequest.merged,
|
||||
mergedDate: this.model.pullRequest.mergedDate,
|
||||
statusMessage: this._model.statusMessage,
|
||||
source: {
|
||||
pullRequestId: this._model.source.pullRequestId,
|
||||
repositoryFullName: this._model.source.repositoryFullName,
|
||||
},
|
||||
pullRequest: this._model.pullRequest,
|
||||
lastView: this._model.lastView,
|
||||
pageView: this._model.pageView,
|
||||
};
|
||||
}
|
||||
}
|
||||
27
src/User.ts
27
src/User.ts
@@ -4,6 +4,8 @@ import RepositoryModel from "./database/repositories/repositories.model";
|
||||
import { IUserDocument } from "./database/users/users.types";
|
||||
import Repository from "./Repository";
|
||||
import { GitHubRepository } from "./source/GitHubRepository";
|
||||
import PullRequest from "./PullRequest";
|
||||
import AnonymizedPullRequestModel from "./database/anonymizedPullRequests/anonymizedPullRequests.model";
|
||||
|
||||
/**
|
||||
* Model for a user
|
||||
@@ -136,6 +138,31 @@ export default class User {
|
||||
await Promise.all(promises);
|
||||
return repositories;
|
||||
}
|
||||
/**
|
||||
* Get the lost of anonymized repositories
|
||||
* @returns the list of anonymized repositories
|
||||
*/
|
||||
async getPullRequests() {
|
||||
const pullRequests = (
|
||||
await AnonymizedPullRequestModel.find({
|
||||
owner: this.id,
|
||||
}).exec()
|
||||
).map((d) => new PullRequest(d));
|
||||
const promises = [];
|
||||
for (let repo of pullRequests) {
|
||||
if (
|
||||
repo.status == "ready" &&
|
||||
repo.options.expirationMode != "never" &&
|
||||
repo.options.expirationDate != null &&
|
||||
repo.options.expirationDate < new Date()
|
||||
) {
|
||||
// expire the repository
|
||||
promises.push(repo.expire());
|
||||
}
|
||||
}
|
||||
await Promise.all(promises);
|
||||
return pullRequests;
|
||||
}
|
||||
|
||||
get model() {
|
||||
return this._model;
|
||||
|
||||
@@ -72,7 +72,24 @@ export function anonymizeStream(filename: string, repository: Repository) {
|
||||
return ts;
|
||||
}
|
||||
|
||||
export function anonymizeContent(content: string, repository: Repository) {
|
||||
interface Anonymizationptions {
|
||||
repoId?: string;
|
||||
source?: {};
|
||||
options: {
|
||||
terms: string[];
|
||||
image: boolean;
|
||||
link: boolean;
|
||||
pageSource?: {
|
||||
branch: string;
|
||||
path: string;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
export function anonymizeContent(
|
||||
content: string,
|
||||
repository: Anonymizationptions
|
||||
) {
|
||||
if (repository.options?.image === false) {
|
||||
// remove image in markdown
|
||||
content = content.replace(
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import { model } from "mongoose";
|
||||
|
||||
import AnonymizedPullRequestSchema from "./anonymizedPullRequests.schema";
|
||||
import {
|
||||
IAnonymizedPullRequestDocument,
|
||||
IAnonymizedPullRequestModel,
|
||||
} from "./anonymizedPullRequests.types";
|
||||
|
||||
const AnonymizedPullRequestModel = model<IAnonymizedPullRequestDocument>(
|
||||
"AnonymizedPullRequest",
|
||||
AnonymizedPullRequestSchema
|
||||
) as IAnonymizedPullRequestModel;
|
||||
|
||||
export default AnonymizedPullRequestModel;
|
||||
@@ -0,0 +1,66 @@
|
||||
import { Schema } from "mongoose";
|
||||
|
||||
const AnonymizedPullRequestSchema = new Schema({
|
||||
pullRequestId: {
|
||||
type: String,
|
||||
index: { unique: true },
|
||||
},
|
||||
status: {
|
||||
type: String,
|
||||
default: "preparing",
|
||||
},
|
||||
statusDate: Date,
|
||||
statusMessage: String,
|
||||
anonymizeDate: Date,
|
||||
lastView: Date,
|
||||
pageView: Number,
|
||||
owner: Schema.Types.ObjectId,
|
||||
conference: String,
|
||||
source: {
|
||||
pullRequestId: Number,
|
||||
repositoryFullName: String,
|
||||
accessToken: String,
|
||||
},
|
||||
options: {
|
||||
terms: [String],
|
||||
expirationMode: { type: String },
|
||||
expirationDate: Date,
|
||||
update: Boolean,
|
||||
image: Boolean,
|
||||
link: Boolean,
|
||||
title: Boolean,
|
||||
body: Boolean,
|
||||
comments: Boolean,
|
||||
diff: Boolean,
|
||||
origin: Boolean,
|
||||
username: Boolean,
|
||||
date: Boolean,
|
||||
},
|
||||
dateOfEntry: {
|
||||
type: Date,
|
||||
default: new Date(),
|
||||
},
|
||||
pullRequest: {
|
||||
diff: String,
|
||||
title: String,
|
||||
body: String,
|
||||
creationDate: Date,
|
||||
updatedDate: Date,
|
||||
draft: Boolean,
|
||||
merged: Boolean,
|
||||
mergedDate: Date,
|
||||
state: String,
|
||||
baseRepositoryFullName: String,
|
||||
headRepositoryFullName: String,
|
||||
comments: [
|
||||
{
|
||||
body: String,
|
||||
creationDate: Date,
|
||||
updatedDate: Date,
|
||||
author: String,
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
export default AnonymizedPullRequestSchema;
|
||||
@@ -0,0 +1,61 @@
|
||||
import { Document, Model } from "mongoose";
|
||||
import { RepositoryStatus } from "../../types";
|
||||
|
||||
export interface IAnonymizedPullRequest {
|
||||
pullRequestId: string;
|
||||
status?: RepositoryStatus;
|
||||
statusMessage?: string;
|
||||
statusDate: Date;
|
||||
anonymizeDate: Date;
|
||||
source: {
|
||||
pullRequestId: number;
|
||||
repositoryFullName?: string;
|
||||
accessToken?: string;
|
||||
};
|
||||
owner: string;
|
||||
conference: string;
|
||||
options: {
|
||||
terms: string[];
|
||||
expirationMode: "never" | "redirect" | "remove";
|
||||
expirationDate?: Date;
|
||||
update: boolean;
|
||||
image: boolean;
|
||||
link: boolean;
|
||||
title: boolean;
|
||||
body: boolean;
|
||||
comments: boolean;
|
||||
diff: boolean;
|
||||
origin: boolean;
|
||||
username: boolean;
|
||||
date: boolean;
|
||||
};
|
||||
pageView: number;
|
||||
lastView: Date;
|
||||
pullRequest: {
|
||||
diff: string;
|
||||
title: string;
|
||||
body: string;
|
||||
creationDate: Date;
|
||||
updatedDate: Date;
|
||||
draft?: boolean;
|
||||
merged?: boolean;
|
||||
mergedDate?: Date;
|
||||
state?: string;
|
||||
baseRepositoryFullName?: string;
|
||||
headRepositoryFullName?: string;
|
||||
comments?: {
|
||||
body: string;
|
||||
creationDate: Date;
|
||||
updatedDate: Date;
|
||||
author: string;
|
||||
}[];
|
||||
};
|
||||
}
|
||||
|
||||
export interface IAnonymizedPullRequestDocument
|
||||
extends IAnonymizedPullRequest,
|
||||
Document {
|
||||
setLastUpdated: (this: IAnonymizedPullRequestDocument) => Promise<void>;
|
||||
}
|
||||
export interface IAnonymizedPullRequestModel
|
||||
extends Model<IAnonymizedPullRequestDocument> {}
|
||||
@@ -3,6 +3,8 @@ import Repository from "../Repository";
|
||||
import config from "../../config";
|
||||
import AnonymizedRepositoryModel from "./anonymizedRepositories/anonymizedRepositories.model";
|
||||
import AnonymousError from "../AnonymousError";
|
||||
import AnonymizedPullRequestModel from "./anonymizedPullRequests/anonymizedPullRequests.model";
|
||||
import PullRequest from "../PullRequest";
|
||||
|
||||
const MONGO_URL = `mongodb://${config.DB_USERNAME}:${config.DB_PASSWORD}@${config.DB_HOSTNAME}:27017/`;
|
||||
|
||||
@@ -17,7 +19,7 @@ export async function connect() {
|
||||
}
|
||||
|
||||
export async function getRepository(repoId: string) {
|
||||
if (!repoId || repoId == 'undefined') {
|
||||
if (!repoId || repoId == "undefined") {
|
||||
throw new AnonymousError("repo_not_found", {
|
||||
object: repoId,
|
||||
httpStatus: 404,
|
||||
@@ -31,3 +33,20 @@ export async function getRepository(repoId: string) {
|
||||
});
|
||||
return new Repository(data);
|
||||
}
|
||||
export async function getPullRequest(pullRequestId: string) {
|
||||
if (!pullRequestId || pullRequestId == "undefined") {
|
||||
throw new AnonymousError("pull_request_not_found", {
|
||||
object: pullRequestId,
|
||||
httpStatus: 404,
|
||||
});
|
||||
}
|
||||
const data = await AnonymizedPullRequestModel.findOne({
|
||||
pullRequestId,
|
||||
});
|
||||
if (!data)
|
||||
throw new AnonymousError("pull_request_not_found", {
|
||||
object: pullRequestId,
|
||||
httpStatus: 404,
|
||||
});
|
||||
return new PullRequest(data);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import pullRequestPrivate from "./pullRequest-private";
|
||||
import pullRequestPublic from "./pullRequest-public";
|
||||
import repositoryPrivate from "./repository-private";
|
||||
import repositoryPublic from "./repository-public";
|
||||
import conference from "./conference";
|
||||
@@ -8,6 +10,8 @@ import option from "./option";
|
||||
import admin from "./admin";
|
||||
|
||||
export default {
|
||||
pullRequestPrivate,
|
||||
pullRequestPublic,
|
||||
repositoryPrivate,
|
||||
repositoryPublic,
|
||||
file,
|
||||
|
||||
245
src/routes/pullRequest-private.ts
Normal file
245
src/routes/pullRequest-private.ts
Normal file
@@ -0,0 +1,245 @@
|
||||
import * as express from "express";
|
||||
import { ensureAuthenticated } from "./connection";
|
||||
|
||||
import {
|
||||
getPullRequest,
|
||||
getUser,
|
||||
handleError,
|
||||
isOwnerOrAdmin,
|
||||
} from "./route-utils";
|
||||
import AnonymousError from "../AnonymousError";
|
||||
import { IAnonymizedPullRequestDocument } from "../database/anonymizedPullRequests/anonymizedPullRequests.types";
|
||||
import PullRequest from "../PullRequest";
|
||||
import AnonymizedPullRequestModel from "../database/anonymizedPullRequests/anonymizedPullRequests.model";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// user needs to be connected for all user API
|
||||
router.use(ensureAuthenticated);
|
||||
|
||||
// refresh pullRequest
|
||||
router.post(
|
||||
"/:pullRequestId/refresh",
|
||||
async (req: express.Request, res: express.Response) => {
|
||||
try {
|
||||
const pullRequest = await getPullRequest(req, res, { nocheck: true });
|
||||
if (!pullRequest) return;
|
||||
|
||||
if (
|
||||
pullRequest.status == "preparing" ||
|
||||
pullRequest.status == "removing" ||
|
||||
pullRequest.status == "expiring"
|
||||
)
|
||||
return;
|
||||
|
||||
const user = await getUser(req);
|
||||
isOwnerOrAdmin([pullRequest.owner.id], user);
|
||||
await pullRequest.anonymize()
|
||||
res.json({ status: pullRequest.status });
|
||||
} catch (error) {
|
||||
handleError(error, res, req);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// delete a pullRequest
|
||||
router.delete(
|
||||
"/:pullRequestId/",
|
||||
async (req: express.Request, res: express.Response) => {
|
||||
const pullRequest = await getPullRequest(req, res, { nocheck: true });
|
||||
if (!pullRequest) return;
|
||||
try {
|
||||
if (pullRequest.status == "removed")
|
||||
throw new AnonymousError("is_removed", {
|
||||
object: req.params.pullRequestId,
|
||||
httpStatus: 410,
|
||||
});
|
||||
const user = await getUser(req);
|
||||
isOwnerOrAdmin([pullRequest.owner.id], user);
|
||||
await pullRequest.remove();
|
||||
return res.json({ status: pullRequest.status });
|
||||
} catch (error) {
|
||||
handleError(error, res, req);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
router.get(
|
||||
"/:owner/:repository/:pullRequestId",
|
||||
async (req: express.Request, res: express.Response) => {
|
||||
const user = await getUser(req);
|
||||
try {
|
||||
const pullRequest = new PullRequest(
|
||||
new AnonymizedPullRequestModel({
|
||||
owner: user.id,
|
||||
source: {
|
||||
pullRequestId: parseInt(req.params.pullRequestId),
|
||||
repositoryFullName: `${req.params.owner}/${req.params.repository}`,
|
||||
},
|
||||
})
|
||||
);
|
||||
await pullRequest.download();
|
||||
res.json(pullRequest.toJSON());
|
||||
} catch (error) {
|
||||
handleError(error, res, req);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// get pullRequest information
|
||||
router.get(
|
||||
"/:pullRequestId/",
|
||||
async (req: express.Request, res: express.Response) => {
|
||||
try {
|
||||
const pullRequest = await getPullRequest(req, res, { nocheck: true });
|
||||
if (!pullRequest) return;
|
||||
|
||||
const user = await getUser(req);
|
||||
isOwnerOrAdmin([pullRequest.owner.id], user);
|
||||
res.json(pullRequest.toJSON());
|
||||
} catch (error) {
|
||||
handleError(error, res, req);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
function validateNewPullRequest(pullRequestUpdate): void {
|
||||
const validCharacters = /^[0-9a-zA-Z\-\_]+$/;
|
||||
if (
|
||||
!pullRequestUpdate.pullRequestId.match(validCharacters) ||
|
||||
pullRequestUpdate.pullRequestId.length < 3
|
||||
) {
|
||||
throw new AnonymousError("invalid_pullRequestId", {
|
||||
object: pullRequestUpdate,
|
||||
httpStatus: 400,
|
||||
});
|
||||
}
|
||||
if (!pullRequestUpdate.source.repositoryFullName) {
|
||||
throw new AnonymousError("repository_not_specified", {
|
||||
object: pullRequestUpdate,
|
||||
httpStatus: 400,
|
||||
});
|
||||
}
|
||||
if (!pullRequestUpdate.source.pullRequestId) {
|
||||
throw new AnonymousError("pullRequestId_not_specified", {
|
||||
object: pullRequestUpdate,
|
||||
httpStatus: 400,
|
||||
});
|
||||
}
|
||||
if (
|
||||
parseInt(pullRequestUpdate.source.pullRequestId) !=
|
||||
pullRequestUpdate.source.pullRequestId
|
||||
) {
|
||||
throw new AnonymousError("pullRequestId_is_not_a_number", {
|
||||
object: pullRequestUpdate,
|
||||
httpStatus: 400,
|
||||
});
|
||||
}
|
||||
if (!pullRequestUpdate.options) {
|
||||
throw new AnonymousError("options_not_provided", {
|
||||
object: pullRequestUpdate,
|
||||
httpStatus: 400,
|
||||
});
|
||||
}
|
||||
if (!Array.isArray(pullRequestUpdate.terms)) {
|
||||
throw new AnonymousError("invalid_terms_format", {
|
||||
object: pullRequestUpdate,
|
||||
httpStatus: 400,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function updatePullRequestModel(
|
||||
model: IAnonymizedPullRequestDocument,
|
||||
pullRequestUpdate: any
|
||||
) {
|
||||
model.options = {
|
||||
terms: pullRequestUpdate.terms,
|
||||
expirationMode: pullRequestUpdate.options.expirationMode,
|
||||
expirationDate: pullRequestUpdate.options.expirationDate
|
||||
? new Date(pullRequestUpdate.options.expirationDate)
|
||||
: null,
|
||||
update: pullRequestUpdate.options.update,
|
||||
image: pullRequestUpdate.options.image,
|
||||
link: pullRequestUpdate.options.link,
|
||||
body: pullRequestUpdate.options.body,
|
||||
title: pullRequestUpdate.options.title,
|
||||
username: pullRequestUpdate.options.username,
|
||||
origin: pullRequestUpdate.options.origin,
|
||||
diff: pullRequestUpdate.options.diff,
|
||||
comments: pullRequestUpdate.options.comments,
|
||||
date: pullRequestUpdate.options.date,
|
||||
};
|
||||
}
|
||||
|
||||
// update a pullRequest
|
||||
router.post(
|
||||
"/:pullRequestId/",
|
||||
async (req: express.Request, res: express.Response) => {
|
||||
try {
|
||||
const pullRequest = await getPullRequest(req, res, { nocheck: true });
|
||||
if (!pullRequest) return;
|
||||
const user = await getUser(req);
|
||||
|
||||
isOwnerOrAdmin([pullRequest.owner.id], user);
|
||||
const pullRequestUpdate = req.body;
|
||||
validateNewPullRequest(pullRequestUpdate);
|
||||
pullRequest.model.anonymizeDate = new Date();
|
||||
|
||||
updatePullRequestModel(pullRequest.model, pullRequestUpdate);
|
||||
// TODO handle conference
|
||||
pullRequest.model.conference = pullRequestUpdate.conference;
|
||||
await pullRequest.updateIfNeeded({ force: true });
|
||||
res.json(pullRequest.toJSON());
|
||||
} catch (error) {
|
||||
return handleError(error, res, req);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// add pullRequest
|
||||
router.post("/", async (req: express.Request, res: express.Response) => {
|
||||
const user = await getUser(req);
|
||||
const pullRequestUpdate = req.body;
|
||||
|
||||
try {
|
||||
validateNewPullRequest(pullRequestUpdate);
|
||||
|
||||
const pullRequest = new PullRequest(
|
||||
new AnonymizedPullRequestModel({
|
||||
owner: user.id,
|
||||
options: pullRequestUpdate.options,
|
||||
})
|
||||
);
|
||||
|
||||
pullRequest.model.pullRequestId = pullRequestUpdate.pullRequestId;
|
||||
pullRequest.model.anonymizeDate = new Date();
|
||||
pullRequest.model.owner = user.id;
|
||||
|
||||
updatePullRequestModel(pullRequest.model, pullRequestUpdate);
|
||||
pullRequest.source.accessToken = user.accessToken;
|
||||
pullRequest.source.pullRequestId = pullRequestUpdate.source.pullRequestId;
|
||||
pullRequest.source.repositoryFullName =
|
||||
pullRequestUpdate.source.repositoryFullName;
|
||||
|
||||
pullRequest.conference = pullRequestUpdate.conference;
|
||||
|
||||
await pullRequest.anonymize()
|
||||
res.send(pullRequest.toJSON());
|
||||
} catch (error) {
|
||||
if (error.message?.indexOf(" duplicate key") > -1) {
|
||||
return handleError(
|
||||
new AnonymousError("pullRequestId_already_used", {
|
||||
httpStatus: 400,
|
||||
cause: error,
|
||||
object: pullRequestUpdate,
|
||||
}),
|
||||
res,
|
||||
req
|
||||
);
|
||||
}
|
||||
return handleError(error, res, req);
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
84
src/routes/pullRequest-public.ts
Normal file
84
src/routes/pullRequest-public.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import * as express from "express";
|
||||
|
||||
import { getPullRequest, handleError } from "./route-utils";
|
||||
import AnonymousError from "../AnonymousError";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get(
|
||||
"/:pullRequestId/options",
|
||||
async (req: express.Request, res: express.Response) => {
|
||||
try {
|
||||
res.header("Cache-Control", "no-cache");
|
||||
const pr = await getPullRequest(req, res, { nocheck: true });
|
||||
if (!pr) return;
|
||||
let redirectURL = null;
|
||||
if (pr.status == "expired" && pr.options.expirationMode == "redirect") {
|
||||
redirectURL = `https://github.com/${pr.source.repositoryFullName}/pull/${pr.source.pullRequestId}`;
|
||||
} else {
|
||||
if (
|
||||
pr.status == "expired" ||
|
||||
pr.status == "expiring" ||
|
||||
pr.status == "removing" ||
|
||||
pr.status == "removed"
|
||||
) {
|
||||
throw new AnonymousError("pull_request_expired", {
|
||||
object: pr,
|
||||
httpStatus: 410,
|
||||
});
|
||||
}
|
||||
|
||||
const fiveMinuteAgo = new Date();
|
||||
fiveMinuteAgo.setMinutes(fiveMinuteAgo.getMinutes() - 5);
|
||||
if (pr.status != "ready") {
|
||||
if (
|
||||
pr.model.statusDate < fiveMinuteAgo
|
||||
// && repo.status != "preparing"
|
||||
) {
|
||||
await pr.updateIfNeeded({ force: true });
|
||||
}
|
||||
if (pr.status == "error") {
|
||||
throw new AnonymousError(
|
||||
pr.model.statusMessage
|
||||
? pr.model.statusMessage
|
||||
: "pull_request_not_available",
|
||||
{
|
||||
object: pr,
|
||||
httpStatus: 500,
|
||||
}
|
||||
);
|
||||
}
|
||||
throw new AnonymousError("pull_request_not_ready", {
|
||||
httpStatus: 404,
|
||||
object: pr,
|
||||
});
|
||||
}
|
||||
|
||||
await pr.updateIfNeeded();
|
||||
}
|
||||
|
||||
res.json({
|
||||
url: redirectURL,
|
||||
lastUpdateDate: pr.model.statusDate,
|
||||
});
|
||||
} catch (error) {
|
||||
handleError(error, res, req);
|
||||
}
|
||||
}
|
||||
);
|
||||
router.get(
|
||||
"/:pullRequestId/content",
|
||||
async (req: express.Request, res: express.Response) => {
|
||||
const pullRequest = await getPullRequest(req, res);
|
||||
if (!pullRequest) return;
|
||||
try {
|
||||
await pullRequest.countView();
|
||||
res.header("Cache-Control", "no-cache");
|
||||
res.json(pullRequest.content());
|
||||
} catch (error) {
|
||||
handleError(error, res, req);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
export default router;
|
||||
@@ -60,11 +60,10 @@ router.get(
|
||||
router.get(
|
||||
"/:repoId/files",
|
||||
async (req: express.Request, res: express.Response) => {
|
||||
res.header("Cache-Control", "no-cache");
|
||||
const repo = await getRepo(req, res);
|
||||
if (!repo) return;
|
||||
try {
|
||||
res.header("Cache-Control", "no-cache");
|
||||
|
||||
res.json(await repo.anonymizedFiles({ includeSha: false }));
|
||||
} catch (error) {
|
||||
handleError(error, res, req);
|
||||
@@ -76,6 +75,7 @@ router.get(
|
||||
"/:repoId/options",
|
||||
async (req: express.Request, res: express.Response) => {
|
||||
try {
|
||||
res.header("Cache-Control", "no-cache");
|
||||
const repo = await getRepo(req, res, { nocheck: true });
|
||||
if (!repo) return;
|
||||
let redirectURL = null;
|
||||
@@ -146,7 +146,6 @@ router.get(
|
||||
download = true;
|
||||
}
|
||||
|
||||
res.header("Cache-Control", "no-cache");
|
||||
res.json({
|
||||
url: redirectURL,
|
||||
download,
|
||||
|
||||
@@ -5,6 +5,35 @@ import UserModel from "../database/users/users.model";
|
||||
import User from "../User";
|
||||
import * as io from "@pm2/io";
|
||||
|
||||
export async function getPullRequest(
|
||||
req: express.Request,
|
||||
res: express.Response,
|
||||
opt?: { nocheck?: boolean }
|
||||
) {
|
||||
try {
|
||||
const pullRequest = await db.getPullRequest(req.params.pullRequestId);
|
||||
if (opt?.nocheck == true) {
|
||||
} else {
|
||||
// redirect if the repository is expired
|
||||
if (
|
||||
pullRequest.status == "expired" &&
|
||||
pullRequest.options.expirationMode == "redirect"
|
||||
) {
|
||||
res.redirect(
|
||||
`http://github.com/${pullRequest.source.repositoryFullName}/pull/${pullRequest.source.pullRequestId}`
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
pullRequest.check();
|
||||
}
|
||||
return pullRequest;
|
||||
} catch (error) {
|
||||
handleError(error, res, req);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getRepo(
|
||||
req: express.Request,
|
||||
res: express.Response,
|
||||
@@ -50,7 +79,7 @@ function printError(error: any, req?: express.Request) {
|
||||
if (req) {
|
||||
message += ` ${req.originalUrl}`;
|
||||
// ignore common error
|
||||
if (req.originalUrl === '/api/repo/undefined/options') return
|
||||
if (req.originalUrl === "/api/repo/undefined/options") return;
|
||||
}
|
||||
console.error(message);
|
||||
} else if (error instanceof Error) {
|
||||
|
||||
@@ -97,6 +97,21 @@ router.get(
|
||||
}
|
||||
}
|
||||
);
|
||||
router.get(
|
||||
"/anonymized_pull_requests",
|
||||
async (req: express.Request, res: express.Response) => {
|
||||
try {
|
||||
const user = await getUser(req);
|
||||
res.json(
|
||||
(await user.getPullRequests()).map((x) => {
|
||||
return x.toJSON();
|
||||
})
|
||||
);
|
||||
} catch (error) {
|
||||
handleError(error, res, req);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
router.get(
|
||||
"/all_repositories",
|
||||
|
||||
@@ -6,7 +6,7 @@ import GitHubDownload from "../source/GitHubDownload";
|
||||
import AnonymousError from "../AnonymousError";
|
||||
import { TreeElement } from "../types";
|
||||
import * as marked from "marked";
|
||||
import { anonymizeContent, streamToString } from "../anonymize-utils";
|
||||
import { streamToString } from "../anonymize-utils";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@@ -103,7 +103,7 @@ async function webView(req: express.Request, res: express.Response) {
|
||||
}
|
||||
if ((await f.extension()) == "md") {
|
||||
const content = await streamToString(await f.anonymizedContent());
|
||||
res.send(marked.marked(content));
|
||||
res.contentType("html").send(marked.marked(content));
|
||||
} else {
|
||||
f.send(res);
|
||||
}
|
||||
|
||||
@@ -90,6 +90,8 @@ export default async function start() {
|
||||
apiRouter.use("/repo", router.repositoryPublic);
|
||||
apiRouter.use("/repo", speedLimiter, router.file);
|
||||
apiRouter.use("/repo", speedLimiter, router.repositoryPrivate);
|
||||
apiRouter.use("/pr", speedLimiter, router.pullRequestPrivate);
|
||||
apiRouter.use("/pr", speedLimiter, router.pullRequestPublic);
|
||||
|
||||
apiRouter.get("/message", async (_, res) => {
|
||||
if (ofs.existsSync("./message.txt")) {
|
||||
|
||||
Reference in New Issue
Block a user