github-clean-issue-types.py

  1#!/usr/bin/env python3
  2"""Replace 'bug/feature/crash' labels with 'Bug/Feature/Crash' types on open
  3GitHub issues.
  4
  5Requires `requests` library and a GitHub access token with "Issues (write)"
  6permission passed as an environment variable.
  7Was used as a quick-and-dirty one-off-bulk-operation script to clean up issue
  8types in the `zed` repository. Leaving it here for reference only; there's no
  9error handling, you've been warned.
 10"""
 11
 12
 13import logging
 14import os
 15
 16import requests
 17
 18logging.basicConfig(level=logging.INFO)
 19log = logging.getLogger(__name__)
 20
 21GITHUB_API_BASE_URL = "https://api.github.com"
 22REPO_OWNER = "zed-industries"
 23REPO_NAME = "zed"
 24GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
 25HEADERS = {
 26    "Authorization": f"token {GITHUB_TOKEN}",
 27    "Accept": "application/vnd.github+json"
 28}
 29LABELS_TO_TYPES = {
 30    'bug': 'Bug',
 31    'feature': 'Feature',
 32    'crash': 'Crash',
 33 }
 34
 35
 36def get_open_issues_without_type(repo):
 37    """Get open issues without type via GitHub's REST API."""
 38    issues = []
 39    issues_url = f"{GITHUB_API_BASE_URL}/repos/{REPO_OWNER}/{repo}/issues"
 40
 41    log.info("Start fetching issues from the GitHub API.")
 42    params = {
 43        "state": "open",
 44        "type": "none",
 45        "page": 1,
 46        "per_page": 100, # worked fine despite the docs saying 30
 47    }
 48    while True:
 49        response = requests.get(issues_url, headers=HEADERS, params=params)
 50        response.raise_for_status()
 51        issues.extend(response.json())
 52        log.info(f"Fetched the next page, total issues so far: {len(issues)}.")
 53
 54        # is there a next page?
 55        link_header = response.headers.get('Link', '')
 56        if 'rel="next"' not in link_header:
 57            break
 58        params['page'] += 1
 59
 60    log.info("Done fetching issues.")
 61    return issues
 62
 63
 64def replace_labels_with_types(issues, labels_to_types):
 65    """Replace labels with types, a new attribute of issues.
 66
 67    Only changes the issues with one type-sounding label, leaving those with
 68    two labels (e.g. `bug` *and* `crash`) alone, logging a warning.
 69    """
 70    for issue in issues:
 71        log.debug(f"Processing issue {issue['number']}.")
 72        # for GitHub, all PRs are issues but not all issues are PRs; skip PRs
 73        if 'pull_request' in issue:
 74            continue
 75        issue_labels = (label['name'] for label in issue['labels'])
 76        matching_labels = labels_to_types.keys() & set(issue_labels)
 77        if len(matching_labels) != 1:
 78            log.warning(
 79                f"Issue {issue['url']} has either no or multiple type-sounding "
 80                "labels, won't be processed.")
 81            continue
 82        label_to_replace = matching_labels.pop()
 83        issue_type = labels_to_types[label_to_replace]
 84        log.debug(
 85            f"Replacing label {label_to_replace} with type {issue_type} "
 86            f"for issue {issue['title']}.")
 87
 88        # add the type
 89        api_url_issue = f"{GITHUB_API_BASE_URL}/repos/{REPO_OWNER}/{REPO_NAME}/issues/{issue['number']}"
 90        add_type_response = requests.patch(
 91            api_url_issue, headers=HEADERS, json={"type": issue_type})
 92        add_type_response.raise_for_status()
 93        log.debug(f"Added type {issue_type} to issue {issue['title']}.")
 94
 95        # delete the label
 96        api_url_delete_label = f"{GITHUB_API_BASE_URL}/repos/{REPO_OWNER}/{REPO_NAME}/issues/{issue['number']}/labels/{label_to_replace}"
 97        delete_response = requests.delete(api_url_delete_label, headers=HEADERS)
 98        delete_response.raise_for_status()
 99        log.info(
100            f"Deleted label {label_to_replace} from issue {issue['title']}.")
101
102
103if __name__ == "__main__":
104    open_issues_without_type = get_open_issues_without_type(REPO_NAME)
105    replace_labels_with_types(open_issues_without_type, LABELS_TO_TYPES)