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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor alerts #105

Merged
merged 21 commits into from Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 0 additions & 3 deletions package.json
Expand Up @@ -3,9 +3,6 @@
"version": "1.0.0",
"description": "Automations for continuous up-to-date documentation",
"main": "server/index.ts",
"engines": {
"node": "16.x"
},
"scripts": {
"dev": "npm run dev --workspace=server & npm run dev --workspace=web"
},
Expand Down
43 changes: 33 additions & 10 deletions server/src/helpers/github/app.ts
@@ -1,8 +1,9 @@
import axios from "axios";
import { Context } from "probot";
import { FileInfo, getEncompassingRangeAndSideForAlert, parsePatch, PatchLineRange } from "./patch";
import { AlertsRequest, Alert } from "./types";
import { ENDPOINT, getReviewComments } from "./octokit";
import { Alert, AlertStatus } from "./types";
import { getReviewComments, ENDPOINT } from "./octokit";
import { CodeType } from '../../models/Code';
import axios from 'axios';

type FilesPatchLineRangesMap = Record<string, PatchLineRange[]>;

Expand Down Expand Up @@ -63,15 +64,13 @@ type AlertsResponse = {
previousAlerts: Alert[],
}

export const getAlerts = async (context: Context, files: FileInfo[]): Promise<AlertsResponse> => {
const owner = context.payload.repository.owner.login;
const repo = context.payload.pull_request.head.repo.name;
const alertsRequest: AlertsRequest = { files, owner, repo }
export const getAlerts = async (context: Context, files: FileInfo[], codes: CodeType[]): Promise<AlertsResponse> => {
const incomingAlertsPromise = axios.post(`${ENDPOINT}/routes/alerts/`, { files, codes });;
const previousAlertsPromise = getReviewComments(context);
const alertsPromise = axios.post(`${ENDPOINT}/routes/alerts/`, alertsRequest);
const [alertsResponse, previousAlerts] = await Promise.all([alertsPromise, previousAlertsPromise]);

const [incomingAlerts, previousAlerts] = await Promise.all([incomingAlertsPromise, previousAlertsPromise]);
return {
incomingAlerts: alertsResponse.data.alerts,
incomingAlerts: incomingAlerts.data.alerts,
previousAlerts,
}
}
Expand Down Expand Up @@ -115,4 +114,28 @@ export const createReviewCommentsFromAlerts = async (context: Context, alerts: A
});
const reviewComments = await Promise.all(reviewCommentPromises);
return reviewComments;
}

export const associateReviewCommentsToAlerts = (alerts : Alert[], reviewComments: any[]): Alert[] => {
return alerts.map((alert) => {
let comment: null|any = null;
reviewComments.forEach((reviewComment) => {
if (reviewComment.data.body === alert.message) {
comment = reviewComment;
}
})
if (comment != null) {
alert.githubCommentId = comment?.data?.node_id;
alert.url = comment?.data?.html_url
}
return alert;
})
}

export const formatReviewComments = (previousAlerts: any[], id: string): AlertStatus => {
const affectedAlert = previousAlerts.find((prevAlert) => prevAlert?.comments?.edges[0]?.node?.id === id);
return {
isResolved: affectedAlert?.isResolved,
id
};
}
10 changes: 5 additions & 5 deletions server/src/helpers/github/octokit.ts
Expand Up @@ -3,6 +3,7 @@ import { ISDEV } from "../environment";

export const ADMIN_LOGIN = ISDEV ? 'mintlify-dev' : 'mintlify';
export const ENDPOINT = ISDEV ? 'http://localhost:5000' : 'https://connect.mintlify.com'
const checkName = ISDEV ? 'Dev - Continuous Documentation Check' : 'Continuous Documentation Check';

