Template request | Bug report | Generate Data Product
Tags: #github #issues #update #rest #api #snippet #operations
Author: Florent Ravenel
Description: This notebook explains how to update 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 issuetitle
: The title of the issue.body
: The contents of the issue.state
: The open or closed state of the issue. Can be one of: open, closedstate_reason
: The reason for the state change. Ignored unless state is changed. Can be one of: completed, not_planned, reopened, nulllabels
: Labels to associate with this issue. Pass one or more labels to replace the set of labels on this issue. Send an empty array ([]) to clear all labels from the issue. Only users with push access can set labels for issues. Without push access to the repository, label changes are silently dropped.assignees
: Usernames to assign to this issue. Pass one or more user logins to replace the set of assignees on this issue. Send an empty array ([]) to clear all assignees from the issue. Only users with push access can set assignees for new issues. Without push access to the repository, assignee changes are silently dropped.
# Inputs
token = naas.secret.get('GITHUB_TOKEN')
owner = "jupyter-naas"
repo = "awesome-notebooks"
issue_number = 1810
# Outputs
title = "GitHub - Update an issue"
body = "This notebook explains how to update an issue on GitHub using the REST API. It is usefull for organizations that need to update issues on GitHub."
state = "open"
state_reason = None
labels = ["good first issue"]
assignees = ["FlorentLvr"]
This function updates an issue on GitHub using the REST API.
def update_issue(
token,
owner,
repo,
issue_number,
title=None,
body=None,
state=None,
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 updated:", 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,
title,
body,
state,
state_reason,
labels,
assignees,
)