Both the SOAP or REST APIs for JIRA are missing a method to add a component to a JIRA project. The shell script below calls the appropriate URL to do this. File under "ugly but effective", and thanks to Justin for his handy post.
~Matt
#
# Add components to a JIRA project.
#
# The parameters to the URL are encoded by passing them with -d
USERNAME=admin
PASSWORD=admin
SERVER_URL="http://jira.example.com:8080"
# Set this to the project you want to add components to
projectid=10001
DASHBOARD_PAGE_URL=$SERVER_URL/secure/Dashboard.jspa
COOKIE_FILE_LOCATION=jiracoookie
# Get the authentication cookie
curl -u $USERNAME:$PASSWORD --cookie-jar $COOKIE_FILE_LOCATION -sS --output /dev/null $DASHBOARD_PAGE_URL
for component in "Able Baker Charlie" "Lowly Work" "Huckle Cat"; do
echo "Adding component: $component"
curl --cookie $COOKIE_FILE_LOCATION --header "X-Atlassian-Token: no-check" -sS --output /dev/null -d "name=$component" -d "pid=$projectid" "$SERVER_URL/secure/project/AddComponent.jspa"
done
rm $COOKIE_FILE_LOCATION
~Matt
#
# Add components to a JIRA project.
#
# The parameters to the URL are encoded by passing them with -d
USERNAME=admin
PASSWORD=admin
SERVER_URL="http://jira.example.com:8080"
# Set this to the project you want to add components to
projectid=10001
DASHBOARD_PAGE_URL=$SERVER_URL/secure/Dashboard.jspa
COOKIE_FILE_LOCATION=jiracoookie
# Get the authentication cookie
curl -u $USERNAME:$PASSWORD --cookie-jar $COOKIE_FILE_LOCATION -sS --output /dev/null $DASHBOARD_PAGE_URL
for component in "Able Baker Charlie" "Lowly Work" "Huckle Cat"; do
echo "Adding component: $component"
curl --cookie $COOKIE_FILE_LOCATION --header "X-Atlassian-Token: no-check" -sS --output /dev/null -d "name=$component" -d "pid=$projectid" "$SERVER_URL/secure/project/AddComponent.jspa"
done
rm $COOKIE_FILE_LOCATION
The JIRA CLI script at https://plugins.atlassian.com/plugin/details/10751 has an example of doing this with Python. See JCLIMD-13 for more details.
ReplyDeleteDoing a similar thing with Perl: https://studio.plugins.atlassian.com/wiki/display/JCLI/JIRA+Command+Line+Interface?focusedCommentId=33587342#comment-33587342
ReplyDeleteIf you need to get past websudo for an action:
ReplyDeleteWEBSUDO_PAGE_URL=$SERVER_URL/secure/admin/WebSudoAuthenticate.jspa?webSudoPassword=$PASSWORD
curl --cookie $COOKIE_FILE_LOCATION --output /dev/null $WEBSUDO_PAGE_URL
Someone please tell me how to do this with the Script Runner plugin.
ReplyDelete
ReplyDelete'''
Add multiple components to multiple projects
Setup an environment with virtualenv
pip install jira-python
Usage:
(environment)[mdoar@mdoar2]$ time python ./add_components.py -z https://jira.example.com -x youruserid -y secret -d0
Matt Doar
CustomWare
'''
from jira.client import JIRA
from optparse import OptionParser
import logging
import os
import sys
def connect_jira(log, options):
'''
Connect to JIRA. Return None on error
'''
try:
log.info("Connecting to JIRA: %s" % options.jira_server)
jira_options = {'server': options.jira_server}
jira = JIRA(options=jira_options,
# Note the tuple
basic_auth=(options.jira_user,
options.jira_password))
return jira
except Exception,e:
log.error("Failed to connect to JIRA: %s" % e)
return None
def add_components(log, options, jira):
'''
Return non-zero on error
'''
component_names = [
"Component A",
"Component B",
"Component C",
]
project_keys = [
"PROJA",
"PROJB",
]
for project_key in project_keys:
for component_name in component_names:
log.info("%s: creating component: %s" % (project_key, component_name))
if not options.dryrun:
res = jira.create_component(component_name,
project_key)
return 0
def setup_logging(options, logname):
'''
'''
logger = logging.getLogger(logname)
logfilename = os.path.join(os.path.dirname(__file__), '%s.log' % logname)
hdlr = logging.FileHandler(logfilename, 'w')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.WARNING)
if options.verbose == 1:
logger.setLevel(logging.INFO)
elif options.verbose == 2:
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
logger.addHandler(ch)
logger = logger
return logger
def get_options():
'''
'''
parser = OptionParser("usage: %prog [options]", version="%prog 0.1")
parser.add_option("-d", "--dryrun", dest="dryrun", type="int",
default=1,
help="Set to zero for a non dryrun, i.e. actually import issues, default: %default")
parser.add_option("-v", "--verbose", dest="verbose", type="int",
default=1,
help="Set to non-zero for more verbose output, default: %default")
parser.add_option("-x", "--jirauser", dest="jira_user", default="admin",
help="The JIRA user, default: %default")
parser.add_option("-y", "--jirapassword", dest="jira_password", default="secret",
help="The password for the JIRA user")
parser.add_option("-z", "--jiraserver", dest="jira_server", default="http://localhost:8080",
help="The JIRA server, default: %default.")
(options, args) = parser.parse_args()
return (options, args)
def main():
'''
'''
(options, args) = get_options()
log = setup_logging(options, 'add_components')
jira = connect_jira(log, options)
if not jira:
return 1
res = add_components(log, options, jira)
if res:
return 1
return 0
if __name__=="__main__":
sys.exit(main())