Skip to content

Commit

Permalink
First-time contributor github action (#16762)
Browse files Browse the repository at this point in the history
* First draft of first-time contributor action

* Try using the github search api

* Minor tweaks
  • Loading branch information
talldan committed Jul 31, 2019
1 parent 823ba36 commit 9160749
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .github/actions/first-time-contributor/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM debian:stable-slim

LABEL "name"="First Time Contributor"
LABEL "maintainer"="The WordPress Contributors"
LABEL "version"="1.0.0"

LABEL "com.github.actions.name"="First Time Contributor"
LABEL "com.github.actions.description"="Assigns the first time contributor label to pull requests"
LABEL "com.github.actions.icon"="award"
LABEL "com.github.actions.color"="green"

RUN apt-get update && \
apt-get install --no-install-recommends -y jq curl ca-certificates && \
apt-get clean -y

COPY entrypoint.sh /entrypoint.sh

ENTRYPOINT [ "/entrypoint.sh" ]
35 changes: 35 additions & 0 deletions .github/actions/first-time-contributor/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash
set -e

# 1. Get the author and pr number for the pull request.
author=$(jq -r '.pull_request.user.login' $GITHUB_EVENT_PATH)
pr_number=$(jq -r '.number' $GITHUB_EVENT_PATH)

if [ "$pr_number" = "null" ] || [ "$author" = "null" ]; then
echo "Could not find PR number or author. $pr_number / $author"
exit 78
fi

# 2. Fetch the author's commit count for the repo to determine if they're a first-time contributor.
commit_count=$(
curl \
--silent \
-H "Accept: application/vnd.github.cloak-preview" \
"https://github.com/gitapi/search/commits?q=repo:$GITHUB_REPOSITORY+author:$author" \
| jq -r '.total_count'
)

# 3. If the response has a commit count of zero, exit early, the author is not a first time contributor.
if [ "$commit_count" != "0" ]; then
echo "Pull request #$pr_number was not created by a first-time contributor ($author)."
exit 78
fi

# 4. Assign the 'First Time Contributor' label.
curl \
--silent \
-X POST \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Content-Type: application/json" \
-d '{"labels":["First-time Contributor"]}' \
"https://github.com/gitapi/repos/$GITHUB_REPOSITORY/issues/$pr_number/labels" > /dev/null
11 changes: 11 additions & 0 deletions .github/main.workflow
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,14 @@ action "Assign Fixed Issues" {
needs = ["Filter opened"]
secrets = ["GITHUB_TOKEN"]
}

workflow "Add the First-time Contributor label to PRs opened by first-time contributors" {
on = "pull_request"
resolves = ["First Time Contributor"]
}

action "First Time Contributor" {
uses = "./.github/actions/first-time-contributor"
needs = ["Filter opened"]
secrets = ["GITHUB_TOKEN"]
}

0 comments on commit 9160749

Please sign in to comment.