Skip to content
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
18 changes: 17 additions & 1 deletion github/GithubApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
IConfigurationExtend,
IHttp,
ILogger,
IMessageExtender,
IModify,
IPersistence,
IRead,
Expand Down Expand Up @@ -40,12 +41,27 @@ 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 { GHCommand } from "./commands/GhCommand";
import { IMessage, IPreMessageSentExtend } from "@rocket.chat/apps-engine/definition/messages";
import { hasRepoLink, isGithubLink } from "./helpers/checkLinks";
import { handleRepoLink } from "./handlers/handleLinks";

export class GithubApp extends App {
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 hasRepoLink(message)){
await handleRepoLink(message,read,http,message.sender,message.room,extend);

}
return extend.getMessage();
}
public async authorizationCallback(
token: IAuthData,
user: IUser,
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"
]
}
49 changes: 49 additions & 0 deletions github/handlers/handleLinks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
IMessage,
IMessageAttachment,
MessageActionButtonsAlignment,
MessageActionType,
} from "@rocket.chat/apps-engine/definition/messages";
import { IRead, IHttp } from "@rocket.chat/apps-engine/definition/accessors";
import { IUser } from "@rocket.chat/apps-engine/definition/users";
import { IRoom } from "@rocket.chat/apps-engine/definition/rooms";
import { IMessageExtender } from "@rocket.chat/apps-engine/definition/accessors";

export async function handleRepoLink(
message: IMessage,
read: IRead,
http: IHttp,
user: IUser,
room: IRoom,
extend: IMessageExtender
) {

const regex = /github\.com\/([A-Za-z0-9-]+)\/([A-Za-z0-9-_.]+)/;
const url = message.text!;
const matches = url.match(regex)!;
const owner = matches[1];
const repo = matches[2];
const attachment: IMessageAttachment = {
actionButtonsAlignment: MessageActionButtonsAlignment.HORIZONTAL,
actions: [
{
type: MessageActionType.BUTTON,
text: "Create issue",
msg: `/github ${owner}/${repo} issue`,
msg_in_chat_window: true,
},
{
type: MessageActionType.BUTTON,
text: "Issues",
url:`https://www.github.com/${owner}/${repo}/issues`
},
{
type: MessageActionType.BUTTON,
text: "Pull Requests",
url:`https://www.github.com/${owner}/${repo}/pulls`
},

],
};
extend.addAttachment(attachment);
}
21 changes: 21 additions & 0 deletions github/helpers/checkLinks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { IMessage } from "@rocket.chat/apps-engine/definition/messages";

export async function isGithubLink(message: IMessage) {
let githubLink: RegExp = /(?:https?:\/\/)?(?:www\.)?github\.com\//;
if (githubLink.test(message.text!)) {
return true;
}
return false;
}

export async function hasRepoLink(message: IMessage) {
// const repoLink1: RegExp = /https:\/\/github\.com\/\S+\/\S+\/?\s/;
const repoLink1 = /https?:\/\/github\.com\/[A-Za-z0-9-]+\/[A-Za-z0-9-_.]+\s(?!\/)/;
const repoLink2 = /https?:\/\/github\.com\/[A-Za-z0-9-]+\/[A-Za-z0-9-_.]+\/?$/;

if (repoLink1.test(message.text!) || repoLink2.test(message.text!)) {

return true;
}
return false;
}
25 changes: 23 additions & 2 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 { NewIssueModal } from "../modals/newIssueModal";

export class CommandUtility implements ExecutorProps {
sender: IUser;
Expand Down Expand Up @@ -138,7 +139,7 @@ export class CommandUtility implements ExecutorProps {
);
break;
}
case SubcommandEnum.ISSUES :{
case SubcommandEnum.ISSUES: {
handleIssues(
this.read,
this.context,
Expand All @@ -147,7 +148,7 @@ export class CommandUtility implements ExecutorProps {
this.http,
this.room,
this.modify
)
);
break;
}
default: {
Expand Down Expand Up @@ -194,6 +195,26 @@ export class CommandUtility implements ExecutorProps {
);
break;
}
case SubcommandEnum.NEW_ISSUE: {
const data = { repository: repository };
const modal = await NewIssueModal({
data: data,
modify: this.modify,
read: this.read,
persistence: this.persistence,
http: this.http,
slashcommandcontext: this.context,
});
const triggerId = this.context.getTriggerId()!;
await this.modify
.getUiController()
.openModalView(
modal,
{ triggerId },
this.context.getSender()
);
break;
}
default: {
await basicQueryMessage({
query,
Expand Down
5 changes: 3 additions & 2 deletions github/lib/helperMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ export async function helperMessage({
11. Subscribe to all repository events -> \`/github Username/RepositoryName subscribe\`
12. Unsubscribe to all repository events -> \`/github Username/RepositoryName unsubscribe\`
13. Add New Issues to GitHub Repository -> \`/github issue\`
14. Search Issues and Pull Requests -> \`/github search\`
15. Assign and Share Issues -> \`/github issues\`
14. Add New Issue to a GitHub Repository -> \`/github Username/RepositoryName issue\`
15. Search Issues and Pull Requests -> \`/github search\`
16. Assign and Share Issues -> \`/github issues\`
`;

const textSender = await modify
Expand Down