Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
This script will add the "hacktoberfest" label to any issue that also has one of the following labels applied: 'help wanted', 'first-timers-only', 'up-for-grabs'
#!/usr/bin/env python3
import os
import sys
import requests
import json
LABELS = ['help wanted', 'help-wanted', 'first-timers-only', 'up-for-grabs',
'good first issue']
class GitHubAPIError(Exception):
def usage():
print('Usage: {0} user/repo'.format(sys.argv[0]))
print('\nThis script will add the "hacktoberfest" label to any issue that '
'also has one of the following labels applied: {0}'.format(LABELS))
print('\nYour GitHub API token with the "repo" scope must be in '
'the "GITHUB_TOKEN" environmental variable.')
def check_hacktoberfest_label(repo, token):
url = API_BASE + 'repos/{0}/labels/hacktoberfest'.format(repo)
headers = {'Accept': 'application/vnd.github.v3+json',
'Authorization': 'token {0}'.format(token)}
r = requests.get(url, headers=headers)
resp = r.json()
if r.status_code == 404:
return False
elif r.status_code == 200 and 'hacktoberfest' in resp['name'].lower():
return True
raise GitHubAPIError(resp)
def create_hacktoberfest_label(repo, token):
url = API_BASE + 'repos/{0}/labels'.format(repo)
payload = {'name': 'hacktoberfest', 'color': 'EB06B0'}
headers = {'Accept': 'application/vnd.github.v3+json',
'Authorization': 'token {0}'.format(token),
'Content-type': 'application/json'}
r =, headers=headers, data=json.dumps(payload))
resp = r.json()
if r.status_code != 201:
raise GitHubAPIError(resp)
def apply_hacktoberfest_label(repo, token):
issues = get_labeled_issues(repo, token)
payload = '["hacktoberfest"]'
headers = {'Accept': 'application/vnd.github.v3+json',
'Authorization': 'token {0}'.format(token)}
for i in issues:
url = API_BASE + 'repos/{0}/issues/{1}/labels'.format(repo, i)
r =, headers=headers, data=payload)
resp = r.json()
if r.status_code != 200:
raise GitHubAPIError(resp)
def get_labeled_issues(repo, token):
issues = []
incomplete = True
url = API_BASE + 'search/issues'
headers = {'Accept': 'application/vnd.github.v3+json',
'Authorization': 'token {0}'.format(token)}
for label in LABELS:
incomplete = True
while incomplete:
payload = 'q=is:issue+is:open+repo:{0}+label:"{1}"'.format(repo,
r = requests.get(url, headers=headers, params=payload)
resp = r.json()
if r.status_code != 200:
raise GitHubAPIError(resp)
for i in resp['items']:
issue_labels = []
n = 0
for label in resp['items'][n]['labels']:
n += 1
if 'hacktoberfest' not in issue_labels:
if 'next' in r.links:
url = r.links['next']['url']
incomplete = False
return issues
def main(repo, token):
if not check_hacktoberfest_label(repo, token):
create_hacktoberfest_label(repo, token)
apply_hacktoberfest_label(repo, token)
if __name__ == "__main__":
token = os.environ['GITHUB_TOKEN']
except KeyError:
if token == '' or len(sys.argv) != 2 or '/' not in sys.argv[1]:
main(sys.argv[1], token)

This comment has been minimized.

Copy link

@sshaw sshaw commented Dec 22, 2018

Also batchlabels, which has an --hacktoberfest option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.