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

2024-09-26 22:50:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用了下面的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的响应是:

^{pr2}$

如何使用API调用获取提交日期详细信息和GitHub内容详细信息?在


Tags: httpsgithubcomapiurl详细信息v3management
1条回答
网友
1楼 · 发布于 2024-09-26 22:50:08

最后利用该方法得到了预期的结果这里是图表是完整的代码

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"]

相关问题 更多 >

    热门问题