Skip to content

Commit

Permalink
Support creating repos if not exist (#31)
Browse files Browse the repository at this point in the history
Add optional key `create_visibility` to action, specifying it adds
`--create --create-visibility` to `buf generate` invocation.

Note that CLI requires both `--create` and `--create-visibility`,
whereas this action only requires `create_visibility`.

I prefer it this way because GitHub action error feedback loop is longer
than the CLI. In an action, a user modifies the workflow file, commits
it, pushes, waits for the action to run, checks result (a few clicks),
sees an error. On the CLI, a user types on the command line and sees the
result right away.

It is easier for the user to get it right on the first try if we only
require one field.

I am also open to require `create` as well, for consistency with the
CLI.

---------

Co-authored-by: Tommy Ma <33741651+cyinma@users.noreply.github.com>
  • Loading branch information
oliversun9 and cyinma committed Jun 6, 2023
1 parent 455042d commit 342fc4c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
15 changes: 9 additions & 6 deletions README.md
Expand Up @@ -22,12 +22,14 @@ jobs:
- uses: bufbuild/buf-push-action@v1
with:
buf_token: ${{ secrets.BUF_TOKEN }}
create_visibility: private
draft: ${{ github.ref_name != 'main'}}
```

With this configuration, upon a push [branches, tags][github-workflow]
the `buf` CLI pushes the [configured module][buf-yaml] to the BSR using the provided to
authenticate the request. When the triggering branch is not `main`, the commit will be pushed
authenticate the request. If the repository does not already exist on the BSR, create it
with private visibility. When the triggering branch is not `main`, the commit will be pushed
as a [draft][buf-draft].

For instructions on creating a BSR API token, see our [official docs][bsr-token]. Once you've
Expand All @@ -41,11 +43,12 @@ We recommend using [`buf-setup-action`][buf-setup] to install it (as in the exam

## Configuration

| Parameter | Description | Required | Default |
|:------------|:-------------------------------------------------------------------------------|:---------|:------------------------------------|
| `buf_token` | The [Buf authentication token][buf-token] used for private [Buf inputs][input] || [`${{github.token}}`][github-token] |
| `input` | The path of the [input] you want to push to BSR as a module | | `.` |
| `draft` | Indicates if the workflows should push to the BSR as a [draft][buf-draft] | | |
| Parameter | Description | Required | Default |
| :------------------ | :----------------------------------------------------------------------------- | :------- | :---------------------------------- |
| `buf_token` | The [Buf authentication token][buf-token] used for private [Buf inputs][input] || [`${{github.token}}`][github-token] |
| `input` | The path of the [input] you want to push to BSR as a module | | `.` |
| `draft` | Indicates if the workflows should push to the BSR as a [draft][buf-draft] | | |
| `create_visibility` | The visibility to create the BSR repository with, if it does not already exist | | |

> These parameters are derived from [`action.yml`](./action.yml).
Expand Down
6 changes: 5 additions & 1 deletion action.yml
Expand Up @@ -10,11 +10,14 @@ inputs:
required: true
input:
description: The Input path.
default: '.'
default: "."
required: false
draft:
description: The identifier of pushing to the BSR as a draft commit, when set to 'true' the action will push the module as draft with the branch name as draft name.
required: false
create_visibility:
description: The visibility of the BSR repository to be created with, if one needs to be created.
required: false
runs:
using: composite
steps:
Expand All @@ -23,4 +26,5 @@ runs:
env:
BUF_TOKEN: ${{ inputs.buf_token }}
DRAFT: ${{ inputs.draft }}
CREATE_VISIBILITY: ${{ inputs.create_visibility }}
run: $GITHUB_ACTION_PATH/push.bash ${{ inputs.input }}
10 changes: 10 additions & 0 deletions push.bash
Expand Up @@ -51,4 +51,14 @@ if [ "${DRAFT}" == "true" ]; then
BUF_ARGS=("--draft" "${GITHUB_REF_NAME}")
fi

if [ -n "${CREATE_VISIBILITY}" ]; then
set +e
BUF_HELP_OUTPUT="$("${BUF_COMMAND}" push example --create --help 2>&1)"
set -e
if [[ "${BUF_HELP_OUTPUT}" == *"unknown flag: --create"* ]]; then
fail "The installed version of buf does not support creating repositories on push. Please use buf v1.19.0 or newer."
fi
BUF_ARGS+=("--create" "--create-visibility" "${CREATE_VISIBILITY}")
fi

BUF_TOKEN="${BUF_TOKEN}" "${BUF_COMMAND}" "push" "${BUF_INPUT}" "${BUF_ARGS[@]}"
18 changes: 16 additions & 2 deletions test/test.bash
Expand Up @@ -17,7 +17,7 @@ chmod +x tmp/test/bin/buf
unset GITHUB_SHA GITHUB_REF_NAME

test_push() {
export GITHUB_SHA GITHUB_REF_NAME BUF_TOKEN DRAFT WANT_BUF_TOKEN WANT_ARGS
export GITHUB_SHA GITHUB_REF_NAME BUF_TOKEN DRAFT WANT_BUF_TOKEN WANT_ARGS WANT_STDOUT WANT_STDERR WANT_EXIT_CODE CREATE_VISIBILITY
set +e
./push.bash "$@" > tmp/test/stdout 2> tmp/test/stderr
GOT_EXIT_CODE="${?}"
Expand All @@ -39,7 +39,7 @@ test_push() {
fi
fi
rm -f tmp/test/stdout tmp/test/stderr
unset GITHUB_SHA GITHUB_REF_NAME BUF_TOKEN WANT_BUF_TOKEN WANT_ARGS
unset GITHUB_SHA GITHUB_REF_NAME BUF_TOKEN DRAFT WANT_BUF_TOKEN WANT_ARGS WANT_STDOUT WANT_STDERR WANT_EXIT_CODE CREATE_VISIBILITY
}

echo "testing happy path"
Expand Down Expand Up @@ -67,6 +67,20 @@ WANT_EXIT_CODE=0
test_push some/input/path
echo "ok"

echo "testing happy path create"
GITHUB_SHA=fake-sha
GITHUB_REF_NAME=main
BUF_TOKEN=fake-token
CREATE_VISIBILITY=private
WANT_BUF_TOKEN=fake-token
WANT_ARGS="push some/input/path --tag fake-sha --create --create-visibility private"
WANT_STDOUT="::add-mask::fake-token"
WANT_STDERR=""
WANT_EXIT_CODE=0
echo "CREATE is ${CREATE_VISIBILITY}"
test_push some/input/path
echo "ok"

echo "testing happy path draft on main branch"
GITHUB_SHA=fake-sha
GITHUB_REF_NAME=main
Expand Down

0 comments on commit 342fc4c

Please sign in to comment.