Use Jira Forms to trigger Bitbucket code
Asked by Raul van der Raaf:

Hello there,

We would like to know if it is possible to link the creation of tickets within Jira, together with executing code in bitbucket based on said filled out form values.

Example:

1. User visits Jira Service Management Portal
2. User fills in a form
3. Ticket is created in JSM
4. Ticket automaticly triggers bitbucket code to run (here we transition from JSM to Bitbucket)
5. The bitbucket code, uses the filled in form values to set variables and run the code
6. Bitbucket code completes, and goes back to update the ticket in JSM, thus notifying the user (here we transition back from bitbucket to JSM)

We would like to use Jira in combination with Bitbucket to allow our users some degree of self-service, and would like to know if this is possible and how.

The problem we currently face is determing wether we can do this between the 2 products, chain the use of JSM en Bitbucket together and allow a more advanced form of self-service.

Does anyone know if this is possible, and if so, how?

Regards


Answers:
author: Theodora Boudale
body: Hi Raul,

This may be possible using [Jira Cloud Automation rules](https://support.atlassian.com/cloud-automation/docs/create-and-configure-jira-automation-rules/) with Bitbucket and Jira API, but it depends on what are the exact actions you want to be performed.

Could you please clarify the following, so I can check if it's possible?  

> Ticket automaticly triggers bitbucket code to run (here we transition from JSM to Bitbucket)

What do you mean by "bitbucket code to run"? Do you want to execute a Bitbucket Pipelines build? Or do you mean something different?  

> The bitbucket code, uses the filled in form values to set variables and run the code

What kind of variables are you referring to in Bitbucket?  
If the answer to my previous question is Pipelines builds, are the variables Repository or Workspace variables for Pipelines builds?  
Or are they variables of a custom pipeline?  

> Bitbucket code completes, and goes back to update the ticket in JSM, thus notifying the user (here we transition back from bitbucket to JSM)

What kind of update do you want to perform on the ticket? Transition the ticket? Add a comment? Or something different?

Kind regards,  
Theodora

comments: author: Raul van der Raaf
body: Hello Theodora,

To clarify:
> Ticket automaticly triggers bitbucket code to run (here we transition from JSM to Bitbucket)
>
> What do you mean by "bitbucket code to run"? Do you want to execute a Bitbucket Pipelines build? Or do you mean something different?  
>
Yes, we already tested out with a piece of code in Bitbucket and running it in a Pipeline. So yes, we would like to utilize this further. Having an agent run code on demand when a user requests a specific service.

> The bitbucket code, uses the filled in form values to set variables and run the code
>
> What kind of variables are you referring to in Bitbucket?  
> If the answer to my previous question is Pipelines builds, are the variables Repository or Workspace variables for Pipelines builds?  
> Or are they variables of a custom pipeline?  
>
The variables mentioned are the Jira form values that are related to the requester. Such as name, email, accountname and such. These values should then be transferred from Jira into the Bitbucket pipeline to run the code. The bitbucket code runs based on the form values to perform a specific task or service for the user.

> Bitbucket code completes, and goes back to update the ticket in JSM, thus notifying the user (here we transition back from bitbucket to JSM)
>
> What kind of update do you want to perform on the ticket? Transition the ticket? Add a comment? Or something different?

After all of the abov magic has been done, we would like to transition the ticket and add a comment to it. Notifying the user the requested service has been delivered.

With this kind of automation, a user can request a ticket using the Jira Portal using the right form, the ticket is then created where automation kicks in to execute requested service. The service will then be delivered and the ticket will be automaticly updated and closed. Thus creating a higher form of self-service where tickets can also be processed by an automation bot.  

*** ** * ** ***

<br />

To sketch the general idea of the self-service chain we would like to implement using JSM as entry point:

![self-service-chain-sample.png](https://community.atlassian.com/t5/image/serverpage/image-id/197481iE22847ECAC5EDCE7/image-size/large?v=v2&px=999 "self-service-chain-sample.png")

1. User visits Jira Service Management Portal
2. User fills in a form
3. Ticket is created in JSM
4. Ticket automaticly triggers bitbucket code to run (here we transition from JSM to Bitbucket, prep the code with the form values from Jira)
5. The bitbucket code, uses the filled in form values to set variables and run the code (agent / pipeline build that executes the magic)
6. Bitbucket code completes, and goes back to update the ticket in JSM.
7. User is notified via said ticket

Regards


author: Theodora Boudale
body: Hi Raul,

Thank you for the info.

This should be possible, but it will require some configuration.

I am sharing below the details of an implementation I tested:

++**1. Create a custom Bitbucket Pipeline**++

I created a custom pipeline in one of my repos, this is the bitbucket-pipelines.yml file:

```
image: atlassian/default-image:3

pipelines:
  custom: 
    my-custom-pipeline: 
      - variables: 
        - name: userName
        - name: userEmail
        - name: issueKey
        - name: myCustomValue
      - step:
          script:
            - echo ${myName}
            - echo ${email}
            - echo ${issueKey}
            - echo ${myCustomValue}
            - curl -v --request POST --url "https://mysite.atlassian.net/rest/api/3/issue/${issueKey}/transitions" --user "${JiraEmail}:${JiraToken}" --header 'Accept:application/json' --header 'Content-Type:application/json' --data '{"transition":{"id":"761"}}'
```

Custom pipelines do not get triggered automatically when you commit to a repo.  
They can only be triggered manually from Bitbucket website, or via an API call.

With custom pipelines, it is also possible to configure variables and you can give values to these variables when you trigger the build.

I have created here four variables (which will get their values from Jira), and in the script, I am simply echoing their values.  
In this example, I want to get

* the name of the ticket's reporter
* the email address of the reporter
* the issue key from Jira
* the value of a custom Text field I have added in my JSM requests

I also have an API call to transition the Jira issue (I'll get into more details about this later in my reply).

<br />

++**2. Configure Jira Service Management**++

[I created a new custom Text Field in Jira](https://support.atlassian.com/jira-service-management-cloud/docs/add-a-new-custom-field-to-a-project/) (so that the JSM user can pass a value), I named this **myCustomField**

Then, in my JSM project's settings, [I added this custom field to the specific request type](https://support.atlassian.com/jira-service-management-cloud/docs/customize-the-fields-of-a-request-type/) where I want to use it.

The next step is configuring the automation.

- Go to the **Projects settings** of the JSM project \> **Automation**   
- Select **Create rule**   
- Jira prompts to select a trigger. Select the trigger **Issue created** and then **Save**   
- Afterwards, select **New action** and then **Send web request**

This action will include an API call to trigger the custom Pipeline I created earlier.  
Here are the details on how to fill in the **Send web request** page:

In the field **Web request URL** fill in

```
https://api.bitbucket.org/2.0/repositories/{workspace-id}/{repo}/pipelines/
```

Replace **{workspace-id}** and **{repo}** with the actual values of your workspace and repo.

Authentication is needed to perform this API call. I generated an app password for my Bitbucket account with permissions only related to Pipelines.

I encoded the string **myBitbucketUsename:myAppPassword** to base 64, let's say the value of that is **myEncodedString**

Then, in the **Headers** of the **Send web request** form, I added a **Header** named **Authorization** and value **Basic myEncodedString**

For **HTTP method** select **POST** and for **Web request body** select **Custom data**.

A new field named **Custom dat**a shows, in there I gave the following data for the Bitbucket API call:

```
{
"target": {
"type": "pipeline_ref_target",
"ref_type": "branch",
"ref_name": "master",
"selector": {
"type": "custom",
"pattern": "my-custom-pipeline"
}
},
"variables": [
{
"key": "myName",
"value": "{{issue.reporter.displayName}}"
},
{
"key": "email",
"value": "{{issue.reporter.emailAddress}}"
},
{
"key": "issueKey",
"value": "{{issue.key}}"
},
{
"key": "myCustomValue",
"value": "{{issue.myCustomField}}"
}
]
}
```

**"ref_name": "master"** means that this pipeline will run on master branch, you can change this to a different branch

**"pattern": "my-custom-pipeline"** has the name of my custom pipeline, as it is defined in my bitbucket-pipelines.yml file

The **"variables"** part determines values for the custom pipeline's variables.  
Each "key" is the name of the variable, as defined in the bitbucket-pipelines.yml file.  
Each "value" is a [Smart value](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-issues/), which allows you to access issue data within Jira.  
Using these values, we get automatically the reporter's name, their email, the Jira issue key, and the value that the reporter fills in for the custom field when they create a JSM ticket.

<br />

Afterwards, select **Save** .  
You will be asked to give a name for the automation, then select **Turn it on**, and the configuration is finished.

The next time someone creates a JSM ticket, the custom pipeline will get triggered with values that the user defines.

++**JIRA API calls from Pipelines**++

In the example bitbucket-pipelines.yml file I shared, I used a JIRA API call to transition the issue.  
You can use another call to [add a comment to the issue](https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-comments/#api-rest-api-3-issue-issueidorkey-comment-post).

Please note that if the build fails before the curl command, the JIRA API call will not be executed.  
If you want the API calls to be executed regardless of whether the build succeeds or fails, you can add it in an after-script section

```
- step:
    script:
      - echo ${myName}
      - echo ${email}
      - echo ${issueKey}
      - echo ${myCustomValue} 
    after-script:
      - curl -v --request POST --url "https://mysite.atlassian.net/rest/api/3/issue/${issueKey}/transitions" --user "${JiraEmail}:${JiraToken}" --header 'Accept:application/json' --header 'Content-Type:application/json' --data '{"transition":{"id":"761"}}'
```

In this API call, replace **mysite** with the name of your Jira site.  
**JiraEmail** and **JiraToken** are the names of repository variables I used (you can create them from the **Repository settings** ).  
Their values are my email and an API token for my Atlassian account respectively.

<br />

I realize this is quite a lot of info. If you have any questions, or if anything is unclear, please feel free to let me know.

Kind regards,  
Theodora


author: Raul van der Raaf
body: Hi Theodora,

The above mentioned method solution helped us getting a POC ready and working.

We can now proceed with further designing and building the services.

Thank you for the help. :)

Regards


Tags: bitbucket-cloud

cloud

jira