forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommitNode.ts
More file actions
122 lines (111 loc) · 3.79 KB
/
Copy pathcommitNode.ts
File metadata and controls
122 lines (111 loc) · 3.79 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as path from 'path';
import * as vscode from 'vscode';
import { getGitChangeType } from '../../common/diffHunk';
import { FILE_LIST_LAYOUT } from '../../common/settingKeys';
import { toReviewUri } from '../../common/uri';
import { OctokitCommon } from '../../github/common';
import { FolderRepositoryManager, SETTINGS_NAMESPACE } from '../../github/folderRepositoryManager';
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 | undefined;
public contextValue?: string;
constructor(
public parent: TreeNodeParent,
private readonly pullRequestManager: FolderRepositoryManager,
private readonly pullRequest: PullRequestModel,
private readonly commit: OctokitCommon.PullsListCommitsResponseItem,
private readonly isCurrent: boolean
) {
super();
this.label = commit.commit.message;
this.sha = commit.sha;
this.collapsibleState = vscode.TreeItemCollapsibleState.Collapsed;
let userIconUri: vscode.Uri | undefined;
try {
if (commit.author && commit.author.avatar_url) {
userIconUri = vscode.Uri.parse(`${commit.author.avatar_url}&s=${64}`);
}
} catch (_) {
// no-op
}
this.iconPath = userIconUri;
this.contextValue = 'commit';
}
getTreeItem(): vscode.TreeItem {
return this;
}
async getChildren(): Promise<TreeNode[]> {
const fileChanges = (await this.pullRequest.getCommitChangedFiles(this.commit)) ?? [];
if (fileChanges.length === 0) {
return [new LabelOnlyNode('No changed files')];
}
const fileChangeNodes = fileChanges.map(change => {
const fileName = change.filename!;
const uri = vscode.Uri.parse(path.posix.join(`commit~${this.commit.sha.substr(0, 8)}`, fileName));
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(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
result.push(...dirNode.children);
} else {
result.push(dirNode);
}
} else {
// flat view
result = fileChangeNodes;
}
return Promise.resolve(result);
}
}