From ac77b790cd49d4e3bd4af77493759e5fd3ed1b32 Mon Sep 17 00:00:00 2001 From: Jan Sandquist Date: Tue, 21 Feb 2023 18:44:19 +0100 Subject: [PATCH 1/6] Replace status step with PowerShell --- .pipelines/pull.yml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/.pipelines/pull.yml b/.pipelines/pull.yml index afad219b..820811c4 100644 --- a/.pipelines/pull.yml +++ b/.pipelines/pull.yml @@ -169,19 +169,19 @@ jobs: # Check for data changes # - - task: Bash@3 + - task: PowerShell@2 displayName: "Status" inputs: targetType: "inline" script: | - STATUS=$(git status --short $(folder)) - echo $STATUS - if [ -z "$STATUS" ] - then - echo "##vso[task.setvariable variable=state]stop" - else - echo "##vso[task.setvariable variable=state]continue" - fi + $gitStatus = git status --short $(folder) + if ($null -ne $gitStatus) { + $gitStatus | Write-Host + Write-Host '##vso[task.setvariable variable=state]continue' + } + else { + Write-Host '##vso[task.setvariable variable=state]stop' + } # # Add From 8ef19be8bd31b0064fd6de251f88d7435628cbe9 Mon Sep 17 00:00:00 2001 From: Jan Sandquist Date: Tue, 21 Feb 2023 18:56:18 +0100 Subject: [PATCH 2/6] Update pull.yml --- .pipelines/pull.yml | 49 ++++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/.pipelines/pull.yml b/.pipelines/pull.yml index 820811c4..287e79f6 100644 --- a/.pipelines/pull.yml +++ b/.pipelines/pull.yml @@ -230,31 +230,44 @@ jobs: # Update remote refs along with associated objects # - - task: Bash@3 + - task: PowerShell@2 displayName: "Merge" condition: contains(variables['state'], 'continue') inputs: targetType: "inline" script: | + # Check if active PR already exists + $prActive = az repos pr list ` + --source-branch "$(branch)" ` + --target-branch "main" ` + --status "active" + + $prId = $prActive | jq '.[0].pullRequestId' + if ($prId -ne 'null') { + Write-Host "Active PR found - nothing more to do: $prId" + exit + } + # Open new PR - PROut=$( - az repos pr create \ - --title "$(pull_request)" \ - --source-branch "$(branch)" \ - --target-branch "main" \ - --squash true \ - --delete-source-branch true \ - --auto-complete true \ - ); + $prNew = az repos pr create ` + --title "$(pull_request)" ` + --source-branch "$(branch)" ` + --target-branch "main" ` + --squash true ` + --delete-source-branch true ` + --auto-complete true # Get PR ID and check status - PRid=$(echo $PROut | jq -r '.pullRequestId'); - PRStatus=$(az repos pr show --id $PRid | jq .status); - - # If PR is not completed, then complete it bypassing policy - if [ $PRStatus == "\"active\"" ]; then - echo "Completing PR bypassing branch policy" - az repos pr update --status completed --id $PRid --bypass-policy true --bypass-policy-reason "Automated pull request" > /dev/null 2>&1 - fi; + $prId = $prNew | jq '.pullRequestId' + $prStatus = az repos pr show --id $PRid | jq '.status' + + Write-Host "Pull Request created with Id: $prId" + + # If PR is not completed, then optionally complete it bypassing policy + $completePullRequest = $env:AZOPS_COMPLETE_PULL_REQUEST -eq 'true' + if ( $completePullRequest -and $prStatus -eq 'active' ) { + Write-Host 'Completing PR bypassing branch policy' + $prUpdate = az repos pr update --status completed --id $PRid --bypass-policy true --bypass-policy-reason 'Automated pull request' + } env: AZURE_DEVOPS_EXT_PAT: $(System.AccessToken) From 24bc4e14d3e531c3124a8a4a4853721fc4b3d5a5 Mon Sep 17 00:00:00 2001 From: Jan Sandquist Date: Tue, 21 Feb 2023 19:26:32 +0100 Subject: [PATCH 3/6] Added AZOPS_COMPLETE_PULL_REQUEST --- .pipelines/.templates/vars.yml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/.pipelines/.templates/vars.yml b/.pipelines/.templates/vars.yml index 08d40872..4599f42d 100644 --- a/.pipelines/.templates/vars.yml +++ b/.pipelines/.templates/vars.yml @@ -16,13 +16,16 @@ variables: # Files listed in the .order file will be deployed before other files and in the # order they are listed. # + # Set AZOPS_COMPLETE_PULL_REQUEST to false to opt-out from pull request completion. + # # - ARM_TENANT_ID # - ARM_SUBSCRIPTION_ID # - ARM_CLIENT_ID # - ARM_CLIENT_SECRET # - ARM_ENVIRONMENT - # - AZOPS_MODULE_VERSION - # - AZOPS_CUSTOM_SORT_ORDER + # - AZOPS_MODULE_VERSION + # - AZOPS_CUSTOM_SORT_ORDER + # - AZOPS_COMPLETE_PULL_REQUEST # - group: credentials @@ -36,4 +39,4 @@ variables: # - name: modulesFolder - value: '$(System.DefaultWorkingDirectory)/Modules' \ No newline at end of file + value: '$(System.DefaultWorkingDirectory)/Modules' From fbedfb6f0a6d854576d2f8e948da6c1731dfe1ae Mon Sep 17 00:00:00 2001 From: Jan Sandquist Date: Mon, 6 Mar 2023 16:21:44 +0100 Subject: [PATCH 4/6] Replace 'jq' with ConvertFrom-Json and some changes to logic and the variable name --- .pipelines/pull.yml | 56 ++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/.pipelines/pull.yml b/.pipelines/pull.yml index 287e79f6..120b4cd1 100644 --- a/.pipelines/pull.yml +++ b/.pipelines/pull.yml @@ -236,38 +236,38 @@ jobs: inputs: targetType: "inline" script: | + $autoComplete = $env:AZOPS_SKIP_PR_COMPLETION -ne 'true' + # Check if active PR already exists - $prActive = az repos pr list ` + $pr = az repos pr list ` --source-branch "$(branch)" ` --target-branch "main" ` - --status "active" - - $prId = $prActive | jq '.[0].pullRequestId' - if ($prId -ne 'null') { - Write-Host "Active PR found - nothing more to do: $prId" - exit + --status "active" | ConvertFrom-Json -NoEnumerate + + if ($pr) { + Write-Host 'Active PR found with Id:' $pr.pullRequestId + } else { + # Open new PR + $pr = az repos pr create ` + --title "$(pull_request)" ` + --source-branch "$(branch)" ` + --target-branch "main" ` + --squash true ` + --delete-source-branch true ` + --auto-complete "$autoComplete" | ConvertFrom-Json -NoEnumerate + + Write-Host 'PR created with Id:' $pr.pullRequestId } - - # Open new PR - $prNew = az repos pr create ` - --title "$(pull_request)" ` - --source-branch "$(branch)" ` - --target-branch "main" ` - --squash true ` - --delete-source-branch true ` - --auto-complete true - - # Get PR ID and check status - $prId = $prNew | jq '.pullRequestId' - $prStatus = az repos pr show --id $PRid | jq '.status' - - Write-Host "Pull Request created with Id: $prId" - - # If PR is not completed, then optionally complete it bypassing policy - $completePullRequest = $env:AZOPS_COMPLETE_PULL_REQUEST -eq 'true' - if ( $completePullRequest -and $prStatus -eq 'active' ) { - Write-Host 'Completing PR bypassing branch policy' - $prUpdate = az repos pr update --status completed --id $PRid --bypass-policy true --bypass-policy-reason 'Automated pull request' + + # Complete any active PR, new or existing + if ($autoComplete) { + $prId = $pr.pullRequestId + $pr = az repos pr show --id $prId | ConvertFrom-Json -NoEnumerate + # If PR is not completed, then complete it bypassing policy + if ($pr.status -eq 'active') { + Write-Host 'Completing PR bypassing branch policy' + az repos pr update --status completed --id $prId --bypass-policy true --bypass-policy-reason 'Automated pull request' + } } env: AZURE_DEVOPS_EXT_PAT: $(System.AccessToken) From 4ec534ffed844af60358f3c68f0951f489ec3783 Mon Sep 17 00:00:00 2001 From: Jan Sandquist Date: Mon, 6 Mar 2023 16:24:41 +0100 Subject: [PATCH 5/6] Renamed variable to AZOPS_SKIP_PR_COMPLETION --- .pipelines/.templates/vars.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pipelines/.templates/vars.yml b/.pipelines/.templates/vars.yml index 4599f42d..36cfc976 100644 --- a/.pipelines/.templates/vars.yml +++ b/.pipelines/.templates/vars.yml @@ -16,7 +16,7 @@ variables: # Files listed in the .order file will be deployed before other files and in the # order they are listed. # - # Set AZOPS_COMPLETE_PULL_REQUEST to false to opt-out from pull request completion. + # Set AZOPS_SKIP_PR_COMPLETION to true to opt-out from pull request completion. # # - ARM_TENANT_ID # - ARM_SUBSCRIPTION_ID @@ -25,7 +25,7 @@ variables: # - ARM_ENVIRONMENT # - AZOPS_MODULE_VERSION # - AZOPS_CUSTOM_SORT_ORDER - # - AZOPS_COMPLETE_PULL_REQUEST + # - AZOPS_SKIP_PR_COMPLETION # - group: credentials From 1827b2883d4389d651a565db99533e16aeb52cfb Mon Sep 17 00:00:00 2001 From: Jan Sandquist Date: Tue, 25 Apr 2023 18:38:45 +0200 Subject: [PATCH 6/6] Update pull.yml --- .pipelines/pull.yml | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/.pipelines/pull.yml b/.pipelines/pull.yml index 120b4cd1..ae5303d9 100644 --- a/.pipelines/pull.yml +++ b/.pipelines/pull.yml @@ -54,7 +54,7 @@ variables: # # Folder Name # By default we generate the hierachy within the - # 'azops' folder within the root of the repository. + # 'root' folder within the root of the repository. # If this property is modified, the config value within # the settings.json file - Core.State will also need # to be changed. @@ -129,7 +129,7 @@ jobs: # Set global options # - - task: Bash@3 + - task: PowerShell@2 displayName: "Configure" inputs: targetType: "inline" @@ -142,7 +142,7 @@ jobs: # Switch branches # - - task: Bash@3 + - task: PowerShell@2 displayName: "Checkout" inputs: targetType: "inline" @@ -188,7 +188,7 @@ jobs: # Add file content to index # - - task: Bash@3 + - task: PowerShell@2 displayName: "Add" condition: contains(variables['state'], 'continue') inputs: @@ -202,7 +202,7 @@ jobs: # Record changes to the repository # - - task: Bash@3 + - task: PowerShell@2 displayName: "Commit" condition: contains(variables['state'], 'continue') inputs: @@ -216,7 +216,7 @@ jobs: # Update remote refs along with associated objects # - - task: Bash@3 + - task: PowerShell@2 displayName: "Push" condition: contains(variables['state'], 'continue') inputs: @@ -236,8 +236,6 @@ jobs: inputs: targetType: "inline" script: | - $autoComplete = $env:AZOPS_SKIP_PR_COMPLETION -ne 'true' - # Check if active PR already exists $pr = az repos pr list ` --source-branch "$(branch)" ` @@ -254,13 +252,14 @@ jobs: --target-branch "main" ` --squash true ` --delete-source-branch true ` - --auto-complete "$autoComplete" | ConvertFrom-Json -NoEnumerate + --auto-complete true | ConvertFrom-Json -NoEnumerate Write-Host 'PR created with Id:' $pr.pullRequestId } # Complete any active PR, new or existing - if ($autoComplete) { + $completePullRequest = $env:AZOPS_SKIP_PR_COMPLETION -ne 'true' + if ($completePullRequest) { $prId = $pr.pullRequestId $pr = az repos pr show --id $prId | ConvertFrom-Json -NoEnumerate # If PR is not completed, then complete it bypassing policy