Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: Securely allow fork repo PRs and unittest coverage #156

Merged
merged 36 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
38c1688
ok to test
alperenkose Feb 12, 2024
e029027
ok to test
alperenkose Feb 12, 2024
0fcd596
ok to test
alperenkose Feb 12, 2024
4cd9b41
ok to test
alperenkose Feb 13, 2024
eb02d76
ok to test
alperenkose Feb 13, 2024
27aba7a
ok to test
alperenkose Feb 13, 2024
62325cd
ok to test
alperenkose Feb 13, 2024
5501b69
ok to test
alperenkose Feb 13, 2024
353dbc1
seperate workflow_run
alperenkose Feb 13, 2024
bd3341c
seperate workflow_run
alperenkose Feb 13, 2024
2d41c6a
seperate workflow_run
alperenkose Feb 13, 2024
7619340
download from other workflow_run
alperenkose Feb 13, 2024
1913dd7
upload/download artifact@v4
alperenkose Feb 13, 2024
6d278ca
debug artifacts
alperenkose Feb 13, 2024
ef8bd6b
debug artifacts
alperenkose Feb 13, 2024
32e95f4
coverage comment
alperenkose Feb 13, 2024
d597b3b
upload coverage seperately
alperenkose Feb 13, 2024
c0079bc
coverage comment changed only
alperenkose Feb 13, 2024
d635c65
post pr run-name
alperenkose Feb 14, 2024
296c252
post pr check
alperenkose Feb 14, 2024
5c2caab
temp disable pan.dev PRs
alperenkose Feb 14, 2024
98c08c6
github script debug
alperenkose Feb 14, 2024
da052cc
debug workflow run
alperenkose Feb 14, 2024
86e5516
github script debug
alperenkose Feb 14, 2024
9452f02
post pr name
alperenkose Feb 14, 2024
b6488ea
create check from workflow run
alperenkose Feb 14, 2024
683f87c
create check from workflow run
alperenkose Feb 14, 2024
5ad4129
create check from workflow run
alperenkose Feb 14, 2024
cd131a8
code clean up
alperenkose Feb 14, 2024
9db0b24
docker packages permissions
alperenkose Feb 14, 2024
e853987
change coverage action
alperenkose Feb 15, 2024
23e5b34
change coverage action
alperenkose Feb 15, 2024
a1468e1
pyproject.toml coverage config
alperenkose Feb 15, 2024
d1b381b
coverage action permissions
alperenkose Feb 15, 2024
a6f674e
clean up
alperenkose Feb 15, 2024
d5a9d6f
close PR securely
alperenkose Feb 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/_docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ jobs:
build:
name: Build ${{ inputs.publish && 'and publish' || '' }} a Docker image
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
# packages write should be needed for publish only - set permissions accordingly from calling workflow
# permissions:
# packages: write
# contents: read
steps:
- name: checkout code
uses: actions/checkout@v4
Expand Down
39 changes: 10 additions & 29 deletions .github/workflows/close_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,16 @@ on:
types: ['closed']

jobs:
close:
name: Close PAN.DEV preview PR
store_pr_details:
name: Upload PR details to artifact for closing
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- uses: actions/github-script@v6
- name: save PR details
run: |
mkdir -p ./pr
echo ${{ github.event.number }} > ./pr/NR
echo ${{ github.event.pull_request.head.ref }} > ./pr/HEAD_REF
- uses: actions/upload-artifact@v4
with:
result-encoding: string
github-token: ${{ secrets.CLSC_PAT }}
script: |
let prs = await github.rest.pulls.list({
owner: "PaloAltoNetworks",
repo: "pan.dev",
state: "open",
})

let prs_list = prs.data

for (let pr of prs_list){
if (pr.head.label == "PaloAltoNetworks:pua_prev_${{ github.event.pull_request.head.ref }}"){
await github.rest.pulls.update({
owner: "PaloAltoNetworks",
repo: "pan.dev",
pull_number: pr.number,
state: "closed",
})
console.log("Closing related PAN.DEV PR: #" + pr.number + " - " + pr.title + " -> " + pr.url)
break
}
}
name: pr
path: pr/
66 changes: 66 additions & 0 deletions .github/workflows/close_pr_workflow_run.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Post Close PR Workflow Run
run-name: "Post ${{ github.event.workflow_run.display_title }}"
# This workflow is to be triggerred after Close PR workflow is completed to get access to secrets for fork repo PRs securely.

