简体   繁体   中英

Use Github API to get the last commit of all repository's

I want to use the Github API for Python to be able to get every repository and check the last change to the repository.

import git
from git import Repo
from github import Github

repos = []

g = Github('Dextron12', 'password')
for repo in g.get_user().get_repos():
    repos.append(str(repo))

#check for last commit to repository HERE

This gets all repository's on my account but I want to be able to also get the last change to each one of them and I want a result like this:

13:46:45

I don't mind if it is 12 hour time either.

According to the documentation, the max info you can get is the SHA of the commit and the commit date:

https://pygithub.readthedocs.io/en/latest/examples/Commit.html#

with your example:

g = Github("usar", "pass")
for repo in g.get_user().get_repos():
    master = repo.get_branch("master")
    sha_com = master.commit
    commit = repo.get_commit(sha=sha_com)
    print(commit.commit.author.date)
from github import Github
from datetime import datetime

repos = {}

g = Github('username', 'password')
for repo in g.get_user().get_repos():
    master = repo.get_branch('master')
    sha_com = master.commit
    sha_com = str(sha_com).split('Commit(sha="')
    sha_com = sha_com[1].split('")')
    sha_com = sha_com[0]
    commit = repo.get_commit(sha_com)
    #get repository name
    repo = str(repo).split('Repository(full_name="Dextron12/')
    repo = repo[1].split('")')
    #CONVERT DATETIME OBJECT TO STRING
    timeObj = commit.commit.author.date
    timeStamp = timeObj.strftime("%d-%b-%Y (%H:%M:%S)")
    #ADD REPOSITORY NAME AND TIMESTAMP TO repos DICTIONARY
    repos[repo[0]] = timeStamp

print(repos)

I got the timeStamp by using the method Damian Lattenero suggested. upon testing his code I got a AssertationError this was because sha_commit was returning Commit=("sha") and not "sha". So I removed the brackets and Commit from the sha_com to be left with the sha all by itslef then I didn't receive that error and it worked. I then use datetime to convert the timestamp to a string and save it to a dictionary

@Dextron Just add .sha because its a property, No need to do split and form dictionary

g = Github("user", "pass")
for repo in g.get_user().get_repos():
    master = repo.get_branch("master")
    sha_com = master.commit
    commit = repo.get_commit(sha=sha_com.sha)
    print(commit.commit.author.date) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM