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
6 changes: 5 additions & 1 deletion github/handlers/ExecuteBlockActionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,15 @@ export class ExecuteBlockActionHandler {
break;
}
case ModalsEnum.VIEW_FILE_ACTION: {
const accessToken = await getAccessTokenForUser(this.read, data.user, this.app.oauth2Config) as IAuthData;
const codeModal = await fileCodeModal({
data,
modify: this.modify,
read: this.read,
persistence: this.persistence,
http: this.http,
uikitcontext: context,
accessToken,
});
return context
.getInteractionResponder()
Expand Down Expand Up @@ -474,13 +476,15 @@ export class ExecuteBlockActionHandler {
number:PullRequestDetails[1]
}
if(triggerId && data){
const accessToken = await getAccessTokenForUser(this.read, user, this.app.oauth2Config) as IAuthData;
const resultsModal = await pullDetailsModal({
data,
modify: this.modify,
read: this.read,
persistence: this.persistence,
http: this.http,
uikitcontext: context
uikitcontext: context,
accessToken,
});
return context
.getInteractionResponder()
Expand Down
2 changes: 2 additions & 0 deletions github/lib/commandUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { SubcommandEnum } from "../enum/Subcommands";
import { GithubApp } from "../GithubApp";
import { basicQueryMessage } from "../helpers/basicQueryMessage";
import { pullDetailsModal } from "../modals/pullDetailsModal";
import { getAccessTokenForUser } from "../persistance/auth";
import { ExecutorProps } from "../definitions/ExecutorProps";
import { handleLogin, handleLogout } from "../handlers/AuthenticationHandler";
import {
Expand Down Expand Up @@ -293,6 +294,7 @@ export class CommandUtility implements ExecutorProps {
persistence: this.persistence,
http: this.http,
slashcommandcontext: this.context,
accessToken: await getAccessTokenForUser(this.read, this.sender, this.app.oauth2Config),
});
await this.modify
.getUiController()
Expand Down
36 changes: 35 additions & 1 deletion github/modals/fileCodeModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@ import {
UIKitBlockInteractionContext,
UIKitInteractionContext,
} from "@rocket.chat/apps-engine/definition/uikit";
import { IAuthData } from "@rocket.chat/apps-engine/definition/oauth2/IOAuth2";

// The button value is a github.com diff or raw file link that comes back from the client.
// The token goes only to the API URL rebuilt from its parts, never to the link itself.
function githubApiRequest(url: string): { url: string; accept: string } | undefined {
if (/\/\.{1,2}(\/|$)/.test(url)) {
return undefined;
}
const diff = url.match(/^https:\/\/github\.com\/([\w.-]+)\/([\w.-]+)\/pull\/(\d+)\.diff$/);
if (diff) {
return {
url: `https://api.github.com/repos/${diff[1]}/${diff[2]}/pulls/${diff[3]}`,
accept: "application/vnd.github.diff",
};
}
const raw = url.match(/^https:\/\/github\.com\/([\w.-]+)\/([\w.-]+)\/raw\/([0-9a-f]{40})\/(.+)$/);
if (raw) {
return {
url: `https://api.github.com/repos/${raw[1]}/${raw[2]}/contents/${raw[4]}?ref=${raw[3]}`,
accept: "application/vnd.github.raw",
};
}
return undefined;
}


export async function fileCodeModal({
Expand All @@ -25,6 +49,7 @@ export async function fileCodeModal({
http,
slashcommandcontext,
uikitcontext,
accessToken,
}: {
data;
modify: IModify;
Expand All @@ -33,6 +58,7 @@ export async function fileCodeModal({
http: IHttp;
slashcommandcontext?: SlashCommandContext;
uikitcontext?: UIKitInteractionContext;
accessToken?: IAuthData;
}): Promise<IUIKitModalViewParam> {
const viewId = ModalsEnum.CODE_VIEW;

Expand All @@ -47,7 +73,15 @@ export async function fileCodeModal({

if (user?.id) {
let roomId;
const pullRawData = await http.get(data.value);
const apiRequest = accessToken?.token ? githubApiRequest(data.value) : undefined;
let pullRawData = apiRequest
? await http.get(apiRequest.url, {
headers: { Authorization: `token ${accessToken?.token}`, Accept: apiRequest.accept },
})
: await http.get(data.value);
if (apiRequest && pullRawData.statusCode === 401) {
pullRawData = await http.get(data.value);
}
const pullData = pullRawData.content;
block.addSectionBlock({
text: { text: `${pullData}`, type: TextObjectType.MARKDOWN },
Expand Down
26 changes: 21 additions & 5 deletions github/modals/pullDetailsModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
UIKitInteractionContext,
} from "@rocket.chat/apps-engine/definition/uikit";
import { storeInteractionRoomData, getInteractionRoomData } from "../persistance/roomInteraction";
import { IAuthData } from "@rocket.chat/apps-engine/definition/oauth2/IOAuth2";

export async function pullDetailsModal({
data,
Expand All @@ -24,6 +25,7 @@ export async function pullDetailsModal({
http,
slashcommandcontext,
uikitcontext,
accessToken,
}: {
data?;
modify: IModify;
Expand All @@ -32,6 +34,7 @@ export async function pullDetailsModal({
http: IHttp;
slashcommandcontext?: SlashCommandContext;
uikitcontext?: UIKitInteractionContext;
accessToken?: IAuthData;
}): Promise<IUIKitModalViewParam> {
const viewId = ModalsEnum.PULL_VIEW;

Expand All @@ -53,15 +56,27 @@ export async function pullDetailsModal({
roomId = (await getInteractionRoomData(read.getPersistenceReader(), user.id)).roomId;
}

const pullRawData = await http.get(
`https://api.github.com/repos/${data?.repository}/pulls/${data?.number}`
);
let requestOptions = accessToken?.token
? { headers: { Authorization: `token ${accessToken.token}` } }
: {};
const pullUrl = `https://api.github.com/repos/${data?.repository}/pulls/${data?.number}`;

let pullRawData = await http.get(pullUrl, requestOptions);

// A revoked or expired token gets 401 even on a public repository, so retry without it.
if (pullRawData.statusCode === 401 && accessToken?.token) {
requestOptions = {};
pullRawData = await http.get(pullUrl);
}
const loggedIn = "headers" in requestOptions;

// If pullsNumber doesn't exist, notify the user
if (pullRawData.statusCode === 404) {
block.addSectionBlock({
text: {
text: `Pull request #${data?.number} doesn't exist.`,
text: loggedIn
? `Pull request #${data?.number} doesn't exist.`
: `Pull request #${data?.number} doesn't exist, or it is in a private repository. Log in with /github login to see private repositories.`,
type: TextObjectType.PLAINTEXT,
},
});
Expand All @@ -84,7 +99,8 @@ export async function pullDetailsModal({
const pullData = pullRawData.data;

const pullRequestFilesRaw = await http.get(
`https://api.github.com/repos/${data?.repository}/pulls/${data?.number}/files`
`https://api.github.com/repos/${data?.repository}/pulls/${data?.number}/files`,
requestOptions
);

const pullRequestFiles = pullRequestFilesRaw.data;
Expand Down