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

testing octokit auth #46

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
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,
});
}
Comment on lines +8 to +12
Copy link

Choose a reason for hiding this comment

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

The function getOctokitInstance correctly creates an Octokit instance using a personal access token. However, consider adding error handling for cases where the token might be undefined or invalid.


export function getOctokitWithInstallationId(installationId: number): Octokit {
return new Octokit({
authStrategy: createAppAuth,
auth: {
appId: appId,
privateKey: privateKey,
installationId: installationId,
},
});
Comment on lines +14 to +22
Copy link

Choose a reason for hiding this comment

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

The function getOctokitWithInstallationId correctly sets up an Octokit instance with app-based authentication. Ensure that appId, privateKey, and installationId are validated before use to prevent runtime errors.

}
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);
Comment on lines +5 to +28
Copy link

Choose a reason for hiding this comment

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

The listeningForGithubWebhookEvents function is intended to return a Promise, but the resolve and reject methods are not being used correctly. They should be called with the appropriate values to resolve or reject the promise.

- return new Promise(() => {
+ return new Promise((resolve, reject) => {
-        Promise.resolve(data);
+        resolve(data);
-      Promise.reject(error);
+      reject(error);

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
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);
export function listeningForGithubWebhookEvents(
app: Probot,
events: any[],
webhook: Webhooks
): Promise<WebhookAndContext> {
return new Promise((resolve, reject) => {
try {
app.log.info(`Listening for ${events} events`);
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,
};
resolve(data);
});
} catch (error) {
app.log.error(`Error occurred while listening for ${events} events`);
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)
Comment on lines +47 to +50
Copy link

Choose a reason for hiding this comment

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

The handleAppInstallationCreatedEvents function is commented out. If this is intentional and the function is no longer needed, consider removing the code. If it's temporarily disabled, ensure there's a tracking system or comment explaining the reason.

.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