Skip to content

Latest commit

 

History

History
86 lines (59 loc) · 3.01 KB

GitHub_List_branches_with_open_PR.md

File metadata and controls

86 lines (59 loc) · 3.01 KB



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:

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)