Skip to content
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
23 changes: 23 additions & 0 deletions github/definitions/IGithubActivity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { IGitHubIssue } from "./githubIssue"
import { IGithubComment } from "./IGithubComment";
import { IGitHubPullRequest } from "./IGithubPullRequest";

export interface IGithubActivity {
type: "IssuesEvent" | "PullRequestEvent" | "IssueCommentEvent";
actor: {
display_login: string;
avatar_url: string;
url: string;
};
repo: {
name: string;
url: string;
};
payload: {
action: string;
comment?: IGithubComment
issue?: IGitHubIssue
pull_request?: IGitHubPullRequest
};
created_at: string;
}
5 changes: 5 additions & 0 deletions github/definitions/IGithubComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface IGithubComment {
body: string;
url: string;
lastUpdatedAt: string;
}
14 changes: 14 additions & 0 deletions github/definitions/IGithubPullRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export interface IGitHubPullRequest{
id: string|number,
title?: string,
html_url?: string,
number?: string|number
labels?: Array<string>,
user_login?:string,
user_avatar?:string,
last_updated_at?: string,
comments?:string|number,
state?: string,
assignees?: Array<string>,//user ids seperated by " "
body?: string,
}
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',
}
75 changes: 75 additions & 0 deletions github/helpers/ParseGithubActivity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { IGithubActivity } from "../definitions/IGithubActivity";

export function parseUserActivity(event: any): IGithubActivity {
let payload: any = {};
if (event.type === "IssuesEvent") {
payload = {
action: event.payload.action,
issue: {
issue_id: event.payload.issue.id,
title: event.payload.issue.title,
html_url: event.payload.issue.html_url,
number: event.payload.issue.number,
labels: event.payload.issue.labels.map((label: any) => label.name),
user_login: event.payload.issue.user.login,
user_avatar: event.payload.issue.user.avatar_url,
last_updated_at: event.payload.issue.updated_at,
comments: event.payload.issue.comments,
state: event.payload.issue.state,
assignees: event.payload.issue.assignees.map(
(assignee: any) => assignee.login
),
repo_url: event.repo.url,
body: event.payload.issue.body,
},
};
} else if (event.type === "PullRequestEvent") {
payload = {
action: event.payload.action,
pull_request: {
id: event.payload.pull_request.id,
title: event.payload.pull_request.title,
html_url: event.payload.pull_request.html_url,
number: event.payload.pull_request.number,
labels: event.payload.pull_request.labels.map(
(label: any) => label.name
),
user_login: event.payload.pull_request.user.login,
user_avatar: event.payload.pull_request.user.avatar_url,
last_updated_at: event.payload.pull_request.updated_at,
comments: event.payload.pull_request.comments,
state: event.payload.pull_request.state,
assignees: event.payload.pull_request.assignees.map(
(assignee: any) => assignee.login
),
body: event.payload.pull_request.body,
},
};
} else if (event.type === "IssueCommentEvent") {
payload = {
action: event.payload.action,
comment: {
body: event.payload.comment.body,
url: event.payload.comment.html_url,
lastUpdatedAt: event.payload.comment.updated_at,
},
};
}

const userActivity: IGithubActivity = {
type: event.type,
actor: {
display_login: event.actor.display_login,
avatar_url: event.actor.avatar_url,
url: event.actor.url,
},
repo: {
name: event.repo.name,
url: event.repo.url,
},
payload: payload,
created_at: event.created_at,
};

return userActivity;
}
30 changes: 30 additions & 0 deletions github/helpers/githubSDK.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { IHttp } from "@rocket.chat/apps-engine/definition/accessors";
import { IGitHubIssue } from "../definitions/githubIssue";
import { IGithubActivity } from "../definitions/IGithubActivity";
import { ModalsEnum } from "../enum/Modals";
import { parseUserActivity } from "./ParseGithubActivity";

const BaseHost = "https://github.com/";
const BaseApiHost = "https://api.github.com/";
Expand Down Expand Up @@ -726,3 +728,31 @@ 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<IGithubActivity[]> {
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
);

const userActivity: IGithubActivity[] = lastWeekEvents.map((event: any) : IGithubActivity => {
return parseUserActivity(event)
})

return userActivity
}
52 changes: 52 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,33 @@ 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);

data.forEach((activity) => {
blocks.addSectionBlock({
text: blocks.newMarkdownTextObject(activity.repo.name), // [4]
accessory: { // [5]
type: BlockElementType.BUTTON,
actionId: 'repositoryList',
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(),
};
}
}