trim \n #60
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: PR Actions | |
on: | |
issue_comment: | |
types: [created] | |
pull_request: | |
types: [labeled] | |
env: | |
GH_TOKEN: ${{ secrets.AGENT_TOKEN }} | |
jobs: | |
handle-comments: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Set up Python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: '3.x' | |
- name: Install yq | |
run: pip install yq | |
- name: Handle PR Comments | |
run: | | |
COMMENT_BODY=$(jq -r '.comment.body' $GITHUB_EVENT_PATH) | |
PR_NUMBER=$(jq -r '.issue.number' $GITHUB_EVENT_PATH) | |
COMMENT_USER=$(jq -r '.comment.user.login' $GITHUB_EVENT_PATH) | |
while IFS= read -r COMMENT_LINE; do | |
COMMENT_LINE=$(echo "$COMMENT_LINE" | sed ':a;N;$!ba;s/\n//g' | awk '{$1=$1};1') | |
if [[ "$COMMENT_LINE" == "/lgtm" ]]; then | |
gh pr edit $PR_NUMBER --add-label lgtm | |
echo "Add label: lgtm" | |
elif [[ "$COMMENT_LINE" == "/approve" ]]; then | |
if yq ".approvers[] | select(. == \"$COMMENT_USER\") " OWNERS | grep -q "$COMMENT_USER"; then | |
gh pr edit $PR_NUMBER --add-label approved | |
echo "Add label: approved" | |
else | |
echo "User $COMMENT_USER is not authorized to approve" | |
fi | |
elif [[ "$COMMENT_LINE" == "/lgtm cancel" ]]; then | |
if gh pr view $PR_NUMBER --json labels --jq '.labels[].name' | grep -q "lgtm"; then | |
gh pr edit $PR_NUMBER --remove-label lgtm | |
echo "Remove label: lgtm" | |
else | |
echo "Label 'lgtm' does not exist" | |
fi | |
elif [[ "$COMMENT_LINE" == "/approve cancel" ]]; then | |
if gh pr view $PR_NUMBER --json labels --jq '.labels[].name' | grep -q "approved"; then | |
if yq ".approvers[] | select(. == \"$COMMENT_USER\") " OWNERS | grep -q "$COMMENT_USER"; then | |
gh pr edit $PR_NUMBER --remove-label approved | |
echo "Remove label: approved" | |
else | |
echo "User $COMMENT_USER is not authorized to cancel the approved label" | |
fi | |
else | |
echo "Label 'approved' does not exist" | |
fi | |
elif [[ "$COMMENT_LINE" =~ ^/kind ]]; then | |
LABEL=$(echo "$COMMENT_LINE" | awk '{print $2}') | |
if [[ "$LABEL" == "lgtm" || "$LABEL" == "approve" ]]; then | |
echo "The label '$LABEL' cannot be added using /kind." | |
else | |
gh pr edit $PR_NUMBER --add-label $LABEL | |
echo "Add label: '$LABEL'" | |
fi | |
elif [[ "$COMMENT_LINE" =~ ^/remove-kind ]]; then | |
LABEL=$(echo "$COMMENT_LINE" | awk '{print $2}') | |
if [[ "$LABEL" == "lgtm" || "$LABEL" == "approve" ]]; then | |
echo "The label '$LABEL' cannot be removed using /remove-kind." | |
else | |
if gh pr view $PR_NUMBER --json labels --jq '.labels[].name' | grep -q $LABEL; then | |
gh pr edit $PR_NUMBER --remove-label $LABEL | |
echo "Remove label: '$LABEL'" | |
fi | |
fi | |
else | |
echo "$COMMENT_LINE is not supported" | |
fi | |
done <<< "$COMMENT_BODY" | |
- name: Merge PR if LGTMed and approved | |
run: | | |
PR_NUMBER=$(jq -r '.issue.number' $GITHUB_EVENT_PATH) | |
LABELS=$(gh pr view $PR_NUMBER --json labels --jq '.labels[].name') | |
if [[ "$LABELS" == *"lgtm"* && "$LABELS" == *"approved"* ]]; then | |
gh pr merge $PR_NUMBER --merge | |
else | |
echo "Not ready to merge" | |
fi |