Skip to content

Commit

Permalink
Merge pull request RocketChat#41 from Nabhag8848/repo-validation
Browse files Browse the repository at this point in the history
[FIX] commands and buttons, unexpected results when repository in github doesn't exist
  • Loading branch information
samad-yar-khan authored Mar 24, 2023
2 parents 6d525f5 + 77d46dd commit 75b0641
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 8 deletions.
30 changes: 30 additions & 0 deletions github/handlers/EventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { sendNotification } from "../lib/message";
import { subsciptionsModal } from "../modals/subscriptionsModal";
import { getAccessTokenForUser } from "../persistance/auth";
import { Subscription } from "../persistance/subscriptions";
import { HandleInvalidRepoName } from "./HandleInvalidRepoName";

export async function SubscribeAllEvents(
read: IRead,
Expand All @@ -36,6 +37,21 @@ export async function SubscribeAllEvents(
const repository = command[0];
if (accessToken && accessToken?.token) {
try {
const sender = context.getSender();
const isValidRepo = await HandleInvalidRepoName(
repository,
http,
app,
modify,
sender,
read,
room
);

if (!isValidRepo) {
return;
}

let events: Array<string> = [
"pull_request",
"push",
Expand Down Expand Up @@ -148,6 +164,20 @@ export async function UnsubscribeAllEvents(
if (accessToken && accessToken?.token) {
try {
let user = await context.getSender();
const isValidRepo = await HandleInvalidRepoName(
repository,
http,
app,
modify,
user,
read,
room
);

if (!isValidRepo) {
return;
}

let events: Array<string> = [
"pull_request",
"push",
Expand Down
60 changes: 60 additions & 0 deletions github/handlers/HandleInvalidRepoName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {
IHttp,
IModify,
IRead,
} from "@rocket.chat/apps-engine/definition/accessors";
import { GithubApp } from "../GithubApp";
import { isRepositoryExist } from "../helpers/githubSDK";
import { getAccessTokenForUser } from "../persistance/auth";
import { IUser } from "@rocket.chat/apps-engine/definition/users";
import { IRoom } from "@rocket.chat/apps-engine/definition/rooms";
import { IAuthData } from "@rocket.chat/apps-engine/definition/oauth2/IOAuth2";

export async function HandleInvalidRepoName(
repoName: string,
http: IHttp,
app: GithubApp,
modify: IModify,
sender: IUser,
read: IRead,
room: IRoom
): Promise<boolean> {
const accessToken = (await getAccessTokenForUser(
read,
sender,
app.oauth2Config
)) as IAuthData;

const isValidRepository: boolean = await isRepositoryExist(
http,
repoName,
accessToken?.token
);

if (!isValidRepository) {
const warningBuilder = await modify
.getCreator()
.startMessage()
.setRoom(room);

if (accessToken) {
warningBuilder.setText(
`Hey ${sender.username}! Provided repository doesn't exist.`
);
} else {
warningBuilder.setText(
`Hey ${sender.username}! Provided repository doesn't exist or you need to login to access private repo.`
);
}

if (room.type !== "l") {
await modify
.getNotifier()
.notifyUser(sender, warningBuilder.getMessage());
} else {
await modify.getCreator().finish(warningBuilder);
}
}

return isValidRepository;
}
31 changes: 30 additions & 1 deletion github/helpers/githubSDK.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IHttp } from "@rocket.chat/apps-engine/definition/accessors";
import { IHttp, HttpStatusCode } from "@rocket.chat/apps-engine/definition/accessors";
import { IGitHubIssue } from "../definitions/githubIssue";
import { ModalsEnum } from "../enum/Modals";

Expand Down Expand Up @@ -726,3 +726,32 @@ export async function updateGithubIssues(
}
return repsonseJSON;
}

export async function isRepositoryExist(
http: IHttp,
repoName: string,
access_token?: string
): Promise<boolean> {
let response;

if (access_token) {
response = await http.get(`${BaseRepoApiHost}${repoName}`, {
headers: {
Authorization: `token ${access_token}`,
"Content-Type": "application/json",
},
});
} else {
response = await http.get(`${BaseRepoApiHost}${repoName}`, {
headers: {
"Content-Type": "application/json",
},
});
}

if (response.statusCode.toString().startsWith("2")) {
return true;
}

return false;
}
44 changes: 37 additions & 7 deletions github/lib/commandUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
import { handleSearch } from "../handlers/SearchHandler";
import { handleIssues, handleNewIssue } from "../handlers/HandleIssues";
import { handleUserProfileRequest } from "../handlers/UserProfileHandler";
import { HandleInvalidRepoName } from "../handlers/HandleInvalidRepoName";

export class CommandUtility implements ExecutorProps {
sender: IUser;
Expand Down Expand Up @@ -54,13 +55,26 @@ export class CommandUtility implements ExecutorProps {
arguments: this.command,
};
if (this.command[0].includes("/")) {
await initiatorMessage({
data,
read: this.read,
persistence: this.persistence,
modify: this.modify,
http: this.http,
});
const repoName = this.command[0];
const isValidRepoName = await HandleInvalidRepoName(
repoName,
this.http,
this.app,
this.modify,
this.sender,
this.read,
this.room
);

if (isValidRepoName) {
await initiatorMessage({
data,
read: this.read,
persistence: this.persistence,
modify: this.modify,
http: this.http,
});
}
} else {
switch (this.command[0]) {
case SubcommandEnum.LOGIN: {
Expand Down Expand Up @@ -167,6 +181,7 @@ export class CommandUtility implements ExecutorProps {
private async handleDualParamCommands() {
const query = this.command[1];
const repository = this.command[0];

switch (query) {
case SubcommandEnum.SUBSCRIBE: {
SubscribeAllEvents(
Expand Down Expand Up @@ -215,6 +230,21 @@ export class CommandUtility implements ExecutorProps {
query: this.command[1],
number: this.command[2],
};

const isValidRepo = await HandleInvalidRepoName(
data.repository,
this.http,
this.app,
this.modify,
this.sender,
this.read,
this.room
);

if (!isValidRepo) {
return;
}

const triggerId = this.context.getTriggerId();
if (triggerId) {
const modal = await pullDetailsModal({
Expand Down

0 comments on commit 75b0641

Please sign in to comment.