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

GitHub.png

GitHub - List commits history from file path

Give Feedback | Bug report

Tags: #github #commits #history #snippet #operations #tracking

Last update: 2023-07-03 (Created: 2023-07-03)

Description: This notebook demonstrateshow to retrieve a list of commits containing a file path that exists in master branch.

Input

Import libraries

import requests import naas import pandas as pd

Setup Variables

  • token: Generate a personal access token

  • owner: The account owner of the repository. The name is not case sensitive.

  • repository: The name of the repository without the .git extension. The name is not case sensitive.

  • file_path: Only commits containing this file path will be returned.

token = naas.secret.get(name="GITHUB_TOKEN") or "YOUR_GITHUB_TOKEN" owner = "jupyter-naas" # Replace with the owner/username of the repository repository = "awesome-notebooks" # Replace with the name of the repository file_path = "generate_readme.ipynb" # Replace with the path to the file in the repository

Model

List commits history from file path

def list_commits(token, owner, repository, file_path): # Requests url = f"https://api.github.com/repos/{owner}/{repository}/commits?path={file_path}" headers = {"Authorization": f"token {token}"} response = requests.get(url, headers=headers) # Response res = requests.get(url) if res.status_code == 200: return res.json() else: print(res.json()) return {} commits = list_commits(token, owner, repository, file_path) print("Commits found:", len(commits))

Output

Display results

if len(commits) > 0: created_at = commits[-1]['commit']['committer']['date'] updated_at = commits[0]['commit']['committer']['date'] print("File first commit date:", created_at) print("File last commit date:", updated_at) else: print("File not found or no commits found for the file.") commits[0]