Skip to content

Commit df4e17f

Browse files
committed
open new pr for signature storage
1 parent 5d7a7ce commit df4e17f

File tree

1 file changed

+98
-6
lines changed

1 file changed

+98
-6
lines changed

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

Lines changed: 98 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ jobs:
141141
core.setOutput('pr_number', prNumber);
142142
core.setOutput('pr_author', prAuthor);
143143
core.setOutput('approved_by', prAuthor);
144+
core.setOutput('pr_head_sha', pr.head.sha);
144145
core.setOutput('pr_head_ref', pr.head.ref);
146+
core.setOutput('pr_head_repo_full_name', pr.head.repo.full_name);
147+
core.setOutput('is_fork', pr.head.repo.full_name !== context.repo.owner + '/' + context.repo.repo);
145148
146149
// Add confirmation comment
147150
const confirmationBody = [
@@ -170,11 +173,13 @@ jobs:
170173
uses: actions/checkout@v4
171174
with:
172175
fetch-depth: 0
173-
# For issue_comment events, checkout the PR head branch
174-
ref: ${{ steps.process-comment.outputs.pr_head_ref }}
176+
# For forked PRs, we need to fetch from the fork and checkout the SHA
177+
# For non-fork PRs, we can checkout the branch directly
178+
ref: ${{ steps.process-comment.outputs.is_fork == 'true' && steps.process-comment.outputs.pr_head_sha || steps.process-comment.outputs.pr_head_ref }}
175179
token: ${{ steps.generate-token.outputs.token || secrets.GITHUB_TOKEN }}
176180

177181
- name: Record signature to file
182+
id: record-signature
178183
if: success() && steps.process-comment.outputs.pr_number != ''
179184
run: |
180185
set -e
@@ -251,10 +256,97 @@ jobs:
251256
252257
# Check if there are staged changes
253258
if ! git diff --cached --quiet; then
254-
git commit -m "Add trademark addendum signature for @$USERNAME (PR #$PR_NUMBER)" \
255-
-m "This signature was recorded automatically by the CLA approval workflow."
256-
git push
257-
echo "✅ Signature committed and pushed successfully"
259+
IS_FORK="${{ steps.process-comment.outputs.is_fork }}"
260+
261+
if [ "$IS_FORK" = "true" ]; then
262+
echo "Fork detected - will create PR with signature changes"
263+
# Create a unique branch name for the signature
264+
TIMESTAMP=$(date +%s)
265+
SIGNATURE_BRANCH="trademark-signature-$USERNAME-pr-$PR_NUMBER-$TIMESTAMP"
266+
267+
# Switch to main and create new branch
268+
git fetch origin main
269+
git checkout -b "$SIGNATURE_BRANCH" origin/main
270+
271+
# Re-apply our signature file changes to the new branch
272+
mkdir -p contribute
273+
if [ ! -f "$SIGNATURES_FILE" ]; then
274+
echo '{"signatures": []}' > "$SIGNATURES_FILE"
275+
fi
276+
277+
# Re-add the signature (check if already exists to avoid duplicates)
278+
EXISTING=$(jq --arg user "$USERNAME" --arg pr "$PR_NUMBER" '.signatures[] | select(.username == $user and .pr_number == ($pr | tonumber))' "$SIGNATURES_FILE" 2>/dev/null || echo "")
279+
if [ -z "$EXISTING" ]; then
280+
jq --arg user "$USERNAME" \
281+
--arg date "$DATE" \
282+
--arg pr "$PR_NUMBER" \
283+
--arg approved_by "$APPROVED_BY" \
284+
'.signatures += [{
285+
"username": $user,
286+
"date": $date,
287+
"pr_number": ($pr | tonumber),
288+
"approved_by": $approved_by
289+
}]' "$SIGNATURES_FILE" > tmp.json && mv tmp.json "$SIGNATURES_FILE"
290+
fi
291+
292+
git add "$SIGNATURES_FILE"
293+
git commit -m "Add trademark addendum signature for @$USERNAME (PR #$PR_NUMBER)" \
294+
-m "This signature was recorded automatically by the CLA approval workflow."
295+
296+
# Push the new branch
297+
git push origin "$SIGNATURE_BRANCH"
298+
299+
# Store branch name for next step
300+
echo "signature_branch=$SIGNATURE_BRANCH" >> $GITHUB_OUTPUT
301+
echo "✅ Signature committed to branch $SIGNATURE_BRANCH"
302+
else
303+
echo "Non-fork PR - committing to current branch"
304+
git commit -m "Add trademark addendum signature for @$USERNAME (PR #$PR_NUMBER)" \
305+
-m "This signature was recorded automatically by the CLA approval workflow."
306+
git push
307+
echo "✅ Signature committed and pushed successfully"
308+
fi
258309
else
259310
echo "ℹ️ No staged changes to commit"
260311
fi
312+
313+
- name: Create signature PR for forked contributions
314+
if: success() && steps.process-comment.outputs.is_fork == 'true' && steps.process-comment.outputs.pr_number != ''
315+
uses: actions/github-script@v7
316+
with:
317+
github-token: ${{ steps.generate-token.outputs.token || secrets.GITHUB_TOKEN }}
318+
script: |
319+
const username = '${{ steps.process-comment.outputs.pr_author }}';
320+
const prNumber = '${{ steps.process-comment.outputs.pr_number }}';
321+
const signatureBranch = '${{ steps.record-signature.outputs.signature_branch }}';
322+
323+
if (!signatureBranch) {
324+
console.log('No signature branch created, skipping PR creation');
325+
return;
326+
}
327+
328+
// Create PR for the signature
329+
const { data: signaturePr } = await github.rest.pulls.create({
330+
owner: context.repo.owner,
331+
repo: context.repo.repo,
332+
title: `Add trademark signature for @${username} (PR #${prNumber})`,
333+
head: signatureBranch,
334+
base: 'main',
335+
body: [
336+
`## Trademark License Addendum Signature`,
337+
``,
338+
`This PR automatically records the trademark license agreement signature for @${username} from PR #${prNumber}.`,
339+
``,
340+
`**Details:**`,
341+
`- **Contributor:** @${username}`,
342+
`- **Original PR:** #${prNumber}`,
343+
`- **Date:** ${new Date().toISOString()}`,
344+
`- **Method:** Self-signed agreement via comment`,
345+
``,
346+
`This signature was recorded automatically by the CLA approval workflow and should be merged to complete the signature recording process.`,
347+
``,
348+
`Related to: #${prNumber}`
349+
].join('\n')
350+
});
351+
352+
console.log(`✅ Created signature PR #${signaturePr.number} for ${username}`);

0 commit comments

Comments
 (0)