I am using BitBucket Rest API to get the branches. Now currently what is happening is, it is returning 10 branches but they are not latest, they are mixed. I am using gentle/bitbucket-api package to make the requests. This is what my code looks like:
$branchesObject = new Branches();
$branchesObject->getClient()->setApiVersion('2.0')->addListener(new OAuthListener($this->oauthParams));
$contentJson = $branchesObject->all('<account>', '<repo>')->getContent();
$contentArray = json_decode($contentJson, true);
$branches = array_column($contentArray['values'], 'name');
If I dd($branches) This is what the output looks like:
array:10 [
0 => "branch1"
1 => "branch2"
2 => "branch3"
3 => "branch4"
4 => "branch5"
5 => "branch6"
6 => "branch7"
7 => "branch8"
8 => "branch9"
9 => "branch10"
]
These are not latest 10 branches. Api version is 2.0. Is there anyway I can get latest 10 branches or all active branches?
In short, it doesn't look like there's any straightforward way to get that particular data (latest branches).
First thing to think about is whether this type of data can be extracted at all.
In git, a branch is just a label that points to a commit in the repository. In git lingo, it's a ref, as are tags. A branch is different from a tag in some ways, one being that the branch is always updated to point to the latest commit, as long as said branch is checked out (you're on that branch).
Branches themselves are only commit hash, and don't have any other properties such as creation date, or when it was last updated. To see this, try going through the files in the .git file system directory from a repo; they're in ./git/refs/heads/.
Having said that, there are some ways of guessing when the branch was first created, like explained in How to determine when a Git branch was created?, but this is outside the scope of Bitbucket's public API.
I suggest you make some direct API queries with curl or Postman, in order to get a feel of what data is available. Looking over the actual API, and not the wrapper that you are using, it seems that the endpoint that you are using is /{workspace}/{repo_slug}/refs/branches/. This seems to return you all the active branches, with 10 results per page, in the order in which git itself returns them, and without any obvious way of requesting a sorted set. To break it down, there's
next property`, holding the URL of the next set of results;The previous method would make the queries every time the program is ran. If you need something closer to realtime, you could set up a webhook to fire on each commit. A webhook is just a program that runs when a URL is requested; you hook it to some other system firing events. Once a commit is pushed, the webhook is called with the commit info, e.g. date, branch. Then you can store them, something like { "branch1": "1590406741", "branch2": "1590406441"}.
Bitbucket webhooks
To sum up, there are some ways to achieve that, but you will need to do some extra coding to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With