Search code examples
azure-devopsazure-devops-rest-apiazure-repos

Fork a repo from one Azure DevOps Org to another Org using the API


Is it possible to fork a repo that exists in a Private Project of the SourceOrganization into another Project of DestOrganization using Azure DevOps API?


Solution

  • No this is not available.

    The question you linked to will allow you to create a one-time mirror of another repository, but you won't have any features like pull requests, diff/merge etc.

    You can perform an import request to create a clone in another organization, but it completely ignores the relationship between the repos and you can't perform any pull requests across the organisations.

    The code is two-step:

    1. Create a temporary endpoint using a PAT that has at least Code (Read) access on the source organisation.
    POST https://dev.azure.com/{{ORG}}/{{PROJECT}}/_apis/serviceendpoint/endpoints
    
    {
        "authorization":{
            "parameters":{
                "password":"{{PAT WITH ENOUGH PERMISSIONS}}",
                "username":"."
            },
            "scheme":"UsernamePassword"
        },
        "name":"{{RANDOM UNIQUE NAME}}",
        "type":"git",
        "url":"{{CLONE URL OF SOURCE REPO}}"
    }
    

    which will respond with an endpoint ID:

    {
       "data":{},
       "id":"72d574f0-05f1-481f-af15-f78579b374d4",
       "name":"wv1w1",
       "type":"git"
       ...
    }
    
    1. Then create the import:
    POST https://dev.azure.com/{{ORG}}/{{PROJECT}}/_apis/git/repositories/{{NEW REPO NAME}}/importRequests
    
    {
       "parameters":{ 
          "deleteServiceEndpointAfterImportIsDone":true,
          "gitSource":{
              "overwrite":false,
              "url":"{{CLONE URL OF SOURCE REPO}}"
          },
          "tfvcSource":null,
          "serviceEndpointId":"{{ID FROM PREVIOUS REQUEST}}"
        }
    }
    

    It also can't be used to sync sources between different organisations.

    You could cobble something together with a Azure Pipeline that pushes the changes from one org to another. Again, no "fork", but really just a sync.