Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 2
Message 1 of 2

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/Ta...

The result of this API call is :

 

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

 

I have checked in github and the commitid for this file is not showing the same one which was given in the 'sha ' key value.

is this API returning some other value in sha?

What i actually need is the download url for the file,commit id and commited date informations.

 

I have tried using another API ,which is :

https://github.***.com/api/v3/repos/exampleowner-Management/examplerepo/commits/1cef8efa8694678e3b7a...

 

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

does anyone have a solution for this issue.i have tested for other files also and the 'sha' value is different from the commit id showing in github.

1 Reply
Copilot Lvl 2
Message 2 of 2

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

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


result = run_query(query)

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