CodeMed
CodeMed

Reputation: 9201

Azure cli repository import fails with Operation returned a 400 status code

What specific syntax and/or configuration needs to be changed below in order for an Azure CLI command to successfully import a private source repo from GitHub into a private destination repo in Azure Repos?

CURRENT COMMAND AND ERROR RESPONSE:

C:\path\to\working\directory>az repos import create --git-source-url https://github.com/ValidGitHubOrgName/valid-gh-repo-name --repository ValidAzureReposDestinationRepoName --organization https://dev.azure.com/ValidOrgName --project ValidProjectName --requires-authorization  
Git Password / PAT:  
Confirm Git Password / PAT:  
ClientRequestError: Operation returned a 400 status code.  

WHAT WORKS:

When we use the above to import a public repository from GitHub into a private Azure Repos repo, the above completes without error as along as we of course remove the --requires-authorization flag.

WHAT FAILS:

However the above fails when we try to import a private GitHub repository with the full syntax shown above.

We have tried this not only with a valid GitHub password but also with a valid Azure DevOps Personal Access token that we give when prompted as shown above.

Do we need to add configuration in order for the import to work without error?
And also, how can we make sure the import command executes without an interactive request of credentials?

This needs to run in automation without an interactive human user.

The documentation here does not seem to include the instructions we require.

Upvotes: 2

Views: 2060

Answers (2)

Joy Wang
Joy Wang

Reputation: 42133

I can also reproduce your issue on my side, to use Azure CLI import a private GitHub repo to DevOps via a non-interactive way, please follow the steps below.

1.Navigate to the Project Settings in the devops portal -> Service connections -> New service connection -> Other Git(Not GitHub) -> create the connection with your github username and password.

enter image description here

2.Use REST API - Endpoints - Get Service Endpoints By Names or CLI az devops service-endpoint list to get the service connection, then note down the id(there are not only one id, use the correct one as below).

enter image description here

enter image description here

3.Then run the command as below, pass id in step 2 to --git-service-endpoint-id, also make sure you have created the devops repo and it is empty.

az repos import create --git-source-url https://github.com/Joyw1/privaterepo.git --repository testrep6 --git-service-endpoint-id 3c04f8a9-43c8-40cb-baf5-90faf292b9ab --organization https://dev.azure.com/xxxx --project testpro1 --requires-authorization

enter image description here

Check the result:

enter image description here

Update:

To create the Other Git service connection in an automatic way, you could powershell to call the REST API - Endpoints - Create directly.

Sample:

$MyPat = '<your devops PAT>'
$B64Pat = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$MyPat"))

$header = @{
    'Authorization' = 'Basic ' + $B64Pat
    'Content-Type' = 'application/json'
}

$body = '{
  "data": {},
  "name": "MyNewServiceEndpoint",
  "type": "git",
  "url": "https://github.com/Joyw1/privaterepo.git",
  "authorization": {
    "parameters": {
      "username": "joyw1",
      "password": "xxxxxx"
    },
    "scheme": "UsernamePassword"
  },
  "isShared": false,
  "isReady": true,
  "serviceEndpointProjectReferences": [
    {
      "projectReference": {
        "id": "53f719f7-2968-4035-8e56-163f239b4161",
        "name": "testpro1"
      },
      "name": "MyNewServiceEndpoint"
    }
  ]
}'

Invoke-RestMethod -Method Post -Uri https://dev.azure.com/v-joyw/_apis/serviceendpoint/endpoints?api-version=6.0-preview.4 -Headers $header -Body $body

enter image description here

Upvotes: 1

Turbot
Turbot

Reputation: 5233

for Integration with GitHub (Private) the current az cli have no option to take password therefore showing you interactive credential input, therefore the suggestion is to set environment variable AZURE_DEVOPS_EXT_GIT_SOURCE_PASSWORD_OR_PAT as part your batch/shell script and it will work , the suggest issue is open and work upon according to https://github.com/Azure/azure-devops-cli-extension/issues/1050

Upvotes: 2

Related Questions