Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
28 changes: 4 additions & 24 deletions .github/workflows/pr-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,16 @@ name: Publish PR Preview
on:
pull_request:
types: [labeled]
workflow_dispatch:
inputs:
pr_number:
description: Pull request number to publish
required: true
type: string

concurrency:
group: publish-pr-preview-${{ github.event.pull_request.number || github.run_id }}
group: publish-pr-preview-${{ github.event.pull_request.number }}
cancel-in-progress: false

jobs:
context:
if: >-
github.event_name == 'workflow_dispatch' ||
(github.event.label.name == 'publish-preview' &&
github.event.pull_request.head.repo.full_name == github.repository)
github.event.label.name == 'publish-preview' &&
github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
timeout-minutes: 5
permissions:
Expand All @@ -33,28 +26,15 @@ jobs:
id: context
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
EVENT_PR_NUMBER: ${{ github.event.pull_request.number }}
INPUT_PR_NUMBER: ${{ inputs.pr_number }}
WORKFLOW_REF: ${{ github.ref }}
with:
script: |
const rawPrNumber = context.eventName === 'workflow_dispatch'
? process.env.INPUT_PR_NUMBER
: process.env.EVENT_PR_NUMBER;
const rawPrNumber = process.env.EVENT_PR_NUMBER;
if (!/^[1-9][0-9]*$/.test(rawPrNumber || '')) {
core.setFailed('Pull request number must contain ASCII digits only.');
return;
}

if (context.eventName === 'workflow_dispatch') {
const defaultRef = `refs/heads/${process.env.DEFAULT_BRANCH}`;
if (process.env.WORKFLOW_REF !== defaultRef) {
core.setFailed(`Run manual previews from ${defaultRef}.`);
return;
}
}

const prNumber = Number(rawPrNumber);
if (!Number.isSafeInteger(prNumber)) {
core.setFailed('Pull request number is outside the supported range.');
Expand Down
13 changes: 4 additions & 9 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,11 @@ that is trusted to run with the repository's publishing permissions.
For a pull request from this repository, apply the `publish-preview` label. The
`Publish PR Preview` workflow will build and validate a uniquely versioned
`socketdev` prerelease, publish it to TestPyPI, and add or update a pull request
comment with the exact version and installation command. Both label-triggered
and manually dispatched previews are limited to open pull requests whose
branches belong to this repository.
comment with the exact version and installation command. Previews are limited
to open pull requests whose branches belong to this repository.

The workflow reacts when the label is added; pushing another commit while the
label remains on the pull request does not publish a new preview. To publish the
new pull request head or retry a failed publication, remove `publish-preview`
and apply it again.

Maintainers can also open **Actions > Publish PR Preview > Run workflow**, run
it from the repository's default branch, and enter the pull request number.
Manual dispatch is useful when a label should remain unchanged or a
publication needs to be retried.
and apply it again. GitHub skips the workflow when the pull request has merge
conflicts. Resolve the conflicts before reapplying the label.
27 changes: 27 additions & 0 deletions tests/unit/test_pr_preview_workflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Regression checks for the preview workflow's event routing configuration."""

import re
from pathlib import Path

WORKFLOW = Path(__file__).resolve().parents[2] / ".github/workflows/pr-preview.yml"


def test_preview_is_triggered_only_by_pull_request_label_events():
workflow = WORKFLOW.read_text(encoding="utf-8")
trigger = re.search(r"^on:\n(.*?)(?=^\S|\Z)", workflow, re.MULTILINE | re.DOTALL)

assert trigger is not None
assert trigger.group(1).split() == ["pull_request:", "types:", "[labeled]"]


def test_preview_context_requires_the_preview_label_and_a_same_repository_pr():
workflow = WORKFLOW.read_text(encoding="utf-8")
condition = re.search(
r"^ context:\n if: >-\n((?: {6}[^\n]+\n)+)", workflow, re.MULTILINE
)

assert condition is not None
assert " ".join(condition.group(1).split()) == (
"github.event.label.name == 'publish-preview' && "
"github.event.pull_request.head.repo.full_name == github.repository"
)