Skip to content

Commit 68d7a2c

Browse files
committed
ci: Open tool version bump PR in codacy-tools for new tool version
1 parent 198a667 commit 68d7a2c

1 file changed

Lines changed: 143 additions & 0 deletions

File tree

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# Automatically create a PR to update the codacy-trivy tool in codacy-tools when there is a new version available.
2+
#
3+
# PRs are opened by user codacybeta (https://github.com/orgs/codacy/people/codacybeta), using its AUTO_MERGE_TOKEN.
4+
name: Bump codacy-trivy version in codacy-tools
5+
6+
on:
7+
push:
8+
tags:
9+
- '[0-9]+\.[0-9]+\.[0-9]+' # matches strict semver: 1.2.3
10+
11+
# ──────────────────────────────────────────────
12+
# CONFIGURATION
13+
# ──────────────────────────────────────────────
14+
env:
15+
DEPENDENCY_NAME: "codacy/codacy-trivy"
16+
TARGET_REPO: "codacy/codacy-tools"
17+
TARGET_REPO_BASE_BRANCH: "master"
18+
TARGET_REPO_DEPENDENCIES_FILE_PATH: "docker-list.txt"
19+
20+
permissions:
21+
contents: read
22+
23+
jobs:
24+
bump-dependency:
25+
runs-on: ubuntu-latest
26+
27+
steps:
28+
# ── 1. Clone the target repo ──────────────────
29+
- name: Checkout target repo
30+
uses: actions/checkout@v4
31+
with:
32+
repository: ${{ env.TARGET_REPO }}
33+
token: ${{ secrets.AUTO_MERGE_TOKEN }}
34+
ref: ${{ env.TARGET_REPO_BASE_BRANCH }}
35+
36+
# ── 2. Create a version bump branch ────────────────
37+
- name: Create version bump branch
38+
id: branch
39+
run: |
40+
BRANCH="auto-bump/${{ env.DEPENDENCY_NAME }}-${{ github.ref_name }}"
41+
git checkout -b "$BRANCH"
42+
echo "name=$BRANCH" >> "$GITHUB_OUTPUT"
43+
44+
# ── 3. Classify semver bmp ───────────────────
45+
- name: Classify semver bump
46+
id: semver
47+
env:
48+
NEW_VERSION: ${{ github.ref_name }}
49+
DEP_FILE: ${{ env.TARGET_REPO_DEPENDENCIES_FILE_PATH }}
50+
DEP: ${{ env.DEPENDENCY_NAME }}
51+
run: |
52+
# Extract current version from the file
53+
OLD_VERSION=$(grep -oP "^${DEP}:\K[0-9]+\.[0-9]+\.[0-9]+" "$FILE" || true)
54+
55+
if [[ -z "$OLD_VERSION" ]]; then
56+
echo "❌ Could not find current version is — '$DEP' present in $DEP_FILE?"
57+
exit 1
58+
fi
59+
60+
echo "📊 $OLD_VERSION → $NEW_VERSION"
61+
62+
OLD_MAJOR=$(echo "$OLD_VERSION" | cut -d. -f1)
63+
OLD_MINOR=$(echo "$OLD_VERSION" | cut -d. -f2)
64+
NEW_MAJOR=$(echo "$NEW_VERSION" | cut -d. -f1)
65+
NEW_MINOR=$(echo "$NEW_VERSION" | cut -d. -f2)
66+
67+
if [[ "$NEW_MAJOR" -gt "$OLD_MAJOR" ]]; then
68+
SEMVER_UPDATE_TYPE="major"
69+
elif [[ "$NEW_MINOR" -gt "$OLD_MINOR" ]]; then
70+
SEMVER_UPDATE_TYPE="minor"
71+
else
72+
SEMVER_UPDATE_TYPE="hotfix"
73+
fi
74+
75+
echo "semver_update_type=$SEMVER_UPDATE_TYPE" >> "$GITHUB_OUTPUT"
76+
echo "old_version=$OLD_VERSION" >> "$GITHUB_OUTPUT"
77+
echo "🏷️ Update type: $SEMVER_UPDATE_TYPE"
78+
79+
# ── 4. Update the dependency version ──────────
80+
#
81+
# Assumes lines in docker-list.txt look like:
82+
# codacy/codacy-tool:1.2.3
83+
#
84+
- name: Bump ${{ env.DEPENDENCY_NAME }} version in ${{ env.TARGET_REPO_DEPENDENCIES_FILE_PATH }}
85+
env:
86+
NEW_VERSION: ${{ github.ref_name }}
87+
DEP_FILE: ${{ env.TARGET_REPO_DEPENDENCIES_FILE_PATH }}
88+
DEP: ${{ env.DEPENDENCY_NAME }}
89+
run: |
90+
echo "📦 Bumping $DEP to $NEW_VERSION in $DEP_FILE"
91+
92+
# Escape slashes in the dependency name for sed
93+
DEP_ESCAPED=$(echo "$DEP" | sed 's|/|\\/|g')
94+
95+
# Replace the version after the colon for the matching image
96+
sed -i -E "s|^(${DEP_ESCAPED}:)[0-9]+\.[0-9]+\.[0-9]+|\1${NEW_VERSION}|" "$DEP_FILE"
97+
98+
# Verify the change was made
99+
if git diff --quiet "$DEP_FILE"; then
100+
echo "❌ No changes made — is '$DEP' present in $DEP_FILE?"
101+
exit 1
102+
fi
103+
104+
echo ""
105+
echo "── diff ──"
106+
git diff "$DEP_FILE"
107+
108+
# ── 5. Commit & push ──────────────────────────
109+
- name: Commit and push
110+
run: |
111+
git config user.name "codacybeta"
112+
git config user.email "codacybeta@users.noreply.github.com"
113+
git add "${{ env.TARGET_REPO_DEPENDENCIES_FILE_PATH }}"
114+
git commit -m "bump: ${{ env.DEPENDENCY_NAME }} to ${{ github.ref_name }}"
115+
git push origin "${{ steps.branch.outputs.name }}"
116+
117+
# ── 6. Open the PR ──────────────────
118+
- name: Open PR
119+
env:
120+
GH_TOKEN: ${{ secrets.AUTO_MERGE_TOKEN }}
121+
OLD_VERSION: "${{ steps.semver.outputs.old_version }}"
122+
SEMVER_UPDATE_TYPE: "${{ steps.semver.outputs.label }}"
123+
run: |
124+
125+
gh pr create \
126+
--repo "${{ env.TARGET_REPO }}" \
127+
--base "${{ env.TARGET_REPO_BASE_BRANCH }}" \
128+
--head "${{ steps.branch.outputs.name }}" \
129+
--title "bump: ${{ env.DEPENDENCY_NAME }} to ${{ github.ref_name }}" \
130+
--body "$(cat <<'EOF'
131+
## Dependency version bump
132+
133+
| Field | Value |
134+
|-------|-------|
135+
| **Tool** | ${{ env.DEPENDENCY_NAME }} |
136+
| **Update** | ${OLD_VERSION} to [${{ github.ref_name }}](${{ github.server_url }}/${{ github.repository }}/releases/tag/${{ github.ref_name }}) |
137+
| **Update type** | ${SEMVER_UPDATE_TYPE} |
138+
| **Triggered by** | ${{ github.actor }} |
139+
140+
---
141+
*Opened automatically by the [codacy-tools-auto-bump](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) workflow.*
142+
EOF
143+
)"

0 commit comments

Comments
 (0)