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

GitHub.png

GitHub - List branches

Give Feedback | Bug report

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

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

Description: This notebook will list all branches from a given GitHub repository;

Input

Import libraries

import naas import pandas as pd try: from github import Github except: !pip install PyGithub --user from github import Github from pprint import pprint

Setup Variables

token = naas.secret.get(name="GITHUB_TOKEN") or "YOUR_GITHUB_TOKEN" owner = "jupyter-naas" #This is an example for naas repository = "awesome-notebooks" #This is an example for naas

Model

List branches

def list_branches(owner, repository): g = Github(token) try: repo = g.get_repo(f"{owner}/{repository}") branches = repo.get_branches() data = [] for branch in branches: branch_data = { 'branch': branch.name, 'creation_date': branch.commit.commit.author.date, 'creator': branch.commit.commit.author.name } data.append(branch_data) df = pd.DataFrame(data) return df except Exception as e: print(f"An error occurred: {e}") return pd.DataFrame() branches_df = list_branches(owner, repository)

Output

Display result

print(f"Branches for {owner}/{repository}:", len(branches_df)) print("-" * 50) pprint(branches_df['branch'].tolist()) print("-" * 50)