提交 a96e8775 编写于 作者: R Rachel Macfarlane

A bit of code cleanup

上级 ec22a1e2
......@@ -6,7 +6,7 @@
import * as vscode from 'vscode';
import * as path from 'path';
import { PullRequestModel } from '../models/pullRequestModel';
import { PullRequestModel, PullRequest } from '../models/pullRequestModel';
import { ReviewManager } from '../review/reviewManager';
export class PullRequestOverviewPanel {
......@@ -107,7 +107,7 @@ export class PullRequestOverviewPanel {
});
return;
case 'pr.close':
vscode.commands.executeCommand('pr.close', this._pullRequest).then(pr => {
vscode.commands.executeCommand<PullRequest>('pr.close', this._pullRequest).then(pr => {
if (pr) {
this._pullRequest.update(pr);
this._panel.webview.postMessage({
......
......@@ -34,6 +34,42 @@ export interface IAccount {
privateRepositoryInPlanCount?: number;
}
export interface Repo {
label: string;
ref: string;
repo: any;
sha: string;
}
// This interface is incomplete
export interface PullRequest {
additions: number;
assignee: any;
assignees: any[];
author_association: string;
base: Repo;
body: string;
changed_files: number;
closed_at: string;
comments: number;
commits: number;
created_at: string;
head: Repo;
html_url: string;
id: number;
labels: any[];
locked: boolean;
maintainer_can_modify: boolean;
merge_commit_sha; boolean;
mergable: boolean;
number: number;
rebaseable: boolean;
state: string;
title: string;
updated_at: string;
user: any;
}
export class PullRequestModel {
public prNumber: number;
public title: string;
......@@ -56,11 +92,11 @@ export class PullRequestModel {
public head: GitHubRef;
public base: GitHubRef;
constructor(public readonly otcokit: any, public readonly remote: Remote, public prItem: any) {
constructor(public readonly otcokit: any, public readonly remote: Remote, public prItem: PullRequest) {
this.update(prItem);
}
update(prItem: any) {
update(prItem: PullRequest) {
this.prNumber = prItem.number;
this.title = prItem.title;
this.html_url = prItem.html_url;
......
......@@ -25,7 +25,6 @@ export class ReviewManager implements vscode.DecorationProvider {
private static _instance: ReviewManager;
private _documentCommentProvider: vscode.Disposable;
private _workspaceCommentProvider: vscode.Disposable;
private _command: vscode.Disposable;
private _disposables: vscode.Disposable[];
private _comments: Comment[] = [];
......@@ -37,38 +36,16 @@ export class ReviewManager implements vscode.DecorationProvider {
private _onDidChangeCommentThreads = new vscode.EventEmitter<vscode.CommentThreadChangedEvent>();
private _prFileChangesProvider: FileChangesProvider;
get prFileChangesProvider() {
if (!this._prFileChangesProvider) {
this._prFileChangesProvider = new FileChangesProvider(this._context);
this._disposables.push(this._prFileChangesProvider);
}
return this._prFileChangesProvider;
}
private _statusBarItem: vscode.StatusBarItem;
get statusBarItem() {
if (!this._statusBarItem) {
this._statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
}
return this._statusBarItem;
}
private _prNumber: number;
private _pr: PullRequestModel;
get currentPullRequest(): PullRequestModel {
return this._pr;
}
private constructor(
private _context: vscode.ExtensionContext,
private _repository: Repository
) {
this._documentCommentProvider = null;
this._workspaceCommentProvider = null;
this._command = null;
this._disposables = [];
let gitContentProvider = new GitContentProvider(_repository);
gitContentProvider.registerTextDocumentContentFallback(this.provideTextDocumentContent.bind(this));
......@@ -97,6 +74,27 @@ export class ReviewManager implements vscode.DecorationProvider {
return ReviewManager._instance;
}
get prFileChangesProvider() {
if (!this._prFileChangesProvider) {
this._prFileChangesProvider = new FileChangesProvider(this._context);
this._disposables.push(this._prFileChangesProvider);
}
return this._prFileChangesProvider;
}
get statusBarItem() {
if (!this._statusBarItem) {
this._statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
}
return this._statusBarItem;
}
get currentPullRequest(): PullRequestModel {
return this._pr;
}
private pollForStatusChange() {
setTimeout(async () => {
await this.updateComments();
......@@ -316,59 +314,64 @@ export class ReviewManager implements vscode.DecorationProvider {
}
private async getPullRequestData(pr: PullRequestModel): Promise<void> {
this._comments = await pr.getComments();
let activeComments = this._comments.filter(comment => comment.position);
let outdatedComments = this._comments.filter(comment => !comment.position);
const data = await pr.getFiles();
await pr.fetchBaseCommitSha();
let baseSha = pr.base.sha;
let headSha = pr.head.sha;
const richContentChanges = await parseDiff(data, this._repository, baseSha);
this._localFileChanges = richContentChanges.map(change => {
let changedItem = new FileChangeTreeItem(
pr,
change.fileName,
change.status,
change.fileName,
change.blobUrl,
toGitUri(vscode.Uri.parse(change.fileName), null, null, change.status === GitChangeType.DELETE ? '' : pr.prItem.head.sha, {}),
toGitUri(vscode.Uri.parse(change.fileName), null, null, change.status === GitChangeType.ADD ? '' : pr.prItem.base.sha, {}),
this._repository.path,
change.diffHunks
);
changedItem.sha = headSha;
changedItem.comments = activeComments.filter(comment => comment.path === changedItem.fileName);
return changedItem;
});
let commitsGroup = groupBy(outdatedComments, comment => comment.original_commit_id);
this._obsoleteFileChanges = [];
for (let commit in commitsGroup) {
let commentsForCommit = commitsGroup[commit];
let commentsForFile = groupBy(commentsForCommit, comment => comment.path);
for (let fileName in commentsForFile) {
let oldComments = commentsForFile[fileName];
let obsoleteFileChange = new FileChangeTreeItem(
try {
this._comments = await pr.getComments();
let activeComments = this._comments.filter(comment => comment.position);
let outdatedComments = this._comments.filter(comment => !comment.position);
const data = await pr.getFiles();
await pr.fetchBaseCommitSha();
let baseSha = pr.base.sha;
let headSha = pr.head.sha;
const richContentChanges = await parseDiff(data, this._repository, baseSha);
this._localFileChanges = richContentChanges.map(change => {
let changedItem = new FileChangeTreeItem(
pr,
fileName,
GitChangeType.MODIFY,
fileName,
null,
toGitUri(vscode.Uri.parse(path.join(`commit~${commit.substr(0, 8)}`, fileName)), fileName, null, oldComments[0].original_commit_id, {}),
toGitUri(vscode.Uri.parse(path.join(`commit~${commit.substr(0, 8)}`, fileName)), fileName, null, oldComments[0].original_commit_id, {}),
change.fileName,
change.status,
change.fileName,
change.blobUrl,
toGitUri(vscode.Uri.parse(change.fileName), null, null, change.status === GitChangeType.DELETE ? '' : pr.prItem.head.sha, {}),
toGitUri(vscode.Uri.parse(change.fileName), null, null, change.status === GitChangeType.ADD ? '' : pr.prItem.base.sha, {}),
this._repository.path,
[] // @todo Peng.
change.diffHunks
);
changedItem.sha = headSha;
changedItem.comments = activeComments.filter(comment => comment.path === changedItem.fileName);
return changedItem;
});
obsoleteFileChange.sha = commit;
obsoleteFileChange.comments = oldComments;
this._obsoleteFileChanges.push(obsoleteFileChange);
let commitsGroup = groupBy(outdatedComments, comment => comment.original_commit_id);
this._obsoleteFileChanges = [];
for (let commit in commitsGroup) {
let commentsForCommit = commitsGroup[commit];
let commentsForFile = groupBy(commentsForCommit, comment => comment.path);
for (let fileName in commentsForFile) {
let oldComments = commentsForFile[fileName];
let obsoleteFileChange = new FileChangeTreeItem(
pr,
fileName,
GitChangeType.MODIFY,
fileName,
null,
toGitUri(vscode.Uri.parse(path.join(`commit~${commit.substr(0, 8)}`, fileName)), fileName, null, oldComments[0].original_commit_id, {}),
toGitUri(vscode.Uri.parse(path.join(`commit~${commit.substr(0, 8)}`, fileName)), fileName, null, oldComments[0].original_commit_id, {}),
this._repository.path,
[] // @todo Peng.
);
obsoleteFileChange.sha = commit;
obsoleteFileChange.comments = oldComments;
this._obsoleteFileChanges.push(obsoleteFileChange);
}
}
return Promise.resolve(null);
} catch (e) {
console.log(e);
}
return Promise.resolve(null);
}
private outdatedCommentsToCommentThreads(fileChange: FileChangeTreeItem, comments: Comment[], collapsibleState: vscode.CommentThreadCollapsibleState = vscode.CommentThreadCollapsibleState.Expanded): vscode.CommentThread[] {
......@@ -687,10 +690,6 @@ export class ReviewManager implements vscode.DecorationProvider {
this._pr = null;
this._updateMessageShown = false;
if (this._command) {
this._command.dispose();
}
if (this._documentCommentProvider) {
this._documentCommentProvider.dispose();
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册