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]: Add new GithubSDK class #144

Merged
Merged
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
12 changes: 6 additions & 6 deletions github/GithubApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
IAppInstallationContext,
IConfigurationExtend,
IConfigurationModify,
IEnvironmentRead,
IHttp,
ILogger,
IMessageBuilder,
Expand Down Expand Up @@ -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";

Expand Down Expand Up @@ -249,8 +250,7 @@ export class GithubApp extends App implements IPreMessageSentExtend {
type:StartupType.RECURRING,
interval:"0 9 * * *"
}

}
},
]);
configuration.api.provideApi({
visibility: ApiVisibility.PUBLIC,
Expand All @@ -270,11 +270,11 @@ export class GithubApp extends App implements IPreMessageSentExtend {
}

public async onSettingUpdated(setting: ISetting, configurationModify: IConfigurationModify, read: IRead, http: IHttp): Promise<void> {
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,
})
}
}
9 changes: 9 additions & 0 deletions github/definitions/Userinfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface UserInformation {
username: string;
name: string;
email: string;
bio: string;
followers: string;
following: string;
avatar: string;
}
1 change: 0 additions & 1 deletion github/handlers/SendReminder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
54 changes: 54 additions & 0 deletions github/helpers/githubSDKclass.ts
Original file line number Diff line number Diff line change
@@ -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<any> {
const response = await this.http.get(url, {
headers: {
Authorization: `token ${this.accessToken}`,
"Content-Type": "application/json",
},
});

if (!response.statusCode.toString().startsWith("2")) {
throw response;
}
Comment on lines +27 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try response.ok() ..probably is available

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry that isn't available
image


return JSON.parse(response.content || "{}");
}

public async getBasicUserInfo(): Promise<UserInformation> {
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 };
42 changes: 27 additions & 15 deletions github/modals/UserProfileModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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()
}
Expand Down
45 changes: 34 additions & 11 deletions github/settings/settings.ts
Original file line number Diff line number Diff line change
@@ -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,
},
];