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

feat: add support for blocking labels #24

Merged
merged 6 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
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:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like prettier cleaned up some trailing white space

on:
issue_comment:

jobs:
Expand Down
48 changes: 41 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:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default, no blocking labels

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

blockingLabelsMessage:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A default message about blocking labels that can be customized.

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,38 @@ 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")')
Comment on lines +45 to +46
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gets the issue labels as an array.

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
BLOCKING_LABELS=$(echo "$RAW_BLOCKING_LABELS" | jq -n --arg str "$RAW_BLOCKING_LABELS" '$str | split(",")')
nickytonline marked this conversation as resolved.
Show resolved Hide resolved

echo "Issue labels: $ISSUE_LABELS"
echo "Blocking labels: $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"

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
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 +86,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 }}"