Skip to content

Commit

Permalink
Move PullRequest to a shared location for reviewing of types (#4578)
Browse files Browse the repository at this point in the history
and some other small cleanup

Co-authored-by: Alex Ross <alros@microsoft.com>
  • Loading branch information
kabel and alexr00 authored Apr 25, 2023
1 parent f1cb117 commit 3dfc6fa
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 148 deletions.
81 changes: 43 additions & 38 deletions src/github/activityBarViewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { PullRequestModel } from './pullRequestModel';
import { getDefaultMergeMethod } from './pullRequestOverview';
import { PullRequestView } from './pullRequestOverviewCommon';
import { isInCodespaces, parseReviewers } from './utils';
import { PullRequest } from './views';

export class PullRequestViewProvider extends WebviewViewBase implements vscode.WebviewViewProvider {
public readonly viewType = 'github:activePullRequest';
Expand Down Expand Up @@ -200,47 +201,51 @@ export class PullRequestViewProvider extends WebviewViewBase implements vscode.W
pullRequest.head &&
!pullRequest.base.repositoryCloneUrl.equals(pullRequest.head.repositoryCloneUrl);

const continueOnGitHub = isCrossRepository && isInCodespaces();
const continueOnGitHub = !!(isCrossRepository && isInCodespaces());

const context: Partial<PullRequest> = {
number: pullRequest.number,
title: pullRequest.title,
url: pullRequest.html_url,
createdAt: pullRequest.createdAt,
body: pullRequest.body,
bodyHTML: pullRequest.bodyHTML,
labels: pullRequest.item.labels,
author: {
login: pullRequest.author.login,
name: pullRequest.author.name,
avatarUrl: pullRequest.userAvatar,
url: pullRequest.author.url,
email: pullRequest.author.email,
},
state: pullRequest.state,
isCurrentlyCheckedOut: isCurrentlyCheckedOut,
isRemoteBaseDeleted: pullRequest.isRemoteBaseDeleted,
base: pullRequest.base.label,
isRemoteHeadDeleted: pullRequest.isRemoteHeadDeleted,
isLocalHeadDeleted: !branchInfo,
head: pullRequest.head?.label ?? '',
canEdit: canEdit,
hasWritePermission,
mergeable: pullRequest.item.mergeable,
isDraft: pullRequest.isDraft,
status: null,
reviewRequirement: null,
events: [],
mergeMethodsAvailability,
defaultMergeMethod,
repositoryDefaultBranch: defaultBranch,
isIssue: false,
isAuthor: currentUser.login === pullRequest.author.login,
reviewers: this._existingReviewers,
continueOnGitHub,
isDarkTheme: vscode.window.activeColorTheme.kind === vscode.ColorThemeKind.Dark,
hasReviewDraft
};

this._postMessage({
command: 'pr.initialize',
pullrequest: {
number: pullRequest.number,
title: pullRequest.title,
url: pullRequest.html_url,
createdAt: pullRequest.createdAt,
body: pullRequest.body,
bodyHTML: pullRequest.bodyHTML,
labels: pullRequest.item.labels,
author: {
login: pullRequest.author.login,
name: pullRequest.author.name,
avatarUrl: pullRequest.userAvatar,
url: pullRequest.author.url,
},
state: pullRequest.state,
isCurrentlyCheckedOut: isCurrentlyCheckedOut,
isRemoteBaseDeleted: pullRequest.isRemoteBaseDeleted,
base: pullRequest.base.label,
isRemoteHeadDeleted: pullRequest.isRemoteHeadDeleted,
isLocalHeadDeleted: !branchInfo,
head: pullRequest.head?.label ?? '',
canEdit: canEdit,
hasWritePermission,
mergeable: pullRequest.item.mergeable,
isDraft: pullRequest.isDraft,
status: { statuses: [] },
events: [],
mergeMethodsAvailability,
defaultMergeMethod,
repositoryDefaultBranch: defaultBranch,
isIssue: false,
isAuthor: currentUser.login === pullRequest.author.login,
reviewers: this._existingReviewers,
continueOnGitHub,
isDarkTheme: vscode.window.activeColorTheme.kind === vscode.ColorThemeKind.Dark,
hasReviewDraft
},
pullrequest: context,
});
})
.catch(e => {
Expand Down
29 changes: 1 addition & 28 deletions src/github/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,33 +393,6 @@ export function parseCommentDiffHunk(comment: IComment): DiffHunk[] {
return diffHunks;
}

export function convertPullRequestsGetCommentsResponseItemToComment(
comment: OctokitCommon.PullsCreateReviewCommentResponseData,
githubRepository: GitHubRepository,
): IComment {
const ret: IComment = {
url: comment.url,
id: comment.id,
pullRequestReviewId: comment.pull_request_review_id ?? undefined,
diffHunk: comment.diff_hunk,
path: comment.path,
position: comment.position,
commitId: comment.commit_id,
originalPosition: comment.original_position,
originalCommitId: comment.original_commit_id,
user: convertRESTUserToAccount(comment.user!, githubRepository),
body: comment.body,
createdAt: comment.created_at,
htmlUrl: comment.html_url,
inReplyToId: comment.in_reply_to_id,
graphNodeId: comment.node_id,
};

const diffHunks = parseCommentDiffHunk(ret);
ret.diffHunks = diffHunks;
return ret;
}

export function convertGraphQLEventType(text: string) {
switch (text) {
case 'PullRequestCommit':
Expand Down Expand Up @@ -1249,4 +1222,4 @@ export async function findDotComAndEnterpriseRemotes(folderManagers: FolderRepos
export function vscodeDevPrLink(pullRequest: PullRequestModel) {
const itemUri = vscode.Uri.parse(pullRequest.html_url);
return `https://${vscode.env.appName.toLowerCase().includes('insider') ? 'insiders.' : ''}vscode.dev/github${itemUri.path}`;
}
}
76 changes: 76 additions & 0 deletions src/github/views.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { TimelineEvent } from '../common/timelineEvent';
import {
GithubItemStateEnum,
IAccount,
ILabel,
IMilestone,
MergeMethod,
MergeMethodsAvailability,
PullRequestChecks,
PullRequestMergeability,
PullRequestReviewRequirement,
ReviewState,
} from './interface';

export enum ReviewType {
Comment = 'comment',
Approve = 'approve',
RequestChanges = 'requestChanges',
}

export interface PullRequest {
number: number;
title: string;
titleHTML: string;
url: string;
createdAt: string;
body: string;
bodyHTML?: string;
author: IAccount;
state: GithubItemStateEnum;
events: TimelineEvent[];
isCurrentlyCheckedOut: boolean;
isRemoteBaseDeleted?: boolean;
base: string;
isRemoteHeadDeleted?: boolean;
isLocalHeadDeleted?: boolean;
head: string;
labels: ILabel[];
assignees: IAccount[];
commitsCount: number;
milestone: IMilestone | undefined;
repositoryDefaultBranch: string;
/**
* User can edit PR title and description (author or user with push access)
*/
canEdit: boolean;
/**
* Users with push access to repo have rights to merge/close PRs,
* edit title/description, assign reviewers/labels etc.
*/
hasWritePermission: boolean;
pendingCommentText?: string;
pendingCommentDrafts?: { [key: string]: string };
pendingReviewType?: ReviewType;
status: PullRequestChecks | null;
reviewRequirement: PullRequestReviewRequirement | null;
mergeable: PullRequestMergeability;
defaultMergeMethod: MergeMethod;
mergeMethodsAvailability: MergeMethodsAvailability;
autoMerge?: boolean;
allowAutoMerge: boolean;
autoMergeMethod?: MergeMethod;
reviewers: ReviewState[];
isDraft?: boolean;
isIssue: boolean;
isAuthor?: boolean;
continueOnGitHub: boolean;
currentUserReviewState: string;
isDarkTheme: boolean;
hasReviewDraft: boolean;
}
2 changes: 1 addition & 1 deletion webviews/activityBarView/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import React, { useContext, useEffect, useState } from 'react';
import { render } from 'react-dom';
import { PullRequest } from '../common/cache';
import { PullRequest } from '../../src/github/views';
import PullRequestContext from '../common/context';
import { Overview } from './overview';

Expand Down
2 changes: 1 addition & 1 deletion webviews/activityBarView/exit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import React, { useContext, useState } from 'react';
import { GithubItemStateEnum } from '../../src/github/interface';
import { PullRequest } from '../common/cache';
import { PullRequest } from '../../src/github/views';
import PullRequestContext from '../common/context';

const ExitButton = ({ repositoryDefaultBranch, isBusy, onClick }: { repositoryDefaultBranch: string, isBusy: boolean, onClick: () => Promise<void> }) => {
Expand Down
2 changes: 1 addition & 1 deletion webviews/activityBarView/overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import React from 'react';
import { PullRequest } from '../common/cache';
import { PullRequest } from '../../src/github/views';

import { AddCommentSimple } from '../components/comment';
import { StatusChecksSection } from '../components/merge';
Expand Down
72 changes: 1 addition & 71 deletions webviews/common/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,79 +3,9 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { TimelineEvent } from '../../src/common/timelineEvent';
import {
GithubItemStateEnum,
IAccount,
ILabel,
IMilestone,
MergeMethod,
MergeMethodsAvailability,
PullRequestChecks,
PullRequestMergeability,
PullRequestReviewRequirement,
ReviewState,
} from '../../src/github/interface';
import { PullRequest } from '../../src/github/views';
import { vscode } from './message';

export enum ReviewType {
Comment = 'comment',
Approve = 'approve',
RequestChanges = 'requestChanges',
}

export interface PullRequest {
number: number;
title: string;
titleHTML: string;
url: string;
createdAt: string;
body: string;
bodyHTML?: string;
author: IAccount;
state: GithubItemStateEnum;
events: TimelineEvent[];
isCurrentlyCheckedOut: boolean;
isRemoteBaseDeleted?: boolean;
base: string;
isRemoteHeadDeleted?: boolean;
isLocalHeadDeleted?: boolean;
head: string;
labels: ILabel[];
assignees: IAccount[];
commitsCount: number;
milestone: IMilestone | undefined;
repositoryDefaultBranch: string;
/**
* User can edit PR title and description (author or user with push access)
*/
canEdit: boolean;
/**
* Users with push access to repo have rights to merge/close PRs,
* edit title/description, assign reviewers/labels etc.
*/
hasWritePermission: boolean;
pendingCommentText?: string;
pendingCommentDrafts?: { [key: string]: string };
pendingReviewType?: ReviewType;
status: PullRequestChecks | null;
reviewRequirement: PullRequestReviewRequirement | null;
mergeable: PullRequestMergeability;
defaultMergeMethod: MergeMethod;
mergeMethodsAvailability: MergeMethodsAvailability;
autoMerge?: boolean;
allowAutoMerge: boolean;
autoMergeMethod?: MergeMethod;
reviewers: ReviewState[];
isDraft?: boolean;
isIssue: boolean;
isAuthor?: boolean;
continueOnGitHub: boolean;
currentUserReviewState: string;
isDarkTheme: boolean;
hasReviewDraft: boolean;
}

export function getState(): PullRequest {
return vscode.getState();
}
Expand Down
3 changes: 2 additions & 1 deletion webviews/common/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { createContext } from 'react';
import { IComment } from '../../src/common/comment';
import { EventType, ReviewEvent, TimelineEvent } from '../../src/common/timelineEvent';
import { MergeMethod, ReviewState } from '../../src/github/interface';
import { getState, PullRequest, setState, updateState } from './cache';
import { PullRequest } from '../../src/github/views';
import { getState, setState, updateState } from './cache';
import { getMessageHandler, MessageHandler } from './message';

export class PRContext {
Expand Down
2 changes: 1 addition & 1 deletion webviews/components/comment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import React, { useCallback, useContext, useEffect, useRef, useState } from 'react';
import { IComment } from '../../src/common/comment';
import { GithubItemStateEnum } from '../../src/github/interface';
import { PullRequest, ReviewType } from '../common/cache';
import { PullRequest, ReviewType } from '../../src/github/views';
import PullRequestContext from '../common/context';
import emitter from '../common/events';
import { useStateProp } from '../common/hooks';
Expand Down
2 changes: 1 addition & 1 deletion webviews/components/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import React, { useContext, useState } from 'react';
import { GithubItemStateEnum } from '../../src/github/interface';
import { PullRequest } from '../common/cache';
import { PullRequest } from '../../src/github/views';
import PullRequestContext from '../common/context';
import { useStateProp } from '../common/hooks';
import { checkIcon, mergeIcon, prClosedIcon, prDraftIcon, prOpenIcon } from './icon';
Expand Down
2 changes: 1 addition & 1 deletion webviews/components/merge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
PullRequestReviewRequirement,
reviewerId,
} from '../../src/github/interface';
import { PullRequest } from '../common/cache';
import { PullRequest } from '../../src/github/views';
import PullRequestContext from '../common/context';
import { Reviewer } from '../components/reviewer';
import { AutoMerge } from './automergeSelect';
Expand Down
2 changes: 1 addition & 1 deletion webviews/components/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import React, { useContext } from 'react';
import { gitHubLabelColor } from '../../src/common/utils';
import { IMilestone, reviewerId } from '../../src/github/interface';
import { PullRequest } from '../common/cache';
import { PullRequest } from '../../src/github/views';
import PullRequestContext from '../common/context';
import { Label } from '../common/label';
import { AuthorLink, Avatar } from '../components/user';
Expand Down
2 changes: 1 addition & 1 deletion webviews/editorWebview/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import React, { useContext, useEffect, useState } from 'react';
import { render } from 'react-dom';
import { PullRequest } from '../common/cache';
import { PullRequest } from '../../src/github/views';
import PullRequestContext from '../common/context';
import { Overview } from './overview';

Expand Down
2 changes: 1 addition & 1 deletion webviews/editorWebview/overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import * as React from 'react';
import { PullRequest } from '../common/cache';
import { PullRequest } from '../../src/github/views';

import { AddComment, CommentView } from '../components/comment';
import { Header } from '../components/header';
Expand Down
2 changes: 1 addition & 1 deletion webviews/editorWebview/test/builder/pullRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
*--------------------------------------------------------------------------------------------*/

import { GithubItemStateEnum, PullRequestMergeability } from '../../../../src/github/interface';
import { PullRequest } from '../../../../src/github/views';
import { createBuilderClass } from '../../../../src/test/builders/base';
import { CombinedStatusBuilder } from '../../../../src/test/builders/rest/combinedStatusBuilder';
import { PullRequest } from '../../../common/cache';

import { AccountBuilder } from './account';

Expand Down

0 comments on commit 3dfc6fa

Please sign in to comment.