Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to populate a Jira custom field with bitbucket project names

cnodwell October 4, 2017

I've been searching for answers to this however I have not been able to find anything other than an old plugin (jira-importers-bitbucket) that is not compatible with Jira 7.X.
I am trying to populate a jira custom filed with the names of the projects within BitBucket.  I have a purchased license for Jira Server, BitBucket Server, as well as the ScriptRunner add-on for Jira and BitBucket also fully licensed.

A solid script example would be really cool.

 

Thanks

-CN

3 answers

1 accepted

0 votes
Answer accepted
Deleted user June 24, 2020

cnodwell  Did you get the script ? We are also looking for the same functionality. It will be really helpful if you share the details.

Craig Nodwell
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 24, 2020

We did.
We combined two behavior scripts 1. for initializing the fields the others to call a rest endpoint script if BitBucket is selected in the Systems listing and then the BitBucket Project List select box is clicked into.
Sorry for the lack of comments in the scripts themselves.
I hope this helps.

The REST Endpoint script.

listBitbucketProjects.groovy

import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.json.JsonBuilder
import groovy.json.*
import groovy.json.JsonOutput
import groovy.transform.CompileStatic
import groovy.json.JsonSlurper
import groovy.json.StreamingJsonBuilder;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue
import org.apache.commons.codec.binary.Base64;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import groovyx.net.http.RESTClient
import org.apache.http.HttpRequest
import org.apache.http.HttpRequestInterceptor
import java.sql.Timestamp
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.transform.BaseScript
import org.apache.commons.lang3.StringUtils
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
log.setLevel(Level.DEBUG)
def log = Logger.getLogger("com.onresolve.jira.groovy")

@BaseScript CustomEndpointDelegate delegate

listBitbucketProjects { MultivaluedMap queryParams ->

AtlassianTool tool = new AtlassianTool()
def query = queryParams.getFirst("query") as String
def json = tool.RESTCall("Bitbucket", "/rest/api/1.0/projects?limit=999&query=${URLEncoder.encode(query)}&type=global&status=current")
log.debug(json)
def rt = [
items : json.values.collect { project ->
def html = "${project.key} (${project.name})"
[
value: project.key,
html : html,
label: project.key
]
},
total : json.totalSize,
footer: "Choose Bitbucket project...",
]

return Response.ok(new JsonBuilder(rt).toString()).build()
}
/////end


The behavior script to initialize the fields.

FieldInitalizer.groovy

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript
import org.apache.log4j.Logger
import org.apache.log4j.Level
import static com.atlassian.jira.issue.IssueFieldConstants.*

@BaseScript FieldBehaviours fieldBehaviours

if (getActionName() != "Create") return;

def log = Logger.getLogger("com.onresolve.jira.groovy")
log.setLevel(Level.DEBUG)

// Custom Fields are initally hidden
getFieldByName("Bitbucket Project List").setHidden(true)
getFieldByName("Bitbucket Project List").setFormValue(null)
getFieldByName("Project").setHidden(true)
getFieldByName("Project Role").setHidden(true)
/////end

The behavior script to call the listener.

BitBucketSystemField.groovy

import com.atlassian.jira.project.Project
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import org.apache.log4j.Logger
import org.apache.log4j.Level
import static com.atlassian.jira.issue.IssueFieldConstants.*

@BaseScript FieldBehaviours fieldBehaviours
def log = Logger.getLogger("com.onresolve.jira.groovy")
log.setLevel(Level.DEBUG)

def systemsField = getFieldById(getFieldChanged())
log.debug (systemsField.value)

// Get the custom fields by Name
def bProj = getFieldByName("Bitbucket Project List")
def jProj = getFieldByName("Project")
def jProjRole = getFieldByName("Project Role")

if (systemsField.value.contains("Bitbucket")) {

bProj.setRequired(true)
bProj.setHidden(false)

//Get the Bitbucket Projects
bProj.convertToSingleSelect([
ajaxOptions: [
url : getBaseUrl() + "/rest/scriptrunner/latest/custom/listBitbucketProjects",
query: true,
formatResponse: "general"
]
])
}

if (!systemsField.value.contains("Bitbucket")) {

bProj.setFormValue(null)
bProj.setRequired(false)
bProj.setHidden(true)

}

/////end

Craig Nodwell
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 24, 2020

Yes we did.
A combination of two behaviors and a REST endpoint.
Behavior 1.
FieldInitalize.groovy

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript
import org.apache.log4j.Logger
import org.apache.log4j.Level
import static com.atlassian.jira.issue.IssueFieldConstants.*

@BaseScript FieldBehaviours fieldBehaviours

if (getActionName() != "Create") return;

def log = Logger.getLogger("com.onresolve.jira.groovy")
log.setLevel(Level.DEBUG)

