/
main.ts
151 lines (142 loc) · 5.56 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { Probot } from "probot";
import { connectMongoDB } from "./db/connections/mongodbConnection";
import { processRepositories } from "./services/repositoryService";
import eventConfigs from "./configs/github.webhook.event.configs.json";
import {
constructMarkdownComment,
createCommentOnGithub,
} from "./services/commentService";
import {
processPullRequestOpenEvent,
updateFilesInDb,
} from "./services/pullRequestService";
import { FileScoreMap } from "./types/File";
import { connectMindsDB } from "./db/connections/mindsdbConnection";
import {
retrainPredictorModel,
trainPredictorModel,
} from "./services/predictionService";
import {
errorFallbackCommentForPRClosedEvent,
errorFallbackCommentForPROpenEvent,
} from "./constants/Comments";
import { predictedScoresUpdationScheduler } from "./schedulers/predictedScoreScheduler";
import { getProbotInstance } from "./auth";
const app = getProbotInstance();
/**
* This function is responsible for initializing the app and
* delegating all the events received from github to various
* modules. Probot handles the event routing, so we just need
* to add listeners for all the events that we want to process.
*
* @param probotApp default probot instance
*/
export async function main(probotApp: Probot) {
try {
connectMongoDB().catch((error: any) => app.log.error(error));
connectMindsDB().catch((error: any) => app.log.error(error));
handleAppInstallationCreatedEvents(probotApp);
handlePullRequestOpenEvents(probotApp);
handlePullRequestClosedEvents(probotApp);
trainPredictorModel();
predictedScoresUpdationScheduler();
} catch (error: any) {
app.log.error("Error occured in main function");
app.log.error(error);
}
}
/**
* App installation event is published when a user
* installs the app on one or more repositories.
* Read more about installation webhooks {@link https://docs.github.com/en/webhooks/webhook-events-and-payloads#installation | here}
* @param app default probot instance
*/
function handleAppInstallationCreatedEvents(app: Probot) {
const events: any[] = [eventConfigs.app_installation.created];
app.on(events, (context: any) => {
app.log.info(
`Received an event with event id: ${context.id}, name: ${context.name} and action: ${context.payload.action}`
);
processRepositories(app, context.payload).catch((error: any) => {
app.log.error("Error while processing app installation event");
app.log.error(error);
});
});
}
/**
* Pull request open event is published when a user opens
* a pull request on any repository. Read more about pull request
* webhooks {@link https://docs.github.com/en/webhooks/webhook-events-and-payloads?actionType=opened#pull_request | here}
*
* @param app default probot instance
*/
function handlePullRequestOpenEvents(app: Probot) {
const events: any[] = [eventConfigs.pull_request.opened];
app.on(events, async (context: any) => {
app.log.info(
`Received an event with event id: ${context.id}, name: ${context.name} and action: ${context.payload.action}`
);
try {
const files: FileScoreMap[] = await processPullRequestOpenEvent(
app,
context.payload
);
const comment = await constructMarkdownComment(app, files);
const installationId = context.payload.installation.id;
const pullRequestNumber = context.payload.number;
const repoName = context.pull_request.base.repo.full_name;
const logParams = {
installationId: installationId,
pullRequestNumber: pullRequestNumber,
repoName: repoName,
};
createCommentOnGithub(app, comment, context, logParams);
} catch (error: any) {
app.log.error("Error while processing pull request opened event");
app.log.error(error);
const comment: string = errorFallbackCommentForPROpenEvent();
createCommentOnGithub(app, comment, context);
}
});
}
/**
* Pull request closed events are published when a user
* closes a pull request on any repository. Read more about
* pull request webhooks {@link https://docs.github.com/en/webhooks/webhook-events-and-payloads?actionType=opened#pull_request | here}
*
* @param app default probot instance
*/
function handlePullRequestClosedEvents(app: Probot) {
const events: any[] = [eventConfigs.pull_request.closed];
app.on(events, async (context: any) => {
app.log.info(
`Received an event with event id: ${context.id}, name: ${context.name} and action: ${context.payload.action}`
);
try {
const areFilesUpdated: boolean = await updateFilesInDb(
app,
context.payload
);
if (areFilesUpdated) {
const comment =
"Risk scores are updated for all the files modified in this pull request.";
const installationId = context.payload.installation.id;
const pullRequestNumber = context.payload.number;
const repoName = context.pull_request.base.repo.full_name;
const logParams = {
installationId: installationId,
pullRequestNumber: pullRequestNumber,
repoName: repoName,
};
createCommentOnGithub(app, comment, context, logParams);
}
retrainPredictorModel(app);
return context;
} catch (error: any) {
app.log.error("Error while processing pull request closed event");
app.log.error(error);
const comment = errorFallbackCommentForPRClosedEvent();
createCommentOnGithub(app, comment, context);
}
});
}