# we always expect a single PR to trigger this workflow_run - take care of event filtering below to match a single workflow
on:
workflow_run:
workflows: ["Close PR"]
types:
- completed

permissions:
contents: write
pull-requests: write
actions: read

jobs:
close_pr:
name: Close PAN.DEV preview PR
runs-on: ubuntu-latest
if: >
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'

steps:
- name: download PR artifact
uses: actions/download-artifact@v4
with:
name: pr
path: pr
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ github.event.workflow_run.id }}

- name: read PR details from artifact
id: read
working-directory: pr
run: |
ls -R ./
echo "pr_number=$(cat NR)" >> "$GITHUB_OUTPUT"
echo "pr_head_ref=$(cat HEAD_REF)" >> "$GITHUB_OUTPUT"

- uses: actions/github-script@v6
with:
result-encoding: string
github-token: ${{ secrets.CLSC_PAT }}
script: |
let prs = await github.rest.pulls.list({
owner: "PaloAltoNetworks",
repo: "pan.dev",
state: "open",
})

let prs_list = prs.data

for (let pr of prs_list){
if (pr.head.label == "PaloAltoNetworks:pua_prev_${{ steps.read.outputs.pr_head_ref }}"){
await github.rest.pulls.update({
owner: "PaloAltoNetworks",
repo: "pan.dev",
pull_number: pr.number,
state: "closed",
})
console.log("Closing related PAN.DEV PR: #" + pr.number + " - " + pr.title + " -> " + pr.url)
break
}
}
76 changes: 13 additions & 63 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ jobs:
name: Unit Tests
needs: pyversion
permissions:
contents: read
pull-requests: write
contents: write
uses: ./.github/workflows/sub_unittest.yml
with:
python_version: ${{ needs.pyversion.outputs.pyversion }}
Expand All @@ -47,10 +46,10 @@ jobs:
docker_image_test_build:
name: Build Docker image
needs: pyversion
uses: ./.github/workflows/_docker.yml
permissions:
contents: read
packages: write
uses: ./.github/workflows/_docker.yml
packages: read # since publish is false, read access is ok
with:
publish: false
python_version: ${{ needs.pyversion.outputs.pyversion }}
Expand All @@ -70,70 +69,21 @@ jobs:
working-directory: docs
run: tar --exclude .DS_Store --exclude sidebars.js -cvf documentation.tar *
- name: upload the documentation artifact
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: documentation
path: docs/documentation.tar

pandev_pr:
name: Create a preview PR for pan.dev
needs: store_documentation
store_pr_details:
name: Upload PR details to artifact
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: checkout pan.dev
uses: actions/checkout@v4
with:
repository: PaloAltoNetworks/pan.dev
token: ${{ secrets.CLSC_PAT }}

- name: download documentation artifact
uses: actions/download-artifact@v3
with:
name: documentation
path: products/panos/docs

- name: unpack the documentation
working-directory: products/panos/docs
- name: save PR details
run: |
rm -rf 'panos-upgrade-assurance'
tar xvf documentation.tar
rm -f documentation.tar

- name: create a PR to upstream pan.dev
id: pr
uses: peter-evans/create-pull-request@v5
with:
token: ${{ secrets.CLSC_PAT }}
delete-branch: true
branch: "pua_prev_${{ github.event.pull_request.head.ref }}"
title: "[PAN-OS Upgrade Assurance][${{ github.event.pull_request.head.ref }}] documentation PREVIEW - do NOT MERGE"
commit-message: "docs: PanOS Upgrade Assurance documentation update"
labels: netsec, DO NOT MERGE
body: |
# Description

DO NOT MERGE - preview PR made for changes on branch: ${{ github.event.pull_request.head.ref }}.

# Types of changes

New feature (non-breaking change which adds functionality)

- name: find if we have a comment
uses: peter-evans/find-comment@v2
id: find
with:
issue-number: ${{ github.event.pull_request.number }}
body-includes: A Preview PR in PanDev repo has been created
repository: ${{ github.repository }}

