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: migrate issues GET APIs to GithubSDK #145

Open
wants to merge 5 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
48 changes: 27 additions & 21 deletions github/handlers/ExecuteBlockActionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from "@rocket.chat/apps-engine/definition/uikit";
import { AddSubscriptionModal } from "../modals/addSubscriptionsModal";
import { deleteSubscriptionsModal } from "../modals/deleteSubscriptions";
import { deleteSubscription, updateSubscription, getIssueTemplateCode, getPullRequestComments, getPullRequestData, getRepositoryIssues, getBasicUserInfo, getIssueData, getIssuesComments, approvePullRequest } from "../helpers/githubSDK";
import { deleteSubscription, updateSubscription, getPullRequestComments, getPullRequestData, getRepositoryIssues, getIssuesComments, approvePullRequest } from "../helpers/githubSDK";
import { Subscription } from "../persistance/subscriptions";
import { getAccessTokenForUser } from "../persistance/auth";
import { GithubApp } from "../GithubApp";
Expand Down Expand Up @@ -55,6 +55,7 @@ import { githubSearchModal } from "../modals/githubSearchModal";
import { NewIssueStarterModal } from "../modals/newIssueStarterModal";
import { removeRepoReminder, unsubscribedPR } from "../persistance/remind";
import { reminderModal } from "../modals/remindersModal";
import { GitHubApi } from "../helpers/githubSDKclass";

