From 1a853b4e3fabcf2808ea0c3aca9b66f58b6aa99e Mon Sep 17 00:00:00 2001 From: vsoch Date: Mon, 23 Mar 2020 13:36:27 -0600 Subject: [PATCH 1/3] testing change to add reviewers (team or individual) to a PR Signed-off-by: vsoch --- README.md | 5 +++- pull-request.sh | 75 +++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 64 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index ff5aa3e..65f7070 100644 --- a/README.md +++ b/README.md @@ -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 | | 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_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 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 `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 an issue or PR, they are ignored otherwise. diff --git a/pull-request.sh b/pull-request.sh index 12111a7..1fddbcb 100755 --- a/pull-request.sh +++ b/pull-request.sh @@ -53,6 +53,8 @@ create_pull_request() { DRAFT="${5}" # pull request draft? MODIFY="${6}" # maintainer can modify 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 DATA="{\"base\":${TARGET}, \"head\":${SOURCE}, \"body\":${BODY}}" @@ -73,26 +75,54 @@ create_pull_request() { 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 + # if we were successful to open, add assignees and reviewers + if [[ "${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/"$//') + # Assignees are defined + if [[ "$ASSIGNEES" != '""' ]; then - # 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}" + # Remove leading and trailing quotes + ASSIGNEES=$(echo "$ASSIGNEES" | sed -e 's/^"//' -e 's/"$//') - # 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" + # 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 + + # 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 } @@ -144,7 +174,22 @@ main () { ASSIGNEES="${PULL_REQUEST_ASSIGNEES}" 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 if [ -z "${PULL_REQUEST_FROM_BRANCH}" ]; then 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}" \ "${PULL_REQUEST_TITLE}" "${PULL_REQUEST_DRAFT}" "${MODIFY}" \ - "${ASSIGNEES}" + "${ASSIGNEES}" "${REVIEWERS}" "${TEAM_REVIEWERS}" fi From a61c2bcfb743a85d7cafd29ba7f8bec2fa392abe Mon Sep 17 00:00:00 2001 From: vsoch Date: Mon, 23 Mar 2020 13:38:49 -0600 Subject: [PATCH 2/3] syntax error Signed-off-by: vsoch --- pull-request.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pull-request.sh b/pull-request.sh index 1fddbcb..d1a1bf2 100755 --- a/pull-request.sh +++ b/pull-request.sh @@ -84,7 +84,7 @@ create_pull_request() { printf "Number opened for PR is ${NUMBER}\n" # Assignees are defined - if [[ "$ASSIGNEES" != '""' ]; then + if [[ "$ASSIGNEES" != '""' ]]; then # Remove leading and trailing quotes ASSIGNEES=$(echo "$ASSIGNEES" | sed -e 's/^"//' -e 's/"$//') From 4a1e5881646da23a4bde04334368a926ac8e91b8 Mon Sep 17 00:00:00 2001 From: vsoch Date: Mon, 23 Mar 2020 13:43:20 -0600 Subject: [PATCH 3/3] adding example for reviewers and changelog This PR will add the ability to specify one or more individual users or team names to the environment to request for review. Signed-off-by: vsoch --- CHANGELOG.md | 18 ++++++++++++++++++ examples/reviewers-example.yml | 17 +++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 CHANGELOG.md create mode 100644 examples/reviewers-example.yml diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..e1d3138 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,18 @@ +# CHANGELOG + +This is a manually generated log to track changes to the repository for each release. +Each section should include general headers such as **Implemented enhancements** +and **Merged pull requests**. All closed issued and bug fixes should be +represented by the pull requests that fixed them. Critical items to know are: + + - renamed commands + - deprecated / removed commands + - changed defaults + - backward incompatible changes + + +Versions correspond with GitHub releases that can be referenced with @ using actions. + +## [master](https://github.com/vsoch/pull-request-action/tree/master) (master) + - added support for reviewer (individual and team) assignments (1.0.4) + - added support for maintainer can modify and assignees (1.0.3) diff --git a/examples/reviewers-example.yml b/examples/reviewers-example.yml new file mode 100644 index 0000000..7dc0ca6 --- /dev/null +++ b/examples/reviewers-example.yml @@ -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.4 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + BRANCH_PREFIX: "update/" + PULL_REQUEST_BRANCH: "master" + PULL_REQUEST_REVIEWERS: vsoch