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:
import requests
import naas
token
: GitHub tokenowner
: owner of the repositoryrepo
: name of the repositoryissue_number
: number of the issuestate_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"
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())
update_issue(
token,
owner,
repo,
issue_number,
state_reason=state_reason,
)