Skip to content

Checking if the repository returns avatars #1146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2019
Merged
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
42 changes: 32 additions & 10 deletions src/github/pullRequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,14 +621,20 @@ export class PullRequestManager {
}

async getReviewRequests(pullRequest: PullRequestModel): Promise<IAccount[]> {
const { remote, octokit } = await pullRequest.githubRepository.ensure();
const githubRepository = pullRequest.githubRepository;
const { remote, octokit } = await githubRepository.ensure();
const result = await octokit.pullRequests.getReviewRequests({
owner: remote.owner,
repo: remote.repositoryName,
number: pullRequest.prNumber
});

return result.data.users.map(user => convertRESTUserToAccount(user));
let repositoryReturnsAvatar = true
if(result.data.users.length) {
repositoryReturnsAvatar = await githubRepository.ensureRepositoryReturnsAvatar(result.data.users[0].avatar_url);
}

return result.data.users.map(user => convertRESTUserToAccount(user, repositoryReturnsAvatar));
}

async getPullRequestComments(pullRequest: PullRequestModel): Promise<Comment[]> {
Expand Down Expand Up @@ -667,15 +673,22 @@ export class PullRequestManager {
*/
private async getPullRequestReviewComments(pullRequest: PullRequestModel): Promise<Comment[]> {
Logger.debug(`Fetch comments of PR #${pullRequest.prNumber} - enter`, PullRequestManager.ID);
const { remote, octokit } = await (pullRequest as PullRequestModel).githubRepository.ensure();
const githubRepository = (pullRequest as PullRequestModel).githubRepository;
const { remote, octokit } = await githubRepository.ensure();
const reviewData = await octokit.pullRequests.getComments({
owner: remote.owner,
repo: remote.repositoryName,
number: pullRequest.prNumber,
per_page: 100
});
Logger.debug(`Fetch comments of PR #${pullRequest.prNumber} - done`, PullRequestManager.ID);
const rawComments = reviewData.data.map(comment => this.addCommentPermissions(convertPullRequestsGetCommentsResponseItemToComment(comment), remote));

let repositoryReturnsAvatar = true
if(reviewData.data.length) {
repositoryReturnsAvatar = await githubRepository.ensureRepositoryReturnsAvatar(reviewData.data[0].user.avatar_url);
}

const rawComments = reviewData.data.map(comment => this.addCommentPermissions(convertPullRequestsGetCommentsResponseItemToComment(comment, repositoryReturnsAvatar), remote));
return rawComments;
}

Expand Down Expand Up @@ -816,7 +829,8 @@ export class PullRequestManager {
return this.addCommentToPendingReview(pullRequest, pendingReviewId, body, { inReplyTo: reply_to.graphNodeId });
}

const { octokit, remote } = await pullRequest.githubRepository.ensure();
const githubRepository = pullRequest.githubRepository;
const { octokit, remote } = await githubRepository.ensure();

try {
let ret = await octokit.pullRequests.createCommentReply({
Expand All @@ -827,7 +841,9 @@ export class PullRequestManager {
in_reply_to: Number(reply_to.id)
});

return this.addCommentPermissions(convertPullRequestsGetCommentsResponseItemToComment(ret.data), remote);
const repositoryReturnsAvatar = await githubRepository.ensureRepositoryReturnsAvatar(ret.data.user.avatar_url);

return this.addCommentPermissions(convertPullRequestsGetCommentsResponseItemToComment(ret.data, repositoryReturnsAvatar), remote);
} catch (e) {
this.handleError(e);
}
Expand Down Expand Up @@ -967,7 +983,8 @@ export class PullRequestManager {
return this.addCommentToPendingReview(pullRequest as PullRequestModel, pendingReviewId, body, { path: commentPath, position });
}

const { octokit, remote } = await (pullRequest as PullRequestModel).githubRepository.ensure();
const githubRepository = (pullRequest as PullRequestModel).githubRepository;
const { octokit, remote } = await githubRepository.ensure();

try {
let ret = await octokit.pullRequests.createComment({
Expand All @@ -980,7 +997,9 @@ export class PullRequestManager {
position: position
});

return this.addCommentPermissions(convertPullRequestsGetCommentsResponseItemToComment(ret.data), remote);
const repositoryReturnsAvatar = await githubRepository.ensureRepositoryReturnsAvatar(ret.data.user.avatar_url);

return this.addCommentPermissions(convertPullRequestsGetCommentsResponseItemToComment(ret.data, repositoryReturnsAvatar), remote);
} catch (e) {
this.handleError(e);
}
Expand Down Expand Up @@ -1119,7 +1138,8 @@ export class PullRequestManager {
return this.editPendingReviewComment(pullRequest, comment.graphNodeId, text);
}

const { octokit, remote } = await pullRequest.githubRepository.ensure();
const githubRepository = pullRequest.githubRepository;
const { octokit, remote } = await githubRepository.ensure();

const ret = await octokit.pullRequests.editComment({
owner: remote.owner,
Expand All @@ -1128,7 +1148,9 @@ export class PullRequestManager {
comment_id: comment.id
});

return this.addCommentPermissions(convertPullRequestsGetCommentsResponseItemToComment(ret.data), remote);
const repositoryReturnsAvatar = await githubRepository.ensureRepositoryReturnsAvatar(ret.data.user.avatar_url);

return this.addCommentPermissions(convertPullRequestsGetCommentsResponseItemToComment(ret.data, repositoryReturnsAvatar), remote);
} catch (e) {
throw new Error(formatError(e));
}
Expand Down
8 changes: 4 additions & 4 deletions src/github/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ export function updateCommentCommands(vscodeComment: vscode.Comment, commentCont
}
}

export function convertRESTUserToAccount(user: Octokit.PullRequestsGetAllResponseItemUser): IAccount {
export function convertRESTUserToAccount(user: Octokit.PullRequestsGetAllResponseItemUser, repositoryReturnsAvatar: boolean): IAccount {
return {
login: user.login,
url: user.html_url,
avatarUrl: user.avatar_url
avatarUrl: repositoryReturnsAvatar ? user.avatar_url : undefined
};
}

Expand Down Expand Up @@ -212,7 +212,7 @@ export function convertIssuesCreateCommentResponseToComment(comment: Octokit.Iss
};
}

export function convertPullRequestsGetCommentsResponseItemToComment(comment: Octokit.PullRequestsGetCommentsResponseItem | Octokit.PullRequestsEditCommentResponse): Comment {
export function convertPullRequestsGetCommentsResponseItemToComment(comment: Octokit.PullRequestsGetCommentsResponseItem | Octokit.PullRequestsEditCommentResponse, repositoryReturnsAvatar: boolean): Comment {
let ret: Comment = {
url: comment.url,
id: comment.id,
Expand All @@ -223,7 +223,7 @@ export function convertPullRequestsGetCommentsResponseItemToComment(comment: Oct
commitId: comment.commit_id,
originalPosition: comment.original_position,
originalCommitId: comment.original_commit_id,
user: convertRESTUserToAccount(comment.user),
user: convertRESTUserToAccount(comment.user, repositoryReturnsAvatar),
body: comment.body,
createdAt: comment.created_at,
htmlUrl: comment.html_url,
Expand Down