FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
vscode-pull-request-github/src/github/pullRequestGitHelper.ts at master · ananya/vscode-pull-request-github · GitHub
ananya
/
vscode-pull-request-github
Public
forked from
microsoft/vscode-pull-request-github
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
vscode-pull-request-github
/
src
/
github
/
pullRequestGitHelper.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
258 lines (218 loc) · 9.51 KB
Breadcrumbs
vscode-pull-request-github
/
src
/
github
/
pullRequestGitHelper.ts
Copy path
File metadata and controls
258 lines (218 loc) · 9.51 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/*
* Inspired by and includes code from GitHub/VisualStudio project, obtained from https://github.com/github/VisualStudio/blob/165a97bdcab7559e0c4393a571b9ff2aed4ba8a7/src/GitHub.App/Services/PullRequestService.cs
*/
import
Logger
from
'../common/logger'
;
import
{
Protocol
}
from
'../common/protocol'
;
import
{
Remote
,
parseRepositoryRemotes
}
from
'../common/remote'
;
import
{
IPullRequestModel
}
from
'./interface'
;
import
{
GitHubRepository
}
from
'./githubRepository'
;
import
{
Repository
,
Branch
}
from
'../typings/git'
;
const
PullRequestRemoteMetadataKey
=
'github-pr-remote'
;
const
PullRequestMetadataKey
=
'github-pr-owner-number'
;
const
PullRequestBranchRegex
=
/
b
r
a
n
c
h
\.
(
.
+
)
\.
g
i
t
h
u
b
-
p
r
-
o
w
n
e
r
-
n
u
m
b
e
r
/
;
export
interface
PullRequestMetadata
{
owner
:
string
;
repositoryName
:
string
;
prNumber
:
number
;
}
export
class
PullRequestGitHelper
{
static
async
createAndCheckout
(
repository
:
Repository
,
pullRequest
:
IPullRequestModel
)
{
let
localBranchName
=
await
PullRequestGitHelper
.
getBranchNameForPullRequest
(
repository
,
pullRequest
)
;
try
{
await
repository
.
getBranch
(
localBranchName
)
;
// already exist but the metadata is missing.
Logger
.
appendLine
(
`GitHelper> branch
${
localBranchName
}
exists locally but metadata is missing.`
)
;
await
repository
.
checkout
(
localBranchName
)
;
}
catch
(
err
)
{
// the branch is from a fork
// create remote for this fork
Logger
.
appendLine
(
`GitHelper> branch
${
localBranchName
}
is from a fork. Create a remote first.`
)
;
let
remoteName
=
await
PullRequestGitHelper
.
createRemote
(
repository
,
pullRequest
.
remote
,
pullRequest
.
head
.
repositoryCloneUrl
)
;
// fetch the branch
let
ref
=
`
${
pullRequest
.
head
.
ref
}
:
${
localBranchName
}
`
;
await
repository
.
fetch
(
remoteName
,
ref
)
;
await
repository
.
checkout
(
localBranchName
)
;
// set remote tracking branch for the local branch
await
repository
.
setBranchUpstream
(
localBranchName
,
`refs/remotes/
${
remoteName
}
/
${
pullRequest
.
head
.
ref
}
`
)
;
}
let
prBranchMetadataKey
=
`branch.
${
localBranchName
}
.
${
PullRequestMetadataKey
}
`
;
await
repository
.
setConfig
(
prBranchMetadataKey
,
PullRequestGitHelper
.
buildPullRequestMetadata
(
pullRequest
)
)
;
}
static
async
fetchAndCheckout
(
repository
:
Repository
,
remote
:
Remote
,
branchName
:
string
,
pullRequest
:
IPullRequestModel
)
:
Promise
<
void
>
{
let
remoteName
=
remote
.
remoteName
;
await
repository
.
fetch
(
remoteName
)
;
let
branch
:
Branch
;
try
{
branch
=
await
repository
.
getBranch
(
branchName
)
;
}
catch
(
err
)
{
Logger
.
appendLine
(
`GitHelper> branch
${
remoteName
}
/
${
branchName
}
doesn't exist on local disk yet.`
)
;
await
PullRequestGitHelper
.
fetchAndCreateBranch
(
repository
,
remote
,
branchName
,
pullRequest
)
;
branch
=
await
repository
.
getBranch
(
branchName
)
;
}
if
(
branch
.
remote
&&
branch
.
remote
!==
remote
.
remoteName
)
{
// the pull request branch is a branch with the same name in a fork
// we should check whehter the branch for this fork
await
PullRequestGitHelper
.
createAndCheckout
(
repository
,
pullRequest
)
;
return
;
}
await
repository
.
checkout
(
branchName
)
;
if
(
!
branch
.
upstream
)
{
// this branch is not associated with upstream yet
const
trackedBranchName
=
`refs/remotes/
${
remoteName
}
/
${
branchName
}
`
;
await
repository
.
setBranchUpstream
(
branchName
,
trackedBranchName
)
;
}
if
(
branch
.
behind
!==
undefined
&&
branch
.
behind
>
0
&&
branch
.
ahead
===
0
)
{
await
repository
.
pull
(
)
;
}
await
PullRequestGitHelper
.
associateBranchWithPullRequest
(
repository
,
pullRequest
,
branchName
)
;
}
static
async
getBranchForPullRequestFromExistingRemotes
(
repository
:
Repository
,
githubRepositories
:
GitHubRepository
[
]
,
pullRequest
:
IPullRequestModel
)
{
let
headRemote
=
PullRequestGitHelper
.
getHeadRemoteForPullRequest
(
repository
,
githubRepositories
,
pullRequest
)
;
if
(
headRemote
)
{
// the head of the PR is in this repository (not fork), we can just fetch
return
{
remote
:
headRemote
,
branch
:
pullRequest
.
head
.
ref
}
;
}
else
{
let
key
=
PullRequestGitHelper
.
buildPullRequestMetadata
(
pullRequest
)
;
let
configs
=
await
repository
.
getConfigs
(
)
;
let
branchInfos
=
configs
.
map
(
config
=>
{
let
matches
=
PullRequestBranchRegex
.
exec
(
config
.
key
)
;
return
{
branch
:
matches
&&
matches
.
length
?
matches
[
1
]
:
null
,
value
:
config
.
value
}
;
}
)
.
filter
(
c
=>
c
.
branch
&&
c
.
value
===
key
)
;
try
{
if
(
branchInfos
&&
branchInfos
.
length
)
{
let
remoteName
=
await
repository
.
getConfig
(
`branch.
${
branchInfos
[
0
]
.
branch
}
.remote`
)
;
let
headRemoteMatches
=
parseRepositoryRemotes
(
repository
)
.
filter
(
remote
=>
remote
.
remoteName
===
remoteName
)
;
if
(
headRemoteMatches
&&
headRemoteMatches
.
length
)
{
return
{
remote
:
headRemoteMatches
[
0
]
,
branch
:
branchInfos
[
0
]
.
branch
}
;
}
}
}
catch
(
_
)
{
return
null
;
}
return
null
;
}
}
static
async
fetchAndCreateBranch
(
repository
:
Repository
,
remote
:
Remote
,
branchName
:
string
,
pullRequest
:
IPullRequestModel
)
{
let
remoteName
=
remote
.
remoteName
;
const
trackedBranchName
=
`refs/remotes/
${
remoteName
}
/
${
branchName
}
`
;
Logger
.
appendLine
(
`GitHelper> fetch branch
${
trackedBranchName
}
`
)
;
try
{
const
trackedBranch
=
await
repository
.
getBranch
(
trackedBranchName
)
;
// create branch
await
repository
.
createBranch
(
branchName
,
false
,
trackedBranch
.
commit
)
;
await
repository
.
setBranchUpstream
(
branchName
,
trackedBranchName
)
;
}
catch
(
err
)
{
throw
new
Error
(
`Could not find branch '
${
trackedBranchName
}
'.`
)
;
}
}
static
buildPullRequestMetadata
(
pullRequest
:
IPullRequestModel
)
{
return
pullRequest
.
base
.
repositoryCloneUrl
.
owner
+
'#'
+
pullRequest
.
base
.
repositoryCloneUrl
.
repositoryName
+
'#'
+
pullRequest
.
prNumber
;
}
static
parsePullRequestMetadata
(
value
:
string
)
:
PullRequestMetadata
{
if
(
value
)
{
let
matches
=
/
(
.
*
)
#
(
.
*
)
#
(
.
*
)
/
g
.
exec
(
value
)
;
if
(
matches
&&
matches
.
length
===
4
)
{
const
[
,
owner
,
repo
,
prNumber
]
=
matches
;
return
{
owner
:
owner
,
repositoryName
:
repo
,
prNumber
:
Number
(
prNumber
)
}
;
}
}
return
null
;
}
static
async
getMatchingPullRequestMetadataForBranch
(
repository
:
Repository
,
branchName
:
string
)
:
Promise
<
PullRequestMetadata
>
{
try
{
let
configKey
=
`branch.
${
branchName
}
.
${
PullRequestMetadataKey
}
`
;
let
configValue
=
await
repository
.
getConfig
(
configKey
)
;
return
PullRequestGitHelper
.
parsePullRequestMetadata
(
configValue
)
;
}
catch
(
_
)
{
return
null
;
}
}
static
async
createRemote
(
repository
:
Repository
,
baseRemote
:
Remote
,
cloneUrl
:
Protocol
)
{
Logger
.
appendLine
(
`GitHelper> create remote for
${
cloneUrl
}
.`
)
;
let
remotes
=
parseRepositoryRemotes
(
repository
)
;
for
(
let
remote
of
remotes
)
{
if
(
new
Protocol
(
remote
.
url
)
.
equals
(
cloneUrl
)
)
{
return
remote
.
remoteName
;
}
}
let
remoteName
=
PullRequestGitHelper
.
getUniqueRemoteName
(
repository
,
cloneUrl
.
owner
)
;
cloneUrl
.
update
(
{
type
:
baseRemote
.
gitProtocol
.
type
}
)
;
await
repository
.
addRemote
(
remoteName
,
cloneUrl
.
toString
(
)
)
;
await
repository
.
setConfig
(
`remote.
${
remoteName
}
.
${
PullRequestRemoteMetadataKey
}
`
,
'true'
)
;
return
remoteName
;
}
static
async
isRemoteCreatedForPullRequest
(
repository
:
Repository
,
remoteName
:
string
)
{
try
{
const
isForPR
=
await
repository
.
getConfig
(
`remote.
${
remoteName
}
.
${
PullRequestRemoteMetadataKey
}
`
)
;
return
isForPR
===
'true'
;
}
catch
(
_
)
{
return
false
;
}
}
static
async
getBranchNameForPullRequest
(
repository
:
Repository
,
pullRequest
:
IPullRequestModel
)
:
Promise
<
string
>
{
let
branchName
=
`pr/
${
pullRequest
.
author
.
login
}
/
${
pullRequest
.
prNumber
}
`
;
let
result
=
branchName
;
let
number
=
1
;
while
(
true
)
{
try
{
await
repository
.
getBranch
(
result
)
;
result
=
branchName
+
'-'
+
number
++
;
}
catch
(
err
)
{
break
;
}
}
return
result
;
}
static
getUniqueRemoteName
(
repository
:
Repository
,
name
:
string
)
{
let
uniqueName
=
name
;
let
number
=
1
;
const
remotes
=
parseRepositoryRemotes
(
repository
)
;
while
(
remotes
.
find
(
e
=>
e
.
remoteName
===
uniqueName
)
)
{
uniqueName
=
name
+
number
++
;
}
return
uniqueName
;
}
static
getHeadRemoteForPullRequest
(
repository
:
Repository
,
githubRepositories
:
GitHubRepository
[
]
,
pullRequest
:
IPullRequestModel
)
:
Remote
{
for
(
let
i
=
0
;
i
<
githubRepositories
.
length
;
i
++
)
{
let
remote
=
githubRepositories
[
i
]
.
remote
;
if
(
remote
.
gitProtocol
&&
remote
.
gitProtocol
.
equals
(
pullRequest
.
head
.
repositoryCloneUrl
)
)
{
return
remote
;
}
}
return
null
;
}
static
async
associateBranchWithPullRequest
(
repository
:
Repository
,
pullRequest
:
IPullRequestModel
,
branchName
:
string
)
{
Logger
.
appendLine
(
`GitHelper> associate
${
branchName
}
with Pull Request #
${
pullRequest
.
prNumber
}
`
)
;
let
prConfigKey
=
`branch.
${
branchName
}
.
${
PullRequestMetadataKey
}
`
;
await
repository
.
setConfig
(
prConfigKey
,
PullRequestGitHelper
.
buildPullRequestMetadata
(
pullRequest
)
)
;
}
static
async
getPullRequestMergeBase
(
repository
:
Repository
,
remote
:
Remote
,
pullRequest
:
IPullRequestModel
)
:
Promise
<
string
>
{
try
{
return
await
repository
.
getMergeBase
(
pullRequest
.
base
.
sha
,
pullRequest
.
head
.
sha
)
;
}
catch
(
err
)
{
const
pullrequestHeadRef
=
`refs/pull/
${
pullRequest
.
prNumber
}
/head`
;
await
repository
.
fetch
(
remote
.
remoteName
,
pullrequestHeadRef
)
;
await
repository
.
fetch
(
remote
.
remoteName
,
pullRequest
.
base
.
ref
)
;
return
await
repository
.
getMergeBase
(
pullRequest
.
base
.
sha
,
pullRequest
.
head
.
sha
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL