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

support pushing draft commit to BSR when draft param is true #21

Merged
merged 14 commits into from Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 22 additions & 2 deletions .github/workflows/ci.yaml
Expand Up @@ -23,11 +23,31 @@ jobs:
cp test/buf.bash tmp/bin/buf
chmod +x tmp/bin/buf
echo "${PWD}/tmp/bin" >> "${GITHUB_PATH}"
- name: run action
- name: run on branch disable draft
if: github.ref_type == 'branch' && github.ref != 'refs/heads/main'
uses: ./
with:
buf_token: fake-buf-token
input: path/to/input
env:
WANT_BUF_TOKEN: fake-buf-token
WANT_ARGS: push --tag ${{ github.sha }} path/to/input
WANT_ARGS: push path/to/input --tag ${{ github.sha }}
robbertvanginkel marked this conversation as resolved.
Show resolved Hide resolved
- name: run on branch enable draft
if: github.ref_type == 'branch' && github.ref != 'refs/heads/main'
uses: ./
with:
buf_token: fake-buf-token
input: path/to/input
enable_draft: true
robbertvanginkel marked this conversation as resolved.
Show resolved Hide resolved
env:
WANT_BUF_TOKEN: fake-buf-token
WANT_ARGS: push path/to/input --draft ${{ github.ref_name }}
- name: run on main
if: github.ref_type == 'branch' && github.ref == 'refs/heads/main'
uses: ./
with:
buf_token: fake-buf-token
input: path/to/input
env:
WANT_BUF_TOKEN: fake-buf-token
WANT_ARGS: push path/to/input --tag ${{ github.sha }}
4 changes: 4 additions & 0 deletions action.yml
Expand Up @@ -12,11 +12,15 @@ inputs:
description: The Input path.
default: '.'
required: false
enable_draft:
description: The identifier of enabling draft, when set to 'true' the action will push the module as draft if the branch that trigger the workflow is not 'main'.
required: false
robbertvanginkel marked this conversation as resolved.
Show resolved Hide resolved
runs:
using: composite
steps:
- name: push
shell: bash
env:
BUF_TOKEN: ${{ inputs.buf_token }}
ENABLE_DRAFT: ${{ inputs.enable_draft }}
run: $GITHUB_ACTION_PATH/push.bash ${{ inputs.input }}
29 changes: 28 additions & 1 deletion push.bash
Expand Up @@ -21,6 +21,14 @@ if [ -z "${GITHUB_SHA}" ]; then
fail "the commit was not provided"
fi

if [ -z "${GITHUB_REF_NAME}" ]; then
fail "the commit ref was not provided"
fi

if [ -z "${GITHUB_REF_TYPE}" ]; then
fail "the commit ref type was not provided"
fi

if [ -z "${BUF_TOKEN}" ]; then
fail "a buf authentication token was not provided"
fi
Expand All @@ -33,4 +41,23 @@ if [ -z "$BUF_COMMAND" ]; then
fail "$NOT_INSTALLED_MESSAGE"
fi

BUF_TOKEN="${BUF_TOKEN}" "${BUF_COMMAND}" push --tag "${GITHUB_SHA}" "${BUF_INPUT}"
if [ "${GITHUB_REF_TYPE}" != "branch" ]; then
echo "reference type is not branch, skipping" >&2
exit 0
fi

BUF_ARGS=("--tag" "${GITHUB_SHA}")
if [ "${GITHUB_REF_NAME}" != "main" ] && [ "${ENABLE_DRAFT}" == "true" ]; then
robbertvanginkel marked this conversation as resolved.
Show resolved Hide resolved
# Check that --draft is supported by running "buf push --draft example --help"
# and checking for "unknown flag: --draft" in the output.
set +e
BUF_HELP_OUTPUT="$("${BUF_COMMAND}" push --draft example --help 2>&1)"
set -e
if [[ "${BUF_HELP_OUTPUT}" == *"unknown flag: --draft"* ]]; then
fail "The installed version of buf does not support setting the draft. Please use buf v1.7.0 or newer."
fi
robbertvanginkel marked this conversation as resolved.
Show resolved Hide resolved

BUF_ARGS=("--draft" "${GITHUB_REF_NAME}")
fi

BUF_TOKEN="${BUF_TOKEN}" "${BUF_COMMAND}" "push" "${BUF_INPUT}" "${BUF_ARGS[@]}"
16 changes: 11 additions & 5 deletions test/test.bash
Expand Up @@ -13,11 +13,11 @@ PATH="${DIR}/tmp/test/bin:$PATH"
cp test/buf.bash tmp/test/bin/buf
chmod +x tmp/test/bin/buf

# prevent the GITHUB_SHA set by actions from being used in test
unset GITHUB_SHA
# prevent the GITHUB_SHA, GITHUB_REF_NAME and GITHUB_REF_TYPE set by actions from being used in test
robbertvanginkel marked this conversation as resolved.
Show resolved Hide resolved
unset GITHUB_SHA GITHUB_REF_NAME GITHUB_REF_TYPE

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

echo "testing happy path"
GITHUB_SHA=fake-sha
GITHUB_REF_NAME=main
GITHUB_REF_TYPE=branch
BUF_TOKEN=fake-token
WANT_BUF_TOKEN=fake-token
WANT_ARGS="push --tag fake-sha some/input/path"
WANT_ARGS="push some/input/path --tag fake-sha"
WANT_STDOUT="::add-mask::fake-token"
WANT_STDERR=""
WANT_EXIT_CODE=0
Expand All @@ -55,6 +57,8 @@ echo "ok"

echo "testing no input"
GITHUB_SHA=fake-sha
GITHUB_REF_NAME=main
GITHUB_REF_TYPE=branch
BUF_TOKEN=fake-token
WANT_STDOUT=""
WANT_STDERR="Usage: ./push.bash <input>"
Expand All @@ -73,6 +77,8 @@ echo "ok"

echo "testing no BUF_TOKEN"
GITHUB_SHA=fake-sha
GITHUB_REF_NAME=main
GITHUB_REF_TYPE=branch
WANT_STDOUT='::add-mask::
::error::a buf authentication token was not provided'
WANT_STDERR=""
Expand Down