export class ExecuteBlockActionHandler {

Expand All @@ -71,6 +72,12 @@ export class ExecuteBlockActionHandler {
context: UIKitBlockInteractionContext
): Promise<IUIKitResponse> {
const data = context.getInteractionData();
const gitHubApiClient = await GitHubApi.getInstance(
this.http,
this.read,
data.user,
this.app
);

try {
const { actionId } = data;
Expand Down Expand Up @@ -126,7 +133,7 @@ export class ExecuteBlockActionHandler {
const repoName = value?.split(",")[0] ?? "";
const issueNumber = value?.split(",")[1] ?? "";

const issueInfo : IGitHubIssue = await getIssueData(repoName, issueNumber, access_token.token, this.http);
const issueInfo : IGitHubIssue = await gitHubApiClient.getIssueData(repoName, issueNumber);

const block = this.modify.getCreator().getBlockBuilder();

Expand Down Expand Up @@ -178,7 +185,7 @@ export class ExecuteBlockActionHandler {
const issueDisplayModal = await IssueDisplayModal({
repoName : repoInfo,
issueNumber : issueNumber,
access_token : access_token.token,
app: this.app,
modify : this.modify,
read : this.read,
persistence : this.persistence,
Expand Down Expand Up @@ -244,7 +251,7 @@ export class ExecuteBlockActionHandler {

let access_token = await getAccessTokenForUser(this.read, user, this.app.oauth2Config) as IAuthData;
const issueModal = await userIssuesModal({
access_token : access_token.token,
app: this.app,
filter : filter,
modify : this.modify,
read : this.read,
Expand All @@ -270,7 +277,7 @@ export class ExecuteBlockActionHandler {

const issuesModal = await userIssuesModal({
filter : filter,
access_token : access_token.token,
app: this.app,
modify: this.modify,
read : this.read,
persistence : this.persistence,
Expand Down Expand Up @@ -398,8 +405,7 @@ export class ExecuteBlockActionHandler {
if(accessToken && actionDetailsArray?.length == 2){

if(actionDetailsArray[1] !== ModalsEnum.BLANK_GITHUB_TEMPLATE){

let templateResponse = await getIssueTemplateCode(this.http,actionDetailsArray[1],accessToken.token);
let templateResponse = await gitHubApiClient.getIssueTemplateCode(actionDetailsArray[1]);
let data = {};
if(templateResponse?.template){
data = {
Expand Down Expand Up @@ -671,13 +677,13 @@ export class ExecuteBlockActionHandler {
return context.getInteractionResponder().openModalViewResponse(shareProfileMod);
}
case ModalsEnum.APPROVE_PULL_REQUEST_ACTION:{

let value: string = context.getInteractionData().value as string;
let splittedValues = value?.split(" ");
let { user } = await context.getInteractionData();
let { room} = await context.getInteractionData();
let accessToken = await getAccessTokenForUser(this.read, user, this.app.oauth2Config) as IAuthData;

if(splittedValues.length==2 && accessToken?.token){
let data={
"repo" : splittedValues[0],
Expand Down Expand Up @@ -722,8 +728,8 @@ export class ExecuteBlockActionHandler {
if(splittedValues.length==2){
let repoName = splittedValues[0];
let issueNumber = splittedValues[1];
let issueComments = await getIssuesComments(this.http,repoName,accessToken?.token,issueNumber);
let issueData = await getIssueData(repoName,issueNumber,accessToken?.token,this.http);
let issueComments = await gitHubApiClient.getIssuesComments(repoName, issueNumber);
let issueData = await gitHubApiClient.getIssueData(repoName, issueNumber);
if(issueData?.issue_compact === "Error Fetching Issue" || issueComments?.issueData){
if(issueData?.issue_compact === "Error Fetching Issue"){
const unauthorizedMessageModal = await messageModal({
Expand Down Expand Up @@ -906,7 +912,7 @@ export class ExecuteBlockActionHandler {
let { user } = await context.getInteractionData();
let accessToken = await getAccessTokenForUser(this.read, user, this.app.oauth2Config);
if (!accessToken) {
let response = await getRepositoryIssues(this.http,repository);
let response = await gitHubApiClient.getRepositoryIssues(repository);
let data = {
issues: response.issues,
pushRights : false, //no access token, so user has no pushRights to the repo,
Expand All @@ -916,7 +922,7 @@ export class ExecuteBlockActionHandler {
await this.modify.getUiController().updateModalView(issuesListModal, { triggerId: context.getInteractionData().triggerId }, context.getInteractionData().user);
}else{
let repoDetails = await getRepoData(this.http,repository,accessToken.token);
let response = await getRepositoryIssues(this.http,repository);
let response = await gitHubApiClient.getRepositoryIssues(repository);
let data = {
issues: response.issues,
pushRights : repoDetails?.permissions?.push || repoDetails?.permissions?.admin,
Expand Down Expand Up @@ -1026,8 +1032,8 @@ export class ExecuteBlockActionHandler {
}
}
break;
}
}

case ModalsEnum.GITHUB_LOGIN_ACTION :{
const {user, room} = context.getInteractionData();
if(room){
Expand Down Expand Up @@ -1073,7 +1079,7 @@ export class ExecuteBlockActionHandler {
});
return context.getInteractionResponder().openModalViewResponse(newIssueModal);
}

case ModalsEnum.TRIGGER_SEARCH_MODAL: {
const searchModal = await githubSearchModal({
modify: this.modify,
Expand All @@ -1096,13 +1102,13 @@ export class ExecuteBlockActionHandler {

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);

}

case ModalsEnum.REMINDER_REMOVE_REPO_ACTION : {
const {value, user} = context.getInteractionData();
const {value, user} = context.getInteractionData();
await removeRepoReminder(this.read, this.persistence, value as string, user);

const updatedReminderModal = await reminderModal({modify: this.modify, read:this.read, persistence: this.persistence, http: this.http, uikitcontext: context});

return context.getInteractionResponder().updateModalViewResponse( updatedReminderModal);
Expand All @@ -1114,4 +1120,4 @@ export class ExecuteBlockActionHandler {

return context.getInteractionResponder().successResponse();
}
}
}
41 changes: 24 additions & 17 deletions github/handlers/ExecuteViewSubmitHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { IGitHubSearchResultData } from '../definitions/searchResultData';
import { githubSearchErrorModal } from '../modals/githubSearchErrorModal';
import { GithubSearchResultStorage } from '../persistance/searchResults';
import { githubSearchResultShareModal } from '../modals/githubSearchResultsShareModal';
import { addSubscribedEvents, createSubscription, updateSubscription, createNewIssue, getIssueTemplates,githubSearchIssuesPulls, mergePullRequest, addNewPullRequestComment, getPullRequestData, getPullRequestComments, getRepoData, getRepositoryIssues, updateGithubIssues, addNewIssueComment, getIssuesComments, getIssueData, getBasicUserInfo } from '../helpers/githubSDK';
import { addSubscribedEvents, createSubscription, updateSubscription, createNewIssue, githubSearchIssuesPulls, mergePullRequest, addNewPullRequestComment, getPullRequestData, getPullRequestComments, getRepoData, getRepositoryIssues, updateGithubIssues, addNewIssueComment, getIssuesComments, getIssueData, getBasicUserInfo } from '../helpers/githubSDK';
import { NewIssueModal } from '../modals/newIssueModal';
import { issueTemplateSelectionModal } from '../modals/issueTemplateSelectionModal';
import { githubIssuesListModal } from '../modals/githubIssuesListModal';
Expand All @@ -29,6 +29,7 @@ import { issueCommentsModal } from '../modals/issueCommentsModal';
import { createReminder } from './CreateReminder';
import { RocketChatAssociationModel, RocketChatAssociationRecord } from '@rocket.chat/apps-engine/definition/metadata';
import { IAuthData } from '@rocket.chat/apps-engine/definition/oauth2/IOAuth2';
import { GitHubApi } from '../helpers/githubSDKclass';
export class ExecuteViewSubmitHandler {
constructor(
private readonly app: GithubApp,
Expand All @@ -40,6 +41,12 @@ export class ExecuteViewSubmitHandler {

public async run(context: UIKitViewSubmitInteractionContext) {
const { user, view } = context.getInteractionData();
const gitHubApiClient = await GitHubApi.getInstance(
this.http,
this.read,
user,
this.app
);

try {
switch (view.id) {
Expand Down Expand Up @@ -154,22 +161,22 @@ export class ExecuteViewSubmitHandler {
}else{
await sendNotification(this.read,this.modify,user,room,`Invalid Issue !`);
}
}
}
break;
}
case ModalsEnum.NEW_ISSUE_STARTER_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;
let accessToken = await getAccessTokenForUser(this.read, user, this.app.oauth2Config);
if (!accessToken) {
await sendNotification(this.read, this.modify, user, room, `Login To Github ! -> /github login`);
}else{

repository=repository?.trim();
let response = await getIssueTemplates(this.http,repository,accessToken.token);
const response = await gitHubApiClient.getIssueTemplates(repository);
if((!response.template_not_found) && response?.templates?.length){
const issueTemplateSelection = await issueTemplateSelectionModal({ data: response, modify: this.modify, read: this.read, persistence: this.persistence, http: this.http, uikitcontext: context });
return context
Expand All @@ -185,7 +192,7 @@ export class ExecuteViewSubmitHandler {
.openModalViewResponse(createNewIssue);
}
}
}
}
break;
}
case ModalsEnum.SEARCH_VIEW: {
Expand Down Expand Up @@ -227,7 +234,7 @@ export class ExecuteViewSubmitHandler {
}else{
resourceState = resourceState?.trim();
}

let accessToken = await getAccessTokenForUser(this.read, user, this.app.oauth2Config);
if(repository?.length == 0 && labelsArray?.length == 0 && authorsArray?.length == 0){
await sendNotification(this.read, this.modify, user, room, "*Invalid Search Query !*");
Expand Down Expand Up @@ -404,7 +411,7 @@ export class ExecuteViewSubmitHandler {
return context
.getInteractionResponder()
.openModalViewResponse(unauthorizedMessageModal);
}else{
}else{
let pullRequestComments = await getPullRequestComments(this.http,repository,accessToken.token,pullNumber);
let pullRequestData = await getPullRequestData(this.http,repository,accessToken.token,pullNumber);
if(pullRequestData?.serverError || pullRequestComments?.pullRequestData){
Expand Down Expand Up @@ -482,9 +489,9 @@ export class ExecuteViewSubmitHandler {
return context
.getInteractionResponder()
.openModalViewResponse(unauthorizedMessageModal);
}else{
let issueComments = await getIssuesComments(this.http,repository,accessToken?.token,issueNumber);
let issueData = await getIssueData(repository,issueNumber,accessToken.token,this.http);
}else{
let issueComments = await gitHubApiClient.getIssuesComments(repository,issueNumber);
let issueData = await gitHubApiClient.getIssueData(repository,issueNumber);
if(issueData?.issue_compact === "Error Fetching Issue" || issueComments?.issueData){
if(issueData?.issue_compact === "Error Fetching Issue"){
const unauthorizedMessageModal = await messageModal({
Expand Down Expand Up @@ -546,10 +553,10 @@ export class ExecuteViewSubmitHandler {
let pushRights: boolean= false;
let accessToken = await getAccessTokenForUser(this.read, user, this.app.oauth2Config);
if (!accessToken) {
response = await getRepositoryIssues(this.http,repository);
response = await gitHubApiClient.getRepositoryIssues(repository);
}else{
let repoDetails = await getRepoData(this.http,repository,accessToken.token);
response = await getRepositoryIssues(this.http,repository);
response = await gitHubApiClient.getRepositoryIssues(repository);
pushRights = repoDetails?.permissions?.push || repoDetails?.permissions?.admin;
}
if(response.serverError){
Expand Down Expand Up @@ -626,7 +633,7 @@ export class ExecuteViewSubmitHandler {
.getInteractionResponder()
.openModalViewResponse(issuesListModal);
}
}
}
break;
}
case ModalsEnum.ADD_ISSUE_ASSIGNEE_VIEW: {
Expand Down Expand Up @@ -696,7 +703,7 @@ export class ExecuteViewSubmitHandler {
}
}
}
}
}
break;
}
case ModalsEnum.ISSUE_LIST_VIEW:{
Expand Down Expand Up @@ -742,7 +749,7 @@ export class ExecuteViewSubmitHandler {
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;
}
Expand Down Expand Up @@ -816,4 +823,4 @@ export class ExecuteViewSubmitHandler {
success: true,
};
}
}
}
30 changes: 16 additions & 14 deletions github/handlers/UserProfileHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { IRead, IPersistence, IHttp, IModify } from "@rocket.chat/apps-engine/definition/accessors";
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 { UIKitInteractionContext } from "@rocket.chat/apps-engine/definition/uikit";
Expand All @@ -16,37 +21,34 @@ export async function handleUserProfileRequest(
room: IRoom,
modify: IModify,
uikitcontext?: UIKitInteractionContext
){
) {
let access_token = await getAccessTokenForUser(
read,
context.getSender(),
app.oauth2Config
);
if (access_token?.token){
if (access_token?.token) {
const triggerId = context.getTriggerId();
if (triggerId){
if (triggerId) {
const modal = await userProfileModal({
access_token: access_token.token,
app: app,
modify: modify,
read: read,
persistence: persistence,
http: http,
slashcommandcontext: context
slashcommandcontext: context,
});
await modify.getUiController().openModalView(
modal,
{triggerId},
context.getSender()
);
await modify
.getUiController()
.openModalView(modal, { triggerId }, context.getSender());
}
}else {
} else {
await sendNotification(
read,
modify,
context.getSender(),
room,
"Login is Mandatory for getting User Info ! `/github login`"
)
);
}

}
Loading