Skip to content

Commit 8b6f4dd

Browse files
author
Rachel Macfarlane
authored
Add commit list to 'Changes in Pull Request' section, fixes #34
1 parent 70a07df commit 8b6f4dd

11 files changed

Lines changed: 329 additions & 130 deletions

src/common/diffHunk.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,21 @@ async function parseModifiedHunkFast(modifyDiffInfo, a, b) {
248248
return new RichFileChange(contentPath, originalContentPath, GitChangeType.MODIFY, b, diffHunks);
249249
}
250250

251+
export function getGitChangeType(status: string): GitChangeType {
252+
switch (status) {
253+
case 'removed':
254+
return GitChangeType.DELETE;
255+
case 'added':
256+
return GitChangeType.ADD;
257+
case 'renamed':
258+
return GitChangeType.RENAME;
259+
case 'modified':
260+
return GitChangeType.MODIFY;
261+
default:
262+
return GitChangeType.UNKNOWN
263+
}
264+
}
265+
251266
export async function parseDiff(reviews: any[], repository: Repository, parentCommit: string): Promise<RichFileChange[]> {
252267
let richFileChanges: RichFileChange[] = [];
253268
for (let i = 0; i < reviews.length; i++) {
@@ -270,21 +285,7 @@ export async function parseDiff(reviews: any[], repository: Repository, parentCo
270285
continue;
271286
}
272287

273-
let gitChangeType = GitChangeType.UNKNOWN;
274-
switch (review.status) {
275-
case 'removed':
276-
gitChangeType = GitChangeType.DELETE;
277-
break;
278-
case 'added':
279-
gitChangeType = GitChangeType.ADD;
280-
break;
281-
case 'renamed':
282-
gitChangeType = GitChangeType.RENAME;
283-
break;
284-
default:
285-
break;
286-
}
287-
288+
const gitChangeType = getGitChangeType(review.status);
288289
let contentArray = [];
289290
let fileName = review.filename;
290291
let prDiffReader = parseDiffHunk(review.patch);

src/github/interface.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,35 @@ export interface IPullRequest {
6565
user: any;
6666
}
6767

68+
export interface FileChange {
69+
additions: number;
70+
blob_url: string;
71+
changes: number;
72+
contents_url: string;
73+
deletions: number;
74+
filename: string;
75+
patch?: string;
76+
raw_url: string;
77+
sha: string;
78+
status: string;
79+
}
80+
81+
export interface Commit {
82+
author: {
83+
login: string;
84+
};
85+
commit: {
86+
author: {
87+
name: string;
88+
date: string;
89+
email: string;
90+
};
91+
message: string;
92+
};
93+
html_url: string;
94+
sha: string;
95+
parents: any;
96+
}
6897

6998
export interface IPullRequestModel {
7099
prNumber: number;
@@ -99,14 +128,16 @@ export interface IPullRequestManager {
99128
getPullRequests(type: PRType, options?: IPullRequestsPagingOptions):Promise<[IPullRequestModel[], boolean]>;
100129
mayHaveMorePages(): boolean;
101130
getPullRequestComments(pullRequest: IPullRequestModel): Promise<Comment[]>;
131+
getPullRequestCommits(pullRequest: IPullRequestModel): Promise<Commit[]>;
132+
getCommitChangedFiles(pullRequest: IPullRequestModel, commit: Commit): Promise<FileChange[]>;
102133
getReviewComments(pullRequest: IPullRequestModel, reviewId: string): Promise<Comment[]>;
103134
getTimelineEvents(pullRequest: IPullRequestModel): Promise<TimelineEvent[]>;
104135
getIssueComments(pullRequest: IPullRequestModel): Promise<Comment[]>;
105136
createIssueComment(pullRequest: IPullRequestModel, text: string): Promise<Comment>;
106137
createCommentReply(pullRequest: IPullRequestModel, body: string, reply_to: string);
107138
createComment(pullRequest: IPullRequestModel, body: string, path: string, position: number);
108139
closePullRequest(pullRequest: IPullRequestModel): Promise<any>;
109-
getPullRequestChagnedFiles(pullRequest: IPullRequestModel): Promise<any>;
140+
getPullRequestChangedFiles(pullRequest: IPullRequestModel): Promise<FileChange[]>;
110141
fullfillPullRequestCommitInfo(pullRequest: IPullRequestModel): Promise<void>;
111142
updateRepositories(): Promise<void>;
112143

src/github/pullRequestManager.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import { Remote } from "../common/remote";
1010
import { Repository } from "../common/repository";
1111
import { TimelineEvent, EventType } from "../common/timelineEvent";
1212
import { GitHubRepository, PULL_REQUEST_PAGE_SIZE } from "./githubRepository";
13-
import { IPullRequestManager, IPullRequestModel, IPullRequestsPagingOptions, PRType } from "./interface";
13+
import { IPullRequestManager, IPullRequestModel, IPullRequestsPagingOptions, PRType, Commit, FileChange } from "./interface";
1414
import { PullRequestGitHelper } from "./pullRequestGitHelper";
1515
import { PullRequestModel } from "./pullRequestModel";
1616
import { parserCommentDiffHunk } from "../common/diffHunk";
1717
import { Configuration } from "../configuration";
18+
import { formatError } from '../common/utils';
1819

1920
interface PageInformation {
2021
pullRequestPage: number;
@@ -174,6 +175,38 @@ export class PullRequestManager implements IPullRequestManager {
174175
return parserCommentDiffHunk(rawComments);
175176
}
176177

178+
async getPullRequestCommits(pullRequest: IPullRequestModel): Promise<Commit[]> {
179+
try {
180+
const { octokit, remote } = (pullRequest as PullRequestModel).githubRepository;
181+
const commitData = await octokit.pullRequests.getCommits({
182+
number: pullRequest.prNumber,
183+
owner: remote.owner,
184+
repo: remote.repositoryName
185+
});
186+
187+
return commitData.data;
188+
} catch (e) {
189+
vscode.window.showErrorMessage(`Fetching commits failed: ${formatError(e)}`);
190+
return [];
191+
}
192+
}
193+
194+
async getCommitChangedFiles(pullRequest: IPullRequestModel, commit: Commit): Promise<FileChange[]> {
195+
try {
196+
const { octokit, remote } = (pullRequest as PullRequestModel).githubRepository;
197+
const fullCommit = await octokit.repos.getCommit({
198+
owner: remote.owner,
199+
repo: remote.repositoryName,
200+
sha: commit.sha
201+
});
202+
203+
return fullCommit.data.files.filter(file => !!file.patch);
204+
} catch (e) {
205+
vscode.window.showErrorMessage(`Fetching commit file changes failed: ${formatError(e)}`);
206+
return [];
207+
}
208+
}
209+
177210
async getReviewComments(pullRequest: IPullRequestModel, reviewId: string): Promise<Comment[]> {
178211
let githubRepository = (pullRequest as PullRequestModel).githubRepository;
179212
let octokit = githubRepository.octokit;
@@ -286,7 +319,7 @@ export class PullRequestManager implements IPullRequestManager {
286319
return ret.data;
287320
}
288321

289-
async getPullRequestChagnedFiles(pullRequest: IPullRequestModel): Promise<any> {
322+
async getPullRequestChangedFiles(pullRequest: IPullRequestModel): Promise<FileChange[]> {
290323
let githubRepository = (pullRequest as PullRequestModel).githubRepository;
291324
let octokit = githubRepository.octokit;
292325
let remote = githubRepository.remote;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import * as vscode from 'vscode';
7+
import { Resource } from '../common/resources';
8+
import { IPullRequestModel, IPullRequestManager } from '../github/interface';
9+
import { FileChangeNode } from './treeNodes/fileChangeNode';
10+
import { DescriptionNode } from './treeNodes/descriptionNode';
11+
import { TreeNode } from './treeNodes/treeNode';
12+
import { FilesCategoryNode } from './treeNodes/filesCategoryNode';
13+
import { CommitsNode } from './treeNodes/commitsCategoryNode';
14+
import { Comment } from '../common/comment';
15+
16+
export class PullRequestChangesTreeDataProvider extends vscode.Disposable implements vscode.TreeDataProvider<TreeNode> {
17+
private _onDidChangeTreeData = new vscode.EventEmitter<FileChangeNode | DescriptionNode>();
18+
readonly onDidChangeTreeData = this._onDidChangeTreeData.event;
19+
20+
private _localFileChanges: FileChangeNode[] = [];
21+
private _comments: Comment[] = [];
22+
private _pullrequest: IPullRequestModel = null;
23+
private _pullRequestManager: IPullRequestManager;
24+
25+
constructor(private context: vscode.ExtensionContext) {
26+
super(() => this.dispose());
27+
this.context.subscriptions.push(vscode.window.registerTreeDataProvider<TreeNode>('prStatus', this));
28+
}
29+
30+
async showPullRequestFileChanges(pullRequestManager: IPullRequestManager, pullrequest: IPullRequestModel, fileChanges: FileChangeNode[], comments: Comment[]) {
31+
this._pullRequestManager = pullRequestManager;
32+
this._pullrequest = pullrequest;
33+
this._comments = comments;
34+
35+
await vscode.commands.executeCommand(
36+
'setContext',
37+
'github:inReviewMode',
38+
true
39+
);
40+
41+
this._localFileChanges = fileChanges;
42+
this._onDidChangeTreeData.fire();
43+
}
44+
45+
async hide() {
46+
await vscode.commands.executeCommand(
47+
'setContext',
48+
'github:inReviewMode',
49+
false
50+
);
51+
}
52+
53+
getTreeItem(element: TreeNode): vscode.TreeItem | Thenable<vscode.TreeItem> {
54+
return element.getTreeItem();
55+
}
56+
57+
getChildren(element?: FileChangeNode): vscode.ProviderResult<TreeNode[]> {
58+
if (!element) {
59+
const descriptionNode = new DescriptionNode('Description',
60+
{
61+
light: Resource.icons.light.Description,
62+
dark: Resource.icons.dark.Description
63+
}, this._pullrequest);
64+
const filesCategoryNode = new FilesCategoryNode(this._localFileChanges);
65+
const commitsCategoryNode = new CommitsNode(this._pullRequestManager, this._pullrequest, this._comments);
66+
return [ descriptionNode, filesCategoryNode, commitsCategoryNode ];
67+
} else {
68+
return element.getChildren();
69+
}
70+
}
71+
}

src/view/prFileChangesTreeDataProvider.ts

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)