Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update entrypoint.sh #94

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 14 additions & 83 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,83 +1,14 @@
#!/bin/bash
set -e

if [[ -z "$GITHUB_TOKEN" ]]; then
echo "Set the GITHUB_TOKEN env variable."
exit 1
fi

if [[ -z "$GITHUB_REPOSITORY" ]]; then
echo "Set the GITHUB_REPOSITORY env variable."
exit 1
fi

if [[ -z "$GITHUB_EVENT_PATH" ]]; then
echo "Set the GITHUB_EVENT_PATH env variable."
exit 1
fi

addLabel=$ADD_LABEL
if [[ -n "$LABEL_NAME" ]]; then
echo "Warning: Plase define the ADD_LABEL variable instead of the deprecated LABEL_NAME."
addLabel=$LABEL_NAME
fi

if [[ -z "$addLabel" ]]; then
echo "Set the ADD_LABEL or the LABEL_NAME env variable."
exit 1
fi

URI="https://api.github.com"
API_HEADER="Accept: application/vnd.github.v3+json"
AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}"

action=$(jq --raw-output .action "$GITHUB_EVENT_PATH")
state=$(jq --raw-output .review.state "$GITHUB_EVENT_PATH")
number=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")

label_when_approved() {
# https://developer.github.com/v3/pulls/reviews/#list-reviews-on-a-pull-request
body=$(curl -sSL -H "${AUTH_HEADER}" -H "${API_HEADER}" "${URI}/repos/${GITHUB_REPOSITORY}/pulls/${number}/reviews?per_page=100")
reviews=$(echo "$body" | jq --raw-output '.[] | {state: .state} | @base64')

approvals=0

for r in $reviews; do
review="$(echo "$r" | base64 -d)"
rState=$(echo "$review" | jq --raw-output '.state')

if [[ "$rState" == "APPROVED" ]]; then
approvals=$((approvals+1))
fi

echo "${approvals}/${APPROVALS} approvals"

if [[ "$approvals" -ge "$APPROVALS" ]]; then
echo "Labeling pull request"

curl -sSL \
-H "${AUTH_HEADER}" \
-H "${API_HEADER}" \
-X POST \
-H "Content-Type: application/json" \
-d "{\"labels\":[\"${addLabel}\"]}" \
"${URI}/repos/${GITHUB_REPOSITORY}/issues/${number}/labels"

if [[ -n "$REMOVE_LABEL" ]]; then
curl -sSL \
-H "${AUTH_HEADER}" \
-H "${API_HEADER}" \
-X DELETE \
"${URI}/repos/${GITHUB_REPOSITORY}/issues/${number}/labels/${REMOVE_LABEL}"
fi

break
fi
done
}

if [[ "$action" == "submitted" ]] && [[ "$state" == "approved" ]]; then
label_when_approved
else
echo "Ignoring event ${action}/${state}"
fi
on: pull_request_review
name: Label approved pull requests
jobs:
labelWhenApproved:
name: Label when approved
runs-on: ubuntu-latest
steps:
- name: Label when approved
uses: pullreminders/label-when-approved-action@master
env:
APPROVALS: "2"
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ADD_LABEL: "approved"
REMOVE_LABEL: "awaiting%20review"