Skip to content

Commit

Permalink
Merge branch 'main' into pr-expand
Browse files Browse the repository at this point in the history
  • Loading branch information
VipinDevelops committed Oct 25, 2023
2 parents f8ca2ff + 34f2648 commit 3588f3d
Show file tree
Hide file tree
Showing 19 changed files with 770 additions and 29 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ The GitHub Rocket.Chat App provides a seamless integration between GitHub and Ro
<li> Get Issues of a Repository -> /github Username/RepositoryName issues </li>
<li> Get Contributors of a Repository -> /github Username/RepositoryName contributors </li>
<li> Get Recent Pull Request of a Repository -> /github Username/RepositoryName pulls </li>
<li>Add a new repository for pull request review reminders -> /github reminder create</li>
<li>Get a list of repositories for which you've set up pull request review reminders -> /github reminder list</li>
</ul>

<h2 align='center'>🚀 Contributing 🚀</h2>
Expand Down
34 changes: 31 additions & 3 deletions github/GithubApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
IAppAccessors,
IAppInstallationContext,
IConfigurationExtend,
IConfigurationModify,
IHttp,
ILogger,
IMessageBuilder,
Expand Down Expand Up @@ -30,28 +31,29 @@ import {
IOAuth2ClientOptions,
} from "@rocket.chat/apps-engine/definition/oauth2/IOAuth2";
import { createOAuth2Client } from "@rocket.chat/apps-engine/definition/oauth2/OAuth2";
import { createSectionBlock } from "./lib/blocks";
import {
sendDirectMessage,
sendDirectMessageOnInstall,
sendMessage,
sendNotification,
} from "./lib/message";
import { OAuth2Client } from "@rocket.chat/apps-engine/server/oauth2/OAuth2Client";
import { deleteOathToken } from "./processors/deleteOAthToken";
import { ProcessorsEnum } from "./enum/Processors";
import {
ApiSecurity,
ApiVisibility,
} from "@rocket.chat/apps-engine/definition/api";
import { githubWebHooks } from "./endpoints/githubEndpoints";
import { IJobContext, StartupType } 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 { IPreMessageSentExtend, IMessage,IPreMessageSentModify, IPostMessageSent } from "@rocket.chat/apps-engine/definition/messages";
import { handleGitHubCodeSegmentLink } from "./handlers/GitHubCodeSegmentHandler";
import { isGithubLink, hasGitHubCodeSegmentLink, hasGithubPRLink } from "./helpers/checkLinks";
import { handleGithubPRLink } from "./handlers/GithubPRlinkHandler";
import { SendReminder } from "./handlers/SendReminder";
import { AppSettings, settings } from "./settings/settings";
import { ISetting } from "@rocket.chat/apps-engine/definition/settings";import { handleGithubPRLink } from "./handlers/GithubPRlinkHandler";

