Skip to content

Latest commit

 

History

History
101 lines (77 loc) · 2.94 KB

GitHub_Close_issue.md

File metadata and controls

101 lines (77 loc) · 2.94 KB



Template request | Bug report | Generate Data Product

Tags: #github #issues #update #rest #api #snippet #operations

Author: Florent Ravenel

Description: This notebook explains how to close an issue on GitHub using the REST API.

References:

Input

Import libraries

import requests
import naas

Setup Variables

  • token: GitHub token
  • owner: owner of the repository
  • repo: name of the repository
  • issue_number: number of the issue
  • state_reason: The reason for the issue closed. Can be one of: completed, not_planned
# Inputs
token = naas.secret.get('GITHUB_TOKEN')
owner = "jupyter-naas"
repo = "awesome-notebooks"
issue_number = 99

# Outputs
state_reason = "not_planned"

Model

Update issue

This function updates an issue on GitHub using the REST API.

def update_issue(
    token,
    owner,
    repo,
    issue_number,
    title=None,
    body=None,
    state="closed",
    state_reason=None,
    labels=[],
    assignees=[],
):
    url = f"https://api.github.com/repos/{owner}/{repo}/issues/{issue_number}"
    headers = {"Authorization": f"token {token}"}
    data = {}
    if title:
        data["title"] = title
    if body:
        data["body"] = body
    if state:
        data["state"] = state
    if state_reason:
        data["state_reason"] = state_reason
    if len(labels) > 0:
        data["labels"] = labels
    if len(assignees) > 0:
        data["assignees"] = assignees
    
    # Send the PATCH request
    res = requests.patch(url, headers=headers, json=data)
    if res.status_code == 200:
        print(f"✅ Issue closed:", f"https://github.com/{owner}/{repo}/issues/{issue_number}")
        return res
    else:
        print(res.status_code, res.json())

Output

Display Result

update_issue(
    token,
    owner,
    repo,
    issue_number,
    state_reason=state_reason,
)