forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuri.ts
More file actions
141 lines (117 loc) · 3.41 KB
/
uri.ts
File metadata and controls
141 lines (117 loc) · 3.41 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { Uri, UriHandler, EventEmitter } from 'vscode';
import { GitChangeType } from './file';
import { PullRequestModel } from '../github/pullRequestModel';
export interface ReviewUriParams {
path: string;
ref?: string;
commit?: string;
base: boolean;
isOutdated: boolean;
}
export function fromReviewUri(uri: Uri): ReviewUriParams {
return JSON.parse(uri.query);
}
export interface PRUriParams {
baseCommit: string;
headCommit: string;
isBase: boolean;
fileName: string;
prNumber: number;
status: GitChangeType;
remoteName: string;
}
export function fromPRUri(uri: Uri): PRUriParams | undefined {
try {
return JSON.parse(uri.query) as PRUriParams;
} catch (e) { }
}
export interface GitUriOptions {
replaceFileExtension?: boolean;
submoduleOf?: string;
base: boolean;
}
export function toDiffViewFileUri(uri: Uri, filePath: string | undefined, ref: string | undefined, commit: string, isOutdated: boolean, options: GitUriOptions): Uri {
const params: ReviewUriParams = {
path: filePath ? filePath : uri.path,
ref,
commit: commit,
base: options.base,
isOutdated
};
let path = uri.path;
if (options.replaceFileExtension) {
path = `${path}.git`;
}
return uri.with({
path,
query: JSON.stringify(params)
});
}
// As a mitigation for extensions like ESLint showing warnings and errors
// for git URIs, let's change the file extension of these uris to .git,
// when `replaceFileExtension` is true.
export function toReviewUri(uri: Uri, filePath: string | undefined, ref: string | undefined, commit: string, isOutdated: boolean, options: GitUriOptions): Uri {
const params: ReviewUriParams = {
path: filePath ? filePath : uri.path,
ref,
commit: commit,
base: options.base,
isOutdated
};
let path = uri.path;
if (options.replaceFileExtension) {
path = `${path}.git`;
}
return uri.with({
scheme: 'review',
path,
query: JSON.stringify(params)
});
}
export interface FileChangeNodeUriParams {
hasComments?: boolean;
status?: GitChangeType;
}
export function toFileChangeNodeUri(uri: Uri, hasComments: boolean, status: GitChangeType) {
const params = {
hasComments: hasComments,
status: status
};
return uri.with({
scheme: 'file',
query: JSON.stringify(params)
});
}
export function fromFileChangeNodeUri(uri: Uri): FileChangeNodeUriParams | undefined {
try {
return JSON.parse(uri.query) as FileChangeNodeUriParams;
} catch (e) { }
}
export function toPRUri(uri: Uri, pullRequestModel: PullRequestModel, baseCommit: string, headCommit: string, fileName: string, base: boolean, status: GitChangeType): Uri {
const params: PRUriParams = {
baseCommit: baseCommit,
headCommit: headCommit,
isBase: base,
fileName: fileName,
prNumber: pullRequestModel.prNumber,
status: status,
remoteName: pullRequestModel.githubRepository.remote.remoteName
};
let path = uri.path;
return uri.with({
scheme: 'pr',
path,
query: JSON.stringify(params)
});
}
class UriEventHandler extends EventEmitter<Uri> implements UriHandler {
public handleUri(uri: Uri) {
this.fire(uri);
}
}
export const handler = new UriEventHandler;