Skip to content

Commit 1e7c169

Browse files
authored
[CI] Prevent previous version upgrade sequence mutation (#20075)
# Introduction Prevent any PR to target a previous already released twenty version by mistake. Especially useful for existing opened PR introducing commands into an upgrade that has just been released leading to a `TWENTY_CURRENT_VERSION` bump <img width="3150" height="1158" alt="image" src="https://github.com/user-attachments/assets/b83d211f-a061-4d63-ae7a-354d7851ec08" /> ## Bypass If intentional add `ci:allow-previous-version-upgrade-mutation` label to the PR and re-run the failed job <img width="3150" height="1158" alt="image" src="https://github.com/user-attachments/assets/f94ee630-d87b-4477-9e50-bf6773a8a280" /> This will require a brand new ci from a commit introduced after the label has been added
1 parent 577312f commit 1e7c169

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

.github/workflows/ci-server.yaml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,83 @@ jobs:
7878
tag: scope:backend
7979
tasks: lint,typecheck
8080

81+
server-previous-version-upgrade-mutation-guard:
82+
timeout-minutes: 5
83+
runs-on: ubuntu-latest
84+
steps:
85+
- name: Fetch custom Github Actions and base branch history
86+
uses: actions/checkout@v4
87+
with:
88+
fetch-depth: 10
89+
- name: Get changed upgrade-version-command files
90+
id: changed-files
91+
uses: tj-actions/changed-files@v45
92+
with:
93+
files: |
94+
packages/twenty-server/src/database/commands/upgrade-version-command/**
95+
- name: Check upgrade version commands are in current version only
96+
if: >
97+
steps.changed-files.outputs.any_changed == 'true' &&
98+
!contains(github.event.pull_request.labels.*.name, 'ci:allow-previous-version-upgrade-mutation')
99+
run: |
100+
VERSION_CONSTANT_FILE="packages/twenty-server/src/engine/core-modules/upgrade/constants/twenty-current-version.constant.ts"
101+
102+
CURRENT_VERSION=$(sed -n "s/.*TWENTY_CURRENT_VERSION = '\([0-9.]*\)'.*/\1/p" "$VERSION_CONSTANT_FILE")
103+
104+
if [ -z "$CURRENT_VERSION" ]; then
105+
echo "::error::Could not extract TWENTY_CURRENT_VERSION from $VERSION_CONSTANT_FILE"
106+
exit 1
107+
fi
108+
109+
CURRENT_DIR=$(echo "$CURRENT_VERSION" | sed -E 's/^([0-9]+)\.([0-9]+)\..*/\1-\2/')
110+
111+
echo "Current version: $CURRENT_VERSION (directory: $CURRENT_DIR)"
112+
113+
ADDED_OFFENDERS=""
114+
MODIFIED_OFFENDERS=""
115+
116+
check_files() {
117+
local category="$1"
118+
shift
119+
for file in "$@"; do
120+
VERSION_DIR=$(echo "$file" | sed -n 's|.*upgrade-version-command/\([0-9]*-[0-9]*\)/.*|\1|p')
121+
122+
if [ -n "$VERSION_DIR" ] && [ "$VERSION_DIR" != "$CURRENT_DIR" ]; then
123+
if [ "$category" = "added" ]; then
124+
ADDED_OFFENDERS="$ADDED_OFFENDERS\n - $file (version directory: $VERSION_DIR)"
125+
else
126+
MODIFIED_OFFENDERS="$MODIFIED_OFFENDERS\n - $file (version directory: $VERSION_DIR)"
127+
fi
128+
fi
129+
done
130+
}
131+
132+
check_files "added" ${{ steps.changed-files.outputs.added_files }}
133+
check_files "modified" ${{ steps.changed-files.outputs.modified_files }}
134+
135+
if [ -n "$ADDED_OFFENDERS" ] || [ -n "$MODIFIED_OFFENDERS" ]; then
136+
echo "This PR touches upgrade command files outside the current version directory ($CURRENT_DIR / $CURRENT_VERSION)."
137+
138+
if [ -n "$ADDED_OFFENDERS" ]; then
139+
echo ""
140+
echo "New files added to non-current version directories:"
141+
echo -e "$ADDED_OFFENDERS"
142+
fi
143+
144+
if [ -n "$MODIFIED_OFFENDERS" ]; then
145+
echo ""
146+
echo "Existing files modified in non-current version directories:"
147+
echo -e "$MODIFIED_OFFENDERS"
148+
fi
149+
150+
echo ""
151+
echo "If this is intentional, add the label 'ci:allow-previous-version-upgrade-mutation' to this PR and re-run CI."
152+
echo "Otherwise, please move your changes to the current version directory ($CURRENT_DIR)."
153+
154+
echo "::error::Upgrade commands were added or modified in non-current version directories."
155+
exit 1
156+
fi
157+
81158
server-validation:
82159
needs: server-build
83160
timeout-minutes: 30
@@ -311,6 +388,7 @@ jobs:
311388
changed-files-check,
312389
server-build,
313390
server-lint-typecheck,
391+
server-previous-version-upgrade-mutation-guard,
314392
server-validation,
315393
server-test,
316394
server-integration-test,

0 commit comments

Comments
 (0)