Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jupyter-naas
GitHub Repository: jupyter-naas/awesome-notebooks
Path: blob/master/GitHub/GitHub_List_branches_with_open_PR.ipynb
2973 views
Kernel: Python 3

GitHub.png

GitHub - List branches with open PR

Give Feedback | Bug report

Tags: #github #branches #list #api #rest #python #active

Last update: 2023-06-26 (Created: 2023-06-26)

Description: This notebook will list branches with open PR from a given GitHub repository.

Input

Import libraries

import naas import pandas as pd import requests from pprint import pprint

Setup Variables

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

Model

List branches with open PR's

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)

Output

Display result

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)