-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Sync Microsoft.Build version in analyzer template with Version.props in the workflow #10345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
aa4648a
Create syncing analyzer template symbol version
JaynieBai a5d4e79
Test
JaynieBai 3e3984f
Update Versions.props
JaynieBai 3738816
Update checkout action version
JaynieBai 52f70cb
test when Update Versions.props
JaynieBai 87f9537
Add branch
JaynieBai b373136
some error handling
JaynieBai 59f5103
Update MicrosoftBuildVersion in analyzer template (#1)
github-actions[bot] 4d41b63
Use the github actions to create pr
JaynieBai 88fca86
Merge branch 'main' into jennybai/SyncVersionFlow
JaynieBai aadcf2f
Set baseBranch env
JaynieBai c2e80eb
Test Update Versions.props
JaynieBai 928e291
Test branch
JaynieBai 20fbdbc
Update Versions.props
JaynieBai c6dcb48
Test Update Versions.props
JaynieBai 20a77e6
Update Versions.props
JaynieBai daa9728
Add condition for create pull Request
JaynieBai 86a92b2
revert test
JaynieBai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
171 changes: 171 additions & 0 deletions
171
.github/workflows/SyncAnalyzerTemplateMSBuildVersion.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| name: Sync Microsoft.Build version in analyzer template with Version.props | ||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| paths: | ||
| - 'eng/Versions.props' | ||
|
|
||
| jobs: | ||
| Sync-version: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - name: Checkout repository | ||
| uses: actions/checkout@v3 | ||
|
|
||
| - name: Set baseBranch variable | ||
| id: vars | ||
| run: echo "baseBranch=${{ github.ref_name }}" >> $GITHUB_ENV | ||
|
|
||
| - name: Update analyzer template version with version from Versions.props | ||
| shell: pwsh | ||
| run: | | ||
| try { | ||
| # Define the paths to your XML and JSON files | ||
| $xmlFilePath = "eng/Versions.props" | ||
| $jsonFilePath = "template_feed/content/Microsoft.AnalyzerTemplate/.template.config/template.json" | ||
|
|
||
| # Check if the XML file exists | ||
| if (-Not (Test-Path -Path $xmlFilePath)) { | ||
| throw "Versions.props file not found: $xmlFilePath" | ||
| } | ||
|
|
||
| # Load and parse the XML content | ||
| [xml]$xmlContent = Get-Content -Path $xmlFilePath | ||
| $versionPrefix = [string]$xmlContent.Project.PropertyGroup.VersionPrefix | ||
| $versionPrefix = $versionPrefix.Trim() | ||
|
|
||
| # Validate the versionPrefix | ||
| if ([string]::IsNullOrWhiteSpace($versionPrefix)) { | ||
| throw "VersionPrefix is empty or null in the XML file: $xmlFilePath" | ||
| } | ||
|
|
||
| # Check if the JSON file exists | ||
| if (-Not (Test-Path -Path $jsonFilePath)) { | ||
| throw "Analyzer template file not found: $jsonFilePath" | ||
| } | ||
|
|
||
| # Load the JSON template | ||
| $jsonContent = Get-Content -Path $jsonFilePath -Raw | ConvertFrom-Json | ||
|
|
||
| # Check if the versionPrefix is different from the current defaultValue | ||
| if ($versionPrefix -ne $jsonContent.symbols.MicrosoftBuildVersion.defaultValue) { | ||
| # Update the defaultValue of MicrosoftBuildVersion in the JSON template | ||
| $jsonContent.symbols.MicrosoftBuildVersion.defaultValue = $versionPrefix | ||
|
|
||
| # Convert the JSON content back to a string | ||
| $jsonString = $jsonContent | ConvertTo-Json -Depth 10 | ||
|
|
||
| # Write the updated JSON back to the file | ||
| Set-Content -Path $jsonFilePath -Value $jsonString | ||
| Write-Output "Updated MicrosoftBuildVersion to $versionPrefix" | ||
|
|
||
| # Set the updateNeeded output variable to true | ||
| $updateNeeded = "true" | ||
| } else { | ||
| Write-Output "No update needed. MicrosoftBuildVersion is already $versionPrefix" | ||
|
|
||
| # Set the updateNeeded output variable to false | ||
| $updateNeeded = "false" | ||
| } | ||
|
|
||
| # Set the versionPrefix and template filePath as an output | ||
| Add-Content -Path $env:GITHUB_ENV -Value "versionPrefix=$versionPrefix" | ||
| Add-Content -Path $env:GITHUB_ENV -Value "jsonFilePath=$jsonFilePath" | ||
| Add-Content -Path $env:GITHUB_ENV -Value "updateNeeded=$updateNeeded" | ||
| Write-Output "Extracted versionPrefix: $versionPrefix" | ||
| Write-Output "Extracted jsonFilePath: $jsonFilePath" | ||
| Write-Output "Update needed: $updateNeeded" | ||
| } | ||
| catch { | ||
| Write-Error "An error occurred: $_" | ||
| } | ||
|
|
||
| - name: Create Pull Request | ||
| if: env.updateNeeded == 'true' | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const baseBranch = process.env.baseBranch; | ||
| const versionPrefix = process.env.versionPrefix; | ||
| const filePath = process.env.jsonFilePath; | ||
| const newBranch = `${baseBranch}-update-msbuild-version-for-analyzer-template`; | ||
| const commitMessage = `Update MicrosoftBuildVersion to ${versionPrefix}`; | ||
| const prBody = '[Automated] Update the MicrosoftBuildVersion defaultValue in the template.json.'; | ||
| const prTitle = 'Update MicrosoftBuildVersion in analyzer template'; | ||
|
|
||
| // Main execution | ||
| (async () => { | ||
| try { | ||
| // Configure git | ||
| await configureGit(); | ||
|
|
||
| // Create and switch to the new branch | ||
| await createAndSwitchBranch(newBranch); | ||
|
|
||
| // Check if the branch PR already exists on the remote | ||
| const shouldOpenPullRequest = await checkBranchPRExists(newBranch,baseBranch); | ||
|
|
||
| // Stage and commit the changes | ||
| await stageAndCommitChanges(filePath, commitMessage); | ||
|
|
||
| // Push the new branch to the repository | ||
| await pushBranch(newBranch); | ||
|
|
||
| // Create the pull request if needed | ||
| if (shouldOpenPullRequest) { | ||
| await createPullRequest(baseBranch, newBranch, prTitle, prBody); | ||
| } else { | ||
| console.log("The PR already exists, skipping opening a new PR."); | ||
| } | ||
| } catch (error) { | ||
| core.setFailed(error); | ||
| } | ||
| })(); | ||
|
|
||
| async function configureGit() { | ||
| await exec.exec(`git config user.name "github-actions"`); | ||
| await exec.exec(`git config user.email "github-actions@github.com"`); | ||
| } | ||
|
|
||
| async function createAndSwitchBranch(branch) { | ||
| await exec.exec('git', ['checkout', '-b', branch]); | ||
| } | ||
|
|
||
| async function checkBranchPRExists(newBranch,baseBranch) { | ||
| // Check if a pull request already exists | ||
| const { data: pullRequests } = await github.rest.pulls.list({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| head: newBranch, | ||
| base: baseBranch, | ||
| state: 'open', | ||
| }); | ||
|
|
||
| if (pullRequests.length === 0) { | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| async function stageAndCommitChanges(filePath, commitMessage) { | ||
| await exec.exec(`git add ${filePath}`); | ||
| await exec.exec(`git commit -m "${commitMessage}"`); | ||
| } | ||
|
|
||
| async function pushBranch(branch) { | ||
| await exec.exec(`git push --force --set-upstream origin HEAD:${branch}`); | ||
| } | ||
|
|
||
| async function createPullRequest(baseBranch, newBranch, title, body) { | ||
| await github.rest.pulls.create({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| title: title, | ||
| body: body, | ||
| head: newBranch, | ||
| base: baseBranch | ||
| }); | ||
| } | ||
82 changes: 41 additions & 41 deletions
82
template_feed/content/Microsoft.AnalyzerTemplate/.template.config/template.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,49 +1,49 @@ | ||
| { | ||
| "$schema": "http://json.schemastore.org/template", | ||
| "author": "Microsoft", | ||
| "classifications": [ | ||
| "Common", | ||
| "Library" | ||
| ], | ||
| "name": "MSBuild custom analyzer skeleton project.", | ||
| "generatorVersions": "[1.0.0.0-*)", | ||
| "description": "A project for creating a MSBuild analyzer library that targets .NET Standard", | ||
| "groupIdentity": "Microsoft.AnalyzerTemplate", | ||
| "identity": "Microsoft.AnalyzerTemplate", | ||
| "shortName": "msbuildanalyzer", | ||
| "tags": { | ||
| "language": "C#", | ||
| "type": "project" | ||
| }, | ||
| "sourceName": "Company.AnalyzerTemplate", | ||
| "preferNameDirectory": true, | ||
| "primaryOutputs": [ | ||
| { | ||
| "path": "Company.AnalyzerTemplate.csproj" | ||
| } | ||
| ], | ||
| "symbols": { | ||
| "$schema": "http://json.schemastore.org/template", | ||
| "author": "Microsoft", | ||
| "classifications": [ | ||
| "Common", | ||
| "Library" | ||
| ], | ||
| "name": "MSBuild custom analyzer skeleton project.", | ||
| "generatorVersions": "[1.0.0.0-*)", | ||
| "description": "A project for creating a MSBuild analyzer library that targets .NET Standard", | ||
| "groupIdentity": "Microsoft.AnalyzerTemplate", | ||
| "identity": "Microsoft.AnalyzerTemplate", | ||
| "shortName": "msbuildanalyzer", | ||
| "tags": { | ||
| "language": "C#", | ||
| "type": "project" | ||
| }, | ||
| "sourceName": "Company.AnalyzerTemplate", | ||
| "preferNameDirectory": true, | ||
| "primaryOutputs": [ | ||
| { | ||
| "path": "Company.AnalyzerTemplate.csproj" | ||
| } | ||
| ], | ||
| "symbols": { | ||
| "MicrosoftBuildVersion": { | ||
| "type": "parameter", | ||
| "description": "Overrides the default Microsoft.Build version where analyzer's interfaces are placed", | ||
| "datatype": "text", | ||
| "defaultValue": "17.11.0", | ||
| "defaultValue": "17.12.0", | ||
| "replaces": "1.0.0-MicrosoftBuildPackageVersion", | ||
| "displayName": "Microsoft.Build default package version override" | ||
| } | ||
| }, | ||
| "postActions": [ | ||
| { | ||
| "id": "restore", | ||
| "condition": "(!skipRestore)", | ||
| "description": "Restore NuGet packages required by this project.", | ||
| "manualInstructions": [ | ||
| { | ||
| "text": "Run 'dotnet restore'" | ||
| } | ||
| ], | ||
| "actionId": "210D431B-A78B-4D2F-B762-4ED3E3EA9025", | ||
| "continueOnError": true | ||
| } | ||
| ] | ||
| } | ||
| }, | ||
| "postActions": [ | ||
| { | ||
| "id": "restore", | ||
| "condition": "(!skipRestore)", | ||
| "description": "Restore NuGet packages required by this project.", | ||
| "manualInstructions": [ | ||
| { | ||
| "text": "Run 'dotnet restore'" | ||
| } | ||
| ], | ||
| "actionId": "210D431B-A78B-4D2F-B762-4ED3E3EA9025", | ||
| "continueOnError": true | ||
| } | ||
| ] | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.