Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Import a Git Project With REST API Between VSTS Team Projects

DZone's Guide to

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.

Free Resource

Share, secure, distribute, control, and monetize your APIs with the platform built with performance, time-to-value, and growth in mind. Free 90-day trial of 3Scale by Red Hat

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.

image

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.

image

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.

image

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.

image

Figure 4: Create an endpoint of type External Git.

image

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

image

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.

image

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

image

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.

image

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:

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.

image

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.

Explore the core elements of owning an API strategy and best practices for effective API programs. Download the API Owner's Manual, brought to you by 3Scale by Red Hat

Topics:
integration ,rest api ,vsts ,git

Published at DZone with permission of Ricci Gian Maria, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}