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

[FEAT]Expand code link added #61

Open
wants to merge 4 commits 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
56 changes: 46 additions & 10 deletions github/GithubApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,48 @@ import {
import { githubWebHooks } from "./endpoints/githubEndpoints";
import { IJobContext } from "@rocket.chat/apps-engine/definition/scheduler";
import { IRoom } from "@rocket.chat/apps-engine/definition/rooms";
import { clearInteractionRoomData, getInteractionRoomData } from "./persistance/roomInteraction";
import {
clearInteractionRoomData,
getInteractionRoomData,
} from "./persistance/roomInteraction";
import { GHCommand } from "./commands/GhCommand";

export class GithubApp extends App {
import {
IPreMessageSentExtend,
IMessage,
} from "@rocket.chat/apps-engine/definition/messages";
import { IMessageExtender } from "@rocket.chat/apps-engine/definition/accessors";
import { hasCodeLink, isGithubLink } from "./helpers/checkLinks";
import { handleCodeLink } from "./handlers/handleLinks";
export class GithubApp extends App implements IPreMessageSentExtend {
constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) {
super(info, logger, accessors);
}

public async checkPreMessageSentExtend(
message: IMessage,
read: IRead,
http: IHttp
): Promise<boolean> {
if (await isGithubLink(message)) {
return true;
}
return false;
}

public async executePreMessageSentExtend(
message: IMessage,
extend: IMessageExtender,
read: IRead,
http: IHttp,
persistence: IPersistence
): Promise<IMessage> {

if(await hasCodeLink(message)){
await handleCodeLink(message,read,http,message.sender,message.room,extend);
}
return extend.getMessage();
}

public async authorizationCallback(
token: IAuthData,
user: IUser,
Expand All @@ -63,23 +97,25 @@ export class GithubApp extends App {
},
};
let text = `GitHub Authentication Succesfull 🚀`;
let interactionData = await getInteractionRoomData(read.getPersistenceReader(),user.id) ;
let interactionData = await getInteractionRoomData(
read.getPersistenceReader(),
user.id
);

if (token) {
// await registerAuthorizedUser(read, persistence, user);
await modify.getScheduler().scheduleOnce(deleteTokenTask);
} else {
text = `Authentication Failure 😔`;
}
if(interactionData && interactionData.roomId){
if (interactionData && interactionData.roomId) {
let roomId = interactionData.roomId as string;
let room = await read.getRoomReader().getById(roomId) as IRoom;
await clearInteractionRoomData(persistence,user.id);
await sendNotification(read,modify,user,room,text);
}else{
let room = (await read.getRoomReader().getById(roomId)) as IRoom;
await clearInteractionRoomData(persistence, user.id);
await sendNotification(read, modify, user, room, text);
} else {
await sendDirectMessage(read, modify, user, text, persistence);
}

}
public oauth2ClientInstance: IOAuth2Client;
public oauth2Config: IOAuth2ClientOptions = {
Expand Down
4 changes: 3 additions & 1 deletion github/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@
"nameSlug": "github",
"classFile": "GithubApp.ts",
"description": "The ultimate app extending Rocket.Chat for all developers collaborating on Github",
"implements": []
"implements": [
"IPreMessageSentExtend"
]
}
57 changes: 57 additions & 0 deletions github/handlers/handleLinks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { IUser } from "@rocket.chat/apps-engine/definition/users";
import { IHttp, IRead } from "@rocket.chat/apps-engine/definition/accessors";
import {
IMessage,
IMessageAttachment,
} from "@rocket.chat/apps-engine/definition/messages";
import { IRoom } from "@rocket.chat/apps-engine/definition/rooms";
import { IMessageExtender } from "@rocket.chat/apps-engine/definition/accessors";
import { TextObjectType } from "@rocket.chat/apps-engine/definition/uikit";

async function checkLines(content, url) {
const regex: RegExp = /(?:L(\d+)+-L(\d+)|L(\d+))/;
const match: RegExpMatchArray = url.match(regex);
if (match[2]) {
const endLine = parseInt(match[2]) - parseInt(match[1]);
const lines = new RegExp(
`(?:.*\n){${parseInt(match[1]) - 1}}(.*(?:\n.*){${endLine}})`
);
const text = await content.match(lines);
return text[1];
} else if (match[3]) {
const lines = new RegExp(
`(?:.*\n){${parseInt(match[3]) - 1}}(.*(?:\n.*){5})`
);
const text = await content.match(lines);
return text[1];
} else {
const lines = new RegExp(`(?:.*\n){0}(.*(?:\n.*){5})`);
const text = await content.match(lines);
return text[1];
}
}

export async function handleCodeLink(
message: IMessage,
read: IRead,
http: IHttp,
user: IUser,
room: IRoom,
extend: IMessageExtender
) {
const regex: RegExp = /\bhttps?:\/\/github\.com\/\S+\b/;
let text = message.text!;
const match: RegExpMatchArray | null = text.match(regex);
const result: string | undefined = match?.[0];
let url: string = result?.replace("blob/", "")!;
url = url.replace("github.com", "raw.githubusercontent.com");
let response: any = await http.get(url);
const { content } = response;
let code = await checkLines(content, url);

let attachment: IMessageAttachment = {
text: `\`\`\`\n${code}\n\`\`\` \n[Show more...](${result})`,
type: TextObjectType.MARKDOWN,
};
extend.addAttachment(attachment);
}
19 changes: 19 additions & 0 deletions github/helpers/checkLinks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { IMessage } from "@rocket.chat/apps-engine/definition/messages";

export async function hasCodeLink(message: IMessage): Promise<Boolean> {
let lineNo: RegExp =
/https?:\/\/github\.com\/[A-Za-z0-9_-]+\/[A-Za-z0-9_.-]+\/blob\/[A-Za-z0-9_-]+\/.+/;

if (lineNo.test(message.text!)) {
return true;
}
return false;
}

export async function isGithubLink(message: IMessage) {
let githubLink: RegExp = /(?:https?:\/\/)?(?:www\.)?github\.com\//;
if (githubLink.test(message.text!)) {
return true;
}
return false;
}
101 changes: 81 additions & 20 deletions github/package-lock.json

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