From 125f29f4723a2865d4250bf81ba243480c8aec0c Mon Sep 17 00:00:00 2001 From: vsoch Date: Wed, 18 Mar 2020 14:07:00 -0600 Subject: [PATCH] adding ability to assign people to PR Signed-off-by: vsoch --- README.md | 5 +++++ pull-request.sh | 31 ++++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f9d5511..2cf103e 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ Unlike standard actions, this action just uses variables from the environment. | PULL REQUEST_TITLE | the title for the pull request | 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 | For `PULL_REQUEST_DRAFT` and `MAINTAINER_CAN_MODIFY`, these are treated as environment booleans. If they are defined in the environment, they trigger the "true" condition. E.g.,: @@ -49,6 +50,10 @@ booleans. If they are defined in the environment, they trigger the "true" condit - 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. diff --git a/pull-request.sh b/pull-request.sh index 9ae6fd2..f41bf3e 100755 --- a/pull-request.sh +++ b/pull-request.sh @@ -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 ################################################################################ @@ -51,6 +52,7 @@ create_pull_request() { TITLE="$(echo -n "${4}" | jq --raw-input --slurp ".")" # pull request title 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}}" @@ -67,8 +69,20 @@ create_pull_request() { # Post the pull request 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}) + NUMBER=$(echo "${RESPONSE}" | jq --raw-output '.[] | .number') + printf "Number opened for PR is ${NUMBER}\n" + + # If there are assignees, assigm them to it + if [[ "${ASSIGNEES}" != "" ]]; then + printf "Attempting to assign ${ASSIGNEES} to ${PR} with number ${NUMBER}" + + # POST /repos/:owner/:repo/issues/:issue_number/assignees + DATA="{\"assignees\":\"${ASSIGNEES}\"}" + ASSIGNEES_URL="${ISSUE_URL}/${NUMBER}/assignees" + curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" --user "${GITHUB_ACTOR}" -X POST --data "${DATA}" ${ASSIGNEES_URL}) + echo $? + fi fi } @@ -110,6 +124,16 @@ main () { 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" @@ -154,7 +178,8 @@ main () { 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}" "${MODIFY}" + "${PULL_REQUEST_TITLE}" "${PULL_REQUEST_DRAFT}" "${MODIFY}" \ + "${ASSIGNEES}" fi