-
Notifications
You must be signed in to change notification settings - Fork 736
Expand file tree
/
Copy pathcommitNode.ts
More file actions
126 lines (116 loc) · 4.43 KB
/
commitNode.ts
File metadata and controls
126 lines (116 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import { getGitChangeType } from '../../common/diffHunk';
import { FILE_LIST_LAYOUT, PR_SETTINGS_NAMESPACE } from '../../common/settingKeys';
import { DataUri, reviewPath, toReviewUri } from '../../common/uri';
import { dateFromNow } from '../../common/utils';
import { OctokitCommon } from '../../github/common';
import { FolderRepositoryManager } from '../../github/folderRepositoryManager';
import { AccountType, IAccount } from '../../github/interface';
import { IResolvedPullRequestModel, PullRequestModel } from '../../github/pullRequestModel';
import { GitFileChangeModel } from '../fileChangeModel';
import { DirectoryTreeNode } from './directoryTreeNode';
import { GitFileChangeNode } from './fileChangeNode';
import { LabelOnlyNode, TreeNode, TreeNodeParent } from './treeNode';
export class CommitNode extends TreeNode implements vscode.TreeItem {
public sha: string;
public collapsibleState: vscode.TreeItemCollapsibleState;
public iconPath: vscode.Uri | vscode.ThemeIcon | undefined;
public contextValue?: string;
constructor(
parent: TreeNodeParent,
private readonly pullRequestManager: FolderRepositoryManager,
private readonly pullRequest: PullRequestModel,
private readonly commit: OctokitCommon.PullsListCommitsResponseItem,
private readonly isCurrent: boolean
) {
super(parent);
this.label = commit.commit.message;
this.sha = commit.sha;
this.collapsibleState = vscode.TreeItemCollapsibleState.Collapsed;
this.contextValue = 'commit';
this.description = commit.commit.author?.date ? dateFromNow(commit.commit.author.date) : undefined;
}
async getTreeItem(): Promise<vscode.TreeItem> {
if (this.commit.author) {
// For enterprise, use placeholder icon instead of trying to fetch avatar
if (!DataUri.isGitHubDotComAvatar(this.commit.author.avatar_url)) {
this.iconPath = new vscode.ThemeIcon('github');
} else {
const author: IAccount = { id: this.commit.author.node_id, login: this.commit.author.login, url: this.commit.author.url, avatarUrl: this.commit.author.avatar_url, accountType: this.commit.author.type as AccountType };
this.iconPath = (await DataUri.avatarCirclesAsImageDataUris(this.pullRequestManager.context, [author], 16, 16))[0];
}
}
return this;
}
override async getChildren(): Promise<TreeNode[]> {
super.getChildren();
const fileChanges = (await this.pullRequest.getCommitChangedFiles(this.commit)) ?? [];
if (fileChanges.length === 0) {
return [new LabelOnlyNode(this, 'No changed files')];
}
const fileChangeNodes = fileChanges.map(change => {
const fileName = change.filename!;
const uri = reviewPath(fileName, this.commit.sha);
const changeModel = new GitFileChangeModel(
this.pullRequestManager,
this.pullRequest,
{
status: getGitChangeType(change.status!),
fileName,
blobUrl: undefined
},
toReviewUri(
uri,
fileName,
undefined,
this.commit.sha,
true,
{ base: false },
this.pullRequestManager.repository.rootUri,
),
toReviewUri(
uri,
fileName,
undefined,
this.commit.sha,
true,
{ base: true },
this.pullRequestManager.repository.rootUri,
),
this.commit.sha);
const fileChangeNode = new GitFileChangeNode(
this,
this.pullRequestManager,
this.pullRequest as (PullRequestModel & IResolvedPullRequestModel),
changeModel,
this.isCurrent
);
fileChangeNode.useViewChangesCommand();
return fileChangeNode;
});
let result: TreeNode[] = [];
const layout = vscode.workspace.getConfiguration(PR_SETTINGS_NAMESPACE).get<string>(FILE_LIST_LAYOUT);
if (layout === 'tree') {
// tree view
const dirNode = new DirectoryTreeNode(this, '');
fileChangeNodes.forEach(f => dirNode.addFile(f));
dirNode.finalize();
if (dirNode.label === '') {
// nothing on the root changed, pull children to parent
dirNode._children.forEach(child => { child.parent = this; });
result.push(...dirNode._children);
} else {
result.push(dirNode);
}
} else {
// flat view
result = fileChangeNodes;
}
this._children = result;
return result;
}
}