2

I am trying to clone a repository from VSTS. I am trying to do it using powershell, and using a server that has no access to the internet, except to MyVSAccount.VisualStudio.com, so that I can clone repositories.

I am able to "talk" to my VS account using my username and a Token. I get back a list of all my repositories with info about them.

Following that, however, I get an error when trying to clone it. It just says: connection refused to MyVSAccount.VisualStudio.com.

Using fiddler, I can see that only when I retrieve the list of repositories that there is a http request to my VS account. When cloning, there isn't any, so there shouldn't be refusal. Plus, there is access to that address of my account, so there shouldn't be problems in cloning.

Has anyone got a suggestion for this? I don't get it why when there is an http request to retrieve the list of repositories, all is successful, and when cloning it isn't, even though there is no http request for cloning.

Is there a proxy set up that is necessary to insert in the code?

Code to clone

git clone --mirror $url

Code to retrieve the list of repositories using a Token.

$url = $h.Get_Item("Url")
$username = $h.Get_Item("Username")
$password = $h.Get_Item("Password")

# Retrieve list of all repositories
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
$headers = @{
    "Authorization" = ("Basic {0}" -f $base64AuthInfo)
    "Accept" = "application/json"
}

Add-Type -AssemblyName System.Web
$gitcred = ("{0}:{1}" -f  [System.Web.HttpUtility]::UrlEncode($username),$password)



$resp = Invoke-WebRequest -Headers $headers -Uri ("{0}/_apis/git/repositories?api-version=1.0" -f $url)
$json = convertFrom-JSON $resp.Content

Error:

fatal: unable to access 'MyVSAccount.visualstudio.com/MyProject/_git/Tests/';: Could not resolve host: myvsaccount.visualstudio.com

I also tried to set up proxy with:

$proxyString = "http://proxy-s-app.mynet.net:80"
$proxyUri = new-object System.Uri($proxyString)
[System.Net.WebRequest]: : DefaultWebProxy = new-object System.Net.WebProxy ($proxyUri, $true)
Consuela
  • 241
  • 5
  • 14

1 Answers1

2

Since the variable $url you defined should with the value https://MyVSAccount.VisualStudio.com. while the URL of git repo in VSTS should with the format https://MyVSAccount.visualstudio.com/projectname/_git/reponame.

So you should use VSTS repo url not VSTS account url to clone a mirror repo. Script to clone a git repo as:

$repourl=$url+"/projectname/_git/reponame"
git clone --mirror $repourl 2>&1|write-Host

Besides, please make sure config git to use the proxy. The command to config proxy for git as below:

git config --global http.proxy http://proxyUsername:proxyPassword@proxy.server.com:port

More details, you can refer Configure Git to use a proxy and Getting git to work with a proxy server.

Marina Liu
  • 36,876
  • 5
  • 61
  • 74
  • I have tried to use the cloning url as you wrote too before, with the same error. fatal: unable to access 'https://MyVSAccount.visuallstudio.com/MyProject/_git/Tests/': Could not resolve host: myvsaccount.visualstudio.com – Consuela Feb 09 '18 at 09:47
  • What if you clone the git repo by command line directly (as the command `git clone https://MyVSAccount.visuallstudio.com/MyProject/_git/Tests')? – Marina Liu Feb 09 '18 at 09:56
  • It cannot be done with the same error. And also I would like to have that in a Powershell ISE script, so that there can be automation later on. I am running this on a server with no access to the internet, except to https://MyVSAccount.VisualStudio.com. I think this might be the issue. – Consuela Feb 09 '18 at 10:13
  • 1
    Did you config the proxy in git? If you haven't configured the proxy in git, you can config it (I added in the end of my answer) and try again. – Marina Liu Feb 12 '18 at 05:17
  • It was the proxy! Thanks for the help. :) – Consuela Feb 12 '18 at 07:24