Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rr): blob diffs #575

Merged
merged 4 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 99 additions & 2 deletions src/routes/v2/authenticated/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
EditedItemDto,
UpdateReviewRequestDto,
ReviewRequestDto,
BlobDiffDto,
} from "@root/types/dto/review"
import ReviewRequestService from "@services/review/ReviewRequestService"
// eslint-disable-next-line import/prefer-default-export
Expand Down Expand Up @@ -947,7 +948,8 @@ export class ReviewsRouter {

if (!isReviewer) {
logger.error({
message: "",
message:
"User with insufficient permissions attempted to delete approval",
method: "deleteReviewRequestApproval",
meta: {
userId: userWithSiteSessionData.isomerUserId,
Expand All @@ -967,6 +969,101 @@ export class ReviewsRouter {
return res.status(200).send()
}

getBlob: RequestHandler<
{ siteName: string; requestId: number },
BlobDiffDto | ResponseErrorBody,
unknown,
{ path: string },
{ userWithSiteSessionData: UserWithSiteSessionData }
> = async (req, res) => {
// Step 1: Check that the site exists
const { siteName, requestId } = req.params
const { path } = req.query
const { userWithSiteSessionData } = res.locals
const site = await this.sitesService.getBySiteName(siteName)

if (!site) {
logger.error({
message: "Invalid site requested",
method: "getBlob",
meta: {
userId: userWithSiteSessionData.isomerUserId,
email: userWithSiteSessionData.email,
siteName,
},
})
return res.status(404).send({
message: "Please ensure that the site exists!",
})
}

// Step 2: Retrieve review request
const possibleReviewRequest = await this.reviewRequestService.getReviewRequest(
site,
requestId
)

if (isIsomerError(possibleReviewRequest)) {
logger.error({
message: "Invalid review request requested",
method: "getBlob",
meta: {
userId: userWithSiteSessionData.isomerUserId,
email: userWithSiteSessionData.email,
siteName,
requestId,
file: path,
},
})
return res.status(404).send({
message: "Please ensure that the site exists!",
})
}

// Step 3: Check if the user is a contributor of the site
const role = await this.collaboratorsService.getRole(
siteName,
userWithSiteSessionData.isomerUserId
)

if (!role) {
logger.error({
message:
"User with insufficient permissions attempted to retrieve blob diff",
method: "getBlob",
meta: {
userId: userWithSiteSessionData.isomerUserId,
email: userWithSiteSessionData.email,
siteName,
},
})
return res.status(404).send({
message: "Please ensure that the site exists!",
})
}

// NOTE: Currently, Isomer only allows comparisons between staging and production.
// This might change in the future and in that case, the `getBlob` method call below
// should have the corresponding ref (`master` or `staging`) changed.
const prodPromise = await this.reviewRequestService.getBlob(
siteName,
path,
"master"
)
const stagingPromise = await this.reviewRequestService.getBlob(
siteName,
path,
"staging"
)
alexanderleegs marked this conversation as resolved.
Show resolved Hide resolved

const data = await Promise.all([prodPromise, stagingPromise])
seaerchin marked this conversation as resolved.
Show resolved Hide resolved

return res.status(200).json({
old: data[0],
new: data[1],
})
}

getRouter() {
const router = express.Router({ mergeParams: true })

Expand Down Expand Up @@ -1020,7 +1117,7 @@ export class ReviewsRouter {
"/:requestId",
attachReadRouteHandlerWrapper(this.closeReviewRequest)
)

router.get("/:requestId/blob", attachReadRouteHandlerWrapper(this.getBlob))
return router
}
}
4 changes: 4 additions & 0 deletions src/services/db/GitHubService.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class GitHubService {
return ReviewApi.getPullRequest(siteName, pullRequestNumber)
}

getBlob(repo, path, ref) {
return ReviewApi.getBlob(repo, path, ref)
}

updatePullRequest(siteName, pullRequestNumber, title, description) {
return ReviewApi.updatePullRequest(
siteName,
Expand Down
13 changes: 13 additions & 0 deletions src/services/db/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,16 @@ export const createComment = async (
{ body: stringifiedMessage }
)
}

export const getBlob = async (
repo: string,
path: string,
ref: string
): Promise<string> =>
axiosInstance
.get<string>(`${repo}/contents/${path}?ref=${ref}`, {
headers: {
Accept: "application/vnd.github.VERSION.raw",
seaerchin marked this conversation as resolved.
Show resolved Hide resolved
},
})
.then(({ data }) => data)
3 changes: 3 additions & 0 deletions src/services/review/ReviewRequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,4 +620,7 @@ export default class ReviewRequestService {

return this.computeCommentData(comments, viewedTime)
}

getBlob = async (repo: string, path: string, ref: string): Promise<string> =>
this.apiService.getBlob(repo, path, ref)
}
5 changes: 5 additions & 0 deletions src/types/dto/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,8 @@ export interface GithubCommentData {
message: string
createdAt: string
}

export interface BlobDiffDto {
old: string
new: string
seaerchin marked this conversation as resolved.
Show resolved Hide resolved
}