Home > OS >  How to update excel input file directly in Azure Git repository without downloading the entire proje
How to update excel input file directly in Azure Git repository without downloading the entire proje

Time:12-31

I have a java maven project (Eclipse) in Azure Git repository. It is a test project to create data in system based on the input given in the excel. Currently I have the entire project cloned in local machine with git, update the input excel file and push to Azure repository, before running the test. Is there a way to push changes to the input excel file directly in repository, without downloading the entire project folder to local?

CodePudding user response:

Maybe, using the Upload

... naturally, it will create a commit but this is easy enough. I've made code changes before without an IDE by simply editing existing files in the repo online.

CodePudding user response:

You can use REST and Powershell to update files on remote repo.... as an example:

$user = ""
$token = "<PAT>" #https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
$orgUrl = "https://dev.azure.com/<org>"
$teamProject = "TestProject"
$repoName = "Repo1"

$localFilePath = 'c:/temp/tst.xlsx'
$gitFilePath = 'testfolder/tst.xlsx'
$gitBranch = "master"

$restApiUpdateFile = "$orgUrl/$teamProject/_apis/git/repositories/$repoName/pushes?api-version=6.1-preview.2"
$restApiGetMasterRef = "$orgUrl/$teamProject/_apis/git/repositories/$repoName/refs?filter=heads/$gitBranch`&api-version=6.1-preview.1"

$fileContentToUpdate = [convert]::ToBase64String((Get-Content -path $localFilePath -Encoding byte))

function InvokeGetRequest ($GetUrl)
{    
    return Invoke-RestMethod -Uri $GetUrl -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}    
}

function InvokePostRequest ($PostUrl, $body)
{   
    return Invoke-RestMethod -Uri $PostUrl -Method Post -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}  -Body $body
}

$updateBody = @"
{
    "refUpdates": [
      {
        "name": "refs/heads/{gitbranchpath}",
        "oldObjectId": "{mainBranchObjectId}"
      }
    ],
    "commits": [
      {
        "comment": "Updates file",
        "changes": [
          {
            "changeType": "edit",
            "item": {
              "path": "{filePathToUpdate}"
            },
            "newContent": {
              "content": "{newFileContentToUpdate}",
              "contentType": "base64encoded"
            }
          }
        ]
      }
    ]
  }
"@

$res = InvokeGetRequest $restApiGetMasterRef

$updateBody = $updateBody.Replace("{gitbranchpath}", $gitBranch);
$updateBody = $updateBody.Replace("{mainBranchObjectId}", $res.value[0].objectId);
$updateBody = $updateBody.Replace("{filePathToUpdate}", $gitFilePath);
$updateBody = $updateBody.Replace("{newFileContentToUpdate}", $fileContentToUpdate);


InvokePostRequest $restApiUpdateFile $updateBody
  • Related