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

Community contribution label #14

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
40 changes: 40 additions & 0 deletions .github/workflows/community-contribution-label.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Community Contribution Label

on:
workflow_call:
secrets:
token:
required: true
jobs:
reusable-job:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
repository: iamshobhraj/.github
path: .github-repo

- name: Generate App Token
id: generate-token
uses: tibdex/github-app-token@v2
with:
app_id: ${{ secrets.LE_BOT_APP_ID }}
private_key: ${{ secrets.LE_BOT_PRIVATE_KEY }}

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install requests

- name: Add/Remove Community Contribution Label
run: python .github-repo/scripts/community-contribution-label.py
env:
token: ${{ steps.generate-token.outputs.token }}
GITHUB_EVENT_PATH: ${{ github.event_path }}
73 changes: 73 additions & 0 deletions scripts/community-contribution-label.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import os
import requests
import json

def get_user_type(owner, repo, username, token):
if not username:
return None

url = f'https://api.github.com/repos/{owner}/{repo}/collaborators/{username}'
headers = {"Authorization": f"token {token}"}
try:
response = requests.get(url, headers=headers)
if response.status_code == 204: # User is a collaborator
return "internal"
elif response.status_code == 404: # User is not a collaborator
return "external"
response.raise_for_status()
except requests.exceptions.RequestException as e:
print(f"Error checking user type: {e}")
return None

def add_label(owner, repo, issue_number, token):
url = f"https://api.github.com/repos/{owner}/{repo}/issues/{issue_number}/labels"
headers = {"Authorization": f"token {token}"}
data = {"labels": ["community-contribution-in-progress"]}
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()

def remove_label(owner, repo, issue_number, token):
url = f"https://api.github.com/repos/{owner}/{repo}/issues/{issue_number}/labels/community-contribution-in-progress"
headers = {"Authorization": f"token {token}"}
try:
response = requests.delete(url, headers=headers)
if response.status_code == 404:
print("Label not found")
return
response.raise_for_status()
print("Label removed")
except requests.exceptions.RequestException as e:
print(f"Error removing label: {e}")
raise

def main():
event_path = os.getenv("GITHUB_EVENT_PATH")
token = os.getenv("token")

if not event_path or not token:
raise Exception("GITHUB_EVENT_PATH and token must be set")

with open(event_path, "r") as f:
event_data = json.load(f)

issue = event_data["issue"]
issue_number = issue["number"]
repo = os.getenv("GITHUB_REPOSITORY")
owner, repo_name = repo.split("/")
action = event_data["action"]

current_assignees = [assignee["login"] for assignee in issue.get("assignees", [])]

has_external_assignee = False
for assignee in current_assignees:
if get_user_type(owner, repo_name, assignee, token) == "external":
has_external_assignee = True
break

if action == "assigned" and has_external_assignee:
add_label(owner, repo_name, issue_number, token)
elif action == "unassigned" and not has_external_assignee:
remove_label(owner, repo_name, issue_number, token)

if __name__ == "__main__":
main()