testing change to add reviewers (team or individual) to a PR

Signed-off-by: vsoch <vsochat@stanford.edu>
This commit is contained in:
vsoch
2020-03-23 13:36:27 -06:00
parent 9d567ac1d1
commit 1a853b4e3f
2 changed files with 64 additions and 16 deletions

View File

@@ -43,6 +43,8 @@ Unlike standard actions, this action just uses variables from the environment.
| PULL_REQUEST_DRAFT | should this be a draft PR? | false | unset | | 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 | | 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 | | PULL_REQUEST_ASSIGNEES | A list (string with spaces) of users to assign | false | unset |
| PULL_REQUEST_REVIEWERS | A list (string with spaces) of users to assign review | false | unset |
| PULL_REQUEST_TEAM_REVIEWERS | A list (string with spaces) of teams to assign review | false | unset |
For `PULL_REQUEST_DRAFT` and `MAINTAINER_CANT_MODIFY`, these are treated as environment 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.,: booleans. If they are defined in the environment, they trigger the "true" condition. E.g.,:
@@ -50,7 +52,8 @@ 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 `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. - 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 For `PULL_REQUEST_ASSIGNEES`, `PULL_REQUEST_REVIEWERS`, and `PULL_REQUEST_TEAM_REVIEWERS`
you can provide a string of one or more GitHub usernames (or team names) to
assign to the issue. Note that only users with push access can add assigness to assign to the issue. Note that only users with push access can add assigness to
an issue or PR, they are ignored otherwise. an issue or PR, they are ignored otherwise.

View File

@@ -53,6 +53,8 @@ create_pull_request() {
DRAFT="${5}" # pull request draft? DRAFT="${5}" # pull request draft?
MODIFY="${6}" # maintainer can modify MODIFY="${6}" # maintainer can modify
ASSIGNEES="$(echo -n "${7}" | jq --raw-input --slurp ".")" ASSIGNEES="$(echo -n "${7}" | jq --raw-input --slurp ".")"
REVIEWERS="$(echo -n "${8}" | jq --raw-input --slurp ".")"
TEAM_REVIEWERS="$(echo -n "${9}" | jq --raw-input --slurp ".")"
# Check if the branch already has a pull request open # Check if the branch already has a pull request open
DATA="{\"base\":${TARGET}, \"head\":${SOURCE}, \"body\":${BODY}}" DATA="{\"base\":${TARGET}, \"head\":${SOURCE}, \"body\":${BODY}}"
@@ -73,26 +75,54 @@ create_pull_request() {
RETVAL=$? RETVAL=$?
printf "Pull request return code: ${RETVAL}\n" printf "Pull request return code: ${RETVAL}\n"
# If there are assignees and we were successful to open, assigm them to it # if we were successful to open, add assignees and reviewers
if [[ "${ASSIGNEES}" != "" ]] && [[ "${RETVAL}" == "0" ]]; then if [[ "${RETVAL}" == "0" ]]; then
echo "${RESPONSE}" echo "${RESPONSE}"
NUMBER=$(echo "${RESPONSE}" | jq --raw-output '.number') NUMBER=$(echo "${RESPONSE}" | jq --raw-output '.number')
printf "Number opened for PR is ${NUMBER}\n" printf "Number opened for PR is ${NUMBER}\n"
# Remove leading and trailing quotes # Assignees are defined
ASSIGNEES=$(echo "$ASSIGNEES" | sed -e 's/^"//' -e 's/"$//') if [[ "$ASSIGNEES" != '""' ]; then
# Parse assignees into a list # Remove leading and trailing quotes
ASSIGNEES=$(echo $ASSIGNEES | sed -e 's/\(\w*\)/,"\1"/g' | cut -d , -f 2-) ASSIGNEES=$(echo "$ASSIGNEES" | sed -e 's/^"//' -e 's/"$//')
printf "Attempting to assign ${ASSIGNEES} to ${PR} with number ${NUMBER}"
# POST /repos/:owner/:repo/issues/:issue_number/assignees # Parse assignees into a list
DATA="{\"assignees\":[${ASSIGNEES}]}" ASSIGNEES=$(echo $ASSIGNEES | sed -e 's/\(\w*\)/,"\1"/g' | cut -d , -f 2-)
echo "${DATA}" printf "Attempting to assign ${ASSIGNEES} to ${PR} with number ${NUMBER}"
ASSIGNEES_URL="${ISSUE_URL}/${NUMBER}/assignees"
curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" --user "${GITHUB_ACTOR}" -X POST --data "${DATA}" ${ASSIGNEES_URL} # POST /repos/:owner/:repo/issues/:issue_number/assignees
printf "$?\n" 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
# Reviewers or team reviewers are defined
if [[ "$REVIEWERS" != '""' ]] || [[ "$TEAM_REVIEWERS" != '""' ]]; then
printf "Found reviewers: ${REVIEWERS} and team reviewers: ${TEAM_REVIEWERS}\n"
REVIEWERS=$(echo "$REVIEWERS" | sed -e 's/^"//' -e 's/"$//')
REVIEWERS=$(echo $REVIEWERS | sed -e 's/\(\w*\)/,"\1"/g' | cut -d , -f 2-)
TEAM_REVIEWERS=$(echo "$TEAM_REVIEWERS" | sed -e 's/^"//' -e 's/"$//')
TEAM_REVIEWERS=$(echo $TEAM_REVIEWERS | sed -e 's/\(\w*\)/,"\1"/g' | cut -d , -f 2-)
# If either is empty, don't include emty string (provide empty list)
if [[ "$REVIEWERS" == '""' ]]; then REVIEWERS=; fi
if [[ "$TEAM_REVIEWERS" == '""' ]]; then TEAM_REVIEWERS=; fi
# POST /repos/:owner/:repo/pulls/:pull_number/requested_reviewers
REVIEWERS_URL="${PULLS_URL}/${NUMBER}/requested_reviewers"
DATA="{\"reviewers\":[${REVIEWERS}], \"team_reviewers\":[${TEAM_REVIEWERS}]}"
echo "${DATA}"
curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" --user "${GITHUB_ACTOR}" -X POST --data "${DATA}" ${REVIEWERS_URL}
printf "$?\n"
fi
fi fi
fi fi
} }
@@ -144,7 +174,22 @@ main () {
ASSIGNEES="${PULL_REQUEST_ASSIGNEES}" ASSIGNEES="${PULL_REQUEST_ASSIGNEES}"
fi fi
# Reviewers (individual and team)
TEAM_REVIEWERS=""
REVIEWERS=""
if [ -z "${PULL_REQUEST_REVIEWERS}" ]; then
printf "PULL_REQUEST_REVIEWERS is not set, no reviewers.\n"
else
printf "PULL_REQUEST_REVIEWERS is set, ${PULL_REQUEST_REVIEWERS}\n"
REVIEWERS="${PULL_REQUEST_REVIEWERS}"
fi
if [ -z "${PULL_REQUEST_TEAM_REVIEWERS}" ]; then
printf "PULL_REQUEST_TEAM_REVIEWERS is not set, no team reviewers.\n"
else
printf "PULL_REQUEST_TEAM_REVIEWERS is set, ${PULL_REQUEST_TEAM_REVIEWERS}\n"
TEAM_REVIEWERS="${PULL_REQUEST_TEAM_REVIEWERS}"
fi
# The user is allowed to explicitly set the name of the branch # The user is allowed to explicitly set the name of the branch
if [ -z "${PULL_REQUEST_FROM_BRANCH}" ]; then if [ -z "${PULL_REQUEST_FROM_BRANCH}" ]; then
printf "PULL_REQUEST_FROM_BRANCH is not set, checking branch in payload.\n" printf "PULL_REQUEST_FROM_BRANCH is not set, checking branch in payload.\n"
@@ -190,7 +235,7 @@ main () {
create_pull_request "${BRANCH}" "${PULL_REQUEST_BRANCH}" "${PULL_REQUEST_BODY}" \ 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}" "${ASSIGNEES}" "${REVIEWERS}" "${TEAM_REVIEWERS}"
fi fi