Skip to content

Commit

Permalink
feat: add support for blocking labels (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickytonline committed Oct 19, 2023
1 parent 52b3bb9 commit 991e8e7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
This GitHub Action lets a prospective contributor assign themselves to an issue, and optionally leaves a comment on the issue.

- `message`<br />The message to display to the user once they have assigned themselves to an issue.
- `blockingLabels`<br />A comma-separated list of labels that will prevent the action from running if they are present on the issue.
- `blockingLabelsMessage`<br />The message to display to the user if the issue has a blocking label.
- `trigger`<br />The string that take action will search for in the comment body to activate the action.

## Setup
Expand All @@ -17,15 +19,15 @@ The Action must be given a PAT with permission to write to Issues in the `token`
The easiest way is to use the built-in `${{ secrets.GITHUB_TOKEN }}` for authentication (as per the example below), but you'll need to ensure you've appropriately set [the permissions for the GitHub Token](https://docs.github.com/en/actions/security-guides/automatic-token-authentication#permissions-for-the-github_token) so that your workflow can update Issues.

To do this, follow the instructions in this doc: [Managing GitHub Actions Permissions for your repository](https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/enabling-features-for-your-repository/managing-github-actions-settings-for-a-repository#managing-github-actions-permissions-for-your-repository).
You can also configure `message:` below to be a custom message. Note that you cannot use words like `it's` or `let's` as the apostrophe messes with the syntax. You can use emojis, however you'll need to copy and paste the emoji directly like `❤️` instead of `:heart:` as the semi-colons ruin the syntax.

You can also configure `message:` below to be a custom message. Note that you cannot use words like `it's` or `let's` as the apostrophe messes with the syntax. You can use emojis, however you'll need to copy and paste the emoji directly like `❤️` instead of `:heart:` as the semi-colons ruin the syntax.

### Example Workflow:

```yaml
# .github/workflows/take.yml
# .github/workflows/take.yml
name: Assign issue to contributor
on:
on:
issue_comment:

jobs:
Expand Down
56 changes: 49 additions & 7 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ inputs:
description: 'Message to contributors if issue is already assigned'
required: false
default: 'The issue you are trying to assign to yourself is already assigned.'

blockingLabels:
description: 'Optional labels that are on an issue that block an issue from being assigned by the trigger.'
required: false
default: ''

blockingLabelsMessage:
description: 'Message to contributors if issue is blocked by a label'
required: false
default: 'The issue you are trying to assign to yourself is blocked by a one or more labels on the issue.'

trigger:
description: 'The string that triggers the action'
required: false
Expand All @@ -31,17 +42,46 @@ runs:
LOGIN="$(jq '.comment.user.login' $GITHUB_EVENT_PATH | tr -d \")"
REPO="$(jq '.repository.full_name' $GITHUB_EVENT_PATH | tr -d \")"
ISSUE_JSON="$(jq '.issue' $GITHUB_EVENT_PATH)"
ISSUE_LABELS="$(jq -r '.issue.labels[].name' $GITHUB_EVENT_PATH)"
ISSUE_LABELS=$(echo "$ISSUE_LABELS" | jq -n --arg str "$ISSUE_LABELS" '$str | split("\n")')
ISSUE_CURRENTLY_ASSIGNED=`echo $ISSUE_JSON | jq '.assignees | length == 0'`
if [[ $BODY == *"$INPUT_TRIGGER"* ]]; then
if [[ "$ISSUE_CURRENTLY_ASSIGNED" == true ]]; then
echo "Is issue currently assigned: $ISSUE_CURRENTLY_ASSIGNED"
echo "Assigning issue $ISSUE_NUMBER to $LOGIN"
echo "Using the link: https://api.github.com/repos/$REPO/issues/$ISSUE_NUMBER/assignees"
curl -H "Authorization: bearer $GITHUB_PAT" -d '{"assignees":["'"$LOGIN"'"]}' https://api.github.com/repos/$REPO/issues/$ISSUE_NUMBER/assignees
if [[ ! -z $INPUT_MESSAGE ]]; then
jq -n -r --arg body "$INPUT_MESSAGE" '{body: $body}' > payload.json
curl -X POST -H "Authorization: bearer $GITHUB_PAT" --data @payload.json https://api.github.com/repos/$REPO/issues/$ISSUE_NUMBER/comments
if [ -n "$RAW_BLOCKING_LABELS" ]; then
BLOCKING_LABELS=$(echo "$RAW_BLOCKING_LABELS" | jq -n --arg str "$RAW_BLOCKING_LABELS" '$str | split(",")')
else
# In case no blocking labels are set, set an empty array
BLOCKING_LABELS=()
fi
echo "Issue labels: $ISSUE_LABELS"
echo "Blocking labels: $BLOCKING_LABELS"
# See if at least one label on the issue is in the list of blocking labels
blocking_label_found=$(jq -n --argjson ISSUE_LABELS "$ISSUE_LABELS" --argjson BLOCKING_LABELS "$BLOCKING_LABELS" '$ISSUE_LABELS as $il | $BLOCKING_LABELS as $bl | any($il[]; . as $i | any($bl[]; . == $i))')
echo "blocking label found: $blocking_label_found"
if [ "$blocking_label_found" == true ]; then
echo "Issue contains one or more blocking labels: $BLOCKING_LABELS"
echo "Unable to assign issue $ISSUE_NUMBER to $LOGIN"
# Post a comment on the issue that there are blocking labels on the issue preventing it from being assigned
if [[ ! -z $BLOCKING_LABELS_MESSAGE ]]; then
jq -n -r --arg body "$BLOCKING_LABELS_MESSAGE" '{body: $body}' > payload.json
curl -X POST -H "Authorization: bearer $GITHUB_PAT" --data @payload.json https://api.github.com/repos/$REPO/issues/$ISSUE_NUMBER/comments
fi
else
# Assign the issue to the user
echo "Is issue currently assigned: $ISSUE_CURRENTLY_ASSIGNED"
echo "Assigning issue $ISSUE_NUMBER to $LOGIN"
echo "Using the link: https://api.github.com/repos/$REPO/issues/$ISSUE_NUMBER/assignees"
curl -H "Authorization: bearer $GITHUB_PAT" -d '{"assignees":["'"$LOGIN"'"]}' https://api.github.com/repos/$REPO/issues/$ISSUE_NUMBER/assignees
if [[ ! -z $INPUT_MESSAGE ]]; then
jq -n -r --arg body "$INPUT_MESSAGE" '{body: $body}' > payload.json
curl -X POST -H "Authorization: bearer $GITHUB_PAT" --data @payload.json https://api.github.com/repos/$REPO/issues/$ISSUE_NUMBER/comments
fi
fi
else
echo "This issue is currently assigned to a different user"
Expand All @@ -54,6 +94,8 @@ runs:
shell: bash
env:
INPUT_MESSAGE: "${{ inputs.message }}"
RAW_BLOCKING_LABELS: "${{ inputs.blockingLabels }}"
BLOCKING_LABELS_MESSAGE: "${{ inputs.blockingLabelsMessage }}"
INPUT_TRIGGER: "${{ inputs.trigger }}"
ISSUE_CURRENTLY_ASSIGNED_MESSAGE: "${{ inputs.issueCurrentlyAssignedMessage }}"
GITHUB_PAT: "${{ inputs.token }}"

0 comments on commit 991e8e7

Please sign in to comment.