forked from dotnet/msbuild
-
Notifications
You must be signed in to change notification settings - Fork 0
171 lines (142 loc) · 6.4 KB
/
SyncAnalyzerTemplateMSBuildVersion.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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.CheckTemplate/.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
});
}