Skip to content

Commit 9be4cd4

Browse files
committed
further improvements
1 parent 9340e15 commit 9be4cd4

File tree

1 file changed

+121
-23
lines changed

1 file changed

+121
-23
lines changed

.github/workflows/trademark-cla-approval.yml

Lines changed: 121 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ permissions: write-all
1717
jobs:
1818
process-cla-approval:
1919
runs-on: ubuntu-latest
20-
if: (github.event_name == 'workflow_dispatch' || github.event.label.name == 'cla-signed' || github.event_name == 'issue_comment') && !contains(github.actor, 'workflow-authentication-public')
20+
if: |
21+
(
22+
github.event_name == 'workflow_dispatch' ||
23+
(github.event_name == 'pull_request_target' && github.event.label.name == 'cla-signed') ||
24+
github.event_name == 'issue_comment'
25+
) && github.actor != 'workflow-authentication-public[bot]'
2126
2227
steps:
2328

@@ -252,38 +257,66 @@ jobs:
252257
- name: Record manual CLA approval
253258
if: success() && steps.process-cla-approval.outputs.pr_number != ''
254259
run: |
260+
set -e # Exit on any error
261+
255262
echo "=== DEBUG: Record manual CLA approval step starting ==="
256263
echo "Available outputs:"
257264
echo " pr_number: '${{ steps.process-cla-approval.outputs.pr_number }}'"
258265
echo " pr_author: '${{ steps.process-cla-approval.outputs.pr_author }}'"
259266
echo " approved_by: '${{ steps.process-cla-approval.outputs.approved_by }}'"
260267
261-
# Ensure signatures file exists
262-
if [ ! -f "cla-signatures.json" ]; then
263-
echo '{"signatures": []}' > cla-signatures.json
264-
echo "Created new cla-signatures.json file"
265-
else
266-
echo "cla-signatures.json already exists"
267-
fi
268-
269-
# Extract approval details from previous step outputs
268+
# Extract and validate approval details from previous step outputs
270269
USERNAME="${{ steps.process-cla-approval.outputs.pr_author }}"
271270
DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
272271
PR_NUMBER="${{ steps.process-cla-approval.outputs.pr_number }}"
273272
APPROVED_BY="${{ steps.process-cla-approval.outputs.approved_by }}"
274273
274+
# Validate required fields
275+
if [ -z "$USERNAME" ] || [ -z "$PR_NUMBER" ] || [ -z "$APPROVED_BY" ]; then
276+
echo "ERROR: Missing required fields for signature recording"
277+
echo " USERNAME: '$USERNAME'"
278+
echo " PR_NUMBER: '$PR_NUMBER'"
279+
echo " APPROVED_BY: '$APPROVED_BY'"
280+
exit 1
281+
fi
282+
275283
echo "Recording manual trademark addendum approval:"
276284
echo " Username: $USERNAME"
277285
echo " PR Number: $PR_NUMBER"
278286
echo " Approved by: $APPROVED_BY"
279287
echo " Date: $DATE"
280288
281-
# Check if this user already has a signature for this PR
282-
EXISTING_SIGNATURE=$(jq --arg user "$USERNAME" --arg pr "$PR_NUMBER" '.signatures[] | select(.username == $user and .pr_number == ($pr | tonumber))' cla-signatures.json)
289+
# Ensure contribute directory exists
290+
mkdir -p contribute
291+
292+
# Ensure signatures file exists and is valid JSON
293+
SIGNATURES_FILE="contribute/trade-addendum-signatures.json"
294+
if [ ! -f "$SIGNATURES_FILE" ]; then
295+
echo '{"signatures": []}' > "$SIGNATURES_FILE"
296+
echo "Created new $SIGNATURES_FILE file"
297+
else
298+
echo "$SIGNATURES_FILE already exists"
299+
# Validate existing JSON
300+
if ! jq empty "$SIGNATURES_FILE" 2>/dev/null; then
301+
echo "WARNING: Existing $SIGNATURES_FILE is invalid JSON, recreating..."
302+
cp "$SIGNATURES_FILE" "contribute/trade-addendum-signatures-backup-$(date +%s).json" || true
303+
echo '{"signatures": []}' > "$SIGNATURES_FILE"
304+
fi
305+
fi
306+
307+
# Check if this user already has a signature for this PR with error handling
308+
EXISTING_SIGNATURE=""
309+
if EXISTING_SIGNATURE=$(jq --arg user "$USERNAME" --arg pr "$PR_NUMBER" '.signatures[]? | select(.username == $user and .pr_number == ($pr | tonumber))' "$SIGNATURES_FILE" 2>/dev/null); then
310+
echo "Successfully checked for existing signature"
311+
else
312+
echo "WARNING: Error checking for existing signature, assuming none exists"
313+
EXISTING_SIGNATURE=""
314+
fi
283315
284316
if [ -z "$EXISTING_SIGNATURE" ]; then
285-
# Add new signature entry
286-
jq --arg user "$USERNAME" \
317+
# Add new signature entry with robust error handling
318+
echo "Adding new signature entry..."
319+
if jq --arg user "$USERNAME" \
287320
--arg date "$DATE" \
288321
--arg pr "$PR_NUMBER" \
289322
--arg approved_by "$APPROVED_BY" \
@@ -292,23 +325,88 @@ jobs:
292325
"date": $date,
293326
"pr_number": ($pr | tonumber),
294327
"approved_by": $approved_by
295-
}]' cla-signatures.json > tmp.json && mv tmp.json cla-signatures.json
328+
}]' "$SIGNATURES_FILE" > tmp.json 2>/dev/null; then
329+
mv tmp.json "$SIGNATURES_FILE"
330+
echo "✅ New trademark addendum signature added successfully"
331+
else
332+
echo "ERROR: Failed to add signature with jq, using fallback method"
333+
# Fallback: manually construct JSON entry
334+
NEW_ENTRY="{\"username\": \"$USERNAME\", \"date\": \"$DATE\", \"pr_number\": $PR_NUMBER, \"approved_by\": \"$APPROVED_BY\"}"
335+
if [ -s "$SIGNATURES_FILE" ]; then
336+
# File has content, merge new entry
337+
jq --argjson newEntry "$NEW_ENTRY" '.signatures += [$newEntry]' "$SIGNATURES_FILE" > tmp.json || {
338+
echo "Fallback failed, creating new signatures file with entry"
339+
echo "{\"signatures\": [$NEW_ENTRY]}" > "$SIGNATURES_FILE"
340+
}
341+
if [ -f tmp.json ]; then mv tmp.json "$SIGNATURES_FILE"; fi
342+
else
343+
echo "{\"signatures\": [$NEW_ENTRY]}" > "$SIGNATURES_FILE"
344+
fi
345+
echo "✅ Signature added using fallback method"
346+
fi
347+
else
348+
echo "ℹ️ Signature already exists for this user and PR"
349+
fi
296350
297-
echo "New trademark adendum signature added"
351+
# Verify the signature was recorded
352+
echo "=== Verifying signature was recorded ==="
353+
if jq --arg user "$USERNAME" --arg pr "$PR_NUMBER" '.signatures[] | select(.username == $user and .pr_number == ($pr | tonumber))' "$SIGNATURES_FILE" >/dev/null 2>&1; then
354+
echo "✅ VERIFICATION PASSED: Signature found in $SIGNATURES_FILE"
298355
else
299-
echo "Signature already exists for this user and PR"
356+
echo "❌ VERIFICATION FAILED: Signature NOT found in $SIGNATURES_FILE"
357+
echo "Current signatures file content:"
358+
cat "$SIGNATURES_FILE" || echo "Failed to read signatures file"
359+
exit 1
300360
fi
301361
302-
# Commit the updated file
362+
# Commit the updated file with enhanced error handling
363+
echo "=== Committing signature file ==="
303364
git config user.name "github-actions[bot]"
304365
git config user.email "github-actions[bot]@users.noreply.github.com"
305366
306367
# Configure git to use the token for authentication
307368
TOKEN="${{ steps.generate-token.outputs.token || secrets.GITHUB_TOKEN }}"
308-
git remote set-url origin "https://x-access-token:${TOKEN}@github.com/${{ github.repository }}.git"
369+
if [ -n "$TOKEN" ]; then
370+
git remote set-url origin "https://x-access-token:${TOKEN}@github.com/${{ github.repository }}.git"
371+
else
372+
echo "WARNING: No authentication token available"
373+
fi
309374
310-
git add cla-signatures.json
311-
git commit -m "Add manual approval for @$USERNAME (PR #$PR_NUMBER) by @$APPROVED_BY" || echo "No changes to commit"
312-
git push
375+
# Check if there are changes to commit
376+
if git diff --quiet "$SIGNATURES_FILE"; then
377+
echo "ℹ️ No changes to commit in $SIGNATURES_FILE"
378+
else
379+
git add "$SIGNATURES_FILE"
380+
if git commit -m "Add trademark addendum signature for @$USERNAME (PR #$PR_NUMBER) by @$APPROVED_BY
381+
382+
This signature was recorded automatically by the CLA approval workflow.
383+
384+
Details:
385+
- PR Number: #$PR_NUMBER
386+
- PR Author: @$USERNAME
387+
- Approved By: @$APPROVED_BY
388+
- Date: $DATE
389+
- Event: ${{ github.event_name }}"; then
390+
echo "✅ Successfully committed signature file"
391+
392+
# Attempt to push with retry logic
393+
for i in {1..3}; do
394+
if git push; then
395+
echo "✅ Successfully pushed signature file (attempt $i)"
396+
break
397+
elif [ $i -eq 3 ]; then
398+
echo "❌ Failed to push after 3 attempts"
399+
echo "Signature was recorded locally but failed to push to remote"
400+
exit 1
401+
else
402+
echo "⚠️ Push attempt $i failed, retrying in 5 seconds..."
403+
sleep 5
404+
fi
405+
done
406+
else
407+
echo "❌ Failed to commit signature file"
408+
exit 1
409+
fi
410+
fi
313411
314-
echo "Manual trademark addendum approval recorded successfully"
412+
echo "Manual trademark addendum approval recorded successfully"

0 commit comments

Comments
 (0)