export class GithubApp extends App implements IPreMessageSentExtend,IPostMessageSent{
constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) {
Expand Down Expand Up @@ -210,11 +212,17 @@ export class GithubApp extends App implements IPreMessageSentExtend,IPostMessage
): Promise<void> {
const gitHubCommand: GithubCommand = new GithubCommand(this);
const ghCommand: GHCommand = new GHCommand(this);

await Promise.all([
configuration.slashCommands.provideSlashCommand(gitHubCommand),
configuration.slashCommands.provideSlashCommand(ghCommand),
this.getOauth2ClientInstance().setup(configuration),
]);
await Promise.all(
settings.map((setting) =>
configuration.settings.provideSetting(setting)
)
);
configuration.scheduler.registerProcessors([
{
id: ProcessorsEnum.REMOVE_GITHUB_LOGIN,
Expand All @@ -241,6 +249,17 @@ export class GithubApp extends App implements IPreMessageSentExtend,IPostMessage
}
},
},
{
id:ProcessorsEnum.PR_REMINDER,
processor:async(jobData,read,modify,http,persis) =>{
await SendReminder(jobData,read,modify,http,persis,this)
},
startupSetting:{
type:StartupType.RECURRING,
interval:"0 9 * * *"
}

}
]);
configuration.api.provideApi({
visibility: ApiVisibility.PUBLIC,
Expand All @@ -258,4 +277,13 @@ export class GithubApp extends App implements IPreMessageSentExtend,IPostMessage
const user = context.user;
await sendDirectMessageOnInstall(read, modify, user, persistence);
}

public async onSettingUpdated(setting: ISetting, configurationModify: IConfigurationModify, read: IRead, http: IHttp): Promise<void> {
const interval:string = await this.getAccessors().environmentReader.getSettings().getValueById(AppSettings.ReminderCORNjobString);
await configurationModify.scheduler.cancelJob(ProcessorsEnum.PR_REMINDER);
await configurationModify.scheduler.scheduleRecurring({
id:ProcessorsEnum.PR_REMINDER,
interval:interval,
})
}
}
9 changes: 9 additions & 0 deletions github/definitions/PRdetails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface IPRdetail{
title: string;
number:string;
url: string;
id: string;
createdAt: Date;
ageInDays?:number;
author: { avatar: string; username: string; };repo:string;
}
6 changes: 6 additions & 0 deletions github/definitions/Reminder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface IReminder {
userid:string,
username:string,
repos: string[];
unsubscribedPR:{repo:string,prnum:number[]}[]
}
10 changes: 5 additions & 5 deletions github/endpoints/githubEndpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ export class githubWebHooks extends ApiEndpoint {
messageText = `*New Commits to* *[${payload.repository.full_name}](${payload.repository.html_url}) by ${payload.pusher.name}*`;
} else if (event == "pull_request") {
if(payload.action == "opened"){
messageText = `*[New Pull Request](${payload.pull_request.html_url})* *|* *#${payload.pull_request.number} ${payload.pull_request.title}* by *[${payload.user.login}](${payload.user.html_url})* *|* *[${payload.repository.full_name}]*`;
messageText = `*[New Pull Request](${payload.pull_request.html_url})* *|* *#${payload.pull_request.number} ${payload.pull_request.title}* by *[${payload.pull_request.user.login}](${payload.pull_request.user.html_url})* *|* *[${payload.repository.full_name}]*`;
}else if(payload.action == "closed" && payload.pull_request.merged ){
messageText = `*[Merged Pull Request](${payload.pull_request.html_url})* *|* *#${payload.pull_request.number} ${payload.pull_request.title}* by *[${payload.user.login}](${payload.user.html_url})* *|* *[${payload.repository.full_name}]*`;
messageText = `*[Merged Pull Request](${payload.pull_request.html_url})* *|* *#${payload.pull_request.number} ${payload.pull_request.title}* by *[${payload.pull_request.user.login}](${payload.pull_request.user.html_url})* *|* *[${payload.repository.full_name}]*`;
}else if(payload.action == "closed" && !payload.pull_request.merged){
messageText = `*[Closed Pull Request](${payload.pull_request.html_url})* *|* *#${payload.pull_request.number} ${payload.pull_request.title}* by *[${payload.user.login}](${payload.user.html_url})* *|* *[${payload.repository.full_name}]*`;
messageText = `*[Closed Pull Request](${payload.pull_request.html_url})* *|* *#${payload.pull_request.number} ${payload.pull_request.title}* by *[${payload.pull_request.user.login}](${payload.pull_request.user.html_url})* *|* *[${payload.repository.full_name}]*`;
}else if(payload.action =="reopened"){
messageText = `*[ReOpened Pull Request](${payload.pull_request.html_url})* *|* *#${payload.pull_request.number} ${payload.pull_request.title}* by *[${payload.user.login}](${payload.user.html_url})* *|* *[${payload.repository.full_name}]*`;
messageText = `*[ReOpened Pull Request](${payload.pull_request.html_url})* *|* *#${payload.pull_request.number} ${payload.pull_request.title}* by *[${payload.pull_request.user.login}](${payload.pull_request.user.html_url})* *|* *[${payload.repository.full_name}]*`;
}else{
return this.success();
}
Expand Down Expand Up @@ -105,4 +105,4 @@ export class githubWebHooks extends ApiEndpoint {

return this.success();
}
}
}
3 changes: 3 additions & 0 deletions github/enum/Modals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,7 @@ export enum ModalsEnum {
MAIN_MODAL_REPOSITORY_SUBSCRIPTIONS_DESCRIPTION = "Subscribe to repositories Events",
MAIN_MODAL_ASSIGN_ISSUES_LABLE = "Assign Issues",
MAIN_MODAL_ASSIGN_ISSUES_DESCRIPTION = "Assign issues to your team members",
NEW_REMINDER_VIEW = "new-reminder-view",
UNSUBSCRIBE_REMINDER_ACTION = "unsubscribe-reminder-action",
REMINDER_LIST_MODAL_VIEW= "reminder-list-view"
}
3 changes: 2 additions & 1 deletion github/enum/Processors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export enum ProcessorsEnum {
REMOVE_GITHUB_LOGIN = 'remove_github_token',
REMOVE_GITHUB_LOGIN = 'remove_github_token',
PR_REMINDER = 'pr_reminder',
}
5 changes: 4 additions & 1 deletion github/enum/Subcommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ export enum SubcommandEnum {
SEARCH = 'search',
NEW_ISSUE = 'issue',
ISSUES = 'issues',
PROFILE = 'me'
PROFILE = 'me',
REMIND = 'remind',
CREATE= 'create',
LIST ='list',
}
40 changes: 40 additions & 0 deletions github/handlers/CreateReminder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { IHttp, IModify, IPersistence, IRead } from "@rocket.chat/apps-engine/definition/accessors";
import { IRoom } from "@rocket.chat/apps-engine/definition/rooms";
import { SlashCommandContext } from "@rocket.chat/apps-engine/definition/slashcommands";
import { IUser } from "@rocket.chat/apps-engine/definition/users";
import { GithubApp } from "../GithubApp";
import { HandleInvalidRepoName } from "./HandleInvalidRepoName";
import { CreateReminder } from "../persistance/remind";
import { sendNotification } from "../lib/message";

