2
votes

Trying to import a private GitHub repository into Azure DevOps using the REST API:

https://docs.microsoft.com/en-us/rest/api/azure/devops/git/import%20requests/create?view=azure-devops-rest-6.0

Unsurprisingly, the documentation doesn't work.

I have a PAT based service endpoint in the DevOps project that has access to the GitHub repository I'm trying to import.

I have the following PowerShell snippet that reproduces the problem

$headers = @{
    Authorization = "Bearer ****"
}
$org = '***'
$project = '***'
$targetRepositoryId = '***'
$sourceRepositoryUrl = '***'
$gitHubServiceEndpointId = '***'
irm https://dev.azure.com/$org/$project/_apis/git/repositories/$targetRepositoryId/importRequests?api-version=6.0-preview.1 -Method Post -Headers $headers -ContentType application/json -Body @"
{
  "parameters":  {
    "gitSource": {
      "overwrite":  false,
      "url":  "$sourceRepositoryUrl"
    },
    "serviceEndpointId":  "$gitHubServiceEndpointId",
    "deleteServiceEndpointAfterImportIsDone": false
  }
}
"@

Throws a 400 error (Bad Request) with no additional information.

If I make the GitHub repository public, the exact same API request and the exact same code above works fine.

I am also 100% certain that the service endpoint has access to the repository in question because I have pipelines in Azure DevOps that use this service endpoint to clone said repository from GitHub.

2

2 Answers

1
votes

I was able to get the import to work using a GitHub service connection ONLY if you create the service connection using UsernamePassword authorization scheme -- which you can't do from DevOps itself you have to do from the API:

irm "https://dev.azure.com/org/project/_apis/serviceendpoint/endpoints?api-version=6.0-preview.1" -Method Post -Headers $headers -ContentType application/json -Body @"
{
    "authorization": {
        "scheme": "UsernamePassword",
        "parameters": {
            "username": "foo",
            "password": "github access token"
        }
    },
    "name": "Test-5",
    "serviceEndpointProjectReferences": [
        {
            "description": "",
            "name": "Test-5",
            "projectReference": {
                "id": "project id",
                "name": "projectName"
            }
        }
    ],
    "type": "github",
    "url": "https://github.com",
    "isShared": false,
    "owner": "library"
}
"@
0
votes

According to captured network log, the service connection type needs to be "Other Git" when create the service connection, then input Git repository URL and Personal access tokens created in GitHub:

enter image description here

With this service connection ID, it's supposed to be able to get a successful import.

enter image description here