Is it possible to get file change logs /commits list or latest commit at particular branch at specified file path? I tried using /commits/?path={filepath}&until={branch -name}.then extracted first element of json to get latest commit. But some data is different from what is there in bitbucket.
Asked
Active
Viewed 307 times
1 Answers
0
Here is a snippet to get the latest commit for a particular file:
#!/usr/bin/python
import os
import tempfile
import sys
import urllib2
import json
import base64
import logging
import re
import pprint
import requests
import subprocess
projectKey= "FW"
repoKey = "fw"
branch = "master"
pathToVersionProperties = "core/CruiseControl/CI_version.properties"
localVersionProperties = "CI_version.properties"
bitbucketBaseUrl = "https://bitbucket.company.com/rest/api/latest"
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
def getLatestCommit():
restEndpoint = "{}/projects/{}/repos/{}/commits?path={}".format(bitbucketBaseUrl, projectKey, repoKey, pathToVersionProperties)
logging.info("REST endpoint : {}".format(restEndpoint))
request = urllib2.Request(restEndpoint)
request.add_header("Authorization", "Bearer %s" % os.environ["PAT"])
result = json.loads(urllib2.urlopen(request).read())
latestCommit = result["values"][0]["displayId"]
if(len(latestCommit) > 0):
logging.info("Latest commit: {}".format(latestCommit))
else:
logging.error("Commit hash is empty, failed to retrieve latest commit")
sys.exit(1)
return latestCommit
latestCommit = getLatestCommit()
You can install stashy with pip install stashy
.

eeijlar
- 1,232
- 3
- 20
- 53
-
This works fine with branch name without forward slash but fails in branch -name with forward slash like feature/abc and in your script you didn't mentioned branch in resturl. – deepak prajapati Nov 03 '18 at 02:21