TutuGeorge
TutuGeorge

Reputation: 2012

Import Repo to Azure DevOps account from another account via REST APIs throws BAD request

I want to import git repository from another Azure DevOps account to my new account. I am using REST APIs in power-shell to import them . Script i am using for that is here :

$TargetTokenName = 'TokenName'
$TargetPAT = 'PAT'
$base64AuthInfoTarget = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "$TargetTokenName","$TargetPAT")))
$BaseTargetUri = 'https://dev.azure.com/myOrg/myProj/_apis/'
$TargetRepoId = 'TargetRepositoryId'
$ServiceEndpointId = 'ServiceEndpointId'

$CreateImportReqRequest = '{
    "parameters" : {
        "gitSource": {
            "url" : "' + $SourceRepoUrl + '",
            }
  }
}'

$CreateImportReqUri = $BaseTargetUri + 'git/repositories/' + $TargetRepoId + '/importRequests?api-version=5.0-preview.1'
$CreateImportReqResponse = Invoke-RestMethod -Method Post -ContentType application/json -Uri $CreateImportReqUri -Body $CreateImportReqRequest -Headers @{Authorization=("Basic {0}" -f $base64AuthInfoTarget)}
echo $CreateImportReqResponse

But Bad request error is coming when invoking. I am able to create empty target repo and service end point via REST.

In the first link below, the request body contains additional parameter for serviceendpointid, but that is also giving error.

Invoke-RestMethod : The remote server returned an error: (400) Bad Request.

References i took are : http://www.codewrecks.com/blog/index.php/2016/10/08/import-a-git-project-with-rest-api-between-vsts-team-projects/ https://learn.microsoft.com/en-us/rest/api/azure/devops/git/import%20requests/create?view=azure-devops-server-rest-5.0

Upvotes: 1

Views: 920

Answers (1)

TutuGeorge
TutuGeorge

Reputation: 2012

The bad request was coming a result of wrong PAT in the service end-point.

The request is now success with this request structure:

$CreateImportReqRequest = '{
    "parameters" : {
        "gitSource": {
            "url" : "'+$SourceRepoUrl+'",
            },
            "serviceEndpointId": "' + $ServiceEndpointId + '",
            deleteServiceEndpointAfterImportIsDone : true 
  }
}'

Upvotes: 2

Related Questions