Skip to content

Commit f9cac25

Browse files
authored
Bugfix/lint workflow (CloudPirates-io#364)
* Fix invalid caching in github workflow * fix invalid testing script * Fix unit tests yet again * hofixes --------- Signed-off-by: Finn Rades <64548817+zOnlyKroks@users.noreply.github.com>
1 parent ce5c550 commit f9cac25

File tree

3 files changed

+206
-172
lines changed

3 files changed

+206
-172
lines changed

.github/workflows/post-merge.yaml

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
name: "Post-Merge Changelog Update"
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
paths:
8+
- 'charts/**'
9+
10+
concurrency:
11+
group: ${{ github.workflow }}
12+
cancel-in-progress: false
13+
14+
jobs:
15+
update-changelog:
16+
runs-on: ubuntu-latest
17+
timeout-minutes: 15
18+
permissions:
19+
contents: write
20+
steps:
21+
- name: Checkout main branch
22+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
23+
with:
24+
fetch-depth: 0
25+
token: ${{ secrets.GITHUB_TOKEN }}
26+
27+
- name: Configure Git
28+
run: |
29+
git config user.name 'github-actions[bot]'
30+
git config user.email 'github-actions[bot]@users.noreply.github.com'
31+
32+
- name: Fetch all tags
33+
run: |
34+
git fetch --tags --force
35+
echo "Available tags:"
36+
git tag -l | head -20
37+
38+
- name: Install yq
39+
run: |
40+
sudo wget -qO /usr/local/bin/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64
41+
sudo chmod +x /usr/local/bin/yq
42+
43+
- name: Setup Helm
44+
uses: Azure/setup-helm@1a275c3b69536ee54be43f2070a358922e12c8d4 # v4.3.1
45+
46+
# Python is required because `ct` uses Python-based tools
47+
- name: Set up Python
48+
uses: actions/setup-python@8d9ed9ac5c53483de85588cdf95a591a75ab9f55 # v5.5.0
49+
with:
50+
python-version: 3.x
51+
52+
- name: Set up chart-testing-action
53+
uses: helm/chart-testing-action@0d28d3144d3a25ea2cc349d6e59901c4ff469b3b # v2.7.0
54+
55+
- name: Get changed charts from last commit
56+
id: list-changed
57+
run: |
58+
# Get the commit SHA before the merge
59+
BEFORE_SHA="${{ github.event.before }}"
60+
61+
# Use chart-testing to find changed charts
62+
changed=$(ct list-changed --target-branch main --since "${BEFORE_SHA}")
63+
64+
if [[ -n "$changed" ]]; then
65+
echo "Changed charts:"
66+
echo "$changed"
67+
echo "changed=true" >> $GITHUB_OUTPUT
68+
echo 'changedCharts<<EOF' >> $GITHUB_OUTPUT
69+
echo $changed >> $GITHUB_OUTPUT
70+
echo 'EOF' >> $GITHUB_OUTPUT
71+
else
72+
echo "No chart changes detected"
73+
echo "changed=false" >> $GITHUB_OUTPUT
74+
fi
75+
76+
- name: Get PR information
77+
id: pr-info
78+
if: steps.list-changed.outputs.changed == 'true'
79+
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7.1.0
80+
with:
81+
github-token: ${{ secrets.GITHUB_TOKEN }}
82+
script: |
83+
const commit = context.payload.head_commit;
84+
const commitSha = commit.id;
85+
86+
// Find the PR that was merged
87+
const { data: prs } = await github.rest.repos.listPullRequestsAssociatedWithCommit({
88+
owner: context.repo.owner,
89+
repo: context.repo.repo,
90+
commit_sha: commitSha
91+
});
92+
93+
const mergedPR = prs.find(pr => pr.merged_at);
94+
95+
if (mergedPR) {
96+
core.setOutput('pr_number', mergedPR.number);
97+
core.setOutput('pr_title', mergedPR.title);
98+
core.setOutput('pr_url', mergedPR.html_url);
99+
console.log(`Found merged PR #${mergedPR.number}: ${mergedPR.title}`);
100+
} else {
101+
console.log('No merged PR found for this commit');
102+
core.setOutput('pr_number', '');
103+
core.setOutput('pr_title', commit.message.split('\n')[0]);
104+
core.setOutput('pr_url', '');
105+
}
106+
107+
- name: Generate changelog
108+
id: generate-changelog
109+
if: steps.list-changed.outputs.changed == 'true'
110+
env:
111+
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
112+
GITHUB_REPOSITORY: "${{ github.repository }}"
113+
GITHUB_REPOSITORY_URL: "${{ github.server_url }}/${{ github.repository }}"
114+
CHANGED_CHARTS: ${{ steps.list-changed.outputs.changedCharts }}
115+
PR_NUMBER: ${{ steps.pr-info.outputs.pr_number }}
116+
PR_TITLE: ${{ steps.pr-info.outputs.pr_title }}
117+
PR_URL: ${{ steps.pr-info.outputs.pr_url }}
118+
run: |
119+
set -e
120+
121+
# Extract chart names from changed chart directories
122+
CHART_NAMES=()
123+
for chart_directory in ${CHANGED_CHARTS}; do
124+
CHART_NAME=${chart_directory#charts/}
125+
CHART_NAMES+=("--chart" "$CHART_NAME")
126+
done
127+
128+
# Build arguments for the changelog script
129+
CHANGELOG_ARGS=("${CHART_NAMES[@]}")
130+
131+
if [[ -n "$PR_TITLE" ]]; then
132+
CHANGELOG_ARGS+=("--pr-title" "${PR_TITLE}")
133+
fi
134+
135+
if [[ -n "$PR_NUMBER" ]]; then
136+
CHANGELOG_ARGS+=("--pr-number" "${PR_NUMBER}")
137+
fi
138+
139+
if [[ -n "$PR_URL" ]]; then
140+
CHANGELOG_ARGS+=("--pr-url" "${PR_URL}")
141+
fi
142+
143+
# Run the changelog generation script
144+
./generate-changelog.sh "${CHANGELOG_ARGS[@]}"
145+
146+
# Check if there are changes
147+
if git status --porcelain | grep -q 'CHANGELOG.md'; then
148+
echo "has_changes=true" >> $GITHUB_OUTPUT
149+
echo "Changelog changes detected"
150+
else
151+
echo "No CHANGELOG changes"
152+
echo "has_changes=false" >> $GITHUB_OUTPUT
153+
fi
154+
155+
- name: Commit and push changelog updates
156+
if: steps.generate-changelog.outputs.has_changes == 'true'
157+
run: |
158+
git add charts/*/CHANGELOG.md
159+
git commit -m "chore: update CHANGELOG.md for merged changes" \
160+
-m "Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>"
161+
git push origin main

.github/workflows/pull-request.yaml

Lines changed: 0 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -139,172 +139,3 @@ jobs:
139139
./test-charts.sh "$CHART_NAME" --no-cleanup
140140
done
141141
142-
update-changelog:
143-
runs-on: ubuntu-latest
144-
timeout-minutes: 15
145-
needs: [lint-test]
146-
name: Automatically update CHANGELOG
147-
permissions:
148-
contents: write
149-
pull-requests: write
150-
if: always() && needs.lint-test.outputs.changed == 'true'
151-
steps:
152-
- name: Checkout PR branch
153-
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
154-
with:
155-
repository: ${{ github.event.pull_request.head.repo.full_name }}
156-
ref: ${{ github.event.pull_request.head.ref }}
157-
token: ${{ secrets.GITHUB_TOKEN }}
158-
fetch-depth: 0
159-
160-
- name: Configure Git
161-
run: |
162-
git config user.name 'github-actions[bot]'
163-
git config user.email 'github-actions[bot]@users.noreply.github.com'
164-
165-
- name: Add upstream remote and fetch tags
166-
run: |
167-
# Always fetch tags from the canonical upstream repository
168-
UPSTREAM_REPO="CloudPirates-io/helm-charts"
169-
echo "Fetching tags from upstream: ${UPSTREAM_REPO}"
170-
git remote add upstream https://github.com/${UPSTREAM_REPO}.git || true
171-
git fetch upstream --tags --force
172-
173-
# Also fetch tags from origin (the fork/current repo)
174-
git fetch origin --tags --force || true
175-
176-
# List all tags for debugging
177-
echo "Available tags:"
178-
git tag -l | head -20
179-
180-
- name: Install yq
181-
run: |
182-
sudo wget -qO /usr/local/bin/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64
183-
sudo chmod +x /usr/local/bin/yq
184-
185-
- name: Generate changelog and commit
186-
id: check-changes
187-
shell: bash
188-
env:
189-
PULL_REQUEST_NUMBER: "${{ github.event.pull_request.number }}"
190-
PULL_REQUEST_URL: "${{ github.server_url }}/${{ github.repository }}/pull/${{ github.event.number }}"
191-
GITHUB_TOKEN: "${{ github.token }}"
192-
GITHUB_REPOSITORY: "${{ github.repository }}"
193-
GITHUB_REPOSITORY_URL: "${{ github.server_url }}/${{ github.repository }}"
194-
CHANGED_CHARTS: ${{ needs.lint-test.outputs.changedCharts }}
195-
run: |
196-
set -e
197-
PR_TITLE="$(gh api "/repos/${GITHUB_REPOSITORY}/pulls/${PULL_REQUEST_NUMBER}" | jq -r '.title')"
198-
199-
# Extract chart names from changed chart directories
200-
CHART_NAMES=()
201-
for chart_directory in ${CHANGED_CHARTS}; do
202-
CHART_NAME=${chart_directory#charts/}
203-
CHART_NAMES+=("--chart" "$CHART_NAME")
204-
done
205-
206-
# Run the changelog generation script
207-
./generate-changelog.sh \
208-
"${CHART_NAMES[@]}" \
209-
--pr-title "${PR_TITLE}" \
210-
--pr-number "${PULL_REQUEST_NUMBER}" \
211-
--pr-url "${PULL_REQUEST_URL}"
212-
213-
# Check if there are changes
214-
if git status --porcelain | grep -q 'CHANGELOG.md'; then
215-
echo "has_changes=true" >> $GITHUB_OUTPUT
216-
else
217-
echo "No CHANGELOG changes"
218-
echo "has_changes=false" >> $GITHUB_OUTPUT
219-
fi
220-
221-
- name: Commit and push via GitHub API
222-
id: push-changes
223-
if: steps.check-changes.outputs.has_changes == 'true'
224-
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7.1.0
225-
with:
226-
github-token: ${{ secrets.GITHUB_TOKEN }}
227-
script: |
228-
const { execSync } = require('child_process');
229-
const fs = require('fs');
230-
231-
try {
232-
const headRef = '${{ github.event.pull_request.head.ref }}';
233-
const headRepo = '${{ github.event.pull_request.head.repo.full_name }}';
234-
const [owner, repo] = headRepo.split('/');
235-
236-
// Get current branch SHA
237-
const { data: refData } = await github.rest.git.getRef({
238-
owner,
239-
repo,
240-
ref: `heads/${headRef}`
241-
});
242-
const currentSha = refData.object.sha;
243-
console.log('Current branch SHA:', currentSha);
244-
245-
// Get the tree for current commit
246-
const { data: commitData } = await github.rest.git.getCommit({
247-
owner,
248-
repo,
249-
commit_sha: currentSha
250-
});
251-
const baseTreeSha = commitData.tree.sha;
252-
253-
// Get all changed CHANGELOG files
254-
const changedFiles = execSync('git status --porcelain').toString()
255-
.split('\n')
256-
.filter(line => line.includes('CHANGELOG.md'))
257-
.map(line => line.trim().split(/\s+/)[1])
258-
.filter(Boolean);
259-
260-
console.log('Changed files:', changedFiles);
261-
262-
// Create blobs for each changed file
263-
const blobs = await Promise.all(
264-
changedFiles.map(async (file) => {
265-
const content = fs.readFileSync(file, 'utf8');
266-
const { data: blob } = await github.rest.git.createBlob({
267-
owner,
268-
repo,
269-
content,
270-
encoding: 'utf-8'
271-
});
272-
return { path: file, sha: blob.sha, mode: '100644', type: 'blob' };
273-
})
274-
);
275-
276-
// Create new tree
277-
const { data: newTree } = await github.rest.git.createTree({
278-
owner,
279-
repo,
280-
base_tree: baseTreeSha,
281-
tree: blobs
282-
});
283-
284-
// Create commit (this will be automatically signed by GitHub)
285-
const { data: newCommit } = await github.rest.git.createCommit({
286-
owner,
287-
repo,
288-
message: 'chore: update CHANGELOG.md for changed charts\n\nSigned-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>',
289-
tree: newTree.sha,
290-
parents: [currentSha]
291-
});
292-
293-
console.log('Created commit:', newCommit.sha);
294-
295-
// Update reference
296-
await github.rest.git.updateRef({
297-
owner,
298-
repo,
299-
ref: `heads/${headRef}`,
300-
sha: newCommit.sha,
301-
force: false
302-
});
303-
304-
console.log('✅ Successfully committed and pushed changelog updates');
305-
core.notice('✅ Changelog updated and pushed successfully');
306-
307-
} catch (error) {
308-
console.error('Failed to commit via GitHub API:', error);
309-
core.setFailed(`Unable to push changelog updates: ${error.message}`);
310-
}

0 commit comments

Comments
 (0)