How to Import a Git Project With REST API Between VSTS Team Projects
Imagine that you want to import a private Git repository from a source repository to a VSTS Target repository using only REST API.
Join the DZone community and get the full member experience.Join For Free
i’ve got an interesting question about the possibility of importing a git repository via rest api between team projects of vsts. imagine that you want to import a private git repository from a source repository (in this situation, it's another vsts git repository, but it could be hosted everywhere) to a vsts target repository using only rest api.
the operation is quite simple thanks to the new api described here . in this post, i’ll give you all the details.
step 1: create a pat
you have many options in how to authenticate the call when trying to access vsts through rest api. the easiest one uses pat (personal access token). if you do no already have a valid pat you can create one using security page of your account.
figure 1. open the security page of your account.
creating a pat is really simple. you only need to select a personal access token, then give a description, an expiration time, and the account where pat is valid. since i have more than one vsts account, i have a combo where all of my accounts are listed.
finally, you should select only the permission that you want to give to the token to. the default option is all scopes, and this will imply that the token can do pretty much anything you can do. if you need this token to manage the importing of your repositories, then you can select only code-related permissions.
figure 2: create a pat to access your account.
personal access tokens are the most secure way to authenticate an application in vsts because they can be revoked, you can choose the permission you want to give to the token, and they have an automatic expiration.
if your source account is on a different account than the target account, then you need to create pat both in source account vsts instance and in target account vsts instance. in this example, the vsts instance is the very same, so i need only one pat.
step 2: create an endpoint to access the source repository
my target repository is called importtest. it's important that this repository is created empty. this is my target repository, the repository where i want to import the source repository.
figure 3. create target repository with the standard web interface.
the import routine should be able to access the source repository. this implies that it needs to be authenticated. to maximize security, you need to create an endpoint that points to the source repository in the team project of the target repository. this can be easily done from the administration page of the team project that contains the target repository. the team project that contains my importtest repository is contained in gitmiscellaneous team project. i can proceed to manually create the endpoint.
figure 4: create an endpoint of type external git.
figure 5: specify endpoint details.
in figure 5, you can see all the options needed. here, you should specify a connection name. the url parameter is the url of the source repository, the same url you use to clone the repository. finally, you need to use the pat as username. then, you can press ok.
this service endpoint should be created in the team project that contains the target repository because it will be used by the import routine in order to authenticate the source repository and take data to import.
an endpoint is basically a url and authentication that is used by the server to access an external service.
if you need to automate the whole process, the endpoint can be created easily with rest api. here is a simple call in postman .
figure 6: creation of the endpoint with rest api.
this does not need any explanation because it is a simple call with the very same option that you specified on the ui.
step 3: create the call to import the repository
to create the call to start the repository import routine, you need some parameters. first of all, you need the id of the endpoint you created in step 2. if you created the endpoint through rest api, then this is not a problem because the id is present in the response.
figure 7: the response of the request shown in figure 6 contains the endpoint id.
if you created the endpoint through web ui, then the id can be grabbed by the url in the administration page of the endpoints. however, a simpler and better method is to list all of the endpoints of the team project through rest api. in my situation, this is a simple get call to this url .
the answer is the very same of figure 7. this gives me the id of the endpoint that points to the source repository df12f2e3-7c40-4885-8dbd-310f1781369a .
now, i need to create the import request, as described here . the only information i’m missing is the id of the target repository
figure 8: repository part of the url in the call should be replaced by repository id.
as shown in figure 8, the only annoying part of the request is the id of the target repository because it is the guid of the repository, not the name. obtaining this value is not difficult because with rest api this is a simple get call to this url . from the answer of this call the id of the importtest repository is 3037268a-0c91-4fe1-8435-a76e9b731f5e .
now i have everything to create the import request. just forge the request in postman or a similar tool and fire the request.
figure 9: the import request where 1 is the id of target repository and 2 is the id of the endpoint.
if you are quick enough and refresh the page of the target repository while the import routine is running, then you should be able to see this image:
figure 10: importing is running!
after a little bit (depending on the source of the source repository), the target repository will be a perfect clone of the source repository.
if there are errors during the import process in the source code page of target repository, then you are warned with the error shown in figure 11.
figure 11: errors in the importing routine were shown to the source code page of target repository.
note: the error in the above image is due to a misconfiguration of the endpoint (done in part 2) as an example if you created the endpoint with wrong credentials.
Published at DZone with permission of Ricci Gian Maria, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.