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

Added logic to check if a contributor has already contributed to good first issue #28

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This GitHub Action lets a prospective contributor assign 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.
- `recurringContributorMessage` <br /> The message to display to the user if they have already contributed to a good first issue.

## Setup

Expand Down
31 changes: 30 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ inputs:
required: false
default: 'The issue you are trying to assign to yourself is blocked by a one or more labels on the issue.'

recurringContributorMessage:
description: 'Message to contributors who have already contributed to a good first issue'
required: false
default: 'Thanks for your interest in this issue. It looks like this is a good first issue, and you've already contributed to the codebase. Let us leave this issue for a newcomer to the project,but please take a look at our other open issues if you would still like to contribute.'

trigger:
description: 'The string that triggers the action'
required: false
Expand All @@ -37,6 +42,18 @@ runs:
using: "composite"
steps:
- run: |
check_if_contributed_to_good_first_issue() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

refactor (blocking): for the good first issue check, all that needs to be checked is if they've ever contributed to the repository, regardless of the labels.

Copy link
Author

Choose a reason for hiding this comment

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

Would something like this make sense:

check_if_contributed_to_repository() {
           local assignee="$1"
           local response=$(curl -s -H "Authorization: token $GITHUB_PAT" "https://api.github.com/search/issues?q=is:pr+author:$assignee")
           local total_count=$(echo "$response" | jq -r '.total_count')

           if [ "$total_count" -gt 0 ]; then
               echo "true"
           else
               echo "false"
           fi
       }

Copy link
Collaborator

Choose a reason for hiding this comment

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

This checks all issues. What I'd suggest is not even PRs or issues. If they have at least one commit in the given project, that disqualifies them from contributing to a good first issue.

Here's a rough example care of ChatGPT (don't necessarily trust it)

# Set your GitHub username, repository name, owner username, and access token
USERNAME="your_username"
REPO="repository_name"
OWNER="repository_owner_username"
ACCESS_TOKEN="your_access_token"

# Make a cURL request to list commits in the repository by the specified user
curl -H "Authorization: token $ACCESS_TOKEN" \
     -H "Accept: application/vnd.github.v3+json" \
     "https://api.github.com/repos/$OWNER/$REPO/commits?author=$USERNAME"

# You can filter the response to check if the user has commits
# For example, you can use 'jq' to parse JSON response and count the number of commits
# Ensure you have 'jq' installed: https://stedolan.github.io/jq/download/
# Example:
# curl -H "Authorization: token $ACCESS_TOKEN" \
#      -H "Accept: application/vnd.github.v3+json" \
#      "https://api.github.com/repos/$OWNER/$REPO/commits?author=$USERNAME" | jq '. | length'

Copy link
Author

Choose a reason for hiding this comment

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

Okay this makes sense. Thanks for clarifying, I will try to take a look over the weekend.

Copy link
Collaborator

Choose a reason for hiding this comment

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

No rush @Haimantika, and thanks again for tackling this!

Copy link
Collaborator

Choose a reason for hiding this comment

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

@Haimantika, do you have a repository where you tested this? That would help speed up the review process.

Copy link
Author

Choose a reason for hiding this comment

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

Here you go - Haimantika/test_repo#6

Copy link
Collaborator

Choose a reason for hiding this comment

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

I created an issue without a good first issue label and I was able to take it, so existing functionality works. ✅

I tried taking an issue with a good first issue label however and it let me take it even though it previously didn't allow you to take it. See Haimantika/test_repo#6

Copy link
Author

Choose a reason for hiding this comment

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

@nickytonline I tried again now, works for me.

Copy link
Author

Choose a reason for hiding this comment

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

All the changes I made are in the workflow. If you think it is good to go, I will update the PR here.

local assignee="$1"
local response=$(curl -s -H "Authorization: token $GITHUB_PAT" "https://api.github.com/search/issues?q=is:pr+author:$assignee+label:\"good first issue\"")
local total_count=$(echo "$response" | jq -r '.total_count')

if [ "$total_count" -gt 0 ]; then
echo "true"
else
echo "false"
fi
}

BODY="$(jq '.comment.body' $GITHUB_EVENT_PATH)"
ISSUE_NUMBER="$(jq '.issue.number' $GITHUB_EVENT_PATH)"
LOGIN="$(jq '.comment.user.login' $GITHUB_EVENT_PATH | tr -d \")"
Expand All @@ -45,6 +62,7 @@ runs:
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'`
ASSIGNEES="$(jq -r '.issue.assignees[].login' "$GITHUB_EVENT_PATH")"

if [[ $BODY == *"$INPUT_TRIGGER"* ]]; then
if [[ "$ISSUE_CURRENTLY_ASSIGNED" == true ]]; then
Expand Down Expand Up @@ -85,12 +103,23 @@ runs:
curl -X POST -H "Authorization: bearer $GITHUB_PAT" --data @payload.json https://api.github.com/repos/$REPO/issues/$ISSUE_NUMBER/comments
fi
fi
fi
# Check if the user has already contributed to a good first issue
for ASSIGN in $ASSIGNEES; do
HAS_CONTRIBUTED=$(check_if_contributed_to_good_first_issue "$ASSIGN")
if [ "$HAS_CONTRIBUTED" = "true" ]; then
echo "Assignee $ASSIGN has already contributed to a good first issue."
echo "Responding with RECURRING_CONTRIBUTOR_MESSAGE..."
echo "$RECURRING_CONTRIBUTOR_MESSAGE"
break
fi
done

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 }}"
RECURRING_CONTRIBUTOR_MESSAGE: "${{ inputs.recurringContributorMessage }}"
GITHUB_PAT: "${{ inputs.token }}"