Template request | Bug report | Generate Data Product
Tags: #github #branches #list #api #rest #python #active
Author: Benjamin Filly
Description: This notebook will list branches with open PR from a given GitHub repository.
References:
import naas
import pandas as pd
import requests
from pprint import pprint
token
: Generate a personal access tokenowner
: owner of the repositoryrepository
: name of the repository
token = naas.secret.get(name="GITHUB_TOKEN") or "YOUR_GITHUB_TOKEN"
owner = "jupyter-naas" #Example for naas
repository = "awesome-notebooks" #Example for naas awesome-notebooks repository
This function will list get active branches, creator and Creation date from a given GitHub repository.
def get_branches_with_open_prs(
token,
owner,
repository
):
url = f"https://api.github.com/repos/{owner}/{repository}/pulls"
headers = {"Authorization": f"token {token}"}
response = requests.get(url, headers=headers)
pulls = response.json()
branches_data = []
for pull in pulls:
branch = pull['head']['ref']
creator = pull['user']['login']
creation_date = pull['created_at']
branches_data.append({
'branch': branch,
'creator': creator,
'creation_date': creation_date
})
branches_df = pd.DataFrame(branches_data)
return branches_df
branches_with_open_prs = get_branches_with_open_prs(token, owner, repository)
print(f"Branches with open pull requests in {owner}/{repository}:", len(branches_with_open_prs))
print("-" * 70)
pprint(branches_with_open_prs['branch'].tolist())
print("-" * 70)