diff --git a/github/enum/Subcommands.ts b/github/enum/Subcommands.ts index 8ba32e2..8f3ee28 100644 --- a/github/enum/Subcommands.ts +++ b/github/enum/Subcommands.ts @@ -8,5 +8,6 @@ export enum SubcommandEnum { SEARCH = 'search', NEW_ISSUE = 'issue', ISSUES = 'issues', - PROFILE = 'me' + PROFILE = 'me', + ACTIVITY = 'activity', } diff --git a/github/helpers/githubSDK.ts b/github/helpers/githubSDK.ts index 8aab870..21e0b53 100644 --- a/github/helpers/githubSDK.ts +++ b/github/helpers/githubSDK.ts @@ -726,3 +726,27 @@ export async function updateGithubIssues( } return repsonseJSON; } + +export async function getUserActivity ( + http: IHttp, + username: String, + access_token : String, + page: number, + till_last : "WEEK" | "MONTH", + per_page?: number, +) : Promise { + per_page = per_page ?? 15; + + const oneWeekAgo = new Date(new Date().getTime() - 7 * 24 * 60 * 60 * 1000); + + // A request need to be made to get the raw data from GitHub Events + const rawFetched = await getRequest(http, access_token, `https://api.github.com/users/${username}/events?per_page=${per_page}?page=${page}`) as any + + // the data needs to be processed to get the last week or month's data + const lastWeekEvents = rawFetched.filter((event: any) => + ['PullRequestEvent', 'IssueCommentEvent', 'IssuesEvent'].includes(event.type) && + new Date(event.created_at) >= oneWeekAgo + ); + + return lastWeekEvents +} diff --git a/github/lib/commandUtility.ts b/github/lib/commandUtility.ts index d2fb1c3..46e9a9d 100644 --- a/github/lib/commandUtility.ts +++ b/github/lib/commandUtility.ts @@ -23,6 +23,9 @@ import { import { handleSearch } from "../handlers/SearchHandler"; import { handleNewIssue } from "../handlers/HandleNewIssue"; import { handleUserProfileRequest } from "../handlers/UserProfileHandler"; +import { BlockElementType } from "@rocket.chat/apps-engine/definition/uikit"; +import { getUserActivity } from "../helpers/githubSDK"; +import { getAccessTokenForUser } from "../persistance/auth"; export class CommandUtility implements ExecutorProps { sender: IUser; @@ -138,6 +141,26 @@ export class CommandUtility implements ExecutorProps { ); break; } + case SubcommandEnum.ACTIVITY: { + let access_token = await getAccessTokenForUser( + this.read, + this.context.getSender(), + this.app.oauth2Config + ); + + if (access_token != undefined && access_token.token != undefined) { + const triggerID = this.context.getTriggerId() as string; + const block = await this.getDummyBlock(access_token.token) + const user = this.context.getSender(); + await this.modify.getUiController().openContextualBarView( + block, + { triggerId: triggerID }, + user + ); + } + + break; + } default: { await helperMessage({ room: this.room, @@ -257,4 +280,31 @@ export class CommandUtility implements ExecutorProps { } } } + + public async getDummyBlock(accessToken: string) { + const blocks = this.modify.getCreator().getBlockBuilder(); + + const date = new Date().toISOString(); + + const data = await getUserActivity(this.http, "henit-chobisa", accessToken, 1, "WEEK", 5); + + blocks.addSectionBlock({ + text: blocks.newMarkdownTextObject(`The current date-time is\n${data}`), // [4] + accessory: { // [5] + type: BlockElementType.BUTTON, + actionId: 'date', + text: blocks.newPlainTextObject('Refresh'), + value: date, + }, + }); + + return { // [6] + id: 'contextualbarId', + title: blocks.newPlainTextObject('Contextual Bar'), + submit: blocks.newButtonElement({ + text: blocks.newPlainTextObject('Submit'), + }), + blocks: blocks.getBlocks(), + }; + } }