// Custom Fields are initally hidden
getFieldByName("Bitbucket Project List").setHidden(true)
getFieldByName("Bitbucket Project List").setFormValue(null)
getFieldByName("Project").setHidden(true)
getFieldByName("Project Role").setHidden(true)
getFieldById(ASSIGNEE).setHidden(true)

 

2. the behavior script to call the rest endpoint script.

BitBucketProjectList.groovy

import com.atlassian.jira.project.Project
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import org.apache.log4j.Logger
import org.apache.log4j.Level
import static com.atlassian.jira.issue.IssueFieldConstants.*

@BaseScript FieldBehaviours fieldBehaviours
def log = Logger.getLogger("com.onresolve.jira.groovy")
log.setLevel(Level.DEBUG)

def systemsField = getFieldById(getFieldChanged())
log.debug (systemsField.value)

// Get the custom fields by Name
def bProj = getFieldByName("Bitbucket Project List")

if (systemsField.value.contains("Bitbucket")) {

bProj.setRequired(true)
bProj.setHidden(false)

//Get the Bitbucket Projects
bProj.convertToSingleSelect([
ajaxOptions: [
url : getBaseUrl() + "/rest/scriptrunner/latest/custom/listBitbucketProjects",
query: true,
formatResponse: "general"
]
])
}

if (!systemsField.value.contains("Bitbucket")) {

bProj.setFormValue(null)
bProj.setRequired(false)
bProj.setHidden(true)

}

 

The REST endpoint script.

listBitbucketProjects.groovy

import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.json.JsonBuilder
import groovy.json.*
import groovy.json.JsonOutput
import groovy.transform.CompileStatic
import groovy.json.JsonSlurper
import groovy.json.StreamingJsonBuilder;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue
import org.apache.commons.codec.binary.Base64;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import groovyx.net.http.RESTClient
import org.apache.http.HttpRequest
import org.apache.http.HttpRequestInterceptor
import java.sql.Timestamp
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.transform.BaseScript
import org.apache.commons.lang3.StringUtils
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
log.setLevel(Level.DEBUG)
def log = Logger.getLogger("com.onresolve.jira.groovy")

@BaseScript CustomEndpointDelegate delegate

listBitbucketProjects { MultivaluedMap queryParams ->

AtlassianTool tool = new AtlassianTool()
def query = queryParams.getFirst("query") as String
def json = tool.RESTCall("Bitbucket", "/rest/api/1.0/projects?limit=999&query=${URLEncoder.encode(query)}&type=global&status=current")
log.debug(json)
def rt = [
items : json.values.collect { project ->
def html = "${project.key} (${project.name})"
[
value: project.key,
html : html,
label: project.key
]
},
total : json.totalSize,
footer: "Choose Bitbucket project...",
]

return Response.ok(new JsonBuilder(rt).toString()).build()
}

cnodwell June 25, 2020

Sorry for that duplication, I was having trouble with the auto login and my personal account/work account.

0 votes
Craig Nodwell
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 16, 2022

For this solution today in 2022.
I would setup a scriptrunner resource field that would directly run a sql statement against the project field value and populate it into that custom resource field.

Saravanan Sekar August 5, 2022

@Craig Nodwell Can u explain in brief on how to implement this. We are trying to do this in our project.

0 votes
Peter DeWitt
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 4, 2017

@cnodwell, Check out the nFeed plugin.  I have used this in the past to connect to the Atlassian DBs to pull project/space names.

pd

Deleted user June 24, 2020

@cnodwell  Did you get the script ? We are also looking for the same functionality. It will be really helpful if you share the details.

Christophe Promé June 24, 2020

Hello @[deleted] 

I am the Product Manager of Elements Connect (previously called nFeed).

Indeed, as @Peter DeWitt said, it's possible to create a Bitbucket project picker with our app.

Here is how:

  • Create an Application link between Jira and Bitbucket (preferred) OR create an Elements Connect datasource of type URL that point to Bitbucket REST API - Read the doc
  • Create an Elements Connect field of type "Live text" or "Snapshot text" (see the difference)
  • Configure the field, select the "Bitbucket" datasource
  • Field configuration is:
    • Query:
      rest/api/1.0/projects?name=$userInput&limit=20
    • Root element:
      $.values
    • 2 json columns:
      • Name: key - JSON Path: $.key
      • Name: name - JSON Path: $.name
    • Key (if you selected "Live text")
    • Editor: Autocomplete (can be any type expect "Read only")
    • Template:
      {1} ({0})

This is the configuration of a bitbucket project picker with autocomplete. The text typed by the end user in the autocomplete field is used to filter the query. ($userInput variable).

You can adapt this configuration to your needs and explore the Bitbucket configuration here: https://docs.atlassian.com/bitbucket-server/rest/7.3.1/bitbucket-rest.html#idp141

If you have another question about Elements Connect, please contact our support team.

Hope it helps

Regards,
Christophe

Lead Product Manager @ Elements

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events