Skip to content

Commit 9c8772a

Browse files
committed
Fix checkout of untrusted code in a privileged context
1 parent 6344512 commit 9c8772a

File tree

1 file changed

+56
-10
lines changed

1 file changed

+56
-10
lines changed

.github/workflows/calculate-size-delta.yml

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ on:
77

88
permissions:
99
contents: read
10-
pull-requests: write
1110

1211
jobs:
1312
build:
@@ -37,18 +36,42 @@ jobs:
3736
run: |
3837
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
3938
echo "Searching for PR from branch '${{ github.ref_name }}'..."
40-
PR_NUMBER=$(gh pr list --state open --head "${{ github.ref_name }}" --json number --jq '.[0].number // empty')
39+
UPSTREAM_REPO=$(gh api repos/${{ github.repository }} --jq '.parent.full_name // empty')
40+
41+
if [ -n "$UPSTREAM_REPO" ]; then
42+
echo "This is a fork. Upstream repository: $UPSTREAM_REPO"
43+
# Get current repo owner and branch
44+
CURRENT_USER=$(gh api repos/${{ github.repository }} --jq '.owner.login')
45+
BRANCH_NAME="${{ github.ref_name }}"
46+
echo "Searching in upstream for PR from $CURRENT_USER:$BRANCH_NAME"
47+
48+
# Use API to search for PR with matching head
49+
PR_NUMBER=$(gh api "repos/$UPSTREAM_REPO/pulls?state=open&head=$CURRENT_USER:$BRANCH_NAME" --jq '.[0].number // empty')
50+
51+
if [ -z "$PR_NUMBER" ]; then
52+
echo "Not found with API, trying gh pr list..."
53+
PR_NUMBER=$(gh pr list --repo "$UPSTREAM_REPO" --state open --json number,headRefName,headRepositoryOwner \
54+
--jq ".[] | select(.headRefName == \"$BRANCH_NAME\" and .headRepositoryOwner.login == \"$CURRENT_USER\") | .number")
55+
fi
56+
TARGET_REPO="$UPSTREAM_REPO"
57+
else
58+
echo "This is not a fork. Searching in current repo..."
59+
PR_NUMBER=$(gh pr list --state open --head "${{ github.ref_name }}" --json number --jq '.[0].number // empty')
60+
TARGET_REPO="${{ github.repository }}"
61+
fi
4162
else
4263
# For issue_comment, the PR number is in the event context
4364
PR_NUMBER=${{ github.event.issue.number }}
65+
TARGET_REPO="${{ github.repository }}"
4466
fi
4567
4668
if [ -z "$PR_NUMBER" ]; then
4769
echo "Could not find an associated open pull request."
4870
else
49-
echo "Found PR #$PR_NUMBER"
71+
echo "Found PR #$PR_NUMBER in repo $TARGET_REPO"
5072
fi
5173
echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT
74+
echo "target_repo=$TARGET_REPO" >> $GITHUB_OUTPUT
5275
5376
- name: Checkout PR Branch (for comment trigger)
5477
if: github.event_name == 'issue_comment'
@@ -109,6 +132,14 @@ jobs:
109132
docker image pull --platform linux/arm64 localhost:5000/app-bricks/python-base:latest
110133
docker image pull --platform linux/arm64 localhost:5000/app-bricks/python-apps-base:latest
111134
135+
- name: Calculate image sizes
136+
id: sizes
137+
run: |
138+
SIZE1=$(docker images 'localhost:5000/app-bricks/python-base:latest' --format '{{.Size}}')
139+
SIZE2=$(docker images 'localhost:5000/app-bricks/python-apps-base:latest' --format '{{.Size}}')
140+
echo "python_base_size=$SIZE1" >> $GITHUB_OUTPUT
141+
echo "python_apps_base_size=$SIZE2" >> $GITHUB_OUTPUT
142+
112143
- name: Add image sizes to Job Summary
113144
run: |
114145
echo "## Docker Image Sizes" >> $GITHUB_STEP_SUMMARY
@@ -117,19 +148,34 @@ jobs:
117148
echo "|-------|------|" >> $GITHUB_STEP_SUMMARY
118149
echo "| app-bricks/python-base | $(docker images 'localhost:5000/app-bricks/python-base:latest' --format '{{.Size}}') |" >> $GITHUB_STEP_SUMMARY
119150
echo "| app-bricks/python-apps-base | $(docker images 'localhost:5000/app-bricks/python-apps-base:latest' --format '{{.Size}}') |" >> $GITHUB_STEP_SUMMARY
120-
151+
outputs:
152+
python_base_size: ${{ steps.sizes.outputs.python_base_size }}
153+
python_apps_base_size: ${{ steps.sizes.outputs.python_apps_base_size }}
154+
pr_number: ${{ steps.pr_info.outputs.pr_number }}
155+
target_repo: ${{ steps.pr_info.outputs.target_repo }}
156+
comment-results:
157+
runs-on: ubuntu-latest
158+
needs: build
159+
if: needs.build.outputs.pr_number != ''
160+
permissions:
161+
pull-requests: write
162+
steps:
121163
- name: Comment on PR with image sizes
122-
if: steps.pr_info.outputs.pr_number != ''
123164
env:
124165
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
125166
run: |
126-
SIZE1=$(docker images 'localhost:5000/app-bricks/python-base:latest' --format '{{.Size}}')
127-
SIZE2=$(docker images 'localhost:5000/app-bricks/python-apps-base:latest' --format '{{.Size}}')
128-
gh pr comment ${{ steps.pr_info.outputs.pr_number }} --body-file - <<EOF
167+
if gh pr comment ${{ needs.build.outputs.pr_number }} --repo "${{ needs.build.outputs.target_repo }}" --body-file - <<EOF
129168
## Docker Image Sizes
130169
131170
| Image | Size |
132171
|-------|------|
133-
| app-bricks/python-base | $SIZE1 |
134-
| app-bricks/python-apps-base | $SIZE2 |
172+
| app-bricks/python-base | ${{ needs.build.outputs.python_base_size }} |
173+
| app-bricks/python-apps-base | ${{ needs.build.outputs.python_apps_base_size }} |
135174
EOF
175+
then
176+
echo "✅ Comment posted successfully"
177+
else
178+
echo "⚠️ Could not post comment (likely permission issue for cross-repo commenting)"
179+
echo "Image sizes are available in the job summary of the build-images job"
180+
exit 0
181+
fi

0 commit comments

Comments
 (0)