【问题标题】:How to get the latest commit date of the file along with content details from GitHub API call如何从 GitHub API 调用中获取文件的最新提交日期以及内容详细信息
【发布时间】:2019-01-16 10:22:29
【问题描述】:

我使用了下面的 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 内容详细信息?

【问题讨论】:

  • 有没有办法使用 GitHub API 调用获取 GitHub 文件的下载 url、提交 id 和提交日期详细信息?

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


【解决方案1】:

通过使用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"]

【讨论】:

    猜你喜欢
    • 2015-07-02
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    相关资源
    最近更新 更多