Skip to content

Commit 64ff40f

Browse files
authored
feat: don't update existing branches if there are manually added commits (#50)
1 parent 03be3f5 commit 64ff40f

File tree

10 files changed

+102
-26
lines changed

10 files changed

+102
-26
lines changed

.github/workflows/updater.yml

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,17 @@ jobs:
136136
}
137137
"baseBranch=$mainBranch" | Tee-Object $env:GITHUB_OUTPUT -Append
138138
"prBranch=$prBranch" | Tee-Object $env:GITHUB_OUTPUT -Append
139+
$nonBotCommits = ${{ runner.temp }}/ghwf/updater/scripts/nonbot-commits.ps1 `
140+
-RepoUrl "$(git config --get remote.origin.url)" -PrBranch $prBranch -MainBranch $mainBranch
141+
$changed = $nonBotCommits.Length -gt 0 ? 'true' : 'false'
142+
"changed=$changed" | Tee-Object $env:GITHUB_OUTPUT -Append
143+
if ("$changed" -eq "true")
144+
{
145+
Write-Output "::warning::Target branch '$prBranch' has been changed manually - skipping updater to avoid overwriting these changes."
146+
}
139147
140148
- name: Fetch an existing PR
141-
if: steps.target.outputs.latestTag != steps.target.outputs.originalTag
149+
if: ${{ ( steps.target.outputs.latestTag != steps.target.outputs.originalTag ) && ( steps.root.outputs.changed == 'false') }}
142150
uses: octokit/request-action@89a1754fe82ca777b044ca8e79e9881a42f15a93 # v2.1.7
143151
id: existing-pr-request
144152
with:
@@ -149,7 +157,7 @@ jobs:
149157
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
150158

151159
- name: Parse the existing PR URL
152-
if: steps.target.outputs.latestTag != steps.target.outputs.originalTag
160+
if: ${{ ( steps.target.outputs.latestTag != steps.target.outputs.originalTag ) && ( steps.root.outputs.changed == 'false') }}
153161
id: existing-pr
154162
run: |
155163
$data = @'
@@ -173,10 +181,10 @@ jobs:
173181
}
174182
175183
- run: git --no-pager diff
176-
if: ${{ ( steps.target.outputs.latestTag != steps.target.outputs.originalTag ) && ( steps.existing-pr.outputs.url == '') }}
184+
if: ${{ ( steps.target.outputs.latestTag != steps.target.outputs.originalTag ) && ( steps.existing-pr.outputs.url == '') && ( steps.root.outputs.changed == 'false') }}
177185

178186
- name: Get target changelog
179-
if: steps.target.outputs.latestTag != steps.target.outputs.originalTag
187+
if: ${{ ( steps.target.outputs.latestTag != steps.target.outputs.originalTag ) && ( steps.root.outputs.changed == 'false') }}
180188
run: |
181189
$changelog = ${{ runner.temp }}/ghwf/updater/scripts/get-changelog.ps1 `
182190
-RepoUrl '${{ steps.target.outputs.url }}' `
@@ -186,7 +194,7 @@ jobs:
186194
187195
# First we create a PR only if it doesn't exist. We will later overwrite the content with the same action.
188196
- name: Create a PR
189-
if: ${{ ( steps.target.outputs.latestTag != steps.target.outputs.originalTag ) && ( steps.existing-pr.outputs.url == '') }}
197+
if: ${{ ( steps.target.outputs.latestTag != steps.target.outputs.originalTag ) && ( steps.existing-pr.outputs.url == '') && ( steps.root.outputs.changed == 'false') }}
190198
uses: peter-evans/create-pull-request@b4d51739f96fca8047ad065eccef63442d8e99f7 # pin#v4
191199
id: create-pr
192200
with:
@@ -204,7 +212,7 @@ jobs:
204212
# draft: true
205213

206214
- name: Verify we have a PR
207-
if: steps.target.outputs.latestTag != steps.target.outputs.originalTag
215+
if: ${{ ( steps.target.outputs.latestTag != steps.target.outputs.originalTag ) && ( steps.root.outputs.changed == 'false') }}
208216
id: pr
209217
run: |
210218
if ('${{ steps.create-pr.outputs.pull-request-url }}' -ne '')
@@ -223,17 +231,17 @@ jobs:
223231
# If we had to create a new PR, we must do a clean checkout & update the submodule again.
224232
# If we didn't do this, the new PR would only have a changelog...
225233
- name: 'After new PR: restore repo'
226-
if: ${{ ( steps.target.outputs.latestTag != steps.target.outputs.originalTag ) && ( steps.existing-pr.outputs.url == '') }}
234+
if: ${{ ( steps.target.outputs.latestTag != steps.target.outputs.originalTag ) && ( steps.existing-pr.outputs.url == '') && ( steps.root.outputs.changed == 'false') }}
227235
uses: actions/checkout@v3
228236
with:
229237
ssh-key: ${{ secrets.api-token }}
230238

231239
- name: 'After new PR: redo the update'
232-
if: ${{ ( steps.target.outputs.latestTag != steps.target.outputs.originalTag ) && ( steps.existing-pr.outputs.url == '') }}
240+
if: ${{ ( steps.target.outputs.latestTag != steps.target.outputs.originalTag ) && ( steps.existing-pr.outputs.url == '') && ( steps.root.outputs.changed == 'false') }}
233241
run: ${{ runner.temp }}/ghwf/updater/scripts/update-dependency.ps1 -Path '${{ inputs.path }}' -Tag '${{ steps.target.outputs.latestTag }}'
234242

235243
- name: Update Changelog
236-
if: ${{ inputs.changelog-entry && ( steps.target.outputs.latestTag != steps.target.outputs.originalTag ) }}
244+
if: ${{ inputs.changelog-entry && ( steps.target.outputs.latestTag != steps.target.outputs.originalTag ) && ( steps.root.outputs.changed == 'false') }}
237245
run: |
238246
${{ runner.temp }}/ghwf/updater/scripts/update-changelog.ps1 `
239247
-Name '${{ inputs.name }}' `
@@ -245,11 +253,11 @@ jobs:
245253
-Section '${{ inputs.changelog-section }}'
246254
247255
- run: git --no-pager diff
248-
if: steps.target.outputs.latestTag != steps.target.outputs.originalTag
256+
if: ${{ ( steps.target.outputs.latestTag != steps.target.outputs.originalTag ) && ( steps.root.outputs.changed == 'false') }}
249257

250258
# Now make the PR in its final state. This way we only have one commit and no updates if there are no changes between runs.
251259
- name: Update the PR
252-
if: steps.target.outputs.latestTag != steps.target.outputs.originalTag
260+
if: ${{ ( steps.target.outputs.latestTag != steps.target.outputs.originalTag ) && ( steps.root.outputs.changed == 'false') }}
253261
uses: peter-evans/create-pull-request@b4d51739f96fca8047ad065eccef63442d8e99f7
254262
with:
255263
base: ${{ steps.root.outputs.baseBranch }}

CHANGELOG.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
## Unreleased
44

5-
### Fixes
6-
7-
- Updater - update deprecated actions ([#48](https://github.com/getsentry/github-workflows/pull/48))
8-
95
### Features
106

7+
- Updater - don't update existing branches if there are manually added commits ([#50](https://github.com/getsentry/github-workflows/pull/50))
118
- Danger - ignore "deps" and "test" PR flavors in changelog checks ([#49](https://github.com/getsentry/github-workflows/pull/49))
129

10+
### Fixes
11+
12+
- Updater - update deprecated actions ([#48](https://github.com/getsentry/github-workflows/pull/48))
13+
1314
## 2.4.0
1415

1516
### Features

updater/scripts/get-changelog.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ if (-not ($RepoUrl -match "^$prefix"))
1414
}
1515

1616
$tmpDir = Join-Path ([System.IO.Path]::GetTempPath()) ([System.Guid]::NewGuid())
17+
New-Item -ItemType Directory $tmpDir | Out-Null
1718

1819
try
1920
{

updater/scripts/nonbot-commits.ps1

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Finds commits in a branch that weren't made by <[email protected]>
2+
param(
3+
[Parameter(Mandatory = $true)][string] $RepoUrl,
4+
[Parameter(Mandatory = $true)][string] $PrBranch,
5+
[Parameter(Mandatory = $true)][string] $MainBranch
6+
)
7+
8+
Set-StrictMode -Version latest
9+
$ErrorActionPreference = "Stop"
10+
11+
12+
$tmpDir = Join-Path ([System.IO.Path]::GetTempPath()) ([System.Guid]::NewGuid())
13+
14+
New-Item -ItemType Directory $tmpDir | Out-Null
15+
Push-Location $tmpDir
16+
try
17+
{
18+
git init | Out-Null
19+
git remote add origin $RepoUrl | Out-Host
20+
git fetch --depth 1 origin $MainBranch | Out-Host
21+
git fetch --depth 1 origin $PrBranch | Out-Host
22+
23+
#Note: we're intentionally ignoring exit codes from git, to make sure we don't fail if the branch doesn't exist.
24+
$allCommits = @(git log --oneline "origin/$MainBranch..origin/$PrBranch")
25+
$botCommits = @(git log --oneline "origin/$MainBranch..origin/$PrBranch" --author=$bot)
26+
$nonbotCommits = @(Compare-Object -ReferenceObject $allCommits -DifferenceObject $botCommits -PassThru)
27+
28+
if ($nonbotCommits.Length -gt 0)
29+
{
30+
Write-Warning "There are commits made by others than $bot"
31+
}
32+
$nonbotCommits
33+
}
34+
finally
35+
{
36+
Pop-Location
37+
Write-Host "Removing $tmpDir"
38+
Remove-Item -Recurse -Force -ErrorAction Continue -Path $tmpDir
39+
}

updater/tests/common/test-utils.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
Set-StrictMode -Version latest
2+
$ErrorActionPreference = "Stop"
3+
14
function RunTest ([string] $name, [ScriptBlock] $code, [string] $skipReason = "")
25
{
36
if ($skipReason -ne "")

updater/tests/get-changelog.ps1

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Set-StrictMode -Version latest
2-
31
. "$PSScriptRoot/common/test-utils.ps1"
42

53
RunTest "get-changelog with existing versions" {

updater/tests/nonbot-commits.ps1

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Set-StrictMode -Version latest
2+
3+
. "$PSScriptRoot/common/test-utils.ps1"
4+
5+
function NonBotCommits([Parameter(Mandatory = $true)][string] $branch)
6+
{
7+
$result = & "$PSScriptRoot/../scripts/nonbot-commits.ps1" `
8+
-RepoUrl 'https://github.com/getsentry/github-workflows' -MainBranch "main" -PrBranch $branch
9+
if (-not $?)
10+
{
11+
throw $result
12+
}
13+
$result
14+
}
15+
16+
RunTest "empty-if-all-commits-by-bot" {
17+
$commits = NonBotCommits 'deps/updater/tests/sentry-cli.properties'
18+
AssertEqual "$commits" ""
19+
}
20+
21+
RunTest "empty-if-branch-doesnt-exist" {
22+
$commits = NonBotCommits 'non-existent-branch'
23+
AssertEqual "$commits" ""
24+
}
25+
26+
RunTest "non-empty-if-changed" {
27+
$commits = NonBotCommits 'test/nonbot-commits'
28+
AssertEqual "$commits" "6133a25 Update README.md"
29+
}

