Anvi
Anvi

Reputation: 2741

How to get the latest commit date of the file along with content details from GitHub API call

I have used the below GitHub api and i'am able to get the files details of the path.

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

The result of this API call is :

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

I need to get the commit date details for the sha in this response.

"sha": "1cef8efa8694678e3b7ab230a6a891afa1a1996d"

I have tried using another API ,which is :

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

but the response of this API for this sha is:

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

How can we get commit date details along with GitHub content details by using API calls?

Upvotes: 0

Views: 719

Answers (1)

Anvi
Anvi

Reputation: 2741

finally got the expected result by using Graphql.here is the complete code

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

Upvotes: 0

Related Questions