Workflow file for this run
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: Workflow As Kubernetes | |
on: | |
issue_comment: | |
types: | |
- created | |
pull_request_target: | |
types: | |
- labeled | |
- unlabeled | |
- synchronize | |
pull_request_review_comment: | |
types: | |
- created | |
env: | |
GH_TOKEN: ${{ secrets.AGENT_TOKEN }} | |
jobs: | |
on-new-push: | |
if: github.event_name == 'pull_request_target' && github.event.action == 'synchronize' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Remove lgtm label on new push | |
run: | | |
PR_NUMBER=${{ github.event.pull_request.number }} | |
if [ -z "$PR_NUMBER" ]; then | |
echo "No PR number found in push event context." | |
exit 0 | |
fi | |
if gh pr view $PR_NUMBER --json labels --jq '.labels[].name' | grep -q "lgtm"; then | |
gh pr edit $PR_NUMBER --remove-label lgtm | |
echo "Removed 'lgtm' label due to new push." | |
fi | |
# Work for issue & PR, ignore update for now. | |
on-new-comment: | |
if: (github.event_name == 'issue_comment' || github.event_name == 'pull_request_review_comment') && github.event.action == 'created' | |
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 Issue 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) | |
# Initialize arrays to store the labels. | |
LABELS_TO_ADD=() | |
LABELS_TO_REMOVE=() | |
while IFS= read -r COMMENT_LINE; do | |
COMMENT_LINE=$(echo "$COMMENT_LINE" | sed 's/^[ \t]*//;s/[ \t]*$//' | tr -d '\r\n' | awk '{$1=$1};1') | |
LABEL=$(echo "$COMMENT_LINE" | awk '{print $2}') | |
if [[ ! "$COMMENT_LINE" =~ ^/ ]]; then | |
continue | |
fi | |
# Handle lgtm command | |
if [[ "$COMMENT_LINE" =~ ^/lgtm ]]; then | |
if [[ "$LABEL" =~ ^cancel ]]; then | |
LABELS_TO_REMOVE+=("lgtm") | |
echo "To remove label: lgtm" | |
else | |
LABELS_TO_ADD+=("lgtm") | |
echo "To add label: lgtm" | |
fi | |
# Handle approved command | |
elif [[ "$COMMENT_LINE" =~ ^/approve ]]; then | |
if yq ".approvers[] | select(. == \"$COMMENT_USER\") " OWNERS | grep -q "$COMMENT_USER"; then | |
if [[ "$LABEL" =~ ^cancel ]]; then | |
LABELS_TO_REMOVE+=("approved") | |
echo "To remove label: approved" | |
else | |
LABELS_TO_ADD+=("approved") | |
echo "To add label: approved" | |
fi | |
else | |
gh pr comment $PR_NUMBER --body "Sorry, @$COMMENT_USER is not authorized to approve/unapprove this PR." | |
echo "User $COMMENT_USER is not authorized to approve/unapprove this PR" | |
fi | |
# Handle hold command | |
# hold/unhold the PR immediately in case of unexpected auto-merge. | |
elif [[ "$COMMENT_LINE" =~ ^/hold ]]; then | |
if [[ "$LABEL" =~ ^cancel ]]; then | |
gh pr edit $PR_NUMBER --remove-label do-not-merge/hold | |
echo "Remove label: do-not-merge/hold" | |
else | |
gh pr edit $PR_NUMBER --add-label do-not-merge/hold | |
echo "Add label: do-not-merge/hold" | |
fi | |
# Handle kind command | |
elif [[ "$COMMENT_LINE" =~ ^/kind ]]; then | |
if [[ "$LABEL" =~ ^lgtm || "$LABEL" =~ ^approve ]]; then | |
echo "The label '$LABEL' cannot be added using /kind." | |
else | |
LABELS_TO_ADD+=("$LABEL") | |
echo "To add label: '$LABEL'" | |
fi | |
elif [[ "$COMMENT_LINE" =~ ^/remove-kind ]]; then | |
if [[ "$LABEL" =~ ^lgtm || "$LABEL" =~ ^approve ]]; then | |
echo "The label '$LABEL' cannot be removed using /remove-kind." | |
else | |
LABELS_TO_REMOVE+=("$LABEL") | |
echo "To remove label: '$LABEL'" | |
fi | |
# Handle assignment | |
elif [[ "$COMMENT_LINE" =~ ^/assign ]]; then | |
USERNAME=$(echo "$LABEL" | sed 's/@//') | |
if [[ -n "$USERNAME" ]]; then | |
if [ -n "${{ github.event.issue }}" ]; then | |
gh issue edit $PR_NUMBER --add-assignee "$USERNAME" | |
echo "Assign to $USERNAME" | |
elif [ -n "${{ github.event.pull_request }}" ]; then | |
gh pr edit $PR_NUMBER --add-assignee "$USERNAME" | |
echo "Assign to $USERNAME" | |
else | |
echo "unexpected event type" | |
fi | |
else | |
echo "No username provided for assignment." | |
fi | |
elif [[ "$COMMENT_LINE" =~ ^/unassign ]]; then | |
USERNAME=$(echo "$LABEL" | sed 's/@//') | |
if [[ -n "$USERNAME" ]]; then | |
if [ -n "${{ github.event.issue }}" ]; then | |
gh issue edit $PR_NUMBER --remove-assignee "$USERNAME" | |
echo "Unassign to $USERNAME" | |
elif [ -n "${{ github.event.pull_request }}" ]; then | |
gh pr edit $PR_NUMBER --remove-assignee "$USERNAME" | |
echo "Unassign to $USERNAME" | |
else | |
echo "unexpected event type" | |
fi | |
else | |
echo "No username provided for unassignment." | |
fi | |
# Handle help & good-first-issue | |
elif [[ "$COMMENT_LINE" =~ ^/help ]]; then | |
LABELS_TO_ADD+=("help wanted") | |
elif [[ "$COMMENT_LINE" =~ ^/remove-help ]]; then | |
LABELS_TO_REMOVE+=("help wanted") | |
elif [[ "$COMMENT_LINE" =~ ^/good-first-issue ]]; then | |
LABELS_TO_ADD+=("good first issue") | |
elif [[ "$COMMENT_LINE" =~ ^/remove-good-first-issue ]]; then | |
LABELS_TO_REMOVE+=("good first issue") | |
else | |
echo "$COMMENT_LINE is not supported" | |
fi | |
done <<< "$COMMENT_BODY" | |
# Convert array to comma-separated strings. | |
LABELS_TO_ADD_STR=$(IFS=,; echo "${LABELS_TO_ADD[*]}") | |
LABELS_TO_REMOVE_STR=$(IFS=,; echo "${LABELS_TO_REMOVE[*]}") | |
if [[ -n "$LABELS_TO_ADD_STR" ]]; then | |
if [ -n "${{ github.event.issue }}" ]; then | |
gh issue edit $PR_NUMBER --add-label "$LABELS_TO_ADD_STR" | |
elif [ -n "${{ github.event.pull_request }}" ]; then | |
gh pr edit $PR_NUMBER --add-label "$LABELS_TO_ADD_STR" | |
else | |
exit 0 | |
fi | |
fi | |
if [[ -n "$LABELS_TO_REMOVE_STR" ]]; then | |
if [ -n "${{ github.event.issue }}" ]; then | |
gh issue edit $PR_NUMBER --remove-label "$LABELS_TO_REMOVE_STR" | |
elif [ -n "${{ github.event.pull_request }}" ]; then | |
gh pr edit $PR_NUMBER --remove-label "$LABELS_TO_REMOVE_STR" | |
else | |
exit 0 | |
fi | |
fi | |
on-pr-merge: | |
if: github.event_name == 'pull_request_target' && (github.event.action == 'labeled' || github.event.action == 'unlabeled') && github.event.pull_request.merged == false | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
# TODO: Before merge, better to run the tests again. | |
- name: Merge PR if approved and lgtm | |
run: | | |
PR_NUMBER=${{ github.event.pull_request.number }} | |
LABELS=$(gh pr view $PR_NUMBER --json labels --jq '.labels[].name') | |
if [[ "$LABELS" == *"lgtm"* && "$LABELS" == *"approved"* && "$LABELS" != *"do-not-merge/hold"* ]]; then | |
gh pr merge $PR_NUMBER --merge | |
echo "Merge!" | |
else | |
echo "Not ready to merge" | |
fi |