updater/tests/sort-versions.ps1

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Set-StrictMode -Version latest
2-
31
. "$PSScriptRoot/common/test-utils.ps1"
42

53
function SortVersions([Parameter(Mandatory = $true)][string[]] $List)

updater/tests/update-changelog.ps1

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Set-StrictMode -Version latest
2-
31
. "$PSScriptRoot/common/test-utils.ps1"
42

53
$testCases = Get-ChildItem "$PSScriptRoot/testdata/changelog/"

updater/tests/update-dependency.ps1

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Set-StrictMode -Version latest
2-
31
. "$PSScriptRoot/common/test-utils.ps1"
42

53
function UpdateDependency([Parameter(Mandatory = $true)][string] $path, [string] $pattern = $null)
@@ -63,7 +61,7 @@ RunTest "writes to env:GITHUB_OUTPUT" {
6361
$repo = 'https://github.com/getsentry/sentry-cli'
6462
@("repo=$repo", "version=0") | Out-File $testFile
6563
$outFile = "$testDir/outfile"
66-
New-Item $outFile -ItemType File
64+
New-Item $outFile -ItemType File | Out-Null
6765
try
6866
{
6967
$env:GITHUB_OUTPUT = $outFile
@@ -76,8 +74,8 @@ RunTest "writes to env:GITHUB_OUTPUT" {
7674
finally
7775
{
7876
# Delete the file and unser the env variable
79-
Remove-Item $outFile
80-
Remove-Item env:GITHUB_OUTPUT
77+
Remove-Item $outFile | Out-Null
78+
Remove-Item env:GITHUB_OUTPUT | Out-Null
8179
}
8280
}
8381

@@ -117,6 +115,7 @@ RunTest "bash-script" {
117115
$testScript = "$testDir/test.sh"
118116
@'
119117
#!/usr/bin/env bash
118+
set -euo pipefail
120119
cd $(dirname "$0")
121120
case $1 in
122121
get-version)
@@ -176,6 +175,7 @@ RunTest "bash-script fails in get-repo" {
176175
$testScript = "$testDir/test.sh"
177176
@'
178177
#!/usr/bin/env bash
178+
set -euo pipefail
179179
case $1 in
180180
get-version)
181181
;;
@@ -210,6 +210,7 @@ RunTest "bash-script fails in set-version" {
210210
$testScript = "$testDir/test.sh"
211211
@'
212212
#!/usr/bin/env bash
213+
set -euo pipefail
213214
cd $(dirname "$0")
214215
case $1 in
215216
get-version)

0 commit comments

Comments
 (0)