Reputation: 37
I am using Bitbucket API to retrieve different information. However I am looking to do a request that retrieves the latest commit for a repository. I initially thought it would be done like this:
https://bitbucket.org/!api/2.0/repositories/xxxx/xxxx/commits?limit=1
This just showed all the commits as normal but I want to show the most recent one. From looking through the API documentation I can't find anything that shows about limiting the number of commits to show. So was wondering if anyone could point me in the right direction?
Upvotes: 2
Views: 9230
Reputation: 8940
The List Branches API allows for sorting the branches by the most recently modified branch. With a limit of 1, it returns the most recent commit in the latestCommit field of the only branch returned.
https://bitbucket.org/rest/api/1.0/projects/<project>/repos/<repo>/branches?orderBy=MODIFICATION&limit=1
Here is the output of the above request.
{
"size": 1,
"limit": 1,
"isLastPage": false,
"values": [
{
"id": "refs/heads/<branch>",
"displayId": "<branch>",
"type": "BRANCH",
"latestCommit": "91d68b4a8fe349e7d1c48ea4da0ba4be8b260b45",
"latestChangeset": "91d68b4a8fe349e7d1c48ea4da0ba4be8b260b45",
"isDefault": false
}
],
"start": 0,
"nextPageStart": 1
}
One can then use the Get Commit API to get the details of the commit.
https://bitbucket.org/rest/api/1.0/projects/<project>/repos/<repo>/commits/<commit>
Sadly, the List Commit API does not return the latest commit on the first page. I don't know if subsequent pages will return the latest commit.
Upvotes: 2
Reputation: 343
There a simple way to do it, using Bitbucket pagination mechanism. Like so: https://bitbucket.org/!api/2.0/repositories/xxxx/xxxx/commits?pagelen=1
Normally you'll need to specify the page number, "page=1", but the default is 1 so...
Upvotes: 0
Reputation: 1261
Okay this wasn't straight forward either and I spent a couple of hours looking for how to do this myself. It ended up being easy if not unfortunate. Simply put the available API will not return just a target commit (like the latest). You have do parse it yourself. The following api:
"https://api.bitbucket.org/2.0/repositories/<project>/<repo>/commits/<branch>?limit=1"
Will still return ALL the commits for that particular branch BUT in order. So you can simply just grab the first result on the first page that is returned and that's the most recent commit for that branch. Here is a basic python example:
import os
import requests
import json
headers = {"Content-Type": "application/json"}
USER = ""
PASS = ""
def get_bitbucket_credentials():
global USER, PASS
USER = "<user>"
PASS = "<pass>"
def get_commits(project, repo, branch):
return json.loads(call_url("https://api.bitbucket.org/2.0/repositories/%s/%s/commits/%s?limit=1" % (project, repo, branch)))
def get_modified_files(url):
data = json.loads(call_url(url))
file_paths = []
for value in data["values"]:
file_paths.append(value["new"]["path"])
return file_paths
def call_url(url):
global USER, PASS
response = requests.get(url, auth=(USER, PASS), headers=headers)
if response.status_code == requests.codes.ok:
return response.text
return ""
if __name__ == "__main__":
get_bitbucket_credentials()
data = get_commits("<project>","<repo>","<branch>")
for item in data["values"]:
print("Author Of Commit: "+item["author"]["raw"])
print("Commit Message: "+item["rendered"]["message"]["raw"])
print("List of Files Changed:")
print(get_modified_files(item["links"]["diff"]["href"].replace("/diff/","/diffstat/")))
break
You can run the above example:
python3 mysavedfile.py
and that will output like:
Author Of Commit: Persons Name <[email protected]>
Commit Message: my commit message
List of Files Changed:
['file1.yaml','file2.yaml']
Upvotes: 2