Skip to content

Commit

Permalink
Fixes #150, New comments are gone
Browse files Browse the repository at this point in the history
  • Loading branch information
Rachel Macfarlane committed Aug 10, 2018
1 parent 6e31f82 commit 7346061
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 167 deletions.
4 changes: 2 additions & 2 deletions src/github/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ export interface IPullRequestManager {
getTimelineEvents(pullRequest: IPullRequestModel): Promise<TimelineEvent[]>;
getIssueComments(pullRequest: IPullRequestModel): Promise<Comment[]>;
createIssueComment(pullRequest: IPullRequestModel, text: string): Promise<Comment>;
createCommentReply(pullRequest: IPullRequestModel, body: string, reply_to: string);
createComment(pullRequest: IPullRequestModel, body: string, path: string, position: number);
createCommentReply(pullRequest: IPullRequestModel, body: string, reply_to: string): Promise<Comment>;
createComment(pullRequest: IPullRequestModel, body: string, path: string, position: number): Promise<Comment>;
closePullRequest(pullRequest: IPullRequestModel): Promise<any>;
getPullRequestChangedFiles(pullRequest: IPullRequestModel): Promise<FileChange[]>;
fullfillPullRequestCommitInfo(pullRequest: IPullRequestModel): Promise<void>;
Expand Down
8 changes: 4 additions & 4 deletions src/github/pullRequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ export class PullRequestManager implements IPullRequestManager {
return promise.data;
}

async createCommentReply(pullRequest: IPullRequestModel, body: string, reply_to: string) {
async createCommentReply(pullRequest: IPullRequestModel, body: string, reply_to: string): Promise<Comment> {
const { octokit, remote } = await (pullRequest as PullRequestModel).githubRepository.ensure();

let ret = await octokit.pullRequests.createCommentReply({
Expand All @@ -313,10 +313,10 @@ export class PullRequestManager implements IPullRequestManager {
in_reply_to: Number(reply_to)
});

return ret;
return ret.data;
}

async createComment(pullRequest: IPullRequestModel, body: string, path: string, position: number) {
async createComment(pullRequest: IPullRequestModel, body: string, path: string, position: number): Promise<Comment> {
const { octokit, remote } = await (pullRequest as PullRequestModel).githubRepository.ensure();

let ret = await octokit.pullRequests.createComment({
Expand All @@ -329,7 +329,7 @@ export class PullRequestManager implements IPullRequestManager {
position: position
});

return ret;
return ret.data;
}

async closePullRequest(pullRequest: IPullRequestModel): Promise<any> {
Expand Down
122 changes: 74 additions & 48 deletions src/view/reviewManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as path from 'path';
import * as vscode from 'vscode';
import { parseDiff } from '../common/diffHunk';
import { getDiffLineByPosition, getLastDiffLine, mapCommentsToHead, mapHeadLineToDiffHunkPosition, mapOldPositionToNew, getZeroBased, getAbsolutePosition } from '../common/diffPositionMapping';
import { toReviewUri, fromReviewUri } from '../common/uri';
import { toReviewUri, fromReviewUri, fromPRUri } from '../common/uri';
import { groupBy, formatError } from '../common/utils';
import { Comment } from '../common/comment';
import { GitChangeType, SlimFileChange } from '../common/file';
Expand All @@ -21,6 +21,7 @@ import { FileChangeNode, RemoteFileChangeNode, fileChangeNodeFilter } from './tr
import Logger from '../common/logger';
import { PullRequestsTreeDataProvider } from './prsTreeDataProvider';
import { IConfiguration } from '../authentication/configuration';
import { providePRDocumentComments } from './treeNodes/pullRequestNode';

export class ReviewManager implements vscode.DecorationProvider {
private static _instance: ReviewManager;
Expand Down Expand Up @@ -176,16 +177,50 @@ export class ReviewManager implements vscode.DecorationProvider {
vscode.commands.executeCommand('pr.refreshList');
}

private findMatchedFileByUri(document: vscode.TextDocument): FileChangeNode {
const uri = document.uri;

let fileName: string;
if (uri.scheme === 'review' || uri.scheme === 'file') {
fileName = uri.path;
}

if (uri.scheme === 'pr') {
fileName = fromPRUri(uri).fileName;
}

const matchedFiles = fileChangeNodeFilter(this._localFileChanges).filter(fileChange => {
if (uri.scheme === 'review' || uri.scheme === 'pr') {
return fileChange.fileName === fileName;
} else {
let absoluteFilePath = vscode.Uri.file(path.resolve(this._repository.path, fileChange.fileName));
let targetFilePath = vscode.Uri.file(fileName);
return absoluteFilePath.fsPath === targetFilePath.fsPath;
}
});

if (matchedFiles && matchedFiles.length) {
return matchedFiles[0];
}
}

private async replyToCommentThread(document: vscode.TextDocument, range: vscode.Range, thread: vscode.CommentThread, text: string) {
try {
let ret = await this._prManager.createCommentReply(this._prManager.activePullRequest, text, thread.threadId);
const matchedFile = this.findMatchedFileByUri(document);
if (!matchedFile) {
throw new Error('Unable to find matching file');
}

const comment = await this._prManager.createCommentReply(this._prManager.activePullRequest, text, thread.threadId);
thread.comments.push({
commentId: ret.data.id,
body: new vscode.MarkdownString(ret.data.body),
userName: ret.data.user.login,
gravatar: ret.data.user.avatar_url
commentId: comment.id,
body: new vscode.MarkdownString(comment.body),
userName: comment.user.login,
gravatar: comment.user.avatar_url
});

matchedFile.comments.push(comment);

setTimeout(() => {
this.updateComments();
}, 0);
Expand All @@ -197,56 +232,43 @@ export class ReviewManager implements vscode.DecorationProvider {

private async createNewCommentThread(document: vscode.TextDocument, range: vscode.Range, text: string) {
try {
let uri = document.uri;
let fileName = uri.path;
let matchedFiles = fileChangeNodeFilter(this._localFileChanges).filter(fileChange => {
if (uri.scheme === 'review') {
return fileChange.fileName === fileName;
} else {
let absoluteFilePath = vscode.Uri.file(path.resolve(this._repository.path, fileChange.fileName));
let targetFilePath = vscode.Uri.file(fileName);
return absoluteFilePath.fsPath === targetFilePath.fsPath;
}
});

const uri = document.uri;
const matchedFile = this.findMatchedFileByUri(document);
const query = uri.query === '' ? undefined : JSON.parse(uri.query);
const isBase = query && query.base;

let query = uri.query === '' ? undefined : JSON.parse(uri.query);
// git diff sha -- fileName
const contentDiff = await this._repository.diff(matchedFile.fileName, this._lastCommitSha);
const position = mapHeadLineToDiffHunkPosition(matchedFile.diffHunks, contentDiff, range.start.line + 1, isBase);

let isBase = query && query.base;

if (matchedFiles && matchedFiles.length) {
let matchedFile = matchedFiles[0];
// git diff sha -- fileName
let contentDiff = await this._repository.diff(matchedFile.fileName, this._lastCommitSha);
let position = mapHeadLineToDiffHunkPosition(matchedFile.diffHunks, contentDiff, range.start.line + 1, isBase);
if (position < 0) {
throw new Error('Comment position cannot be negative');
}

if (position < 0) {
return;
}
// there is no thread Id, which means it's a new thread
let rawComment = await this._prManager.createComment(this._prManager.activePullRequest, text, matchedFile.fileName, position);

// there is no thread Id, which means it's a new thread
let ret = await this._prManager.createComment(this._prManager.activePullRequest, text, matchedFile.fileName, position);
let comment = {
commentId: rawComment.id,
body: new vscode.MarkdownString(rawComment.body),
userName: rawComment.user.login,
gravatar: rawComment.user.avatar_url
};

let comment = {
commentId: ret.data.id,
body: new vscode.MarkdownString(ret.data.body),
userName: ret.data.user.login,
gravatar: ret.data.user.avatar_url
};
let commentThread: vscode.CommentThread = {
threadId: comment.commentId,
resource: uri,
range: range,
comments: [comment]
};

let commentThread: vscode.CommentThread = {
threadId: comment.commentId,
resource: uri,
range: range,
comments: [comment]
};
matchedFile.comments.push(rawComment);

setTimeout(() => {
this.updateComments();
}, 0);
setTimeout(() => {
this.updateComments();
}, 0);

return commentThread;
}
return commentThread;
} catch (e) {
throw new Error(formatError(e));
}
Expand Down Expand Up @@ -579,6 +601,10 @@ export class ReviewManager implements vscode.DecorationProvider {
};
}

if (document.uri.scheme === 'pr') {
return providePRDocumentComments(document, this._prNumber, this._localFileChanges);
}

if (document.uri.scheme === 'review') {
// we should check whehter the docuemnt is original or modified.
let query = fromReviewUri(document.uri);
Expand Down
Loading

0 comments on commit 7346061

Please sign in to comment.