Merge pull request #23 from vsoch/add/assignees

Add/assignees
This commit is contained in:
Vanessasaurus
2020-03-23 11:25:34 -06:00
committed by GitHub
3 changed files with 85 additions and 17 deletions

View File

@@ -40,9 +40,19 @@ Unlike standard actions, this action just uses variables from the environment.
| PULL_REQUEST_FROM_BRANCH | if a branch isn't found in your GitHub payload, use this branch | false | |
| PULL_REQUEST_BODY | the body for the pull request | false | |
| PULL_REQUEST_TITLE | the title for the pull request | false | |
| PULL_REQUEST_DRAFT | should this be a draft PR? | false | false |
| PULL_REQUEST_DRAFT | should this be a draft PR? | false | unset |
| MAINTAINER_CANT_MODIFY | Do not allow the maintainer to modify the PR | false | unset |
| PULL_REQUEST_ASSIGNEES | A list (string with spaces) of users to assign | false | unset |
All booleans should be lowercase.
For `PULL_REQUEST_DRAFT` and `MAINTAINER_CANT_MODIFY`, these are treated as environment
booleans. If they are defined in the environment, they trigger the "true" condition. E.g.,:
- Define `MAINTAINER_CANT_MODIFY` if you don't want the maintainer to be able to modify the pull request.
- Define `PULL_REQUEST_DRAFT` if you want the PR to be a draft.
For `PULL_REQUEST_ASSIGNEES`, you can provide a string of one or more GitHub usernames to
assign to the issue. Note that only users with push access can add assigness to
an issue or PR, they are ignored otherwise.
The `GITHUB_TOKEN` secret is required to interact and authenticate with the GitHub API to open
the pull request. The example is [deployed here](https://github.com/vsoch/pull-request-action-example) with an example opened (and merged) [pull request here](https://github.com/vsoch/pull-request-action-example/pull/1) if needed.

View File

@@ -0,0 +1,17 @@
name: Pull Request on Branch Push
on:
push:
branches-ignore:
- devel
jobs:
auto-pull-request:
name: PullRequestAction
runs-on: ubuntu-latest
steps:
- name: pull-request-action
uses: vsoch/pull-request-action@1.0.3
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BRANCH_PREFIX: "update/"
PULL_REQUEST_BRANCH: "master"
PULL_REQUEST_ASSIGNEES: vsoch

View File

@@ -16,6 +16,7 @@ HEADER="${HEADER}; application/vnd.github.antiope-preview+json; application/vnd.
# URLs
REPO_URL="${BASE}/repos/${GITHUB_REPOSITORY}"
ISSUE_URL=${REPO_URL}/issues
PULLS_URL=$REPO_URL/pulls
################################################################################
@@ -36,7 +37,7 @@ check_events_json() {
if [[ ! -f "${GITHUB_EVENT_PATH}" ]]; then
printf "Cannot find Github events file at ${GITHUB_EVENT_PATH}\n";
exit 1;
exit 1
fi
printf "Found ${GITHUB_EVENT_PATH}\n";
@@ -49,16 +50,11 @@ create_pull_request() {
TARGET="$(echo -n "${2}" | jq --raw-input --slurp ".")" # pull request TO this target
BODY="$(echo -n "${3}" | jq --raw-input --slurp ".")" # this is the content of the message
TITLE="$(echo -n "${4}" | jq --raw-input --slurp ".")" # pull request title
# JSON boolean
if [[ "${5}" == "true" ]]; then # if PRs are draft
DRAFT="true";
else
DRAFT="false";
fi
DRAFT="${5}" # pull request draft?
MODIFY="${6}" # maintainer can modify
ASSIGNEES="$(echo -n "${7}" | jq --raw-input --slurp ".")"
# Check if the branch already has a pull request open
DATA="{\"base\":${TARGET}, \"head\":${SOURCE}, \"body\":${BODY}}"
RESPONSE=$(curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" --user "${GITHUB_ACTOR}" -X GET --data "${DATA}" ${PULLS_URL})
PR=$(echo "${RESPONSE}" | jq --raw-output '.[] | .head.ref')
@@ -71,10 +67,33 @@ create_pull_request() {
# Option 2: Open a new pull request
else
# Post the pull request
DATA="{\"title\":${TITLE}, \"body\":${BODY}, \"base\":${TARGET}, \"head\":${SOURCE}, \"draft\":${DRAFT}}"
DATA="{\"title\":${TITLE}, \"body\":${BODY}, \"base\":${TARGET}, \"head\":${SOURCE}, \"draft\":${DRAFT}, \"maintainer_can_modify\":${MODIFY}}"
printf "curl --user ${GITHUB_ACTOR} -X POST --data ${DATA} ${PULLS_URL}\n"
curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" --user "${GITHUB_ACTOR}" -X POST --data "${DATA}" ${PULLS_URL}
echo $?
RESPONSE=$(curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" --user "${GITHUB_ACTOR}" -X POST --data "${DATA}" ${PULLS_URL})
RETVAL=$?
printf "Pull request return code: ${RETVAL}\n"
# If there are assignees and we were successful to open, assigm them to it
if [[ "${ASSIGNEES}" != "" ]] && [[ "${RETVAL}" == "0" ]]; then
echo "${RESPONSE}"
NUMBER=$(echo "${RESPONSE}" | jq --raw-output '.number')
printf "Number opened for PR is ${NUMBER}\n"
# Remove leading and trailing quotes
ASSIGNEES=$(echo "$ASSIGNEES" | sed -e 's/^"//' -e 's/"$//')
# Parse assignees into a list
ASSIGNEES=$(echo $ASSIGNEES | sed -e 's/\(\w*\)/,"\1"/g' | cut -d , -f 2-)
printf "Attempting to assign ${ASSIGNEES} to ${PR} with number ${NUMBER}"
# POST /repos/:owner/:repo/issues/:issue_number/assignees
DATA="{\"assignees\":[${ASSIGNEES}]}"
echo "${DATA}"
ASSIGNEES_URL="${ISSUE_URL}/${NUMBER}/assignees"
curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" --user "${GITHUB_ACTOR}" -X POST --data "${DATA}" ${ASSIGNEES_URL}
printf "$?\n"
fi
fi
}
@@ -98,6 +117,7 @@ main () {
fi
printf "Pull requests will go to ${PULL_REQUEST_BRANCH}\n"
# Pull request draft
if [ -z "${PULL_REQUEST_DRAFT}" ]; then
printf "No explicit preference for draft PR: created PRs will be normal PRs.\n"
PULL_REQUEST_DRAFT="false"
@@ -106,6 +126,25 @@ main () {
PULL_REQUEST_DRAFT="true"
fi
# Maintainer can modify, defaults to CAN, unless user sets MAINTAINER_CANT_MODIFY
if [ -z "${MAINTAINER_CANT_MODIFY}" ]; then
printf "No explicit preference for maintainer being able to modify: default is true.\n"
MODIFY="true"
else
printf "Environment variable MAINTAINER_CANT_MODIFY set to a value: maintainer will not be able to modify.\n"
MODIFY="false"
fi
# Assignees
ASSIGNEES=""
if [ -z "${PULL_REQUEST_ASSIGNEES}" ]; then
printf "PULL_REQUEST_ASSIGNEES is not set, no assignees.\n"
else
printf "PULL_REQUEST_ASSIGNEES is set, ${PULL_REQUEST_ASSIGNEES}\n"
ASSIGNEES="${PULL_REQUEST_ASSIGNEES}"
fi
# The user is allowed to explicitly set the name of the branch
if [ -z "${PULL_REQUEST_FROM_BRANCH}" ]; then
printf "PULL_REQUEST_FROM_BRANCH is not set, checking branch in payload.\n"
@@ -138,18 +177,20 @@ main () {
# Pull request body (optional)
if [ -z "${PULL_REQUEST_BODY}" ]; then
echo "No pull request body is set, will use default."
PULL_REQUEST_BODY="This is an automated pull request to update the container collection ${BRANCH}"
PULL_REQUEST_BODY="This is an automated pull request to update from branch ${BRANCH}"
fi
printf "Pull request body is ${PULL_REQUEST_BODY}\n"
# Pull request title (optional)
if [ -z "${PULL_REQUEST_TITLE}" ]; then
printf "No pull request title is set, will use default.\n"
PULL_REQUEST_TITLE="Update container ${BRANCH}"
PULL_REQUEST_TITLE="Update from ${BRANCH}"
fi
printf "Pull request title is ${PULL_REQUEST_TITLE}\n"
create_pull_request "${BRANCH}" "${PULL_REQUEST_BRANCH}" "${PULL_REQUEST_BODY}" "${PULL_REQUEST_TITLE}" "${PULL_REQUEST_DRAFT}"
create_pull_request "${BRANCH}" "${PULL_REQUEST_BRANCH}" "${PULL_REQUEST_BODY}" \
"${PULL_REQUEST_TITLE}" "${PULL_REQUEST_DRAFT}" "${MODIFY}" \
"${ASSIGNEES}"
fi