export async function createReminder(
repository: string,
room: IRoom,
read: IRead,
app: GithubApp,
persistence: IPersistence,
modify: IModify,
http: IHttp,
user: IUser
) {

const repoName = repository;

const isValidRepo = await HandleInvalidRepoName(
repoName,
http,
app,
modify,
user,
read,
room
)

if (!isValidRepo) {
return;
} else {
await CreateReminder(read, persistence, user, repoName);
}

await sendNotification(read, modify, user, room, `Pull Request Reminder Set for [${repoName}](https://github.com/${repoName}) 👍`)
}
22 changes: 12 additions & 10 deletions github/handlers/ExecuteBlockActionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import { addIssueCommentsModal } from "../modals/addIssueCommentModal";
import { GitHubIssuesStarterModal } from "../modals/getIssuesStarterModal";
import { githubSearchModal } from "../modals/githubSearchModal";
import { NewIssueStarterModal } from "../modals/newIssueStarterModal";
import { unsubscribedPR } from "../persistance/remind";

export class ExecuteBlockActionHandler {

Expand Down Expand Up @@ -1149,16 +1150,17 @@ export class ExecuteBlockActionHandler {
return context.getInteractionResponder().openModalViewResponse(searchModal);
}

case ModalsEnum.GITHUB_LOGIN_ACTION :{
const {user, room} = context.getInteractionData();
if(room){
await storeInteractionRoomData(
this.persistence,
user.id,
room.id
);
}
break;
case ModalsEnum.UNSUBSCRIBE_REMINDER_ACTION:{
const param:string[] = data.value?.split('|') as string[];
let { user, room } = await context.getInteractionData();
const repo = param[0];
const number = param[1];

await unsubscribedPR(this.read,this.persistence,repo,Number(number),user);

const message = `You have unsubscribed from repository [${repo} Pull Request #${number}](https://github.com/${repo}/pull/${number})`;
await sendNotification(this.read, this.modify, user, room as IRoom, message);

}
}
} catch (error) {
Expand Down
13 changes: 13 additions & 0 deletions github/handlers/ExecuteViewSubmitHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { GithubRepoIssuesStorage } from '../persistance/githubIssues';
import { IGitHubIssueData } from '../definitions/githubIssueData';
import { githubIssuesShareModal } from '../modals/githubIssuesShareModal';
import { issueCommentsModal } from '../modals/issueCommentsModal';
import { createReminder } from './CreateReminder';

export class ExecuteViewSubmitHandler {
constructor(
Expand Down Expand Up @@ -732,6 +733,18 @@ export class ExecuteViewSubmitHandler {
}
break;
}
case ModalsEnum.NEW_REMINDER_VIEW: {
const { roomId } = await getInteractionRoomData(this.read.getPersistenceReader(), user.id);

if(roomId){
let room = await this.read.getRoomReader().getById(roomId) as IRoom;
let repository = view.state?.[ModalsEnum.REPO_NAME_INPUT]?.[ModalsEnum.REPO_NAME_INPUT_ACTION] as string;

await createReminder(repository,room,this.read,this.app,this.persistence,this.modify,this.http,user)

}
break;
}

default:
break;
Expand Down
96 changes: 96 additions & 0 deletions github/handlers/HandleRemider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { IRead, IPersistence, IHttp, IModify } from "@rocket.chat/apps-engine/definition/accessors";
import { IRoom } from "@rocket.chat/apps-engine/definition/rooms";
import { SlashCommandContext } from "@rocket.chat/apps-engine/definition/slashcommands";
import { GithubApp } from "../GithubApp";
import { sendNotification } from "../lib/message";
import { NewIssueStarterModal } from "../modals/newIssueStarterModal";
import { getAccessTokenForUser } from "../persistance/auth";
import { GitHubIssuesStarterModal } from "../modals/getIssuesStarterModal";
import { NewReminderStarterModal } from "../modals/newreminderModal";
import { reminderModal } from "../modals/remindersModal";

export async function handleReminder(
read: IRead,
context: SlashCommandContext,
app: GithubApp,
persistence: IPersistence,
http: IHttp,
room: IRoom,
modify: IModify
){
let accessToken = await getAccessTokenForUser(
read,
context.getSender(),
app.oauth2Config
);
if (accessToken && accessToken.token) {
const triggerId = context.getTriggerId();
if (triggerId) {
const modal = await NewReminderStarterModal({
modify: modify,
read: read,
persistence: persistence,
http: http,
slashcommandcontext: context,
});
await modify
.getUiController()
.openModalView(
modal,
{ triggerId },
context.getSender()
);
} else {
console.log("invalid Trigger ID !");
}
} else {
await sendNotification(
read,
modify,
context.getSender(),
room,
"Login to add Pull Request reminder! `/github login`"
);
}
}

export async function ManageReminders(
read:IRead,
context:SlashCommandContext,
app:GithubApp,
persistence:IPersistence,
http:IHttp,
room:IRoom,
modify:IModify
){
let accessToken = await getAccessTokenForUser(
read,
context.getSender(),
app.oauth2Config
);
if (accessToken && accessToken.token) {
const triggerId = context.getTriggerId();
if (triggerId) {
const modal = await reminderModal({
modify: modify,
read: read,
persistence: persistence,
http: http,
slashcommandcontext: context,
});
await modify
.getUiController()
.openModalView(modal, { triggerId }, context.getSender());
} else {
console.log("Invalid Trigger ID !");
}
} else {
await sendNotification(
read,
modify,
context.getSender(),
room,
"Login to see to pull request reminders! `/github login`"
);
}
}
Loading

0 comments on commit 3588f3d

Please sign in to comment.