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

Added Activity Method to GithubSDK for fetching Github Activity of User 🧑‍🚀 #58

Open
wants to merge 2 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
3 changes: 2 additions & 1 deletion github/enum/Subcommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export enum SubcommandEnum {
SEARCH = 'search',
NEW_ISSUE = 'issue',
ISSUES = 'issues',
PROFILE = 'me'
PROFILE = 'me',
ACTIVITY = 'activity',
}
24 changes: 24 additions & 0 deletions github/helpers/githubSDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> {
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
}
50 changes: 50 additions & 0 deletions github/lib/commandUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
};
}
}