Skip to content

Commit

Permalink
testing octokit auth
Browse files Browse the repository at this point in the history
  • Loading branch information
vishesh-baghel committed Jan 9, 2024
1 parent 389d335 commit 65187bb
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 84 deletions.
113 changes: 74 additions & 39 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
"dependencies": {
"@netlify/functions": "^2.4.1",
"@octokit/app": "^14.0.2",
"@octokit/webhooks": "^12.0.11",
"@probot/adapter-aws-lambda-serverless": "^3.0.4",
"axios": "^1.6.3",
"eventsource": "^2.0.2",
"from-exponential": "^1.1.1",
"json2md": "^2.0.0",
"markdown-table-ts": "^1.0.3",
Expand All @@ -34,6 +36,7 @@
"toad-scheduler": "^3.0.0"
},
"devDependencies": {
"@types/eventsource": "^1.1.15",
"@types/jest": "^29.0.0",
"@types/json2md": "^1.5.4",
"@types/node": "^18.0.0",
Expand Down
23 changes: 23 additions & 0 deletions src/auth/octokit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Octokit } from "octokit";
import { createAppAuth } from "@octokit/auth-app";

const token = process.env.GITHUB_ACCESS_TOKEN;
const appId = process.env.APP_ID;
const privateKey = process.env.PRIVATE_KEY;

export function getOctokitInstance(): Octokit {
return new Octokit({
auth: token,
});
}

export function getOctokitWithInstallationId(installationId: number): Octokit {
return new Octokit({
authStrategy: createAppAuth,
auth: {
appId: appId,
privateKey: privateKey,
installationId: installationId,
},
});
}
35 changes: 10 additions & 25 deletions src/fetch/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
import { Octokit } from "octokit";
import { RequestParameters } from "@octokit/types";
import { Probot } from "probot";
import { createAppAuth } from "@octokit/auth-app";
import {
getOctokitInstance,
getOctokitWithInstallationId,
} from "../auth/octokit";

const token = process.env.GITHUB_ACCESS_TOKEN;
const appId = process.env.APP_ID;
const privateKey = process.env.PRIVATE_KEY;

const octokitApp = new Octokit({
auth: token,
});

function fetchDetailsWithInstallationId(
export function fetchDetailsWithInstallationId(
app: Probot,
installationId: number,
endpoint: string,
parameters: RequestParameters
) {
const octokit = new Octokit({
authStrategy: createAppAuth,
auth: {
appId: appId,
privateKey: privateKey,
installationId: installationId,
},
});

return new Promise(async (resolve, reject) => {
try {
const data = await octokitApp.request(endpoint, parameters);
const octokit = getOctokitWithInstallationId(installationId);
const data = await octokit.request(endpoint, parameters);

resolve(data);
} catch (err: any) {
Expand All @@ -40,14 +26,15 @@ function fetchDetailsWithInstallationId(
});
}

function fetchDetails(
export function fetchDetails(
app: Probot,
endpoint: string,
parameters: RequestParameters
) {
return new Promise(async (resolve, reject) => {
try {
const data = await octokitApp.request(endpoint, parameters);
const octokit = getOctokitInstance();
const data = await octokit.request(endpoint, parameters);

resolve(data);
} catch (err: any) {
Expand All @@ -58,5 +45,3 @@ function fetchDetails(
}
});
}

export { fetchDetails, fetchDetailsWithInstallationId };
2 changes: 1 addition & 1 deletion src/fetch/fetchFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export async function getAllFilesFromPullRequest(
}));

app.log.info(
`Total ${files.length} files fetched from github for pull request(number:${pullNumber}) of the repository: [${owner}/${repoName}]`
`Total ${files.length} files fetched from github for pull request(number:${pullNumber}) of the repository: [${owner}/${repoName}] and installationId: [${installationId}]`
);

return files;
Expand Down
25 changes: 20 additions & 5 deletions src/listeners/githubWebhookListener.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import { Webhooks } from "@octokit/webhooks";
import { Probot } from "probot";
import { WebhookAndContext } from "../main";

export function listeningForGithubWebhookEvents(app: Probot, events: any[]) {
return new Promise((resolve, reject) => {
export function listeningForGithubWebhookEvents(
app: Probot,
events: any[],
webhook: Webhooks
): Promise<WebhookAndContext> {
return new Promise(() => {
try {
app.log.info(`Listening for ${events} events`);

app.on(events, async (context) => {
resolve(context);
let context: any;
app.on(events, (res: any) => {
context = res;
});
webhook.on(events, (response: any) => {
app.log.info(response);
const data: WebhookAndContext = {
context: context,
webhook: response,
};
Promise.resolve(data);
});
} catch (error) {
app.log.error(`Error occurred while listening for ${events} events`);
reject(error);
Promise.reject(error);
}
});
}
36 changes: 24 additions & 12 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,52 @@ import {
errorFallbackCommentForPROpenEvent,
} from "./constants/Comments";
import { predictedScoresUpdationScheduler } from "./schedulers/predictedScoreScheduler";
import { receiveGithubEvents } from "./webhooks/octokit";
import { Webhooks } from "@octokit/webhooks";

const debugFlag: boolean = false;

export async function main(app: Probot) {
connectMongoDB(app).catch((error: any) => app.log.error(error));
connectMindsDB(app).catch((error: any) => app.log.error(error));
handleAppInstallationCreatedEvents(app);
handlePullRequestOpenEvents(app);
handlePullRequestClosedEvents(app);
trainPredictorModel(app);
debug(app);
predictedScoresUpdationScheduler(app);

receiveGithubEvents().then((webhook: Webhooks) => {
// handleAppInstallationCreatedEvents(app, webhook);
handlePullRequestOpenEvents(app, webhook);
// handlePullRequestClosedEvents(app, webhook);
});
debug(app);
}

function handleAppInstallationCreatedEvents(app: Probot) {
function handleAppInstallationCreatedEvents(app: Probot, webhook: Webhooks) {
const events: any[] = [eventConfigs.app_installation.created];

listeningForGithubWebhookEvents(app, events)
listeningForGithubWebhookEvents(app, events, webhook)
.then((context: any) => processRepositories(app, context.payload))
.catch((error: any) => {
app.log.error("Error while processing app installation event");
app.log.error(error);
});
}

function handlePullRequestOpenEvents(app: Probot) {
export type WebhookAndContext = {
context: any;
webhook: any;
};

function handlePullRequestOpenEvents(app: Probot, webhook: Webhooks) {
const events: any[] = [eventConfigs.pull_request.opened];

listeningForGithubWebhookEvents(app, events)
.then(async (context: any) => {
listeningForGithubWebhookEvents(app, events, webhook)
.then(async (res: WebhookAndContext) => {
const context = res.context;
const webhook = res.webhook;
try {
const files: FileScoreMap[] = await processPullRequestOpenEvent(
app,
context.payload
webhook.payload
);
const comment = await constructComment(app, files);
createCommentOnGithub(app, comment, context);
Expand All @@ -75,10 +87,10 @@ function handlePullRequestOpenEvents(app: Probot) {
});
}

function handlePullRequestClosedEvents(app: Probot) {
function handlePullRequestClosedEvents(app: Probot, webhook: Webhooks) {
const events: any[] = [eventConfigs.pull_request.closed];

listeningForGithubWebhookEvents(app, events)
listeningForGithubWebhookEvents(app, events, webhook)
.then(async (context: any) => {
try {
const areFilesUpdated: boolean = await updateFilesInDb(
Expand Down
Loading

0 comments on commit 65187bb

Please sign in to comment.