如何从GitHub API调用中获取文件的最新提交日期以及内容详细信息

时间:2019-01-16 10:22:29

标签: python git api github github-api-v3

我已经使用了下面的GitHub API,并且能够获取路径的文件详细信息。

https://github.***.com/api/v3/repos/exampleowner-Management/examplerepo/contents/Compile/Teradata/Tables?access_token=*****

此API调用的结果是:

[
{
    "name": ".DS_Store",
    "path": "Compile/Tables/test",
    "sha": "1cef8efa8694678e3b7ab230a6a891afa1a1996d",
    "size": 8196,
    "url": "***",
    "html_url": "***",
    "git_url": "***",
    "download_url": "***",
    "type": "file",
    "_links": {
        "self": "***",
        "git": "***",
        "html": "***"
    }
}]

我需要在此响应中获取有关sha的提交日期的详细信息。

“ sha”:“ 1cef8efa8694678e3b7ab230a6a891afa1a1996d”

我尝试使用另一个API:

https://github.***.com/api/v3/repos/exampleowner-Management/examplerepo/commits/1cef8efa8694678e3b7ab230a6a891afa1a1996d?access_token=*****

但此API对此sha的响应为:

{
"message": "Not Found",
"documentation_url": "https://developer.github.com/enterprise/2.14/v3/repos/commits/#get-a-single-commit"}

我们如何通过API调用获得提交日期详细信息以及GitHub内容详细信息?

1 个答案:

答案 0 :(得分:0)

使用Graphql最终获得了预期的结果。这是完整的代码

def run_query(query): # A simple function to use requests.post to make the API call. Note the json= section.
    try:
        request = requests.post('https://api.github.***.com/graphql', json={'query': query}, headers=headers)
        return request.json()
    except e:
        returnVal = '404'


            query = """
                        {
                          repository(owner: \""""+ownerVal+"""\", name: \""""+repoVal+"""\") {
                            object(expression: \""""+branchVal+"""\") {
                              ... on Commit {
                                blame(path: \""""+folderVal+"/"+data['name']+"""\") {
                                  ranges {
                                    commit {              
                                      committedDate            
                                    }
                                  }
                                }
                              }
                            }
                          }
                        }
                        """

headers = {"Authorization": "Bearer "+access_token}                        
result = run_query(query)

commit_date = result["data"]["repository"]["object"]["blame"]["ranges"][0]["commit"]["committedDate"]