Why is the required builds merge check still failing after a successful build?
Asked by Alex Mosolgo:

I am attempting to configure the [required builds merge check](https://confluence.atlassian.com/bitbucketserver0721/checks-for-merging-pull-requests-1115665892.html#Checksformergingpullrequests-Requiredbuildsmergecheck) on Bitbucket Server to meet the following requirements:

1. Prevent merging PRs until one specific Jenkins job succeeds on the latest commit of the PR.
   * Note: this requirement could be relaxed to "until any Jenkins job succeeds" instead of requiring a specific job to pass, but it would be ideal to require a specific job.
2. Do not prevent merging PRs if any other Jenkins job(s) fail on the same commit.

I was originally using the [minimum successful builds merge check](https://confluence.atlassian.com/bitbucketserver0721/checks-for-merging-pull-requests-1115665892.html#Checksformergingpullrequests-Defaultmergechecks), but that does not meet requirement #2 because it prevents merging PRs if any failed builds appear (even if there is also 1 successful build).

The required builds merge check configuration is not working as I expected. It seems to fail to recognize a successful build regardless of what build key I specify.

*** ** * ** ***

I set up a simplified test to confirm what I was seeing with these steps:

1. Create a new repo to test
2. Make an initial commit on the "main" branch
3. Create a branch "test" and make a single commit on the "test" branch
   * The commit ID was 7eceb4d213a211c86499e708a05efae644a867e3
4. Open a PR with source branch "test" and target branch "main"
   * Confirmed the PR was ready to merge (no merge check issues)
5. Configure a required build for key "testKey":  
   ![required builds config.png](/t5/image/serverpage/image-id/233092iEA25B626F79B8BBC/image-size/large?v=v2&px=999 "required builds config.png")  
   * Confirmed the PR merge is blocked due to missing required build:  
     ![blocked merge 1.png](/t5/image/serverpage/image-id/233093i1EE4F7CC8D8EFA6A/image-size/large?v=v2&px=999 "blocked merge 1.png")
6. Add a successful build to the latest commit via [the REST API](https://developer.atlassian.com/server/bitbucket/how-tos/updating-build-status-for-commits/):
   *

     ```
      ? curl --location --request POST 'https://<redacted>/rest/build-status/latest/commits/7eceb4d213a211c86499e708a05efae644a867e3' \
     --header 'Content-Type: application/json' \
     --header 'Authorization: Basic <redacted>' \
     --header 'Cookie: BITBUCKETSESSIONID=<redacted>' \
     --data-raw '{
     "state": "SUCCESSFUL",
     "key": "testKey",
     "name": "testName",
     "url": "https://www.lutron.com",
     "description": "testDescription"
     }'
     ```

7.

   #### **? The PR merge was still blocked due to missing required build:** {#toc-hId-203509278}

   ![blocked merge 2.png](/t5/image/serverpage/image-id/233094iF576BDAAF5710820/image-size/large?v=v2&px=999 "blocked merge 2.png")
   * **My expectation was that the required build merge check would pass after posting the successful build result with key "testKey".**
   * Confirmed the build with key "testKey" is listed as expected via the REST API:
     *

       ```
        % curl --location --request GET 'https://<redacted>/rest/build-status/latest/commits/7eceb4d213a211c86499e708a05efae644a867e3' \
       --header 'Accept: application/json' \
       --header 'Authorization: Basic <redacted>' \
       --header 'Cookie: BITBUCKETSESSIONID=<redacted>'

       {"size":1,"limit":25,"isLastPage":true,"values":[{"state":"SUCCESSFUL","key":"testKey","name":"testName","url":"https://www.lutron.com","description":"testDescription","dateAdded":1672868354718}],"start":0}
       ```

*** ** * ** ***

**What is missing from this test to allow the required builds merge check to pass?** I'm hoping that a resolution to this simple example will help me find the issue when attempting similar config on my real repo.


Answers:
author: Alex Mosolgo
body: I got an answer from Atlassian support on this so I'm sharing it here in case it helps someone finding this question later.

My testing was using a deprecated API to report the build status, deprecated in Bitbucket 7.14:  
<https://developer.atlassian.com/server/bitbucket/reference/api-changelog/#build-status-1>
> **Build-status**   
> In Bitbucket 7.14, the following new REST end-points are added for build-status:
>
> GET /rest/api/latest/projects/{projectKey}/repos/{repositorySlug}/commits/{commitId}/builds  
> DELETE /rest/api/latest/projects/{projectKey}/repos/{repositorySlug}/commits/{commitId}/builds
>
> The following REST end-points are deprecated:
>
> GET /rest/api/1.0/build-status/latest/commits/{commitId}  
> POST /rest/api/1.0/build-status/latest/commits/{commitId}

Using the new API worked as expected and allowed the required builds merge check to pass. Here's the example request that the support representative used to test it:

```
curl -u admin:"the password" --location --request POST 'https://<url>/rest/api/1.0/projects/~admin/repos/tr/commits/c558c598cb74376ecff6347fe2c3dedae8eaba01/builds' \
--header 'Content-Type: application/json' \
--data-raw '{"key":"testKeyCF1","state":"SUCCESSFUL","url":"https://bamboo.url/browse/TEST-REP1-3","buildNumber":"1","description":"Unit test build","duration":1500000,"lastUpdated":1359075920,"name":"Database Matrix Tests","parent":"testKeyCF","ref":"refs/heads/testcfb","testResults":{"failed":0,"skipped":0,"successful":1}}'
```

And some important details shared about the data:

1. The "parent":"testKeyCF" is correlated with the "key":"testKeyCF1". It's important to make sure the parent and key correlate.
2. The key should have an incrementing number after
3. The "ref":"refs/heads/testcfb" should point to the correct branch

I also confirmed that the Bitbucket Branch Source plugin used with Jenkins to report build status in real use cases (as opposed to my testing using curl) also uses the deprecated API. This explains why the required builds merge check was not working in real use cases as well.  
<https://github.com/jenkinsci/bitbucket-branch-source-plugin/blob/b8128a1921b48a4e319a1a3c9153353959f397ae/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/server/client/BitbucketServerAPIClient.java#L156>


Tags: bitbucket-server

build

merge

pull-request

required-field

server