- name: comment back on the original PR
if: steps.find.outputs.comment-id == '' && steps.pr.outputs.pull-request-url != ''
uses: peter-evans/create-or-update-comment@v3
mkdir -p ./pr
echo ${{ github.event.number }} > ./pr/NR
echo ${{ github.event.pull_request.head.ref }} > ./pr/HEAD_REF
- uses: actions/upload-artifact@v4
with:
issue-number: ${{ github.event.pull_request.number }}
repository: ${{ github.repository }}
body: |
A Preview PR in PanDev repo has been created. You can view it [here](${{ steps.pr.outputs.pull-request-url }}).
name: pr
path: pr/
120 changes: 120 additions & 0 deletions .github/workflows/pr_workflow_run.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
name: Post PR Workflow Run
run-name: "Post ${{ github.event.workflow_run.display_title }}"
# This workflow is to be triggerred after PR workflow is completed to have "write" access to the PR from fork repos securely.

# we always expect a single PR to trigger this workflow_run - take care of event filtering below to match a single workflow
on:
workflow_run:
workflows: ["PR"]
types:
- completed

permissions:
contents: write
pull-requests: write
# Gives the python-coverage-comment-action the necessary "actions" permissions for looking up the workflow that launched this
# workflow, and download the related artifact that contains the comment to be published
actions: read

jobs:

pr_status:
name: Get PR details from artifacts
runs-on: ubuntu-latest
if: >
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'

outputs:
pr_number: ${{ steps.read.outputs.pr_number }}
pr_head_ref: ${{ steps.read.outputs.pr_head_ref }}

steps:
- name: download PR artifact
uses: actions/download-artifact@v4
with:
name: pr
path: pr
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ github.event.workflow_run.id }}

- name: read PR details from artifact
id: read
working-directory: pr
run: |
ls -R ./
echo "pr_number=$(cat NR)" >> "$GITHUB_OUTPUT"
echo "pr_head_ref=$(cat HEAD_REF)" >> "$GITHUB_OUTPUT"

update_coverage:
name: Update coverage report on PR
needs: pr_status
runs-on: ubuntu-latest
steps:
- name: post coverage comment
uses: py-cov-action/python-coverage-comment-action@v3
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_PR_RUN_ID: ${{ github.event.workflow_run.id }}

pandev_pr:
name: Create a preview PR for pan.dev
needs: pr_status
runs-on: ubuntu-latest
steps:
- name: checkout pan.dev
uses: actions/checkout@v4
with:
repository: PaloAltoNetworks/pan.dev
token: ${{ secrets.CLSC_PAT }}

- name: download documentation artifact
uses: actions/download-artifact@v4
with:
name: documentation
path: products/panos/docs
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ github.event.workflow_run.id }}

- name: unpack the documentation
working-directory: products/panos/docs
run: |
rm -rf 'panos-upgrade-assurance'
tar xvf documentation.tar
rm -f documentation.tar

- name: create a PR to upstream pan.dev
id: pr
uses: peter-evans/create-pull-request@v5
with:
token: ${{ secrets.CLSC_PAT }}
delete-branch: true
branch: "pua_prev_${{ needs.pr_status.outputs.pr_head_ref }}"
title: "[PAN-OS Upgrade Assurance][${{ needs.pr_status.outputs.pr_head_ref }}] documentation PREVIEW - do NOT MERGE"
commit-message: "docs: PanOS Upgrade Assurance documentation update"
labels: netsec, DO NOT MERGE
body: |
# Description

DO NOT MERGE - preview PR made for changes on branch: ${{ needs.pr_status.outputs.pr_head_ref }}.

# Types of changes

New feature (non-breaking change which adds functionality)

- name: find if we have a comment
uses: peter-evans/find-comment@v2
id: find
with:
issue-number: ${{ needs.pr_status.outputs.pr_number }}
body-includes: A Preview PR in PanDev repo has been created
repository: ${{ github.repository }}

- name: comment back on the original PR
if: steps.find.outputs.comment-id == '' && steps.pr.outputs.pull-request-url != ''
uses: peter-evans/create-or-update-comment@v3
with:
issue-number: ${{ needs.pr_status.outputs.pr_number }}
repository: ${{ github.repository }}
body: |
A Preview PR in PanDev repo has been created. You can view it [here](${{ steps.pr.outputs.pull-request-url }}).
Loading
Loading