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

Next challenges

Recent achievements

Recognition

  • Give kudos
  • My kudos

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to populate a Jira custom field with bitbucket project names

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

2 answers

1 accepted

0 votes
Answer accepted

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

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

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()
}

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

0 votes
Peter DeWitt Community Leader Oct 04, 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

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

Hello @Prashant Bagewadi 

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
Community showcase
Published in Bitbucket

Powering DevOps with Bitbucket Server & Data Center

Hi everyone, The Cloud team recently announced 12 new DevOps features that help developers ship better code, faster   ! While we’re all excited about the new improvements to Bitbucket ...

1,922 views 0 7
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you