export const getReviewComments = async (context: Context) => {
const owner = context.payload.repository.owner.login;
Expand All @@ -22,6 +23,7 @@ export const getReviewComments = async (context: Context) => {
edges {
node {
body
id
author {
login
}
Expand All @@ -34,11 +36,9 @@ export const getReviewComments = async (context: Context) => {
}
}
}`);

const allAdminReviewComments = reviewComments.repository.pullRequest.reviewThreads.edges.filter((edge: any) => {
return edge.node.comments.edges[0].node.author.login === ADMIN_LOGIN;
});

return allAdminReviewComments.map((reviewComment: any) => reviewComment.node);
}

Expand All @@ -54,7 +54,7 @@ export const createInProgressCheck = (context: Context) => {
owner,
repo,
head_sha: context.payload.pull_request.head.sha,
name: 'Continuous Documentation Check',
name: checkName,
status: 'in_progress'
})
}
Expand All @@ -67,7 +67,7 @@ export const createSuccessCheck = (context: Context) => {
owner,
repo,
head_sha: context.payload.pull_request.head.sha,
name: 'Continuous Documentation Check',
name: checkName,
status: 'completed',
conclusion: 'success',
})
Expand All @@ -81,7 +81,7 @@ export const createActionRequiredCheck = (context: Context, detailsUrl?: string)
owner,
repo,
head_sha: context.payload.pull_request.head.sha,
name: 'Continuous Documentation Check',
name: checkName,
status: 'completed',
conclusion: 'action_required',
details_url: detailsUrl,
Expand Down
15 changes: 8 additions & 7 deletions server/src/helpers/github/types.ts
@@ -1,4 +1,4 @@
import { FileInfo } from "./patch";
import { CodeType } from '../../models/Code';

export type Change = {
type: 'add' | 'delete';
Expand All @@ -17,6 +17,8 @@ export type Alert = {
filename: string;
lineRange: LineRange;
type: string;
code: CodeType;
githubCommentId?: string;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unique identifier for the github comment

}

export type Link = {
Expand All @@ -25,15 +27,14 @@ export type Link = {
type: string;
}

export type AlertsRequest = {
files: FileInfo[],
owner: string,
repo: string,
}

export type TaskRequest = {
owner: string,
repo: string,
pullNumber: number,
installationId?: number,
}

export type AlertStatus = {
isResolved: boolean;
id: string;
}
1 change: 1 addition & 0 deletions server/src/helpers/routes/alerts.ts
Expand Up @@ -33,6 +33,7 @@ export const codeToAlert = async (code: CodeType, file: FileInfo): Promise<Alert
filename: file.filename,
type: code.type,
lineRange,
code
}
};

Expand Down
51 changes: 42 additions & 9 deletions server/src/index.ts
@@ -1,17 +1,40 @@
import { ApplicationFunctionOptions, Context, Probot } from "probot";
import './services/mongoose';
import { getReviewComments, checkIfAllAlertsAreResolve, createSuccessCheck, createActionRequiredCheck, createInProgressCheck } from "./helpers/github/octokit";
import axios from "axios";
import { getReviewComments, checkIfAllAlertsAreResolve, createSuccessCheck, createActionRequiredCheck, createInProgressCheck, ENDPOINT } from "./helpers/github/octokit";
import headRouter from "./routes";
import { createReviewCommentsFromAlerts, filterNewAlerts, getAlerts, getAllFilesAndMap } from "./helpers/github/app";
import { createReviewCommentsFromAlerts, filterNewAlerts, getAlerts, getAllFilesAndMap, associateReviewCommentsToAlerts, formatReviewComments } from "./helpers/github/app";
import './services/mongoose';

export = (app: Probot, { getRouter }: ApplicationFunctionOptions) => {
app.on(["pull_request.opened", "pull_request.reopened", "pull_request.synchronize"], async (context) => {
await createInProgressCheck(context);

const { files, filesPatchLineRangesMap } = await getAllFilesAndMap(context);
const { incomingAlerts, previousAlerts } = await getAlerts(context, files);
const owner = context.payload.repository.owner.login;
const repo = context.payload.pull_request.head.repo.name;

if (files == null || owner == null || repo == null) {
await createSuccessCheck(context);
return;
}

const orgResponse = await axios.get(`${ENDPOINT}/routes/org/gitOrg/${owner}/details`, {
params: {
repo
}
});

const { org, codes } = orgResponse.data;

if (codes.length === 0 || org == null) {
await createSuccessCheck(context);
return;
}

const { incomingAlerts, previousAlerts } = await getAlerts(context, files, codes);

if (incomingAlerts == null) {
await createSuccessCheck(context);
return;
}

Expand All @@ -22,23 +45,33 @@ export = (app: Probot, { getRouter }: ApplicationFunctionOptions) => {
return;
};

await createReviewCommentsFromAlerts(context, newAlerts, filesPatchLineRangesMap);
// Create tasks using review comments
// const taskRequests = reviewComments.map(())
await createActionRequiredCheck(context, newAlerts[0]?.url);
const reviewCommentsPromise = createReviewCommentsFromAlerts(context, newAlerts, filesPatchLineRangesMap);
const checkPromise = createActionRequiredCheck(context, newAlerts[0]?.url);
const [reviewComments, _] = await Promise.all([reviewCommentsPromise, checkPromise]);
const alerts = associateReviewCommentsToAlerts(newAlerts, reviewComments);
await axios.post(`${ENDPOINT}/routes/tasks/github`, {
alerts
});
return;
});

app.on(['pull_request_review_thread.resolved', 'pull_request_review_thread.unresolved'] as any, async (context: Context) => {
await createInProgressCheck(context);
const previousAlerts = await getReviewComments(context);
const isAllPreviousAlertsResolved = checkIfAllAlertsAreResolve(previousAlerts);

if (isAllPreviousAlertsResolved) {
await createSuccessCheck(context);
} else {
await createActionRequiredCheck(context);
}
const alertStatus = formatReviewComments(previousAlerts, context.payload.thread.comments[0]?.node_id);
const owner = context.payload.repository.owner.login;
const repo = context.payload.pull_request.head.repo.name;
await axios.post(`${ENDPOINT}/routes/tasks/github/update`, {
alertStatus,
gitOrg: owner,
repo
});
});

const router = getRouter!("/routes");
Expand Down
4 changes: 2 additions & 2 deletions server/src/models/Code.ts
Expand Up @@ -6,7 +6,7 @@ export type CodeType = {
sha: string;
provider: string;
file: string;
org: string;
org: Types.ObjectId;
gitOrg: string;
repo: string;
type: string;
Expand All @@ -22,7 +22,7 @@ const CodeSchema = new Schema({
provider: { type: String, required: true },
file: { type: String, required: true },
gitOrg: { type: String, required: true },
org: { type: String, required: true },
org: { type: Schema.Types.ObjectId, required: true },
repo: { type: String, required: true },
type: { type: String, required: true },
url: { type: String, required: true },
Expand Down
4 changes: 2 additions & 2 deletions server/src/models/Event.ts
Expand Up @@ -9,7 +9,7 @@ export type EventType = {
type: EventTypeMeta;
change?: Array<Diff.Change>;
add?: Object;
code?: Types.ObjectId;
code?: Object;
};

const EventSchema = new Schema({
Expand All @@ -19,7 +19,7 @@ const EventSchema = new Schema({
createdAt: { type: Date, default: Date.now },
change: Array, // only for change events
add: Object, // only for add events
code: { type: Schema.Types.ObjectId }
code: Object // only for code events
});

const Event = mongoose.model<EventType>('Event', EventSchema, 'events');
Expand Down
9 changes: 7 additions & 2 deletions server/src/models/Task.ts
@@ -1,6 +1,7 @@
import mongoose, { Schema, Types } from 'mongoose';

export type TaskStatus = 'todo' | 'done';
export type TaskSource = 'github' | 'mintlify';
export type TaskTypeMeta = 'new' | 'update' | 'review';

export type TaskType = {
Expand All @@ -9,8 +10,10 @@ export type TaskType = {
code: Types.ObjectId;
status: TaskStatus;
type: TaskTypeMeta;
createdAt: Date;
url?: String;
source: TaskSource;
createdAt?: Date;
url?: string;
githubCommentId?: string;
};

const TaskSchema = new Schema({
Expand All @@ -19,8 +22,10 @@ const TaskSchema = new Schema({
code: Schema.Types.ObjectId,
status: { type: String, required: true },
type: { type: String, required: true },
source: String,
createdAt: { type: Date, default: Date.now },
url: String,
githubCommentId: String
});

const Task = mongoose.model<TaskType>('Task', TaskSchema, 'tasks');
Expand Down