0

我使用了下面的 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 的提交日期详细信息。

“沙”:“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 内容详细信息?

4

1 回答 1

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"]
于 2019-01-19T04:39:50.707 回答