From 230b0cfb1e0674179ed8e4d1bbf445f72c12f077 Mon Sep 17 00:00:00 2001 From: Vanessa Sochat Date: Thu, 31 Jan 2019 18:56:56 -0500 Subject: [PATCH] adding first test --- Dockerfile | 13 ++++++ README.md | 38 +++++++++++++++++ pull-request.sh | 108 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 159 insertions(+) create mode 100644 Dockerfile create mode 100644 README.md create mode 100755 pull-request.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b2caf52 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM debian:jessie-slim + +# docker build -t vanessa/pull-request-action + +LABEL "com.github.actions.name"="Pull Request on Branch Push" +LABEL "com.github.actions.description"="Create a pull request when a branch is created or updated" +LABEL "com.github.actions.icon"="activity" +LABEL "com.github.actions.color"="yellow" + +COPY pull-request.sh /pull-request.sh + +RUN chmod u+x /pull-request.sh +ENTRYPOINT ["/pull-request"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..98b270b --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +# Automated Branch Pull Requests + +This action will open a pull request to master branch (or otherwise specified) +whenever a branch with some prefix is pushed to. The idea is that you can +set up some workflow that pushes content to branches of the repostory, +and you would then want this push reviewed for merge to master. + +Here is an example of what to put in your `.github/main.workflow` file to +trigger the action. + +``` +workflow "Create Pull Request" { + on = "push" + resolves = "Create New Pull Request" +} + +action "Create New Pull Request" { + uses = "vsoch/pull-request-action@master" + env = { + BRANCH_PREFIX = "update/" + PULL_REQUEST_BRANCH = "master" + } +} +``` + +Environment variables include: + + - **BRANCH_PREFIX**: the prefix to filter to. If the branch doesn't start with the prefix, it will be ignored + - **PULL_REQUEST_BRANCH**: the branch to issue the pull request to. Defaults to master. + +## Example use Case: Update Registry + +As an example, I created this action to be intended for an [organizational static registry](https://www.github.com/singularityhub/registry-org) for container builds. Specifically, you +have modular repositories building container recipes, and then opening pull requests to the +registry to update it. + + - the container collection pull request should be generated from a separate GitHub repository, including the folder structure (manifests, tags, collection README) that are expected. + - pushing a branch that starts with update/ should open a pull request, if it doens't exist. If the branch is already open for PR, it updates it. diff --git a/pull-request.sh b/pull-request.sh new file mode 100755 index 0000000..4a09c63 --- /dev/null +++ b/pull-request.sh @@ -0,0 +1,108 @@ +#!/bin/bash + +# Suggested by Github actions to be strict +set -e +set -o pipefail + +################################################################################ +# Global Variables (we can't use GITHUB_ prefix) +################################################################################ + +API_VERSION=v3 +BASE=https://api.github.com +AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}" +HEADER="Accept: application/vnd.github.${API_VERSION}+json" +HEADER="${HEADER}; application/vnd.github.antiope-preview+json" + +# URLs +REPO_URL="${BASE}/repos/${GITHUB_REPOSITORY}" +PULLS_URL=$REPO_URL/pulls + +################################################################################ +# Helper Functions +################################################################################ + +get_url() { + + RESPONSE=$(curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" "${1:-}") + echo ${RESPONSE} +} + +check_credentials() { + + if [[ -z "${GITHUB_TOKEN}" ]]; then + echo "You must include the GITHUB_TOKEN as an environment variable." + exit 1 + fi + +} + +check_events_json() { + + if [[ ! -f "${GITHUB_EVENT_PATH}" ]]; then + echo "Cannot find Github events file at ${GITHUB_EVENT_PATH}"; + exit 1; + fi + echo "Found ${GITHUB_EVENT_PATH}"; + cat "${GITHUB_EVENT_PATH}" + +} + +create_pull_request() { + + SOURCE=${1} # from this branch + TARGET=${2} # pull request TO this target + + TITLE="Update container ${SOURCE}" + BODY="This is an automated pull request to update the container collection ${SOURCE}" + + # Post the pull request + curl -d "{\"title\":\"${TITLE}\", \"body\":\"${BODY}\", \"head\":\"${SOURCE}\", \"base\":\"${TARGET}\"}" -H "Content-Type: application/json" -H "\"${HEADER}\"" -H \""${AUTH_HEADER}"\" -X POST ${PULLS_URL}; + echo $? +} + + +main () { + + # path to file that contains the POST response of the event + # Example: https://github.com/actions/bin/tree/master/debug + # Value: /github/workflow/event.json + check_events_json; + + # User specified branch to PR to, and check + if [ -z "${BRANCH_PREFIX}" ]; then + echo "No branch prefix is set, all branches will be used." + BRANCH_PREFIX="" + fi + + if [ -z "${PULL_REQUEST_BRANCH}" ]; then + PULL_REQUEST_BRANCH=master + fi + echo "Pull requests will go to ${PULL_REQUEST_BRANCH}" + + # Get the name of the action that was triggered + BRANCH=$(jq --raw-output .ref "${GITHUB_EVENT_PATH}"); + echo "Found branch $BRANCH" + + # If it's to the target branch, ignore it + if [[ "${BRANCH}" == "${PULL_REQUEST_BRANCH}" ]] + echo "Target and current branch are identical (${BRANCH}), skipping." + else + + # If the prefix for the branch matches + if [[ $BRANCH == ${BRANCH_PREFIX}* ]]; then + + # Ensure we have a GitHub token + check_credentials + create_pull_request $BRANCH $PULL_REQUEST_BRANCH + + fi + + fi +} + +echo "========================================================================== +START: Running Pull Request on Branch Update Action!"; +main; +echo "========================================================================== +END: Finished";