|
| 1 | +name: Sync Microsoft.Build version in analyzer template with Version.props |
| 2 | +on: |
| 3 | + push: |
| 4 | + branches: |
| 5 | + - main |
| 6 | + paths: |
| 7 | + - 'eng/Versions.props' |
| 8 | + |
| 9 | +jobs: |
| 10 | + Sync-version: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + |
| 13 | + steps: |
| 14 | + - name: Checkout repository |
| 15 | + uses: actions/checkout@v3 |
| 16 | + |
| 17 | + - name: Set baseBranch variable |
| 18 | + id: vars |
| 19 | + run: echo "baseBranch=${{ github.ref_name }}" >> $GITHUB_ENV |
| 20 | + |
| 21 | + - name: Update analyzer template version with version from Versions.props |
| 22 | + shell: pwsh |
| 23 | + run: | |
| 24 | + try { |
| 25 | + # Define the paths to your XML and JSON files |
| 26 | + $xmlFilePath = "eng/Versions.props" |
| 27 | + $jsonFilePath = "template_feed/content/Microsoft.CheckTemplate/.template.config/template.json" |
| 28 | +
|
| 29 | + # Check if the XML file exists |
| 30 | + if (-Not (Test-Path -Path $xmlFilePath)) { |
| 31 | + throw "Versions.props file not found: $xmlFilePath" |
| 32 | + } |
| 33 | +
|
| 34 | + # Load and parse the XML content |
| 35 | + [xml]$xmlContent = Get-Content -Path $xmlFilePath |
| 36 | + $versionPrefix = [string]$xmlContent.Project.PropertyGroup.VersionPrefix |
| 37 | + $versionPrefix = $versionPrefix.Trim() |
| 38 | +
|
| 39 | + # Validate the versionPrefix |
| 40 | + if ([string]::IsNullOrWhiteSpace($versionPrefix)) { |
| 41 | + throw "VersionPrefix is empty or null in the XML file: $xmlFilePath" |
| 42 | + } |
| 43 | +
|
| 44 | + # Check if the JSON file exists |
| 45 | + if (-Not (Test-Path -Path $jsonFilePath)) { |
| 46 | + throw "Analyzer template file not found: $jsonFilePath" |
| 47 | + } |
| 48 | +
|
| 49 | + # Load the JSON template |
| 50 | + $jsonContent = Get-Content -Path $jsonFilePath -Raw | ConvertFrom-Json |
| 51 | +
|
| 52 | + # Check if the versionPrefix is different from the current defaultValue |
| 53 | + if ($versionPrefix -ne $jsonContent.symbols.MicrosoftBuildVersion.defaultValue) { |
| 54 | + # Update the defaultValue of MicrosoftBuildVersion in the JSON template |
| 55 | + $jsonContent.symbols.MicrosoftBuildVersion.defaultValue = $versionPrefix |
| 56 | +
|
| 57 | + # Convert the JSON content back to a string |
| 58 | + $jsonString = $jsonContent | ConvertTo-Json -Depth 10 |
| 59 | +
|
| 60 | + # Write the updated JSON back to the file |
| 61 | + Set-Content -Path $jsonFilePath -Value $jsonString |
| 62 | + Write-Output "Updated MicrosoftBuildVersion to $versionPrefix" |
| 63 | +
|
| 64 | + # Set the updateNeeded output variable to true |
| 65 | + $updateNeeded = "true" |
| 66 | + } else { |
| 67 | + Write-Output "No update needed. MicrosoftBuildVersion is already $versionPrefix" |
| 68 | +
|
| 69 | + # Set the updateNeeded output variable to false |
| 70 | + $updateNeeded = "false" |
| 71 | + } |
| 72 | +
|
| 73 | + # Set the versionPrefix and template filePath as an output |
| 74 | + Add-Content -Path $env:GITHUB_ENV -Value "versionPrefix=$versionPrefix" |
| 75 | + Add-Content -Path $env:GITHUB_ENV -Value "jsonFilePath=$jsonFilePath" |
| 76 | + Add-Content -Path $env:GITHUB_ENV -Value "updateNeeded=$updateNeeded" |
| 77 | + Write-Output "Extracted versionPrefix: $versionPrefix" |
| 78 | + Write-Output "Extracted jsonFilePath: $jsonFilePath" |
| 79 | + Write-Output "Update needed: $updateNeeded" |
| 80 | + } |
| 81 | + catch { |
| 82 | + Write-Error "An error occurred: $_" |
| 83 | + } |
| 84 | +
|
| 85 | + - name: Create Pull Request |
| 86 | + if: env.updateNeeded == 'true' |
| 87 | + uses: actions/github-script@v7 |
| 88 | + with: |
| 89 | + script: | |
| 90 | + const baseBranch = process.env.baseBranch; |
| 91 | + const versionPrefix = process.env.versionPrefix; |
| 92 | + const filePath = process.env.jsonFilePath; |
| 93 | + const newBranch = `${baseBranch}-update-msbuild-version-for-analyzer-template`; |
| 94 | + const commitMessage = `Update MicrosoftBuildVersion to ${versionPrefix}`; |
| 95 | + const prBody = '[Automated] Update the MicrosoftBuildVersion defaultValue in the template.json.'; |
| 96 | + const prTitle = 'Update MicrosoftBuildVersion in analyzer template'; |
| 97 | +
|
| 98 | + // Main execution |
| 99 | + (async () => { |
| 100 | + try { |
| 101 | + // Configure git |
| 102 | + await configureGit(); |
| 103 | +
|
| 104 | + // Create and switch to the new branch |
| 105 | + await createAndSwitchBranch(newBranch); |
| 106 | +
|
| 107 | + // Check if the branch PR already exists on the remote |
| 108 | + const shouldOpenPullRequest = await checkBranchPRExists(newBranch,baseBranch); |
| 109 | +
|
| 110 | + // Stage and commit the changes |
| 111 | + await stageAndCommitChanges(filePath, commitMessage); |
| 112 | +
|
| 113 | + // Push the new branch to the repository |
| 114 | + await pushBranch(newBranch); |
| 115 | +
|
| 116 | + // Create the pull request if needed |
| 117 | + if (shouldOpenPullRequest) { |
| 118 | + await createPullRequest(baseBranch, newBranch, prTitle, prBody); |
| 119 | + } else { |
| 120 | + console.log("The PR already exists, skipping opening a new PR."); |
| 121 | + } |
| 122 | + } catch (error) { |
| 123 | + core.setFailed(error); |
| 124 | + } |
| 125 | + })(); |
| 126 | +
|
| 127 | + async function configureGit() { |
| 128 | + await exec.exec(`git config user.name "github-actions"`); |
| 129 | + await exec.exec(`git config user.email "[email protected]"`); |
| 130 | + } |
| 131 | +
|
| 132 | + async function createAndSwitchBranch(branch) { |
| 133 | + await exec.exec('git', ['checkout', '-b', branch]); |
| 134 | + } |
| 135 | +
|
| 136 | + async function checkBranchPRExists(newBranch,baseBranch) { |
| 137 | + // Check if a pull request already exists |
| 138 | + const { data: pullRequests } = await github.rest.pulls.list({ |
| 139 | + owner: context.repo.owner, |
| 140 | + repo: context.repo.repo, |
| 141 | + head: newBranch, |
| 142 | + base: baseBranch, |
| 143 | + state: 'open', |
| 144 | + }); |
| 145 | +
|
| 146 | + if (pullRequests.length === 0) { |
| 147 | + return true; |
| 148 | + } else { |
| 149 | + return false; |
| 150 | + } |
| 151 | + } |
| 152 | +
|
| 153 | + async function stageAndCommitChanges(filePath, commitMessage) { |
| 154 | + await exec.exec(`git add ${filePath}`); |
| 155 | + await exec.exec(`git commit -m "${commitMessage}"`); |
| 156 | + } |
| 157 | +
|
| 158 | + async function pushBranch(branch) { |
| 159 | + await exec.exec(`git push --force --set-upstream origin HEAD:${branch}`); |
| 160 | + } |
| 161 | +
|
| 162 | + async function createPullRequest(baseBranch, newBranch, title, body) { |
| 163 | + await github.rest.pulls.create({ |
| 164 | + owner: context.repo.owner, |
| 165 | + repo: context.repo.repo, |
| 166 | + title: title, |
| 167 | + body: body, |
| 168 | + head: newBranch, |
| 169 | + base: baseBranch |
| 170 | + }); |
| 171 | + } |
0 commit comments