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

[feature] ok-to-test allows testing on forked branches with secrets #320

Merged
merged 10 commits into from
Dec 3, 2020
117 changes: 117 additions & 0 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Run secret-dependent integration tests only after /ok-to-test approval
on:
pull_request:
repository_dispatch:
types: [ok-to-test-command]

name: Integration tests

jobs:
# Branch-based pull request
alldoami marked this conversation as resolved.
Show resolved Hide resolved
integration-trusted:
runs-on: ubuntu-latest
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
edulop91 marked this conversation as resolved.
Show resolved Hide resolved
steps:

- name: Branch based PR checkout
uses: actions/checkout@v2

strategy:
matrix:
target:
- check-docs
- check-mod
- lint-ci
- test
steps:
alldoami marked this conversation as resolved.
Show resolved Hide resolved
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '1.15.2'
- name: Install dependencies
run: make setup

- name: make ${{ matrix.target }}
env:
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.REVIEWDOG_GITHUB_API_TOKEN }}
SNOWFLAKE_USER: ${{ secrets.SNOWFLAKE_USER }}
SNOWFLAKE_PASSWORD: ${{ secrets.SNOWFLAKE_PASSWORD }}
SNOWFLAKE_ACCOUNT: ${{ secrets.SNOWFLAKE_ACCOUNT }}
SNOWFLAKE_ROLE: ${{ secrets.SNOWFLAKE_ROLE }}
run: make ${{ matrix.target }}

# Repo owner has commented /ok-to-test on a (fork-based) pull request
integration-fork:
runs-on: ubuntu-latest
if:
github.event_name == 'repository_dispatch' &&
github.event.client_payload.slash_command.sha == github.event.client_payload.pull_request.head.sha
steps:

# Check out merge commit
- name: Fork based /ok-to-test checkout
uses: actions/checkout@v2
with:
ref: 'refs/pull/${{ github.event.client_payload.pull_request.number }}/merge'
alldoami marked this conversation as resolved.
Show resolved Hide resolved

# Integration tests needing secrets
strategy:
matrix:
target:
- check-docs
- check-mod
alldoami marked this conversation as resolved.
Show resolved Hide resolved
- lint-ci
- test
alldoami marked this conversation as resolved.
Show resolved Hide resolved
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '1.15.2'
- name: Install dependencies
run: make setup

- name: make ${{ matrix.target }}
env:
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.REVIEWDOG_GITHUB_API_TOKEN }}
SNOWFLAKE_USER: ${{ secrets.SNOWFLAKE_USER }}
SNOWFLAKE_PASSWORD: ${{ secrets.SNOWFLAKE_PASSWORD }}
SNOWFLAKE_ACCOUNT: ${{ secrets.SNOWFLAKE_ACCOUNT }}
SNOWFLAKE_ROLE: ${{ secrets.SNOWFLAKE_ROLE }}
run: make ${{ matrix.target }}

- run: |
echo "Integration tests... success! ;-)"

# Update check run called "integration-fork"
- uses: actions/github-script@v1
id: update-check-run
if: ${{ always() }}
env:
number: ${{ github.event.client_payload.pull_request.number }}
job: ${{ github.job }}
# Conveniently, job.status maps to https://developer.github.com/v3/checks/runs/#update-a-check-run
conclusion: ${{ job.status }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { data: pull } = await github.pulls.get({
...context.repo,
pull_number: process.env.number
});
const ref = pull.head.sha;

const { data: checks } = await github.checks.listForRef({
...context.repo,
ref
});

const check = checks.check_runs.filter(c => c.name === process.env.job);

const { data: result } = await github.checks.update({
...context.repo,
check_run_id: check[0].id,
status: 'completed',
conclusion: process.env.conclusion
});

return result;
alldoami marked this conversation as resolved.
Show resolved Hide resolved
33 changes: 33 additions & 0 deletions .github/workflows/ok-to-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# If someone with write access comments "/ok-to-test" on a pull request, emit a repository_dispatch event
name: Label

on:
issue_comment:
types: [created]

jobs:
ok-to-test:
runs-on: ubuntu-latest
steps:
# Generate a GitHub App installation access token from an App ID and private key
# To create a new GitHub App:
# https://developer.github.com/apps/building-github-apps/creating-a-github-app/
# See app.yml for an example app manifest
alldoami marked this conversation as resolved.
Show resolved Hide resolved
- name: Generate token
id: generate_token
uses: tibdex/github-app-token@v1
with:
app_id: ${{ secrets.OK_TO_TEST_APP_ID }}
private_key: ${{ secrets.OK_TO_TEST_PRIVATE_KEY }}

- name: Slash Command Dispatch
uses: peter-evans/slash-command-dispatch@v1
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a v2 for this action, I wonder if worth upgrading (either now or subsequent pr)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should try using v1 first and then try with v2 since I'm expecting some other things might need to change besides the version number.

env:
TOKEN: ${{ steps.generate_token.outputs.token }}
with:
token: ${{ env.TOKEN }} # GitHub App installation access token
reaction-token: ${{ secrets.GITHUB_TOKEN }}
issue-type: pull-request
commands: ok-to-test
named-args: true
permission: write
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how we verify this is enforced

17 changes: 8 additions & 9 deletions .github/workflows/ci.yml → .github/workflows/unit.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
on: push
# Run unit tests on any branch/fork pull request
alldoami marked this conversation as resolved.
Show resolved Hide resolved
on:
pull_request
alldoami marked this conversation as resolved.
Show resolved Hide resolved

name: Unit tests

jobs:
run:
unit:
runs-on: ubuntu-latest
strategy:
matrix:
target:
- check-docs
- check-mod
- lint-ci
- test-acceptance-ci
- test
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
Expand All @@ -19,10 +23,5 @@ jobs:
run: make setup

- name: make ${{ matrix.target }}
env:
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.REVIEWDOG_GITHUB_API_TOKEN }}
SNOWFLAKE_USER: ${{ secrets.SNOWFLAKE_USER }}
SNOWFLAKE_PASSWORD: ${{ secrets.SNOWFLAKE_PASSWORD }}
SNOWFLAKE_ACCOUNT: ${{ secrets.SNOWFLAKE_ACCOUNT }}
SNOWFLAKE_ROLE: ${{ secrets.SNOWFLAKE_ROLE }}
run: make ${{ matrix.target }}
- run: echo "Unit tests... success! ;-)"
alldoami marked this conversation as resolved.
Show resolved Hide resolved