How to get repository id in azure devops

To get the repository ID in Azure DevOps, you can make use of the Azure DevOps REST API. Here is an example of how you can retrieve the repository ID using the API:

const axios = require('axios');

  const getRepositoryId = async (organization, project, repositoryName) => {
    try {
      const response = await axios.get(`https://dev.azure.com/${organization}/${project}/_apis/git/repositories?api-version=6.0`);
  
      const repositories = response.data.value;
      const repository = repositories.find(repo => repo.name === repositoryName);
      
      if (repository) {
        const repositoryId = repository.id;
        console.log("Repository ID:", repositoryId);
      } else {
        console.log("Repository not found.");
      }
    } catch (error) {
      console.error(error);
    }
  };

  getRepositoryId("YourOrganization", "YourProject", "YourRepositoryName");

In this example, we are using the Axios library to make an HTTP GET request to the Azure DevOps REST API. The URL `https://dev.azure.com/${organization}/${project}/_apis/git/repositories?api-version=6.0` is used to retrieve a list of repositories in the specified organization and project.

Once we have the list of repositories, we can use the `find` method to search for a repository with a specific name (`repositoryName` in the example). If the repository is found, we can access its ID (`repository.id`) and perform any necessary operations using that ID.

Remember to replace `”YourOrganization”`, `”YourProject”`, and `”YourRepositoryName”` with your actual organization, project, and repository names.

Example Response:

{
    "count": 1,
    "value": [
        {
            "id": "01234567-89ab-cdef-0123-456789abcdef",
            "name": "YourRepositoryName",
            "url": "https://dev.azure.com/YourOrganization/YourProject/_apis/git/repositories/01234567-89ab-cdef-0123-456789abcdef",
            "project": {
                "id": "98765432-10fe-dcba-3210-987654fedcba",
                "name": "YourProject",
                "url": "https://dev.azure.com/YourOrganization/YourProject/_apis/projects/98765432-10fe-dcba-3210-987654fedcba",
                "state": "wellFormed",
                "revision": 105
            },
            // ...
        }
    ]
}

In the example response, you can find the repository ID under the `”id”` field. You can also see other repository details like name, URL, and the project it belongs to.

Make sure to handle any errors that may occur during the API request or search process and adjust the API version in the URL if necessary.

Leave a comment