diff --git a/github/GithubApp.ts b/github/GithubApp.ts index b625eb8..b368b5d 100644 --- a/github/GithubApp.ts +++ b/github/GithubApp.ts @@ -3,6 +3,7 @@ import { IAppInstallationContext, IConfigurationExtend, IConfigurationModify, + IEnvironmentRead, IHttp, ILogger, IMessageBuilder, @@ -52,7 +53,7 @@ import { IPreMessageSentExtend, IMessage } from "@rocket.chat/apps-engine/defini import { handleGitHubCodeSegmentLink } from "./handlers/GitHubCodeSegmentHandler"; import { isGithubLink, hasGitHubCodeSegmentLink, hasGithubPRLink } from "./helpers/checkLinks"; import { SendReminder } from "./handlers/SendReminder"; -import { AppSettings, settings } from "./settings/settings"; +import { AppSettingsEnum, settings } from "./settings/settings"; import { ISetting } from "@rocket.chat/apps-engine/definition/settings"; import { handleGithubPRLinks } from "./handlers/GithubPRlinkHandler"; @@ -249,8 +250,7 @@ export class GithubApp extends App implements IPreMessageSentExtend { type:StartupType.RECURRING, interval:"0 9 * * *" } - - } + }, ]); configuration.api.provideApi({ visibility: ApiVisibility.PUBLIC, @@ -270,11 +270,11 @@ export class GithubApp extends App implements IPreMessageSentExtend { } public async onSettingUpdated(setting: ISetting, configurationModify: IConfigurationModify, read: IRead, http: IHttp): Promise { - const interval:string = await this.getAccessors().environmentReader.getSettings().getValueById(AppSettings.ReminderCRONjobString); + const interval: string = await this.getAccessors().environmentReader.getSettings().getValueById(AppSettingsEnum.ReminderCRONjobID); await configurationModify.scheduler.cancelJob(ProcessorsEnum.PR_REMINDER); await configurationModify.scheduler.scheduleRecurring({ - id:ProcessorsEnum.PR_REMINDER, - interval:interval, + id: ProcessorsEnum.PR_REMINDER, + interval: interval, }) } } diff --git a/github/definitions/Userinfo.ts b/github/definitions/Userinfo.ts new file mode 100644 index 0000000..4fae097 --- /dev/null +++ b/github/definitions/Userinfo.ts @@ -0,0 +1,9 @@ +export interface UserInformation { + username: string; + name: string; + email: string; + bio: string; + followers: string; + following: string; + avatar: string; +} diff --git a/github/handlers/SendReminder.ts b/github/handlers/SendReminder.ts index 6bc29b5..35830a4 100644 --- a/github/handlers/SendReminder.ts +++ b/github/handlers/SendReminder.ts @@ -10,7 +10,6 @@ import { IReminder } from "../definitions/Reminder"; import { IRoom } from "@rocket.chat/apps-engine/definition/rooms"; import { ButtonStyle } from "@rocket.chat/ui-kit"; import { ModalsEnum } from "../enum/Modals"; -import { AppSettings } from "../settings/settings"; export async function SendReminder(jobData: any, read: IRead, modify: IModify, http: IHttp, persis: IPersistence, app: GithubApp) { const reminders: IReminder[] = await getAllReminders(read); diff --git a/github/helpers/githubSDKclass.ts b/github/helpers/githubSDKclass.ts new file mode 100644 index 0000000..24f8994 --- /dev/null +++ b/github/helpers/githubSDKclass.ts @@ -0,0 +1,54 @@ +import { IHttp, HttpStatusCode, IRead } from "@rocket.chat/apps-engine/definition/accessors"; +import { UserInfo } from "os"; +import { UserInformation } from "../definitions/Userinfo"; +import { ModalsEnum } from "../enum/Modals"; + +class GitHubApi { + private http: IHttp; + private BaseHost: string; + private BaseApiHost: string; + private accessToken: String; + + constructor(http: IHttp, accessToken: String, BaseHost: string, BaseApiHost: string) { + this.http = http; + this.accessToken = accessToken; + this.BaseApiHost = BaseApiHost; + this.BaseHost = BaseHost; + } + + private async getRequest(url: string): Promise { + const response = await this.http.get(url, { + headers: { + Authorization: `token ${this.accessToken}`, + "Content-Type": "application/json", + }, + }); + + if (!response.statusCode.toString().startsWith("2")) { + throw response; + } + + return JSON.parse(response.content || "{}"); + } + + public async getBasicUserInfo(): Promise { + try { + const response = await this.getRequest( + this.BaseApiHost + 'user' + ); + return { + username: response.login, + name: response.name, + email: response.email, + bio: response.bio, + followers: response.followers, + following: response.following, + avatar: response.avatar_url + } + } catch (error) { + throw error; + } + } +} + +export { GitHubApi }; diff --git a/github/modals/UserProfileModal.ts b/github/modals/UserProfileModal.ts index 8781639..77ca4cf 100644 --- a/github/modals/UserProfileModal.ts +++ b/github/modals/UserProfileModal.ts @@ -2,11 +2,14 @@ import { IHttp, IModify, IPersistence, IRead } from "@rocket.chat/apps-engine/de import { SlashCommandContext } from "@rocket.chat/apps-engine/definition/slashcommands"; import { ButtonStyle, TextObjectType, UIKitInteractionContext } from "@rocket.chat/apps-engine/definition/uikit"; import { IUIKitModalViewParam } from "@rocket.chat/apps-engine/definition/uikit/UIKitInteractionResponder"; -import { AppEnum } from "../enum/App"; import { ModalsEnum } from "../enum/Modals"; -import { getBasicUserInfo } from "../helpers/githubSDK"; -import { getInteractionRoomData, storeInteractionRoomData } from "../persistance/roomInteraction"; -import {} from "@rocket.chat/apps-engine/definition/uikit/" +import { GitHubApi } from "../helpers/githubSDKclass"; +import { UserInformation } from "../definitions/Userinfo"; +import { + getInteractionRoomData, + storeInteractionRoomData, +} from "../persistance/roomInteraction"; +import { AppSettingsEnum } from "../settings/settings"; export async function userProfileModal({ access_token, @@ -41,15 +44,25 @@ export async function userProfileModal({ roomId = (await getInteractionRoomData(read.getPersistenceReader(), user.id)).roomId; } } + let userInfo: UserInformation | undefined; + try { + let BaseHost = await read.getEnvironmentReader().getSettings().getValueById(AppSettingsEnum.BaseHostID); + let BaseApiHost = await read.getEnvironmentReader().getSettings().getValueById(AppSettingsEnum.BaseApiHostID); + const gitHubApiClient = new GitHubApi( + http, + access_token, + BaseHost, + BaseApiHost + ); + userInfo = await gitHubApiClient.getBasicUserInfo(); + } catch (error) { + console.log("Error occurred while fetching user info:", error); + } - const userInfo = await getBasicUserInfo(http, access_token); - - - block.addContextBlock({ - elements: [ - block.newPlainTextObject(userInfo.email, true), - ] - }) + if (userInfo) { + block.addContextBlock({ + elements: [block.newPlainTextObject(userInfo.email, true)], + }); block.addSectionBlock({ text: block.newPlainTextObject(userInfo.bio), @@ -95,13 +108,12 @@ export async function userProfileModal({ ) ] }) - - +} return { id: viewId, title: { type: TextObjectType.PLAINTEXT, - text: userInfo.name + text: userInfo ? userInfo.name : "User Profile", }, blocks: block.getBlocks() } diff --git a/github/settings/settings.ts b/github/settings/settings.ts index 1381bb3..c9de43a 100644 --- a/github/settings/settings.ts +++ b/github/settings/settings.ts @@ -1,16 +1,39 @@ import { ISetting, SettingType } from '@rocket.chat/apps-engine/definition/settings'; -export enum AppSettings{ - ReminderCRONjobString = 'reminderCronJobString' +export enum AppSettingsEnum { + ReminderCRONjobID = 'reminder_cron_job_id', + ReminderCRONjobLabel = 'cron_job_string_for_pr_reminders_label', + ReminderCRONjobPackageValue = '0 9 * * *', + BaseHostID = "base_host_id", + BaseHostLabel = "base_host_label", + BaseHostPackageValue = "https://github.com/", + BaseApiHostID = "base_api_host_id", + BaseApiHostLabel = "base_api_host_label", + BaseApiHostPackageValue = "https://api.github.com/" } - export const settings: ISetting[] = [ - { - id: AppSettings.ReminderCRONjobString, - i18nLabel: 'cron-job-string-for-pr-reminders', - type: SettingType.STRING, - required: true, - public: false, - packageValue: '0 9 * * *', - }, + { + id: AppSettingsEnum.ReminderCRONjobID, + i18nLabel: AppSettingsEnum.ReminderCRONjobLabel, + type: SettingType.STRING, + required: true, + public: false, + packageValue: AppSettingsEnum.ReminderCRONjobPackageValue, + }, + { + id: AppSettingsEnum.BaseHostID, + i18nLabel: AppSettingsEnum.BaseHostLabel, + type: SettingType.STRING, + required: true, + public: false, + packageValue: AppSettingsEnum.BaseHostPackageValue, + }, + { + id: AppSettingsEnum.BaseApiHostID, + i18nLabel: AppSettingsEnum.BaseApiHostLabel, + type: SettingType.STRING, + required: true, + public: false, + packageValue: AppSettingsEnum.BaseApiHostPackageValue, + }, ];