2

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

https://learn.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.

0

2 Answers 2

1

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"
}
"@
Sign up to request clarification or add additional context in comments.

1 Comment

Nice find! Sounds like MS has no clue how this works.
0

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

3 Comments

can I use that one instead of a GitHub service endpoint for repositories with pipeline ymls?
The only way I was able to get this to work was by adding a UserName along with the token when setting up the Service Connection.
@Jeff Pipelines support the following values for the repository type only: git, github, githubenterprise, and bitbucket. The git type refers to Azure Repos Git repos. Check here for more details: learn.microsoft.com/en-us/azure/devops/pipelines/process/….

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.