예제 #1
0
class Session(Base):
    """Manage sessions"""
    def run(self):
        #print('You supplied the following options:', json.dumps(self.options, indent=2, sort_keys=True))
        self.jira_client = JiraClient(self.loadConfiguration())
        if self.hasOption('login'):
            self.login()
        elif self.hasOption('logout'):
            self.logout()
        elif self.hasOption('dump'):
            self.dump()
        else:
            print("Nothing to do.")

    def login(self):
        url = self.options['<url>']
        username = self.options['<username>']
        password = self.options['<password>']

        rc = self.jira_client.login(url, username, password)

        if rc == 200:
            self.saveConfiguration(self.jira_client.getConfiguration())
        self.processResultCode(rc)

    def logout(self):
        rc = self.jira_client.logout()
        self.processResultCode(rc)

    def dump(self):
        configuration = self.jira_client.getConfiguration()
        print(json.dumps(configuration))
예제 #2
0
 def run(self):
     #print('You supplied the following options:', json.dumps(self.options, indent=2, sort_keys=True))
     self.jira_client = JiraClient(self.loadConfiguration())
     if self.hasOption('get'):
         self.getRole()
     else:
         print("Nothing to do.")
예제 #3
0
 def run(self):
     #print('You supplied the following options:', json.dumps(self.options, indent=2, sort_keys=True))
     self.jira_client = JiraClient(self.loadConfiguration())
     if self.hasOption('get'):
         self.getPage()
     elif self.hasOption('create'):
         self.createPage()
     elif self.hasOption('update'):
         self.updatePage()
     elif self.hasOption('delete'):
         self.deletePage()
     elif self.hasOption('move'):
         self.movePage()
     else:
         print("Nothing to do.")
예제 #4
0
 def run(self):
     #print('You supplied the following options:', json.dumps(self.options, indent=2, sort_keys=True))
     self.jira_client = JiraClient(self.loadConfiguration())
     if self.hasOption('get'):
         self.getOption()
     elif self.hasOption('add'):
         self.addOption()
     elif self.hasOption('del'):
         self.deleteOption()
     elif self.hasOption('replace'):
         self.replaceOption()
     elif self.hasOption('exist'):
         self.existOption()
     elif self.hasOption('getid'):
         self.getOptionId()
     else:
         print("Nothing to do.")
예제 #5
0
class Issues(Base):
    """Manage issues"""
    def run(self):
        #print('You supplied the following options:', json.dumps(self.options, indent=2, sort_keys=True))
        self.jira_client = JiraClient(self.loadConfiguration())
        if self.hasOption('get'):
            self.getIssues()
        elif self.hasOption('getkeys'):
            self.getIssuesKeys()
        elif self.hasOption('create'):
            self.createIssues()
        else:
            print("Nothing to do.")

    def getIssues(self):
        jql = self.options['<jql>']
        if self.hasOption('--page'):
            page_index = int(self.options['--page'])
        else:
            page_index = 0
        if self.hasOption('--page-size'):
            page_size = int(self.options['--page-size'])
        else:
            page_size = 50
        #print("Get page %s/%s of issues for jql %s" % (page_index, page_size, jql))
        rc, datas = self.jira_client.getIssuesPage(jql, page_index, page_size)
        self.processResults(rc, datas)

    def getIssuesKeys(self):
        jql = self.options['<jql>']
        rc, datas = self.jira_client.getIssues(jql)
        issues = json.loads(datas)['issues']
        issuesKeys = list()
        for issue in issues:
            issuesKeys.append(issue['key'])
        print(json.dumps(issuesKeys))

    def createIssues(self):
        json_file = self.options['<json_file>']
        with open(json_file, 'r') as issues_raw:
            issues = json.load(issues_raw)
            rc, datas = self.jira_client.createIssues(json_file)
            self.processResults(rc, datas)
예제 #6
0
class Project(Base):
    """Manage project"""

    def run(self):
        #print('You supplied the following options:', json.dumps(self.options, indent=2, sort_keys=True))
        self.jira_client = JiraClient(self.loadConfiguration())
        if self.hasOption('get'):
            self.getProject()
        elif self.hasOption('list'):
            self.listProjects()
        else:
            print("Nothing to do.")

    def getProject(self):
        project_key_or_id = self.options['<project_key_or_id>']
        rc, datas = self.jira_client.getProject(project_key_or_id)
        self.processResults(rc, datas)

    def listProjects(self):
        rc, datas = self.jira_client.listProjects()
        self.processResults(rc, datas)
예제 #7
0
class Page(Base):
    """Manage page"""
    def run(self):
        #print('You supplied the following options:', json.dumps(self.options, indent=2, sort_keys=True))
        self.jira_client = JiraClient(self.loadConfiguration())
        if self.hasOption('get'):
            self.getPage()
        elif self.hasOption('create'):
            self.createPage()
        elif self.hasOption('update'):
            self.updatePage()
        elif self.hasOption('delete'):
            self.deletePage()
        elif self.hasOption('move'):
            self.movePage()
        else:
            print("Nothing to do.")

    def getPage(self):
        page_title = self.options['<page_title>']
        space_key = self.options['<space_key>']
        rc, datas = self.jira_client.getPage(space_key, page_title)
        #print("{} / {} / {} / {}".format(page_title, space_key, rc, self.options), file=sys.stderr)
        self.processResults(rc, datas)

    def deletePage(self):
        page_id = self.options['<page_id>']
        rc, datas = self.jira_client.deletePage(page_id)
        self.processResults(rc, datas)

    def createPage(self):
        page_title = self.options['<page_title>']
        space_key = self.options['<space_key>']
        parent_id = self.options['<parent_id>']

        if self.hasOption('<page_file>'):
            page_file = self.options['<page_file>']
            with open(page_file, 'r') as page_raw:
                page_content = page_raw.read()
        else:
            page_content = ""

        rc, datas = self.jira_client.createPage(page_title, space_key,
                                                page_content, parent_id)
        self.processResults(rc, datas)

    def updatePage(self):
        page_title = self.options['<page_title>']
        space_key = self.options['<space_key>']
        page_file = self.options['<page_file>']
        with open(page_file, 'r') as page_raw:
            page_content = page_raw.read()
            rc, datas = self.jira_client.updatePage(page_title, space_key,
                                                    page_content)
            self.processResults(rc, datas)

    def movePage(self):
        page_title = self.options['<page_title>']
        space_key = self.options['<space_key>']
        parent_id = self.options['<parent_id>']
        rc, datas = self.jira_client.movePage(page_title, space_key, parent_id)
        self.processResults(rc, datas)
예제 #8
0
class Option(Base):
    """Manage options"""
    def run(self):
        #print('You supplied the following options:', json.dumps(self.options, indent=2, sort_keys=True))
        self.jira_client = JiraClient(self.loadConfiguration())
        if self.hasOption('get'):
            self.getOption()
        elif self.hasOption('add'):
            self.addOption()
        elif self.hasOption('del'):
            self.deleteOption()
        elif self.hasOption('replace'):
            self.replaceOption()
        elif self.hasOption('exist'):
            self.existOption()
        elif self.hasOption('getid'):
            self.getOptionId()
        else:
            print("Nothing to do.")

    def getOptionId(self):
        field_key = self.options['<field_key>']
        option_value = self.options['<option_value>']
        rc, datas = self.jira_client.getFieldOptions(field_key)
        option_values = json.loads(datas)['values']
        for current_option in option_values:
            current_option_value = current_option['value']
            if current_option_value == option_value:
                print(current_option['id'])
                break

    # replace <field_key> <option_to_replace> <option_to_use> <jql_filter>
    def replaceOption(self):
        field_key = self.options['<field_key>']
        option_to_replace = self.options['<option_to_replace>']
        option_to_use = self.options['<option_to_use>']
        jql_filter = self.options['<jql_filter>']
        rc, datas = self.jira_client.replaceOption(field_key,
                                                   option_to_replace,
                                                   option_to_use, jql_filter)
        print(datas)

    def existOption(self):
        field_key = self.options['<field_key>']
        option_value = self.options['<option_value>'].encode('utf-8')
        result = self.jira_client.hasOption(field_key, option_value)
        if result:
            print('TRUE')
        else:
            print('FALSE')

    def getOption(self):
        field_key = self.options['<field_key>']
        option_id = self.options['<option_id>']
        rc, datas = self.jira_client.getFieldOption(field_key, option_id)
        self.processResults(rc, datas)

    def deleteOption(self):
        field_key = self.options['<field_key>']
        option_id = self.options['<option_id>']
        if str(option_id).find('..') == -1:
            rc, datas = self.jira_client.deleteFieldOption(
                field_key, option_id)
            if rc != 204:
                try:
                    print(json.dumps(json.loads(datas), indent=2))
                except:
                    print(datas)
                self.processResults(rc, datas)
        else:
            options_limits = str(option_id).split('..')
            option_low = int(options_limits[0])
            option_high = int(options_limits[1])
            results = dict()
            for opt in range(option_low, option_high):
                rc, datas = self.jira_client.deleteFieldOption(field_key, opt)
                if rc != 204:
                    try:
                        errorMessages = json.loads(datas)['errorMessages']
                        if len(errorMessages) == 1:
                            results[opt] = errorMessages[0]
                        else:
                            results[opt] = errorMessages
                    except:
                        print(datas)
            print(json.dumps(results, indent=2))

    def addOption(self):
        field_key = self.options['<field_key>']
        option_value = self.options['<option_value>']
        project_keys = self.options['<project_keys>']
        projects = project_keys.split(',')
        config = dict(scope=dict(projects=projects))
        print("Will add %s option to field %s" % (option_value, field_key))
        if not self.jira_client.hasOption(field_key, option_value):
            jsonOption = dict(value=option_value, config=config)
            self.jira_client.addOption(field_key, jsonOption)

    def indexOf(self, item, array):
        idx = 0
        for i in array:
            if str(i) == str(item):
                return idx
            idx = idx + 1
        return -1
예제 #9
0
class Issue(Base):
    """Manage issues"""
    def run(self):
        #print('You supplied the following options:', json.dumps(self.options, indent=2, sort_keys=True))
        self.jira_client = JiraClient(self.loadConfiguration())
        if self.hasOption('get'):
            self.getIssue()
        elif self.hasOption('create'):
            self.createIssue()
        elif self.hasOption('del'):
            self.deleteIssue()
        elif self.hasOption('set'):
            self.updateIssue()
        elif self.hasOption('createmeta'):
            self.createMeta()
        else:
            print("Nothing to do.")

    def getIssue(self):
        issue_key = self.options['<issue_key>']
        rc, datas = self.jira_client.getIssue(issue_key)
        self.processResults(rc, datas)

    def deleteIssue(self):
        issue_key = self.options['<issue_key>']
        rc, datas = self.jira_client.deleteIssue(issue_key)
        if rc != 200:
            print(datas)
        self.processResults(rc, datas)

    def createIssue(self):
        json_file = self.options['<json_file>']
        with open(json_file, 'r') as issue_raw:
            issue = json.load(issue_raw)
            rc, datas = self.jira_client.createIssue(issue)
            self.processResults(rc, datas)

    def updateIssue(self):
        issue_keys = self.options['<issue_keys>']
        field_key = self.options['<field_key>']
        value_or_field_key = self.options['<value_or_field_key>']
        isLiteralValue = not (value_or_field_key.startswith('*'))
        if (str(issue_keys).find('..') > -1) or (str(issue_keys).find(',') >
                                                 -1):
            # A key is like 'DSP-78'
            issue_keys_array = list()
            if str(issue_keys).find('..') > -1:
                issue_limits = str(issue_keys).split('..')
                project_key = str(issue_limits[0].split('-')[0])
                issue_low = int(issue_limits[0].split('-')[1])
                project_key2 = str(issue_limits[1].split('-')[0])
                if (project_key != project_key2):
                    raise ValueError(
                        'The issues must be on the same project (%s/%s).' %
                        (project_key, project_key2))
                issue_high = int(issue_limits[1].split('-')[1])
                for id in range(issue_low, issue_high):
                    issue_keys_array.append(project_key + '-' + str(id))
            elif str(issue_keys).find(',') > -1:
                issue_keys_array = issue_keys.split(',')

            i = 1
            for issue_key in issue_keys_array:
                print("Update issue %s (%d/%d)" %
                      (issue_key, i, len(issue_keys_array)))
                rc, current_issue_json = self.jira_client.getIssue(issue_key)
                current_issue = json.loads(current_issue_json)
                issuetype_id = current_issue['fields']['issuetype']['id']
                project_key = current_issue['fields']['project']['id']
                rc, current_meta_json = self.jira_client.getCreateMeta(
                    project_key, issuetype_id)
                current_meta = json.loads(current_meta_json)
                targetFieldId = self.jira_client.getFieldIdFromKeyAndMeta(
                    field_key, current_meta)
                value = None
                if not (isLiteralValue):
                    sourceFieldKey = value_or_field_key[1:]
                    sourceFieldId = self.jira_client.getFieldIdFromKeyAndMeta(
                        sourceFieldKey, current_meta)
                    value = current_issue['fields'][sourceFieldId]
                else:
                    value = value_or_field_key
                # Now get the field id from the field name
                current_issue['fields'][targetFieldId] = value
                rc, datas = self.jira_client.updateIssue(
                    issue_key, field_key, value)
                self.processResults(rc, datas)
                i = i + 1
        else:
            rc, current_issue_json = self.jira_client.getIssue(issue_keys)
            current_issue = json.loads(current_issue_json)
            issuetype_id = current_issue['fields']['issuetype']['id']
            project_key = current_issue['fields']['project']['id']
            print('Get meta for ' + issue_keys + ', issuetype=' +
                  str(issuetype_id) + ', project_key=' + str(project_key))
            rc, current_meta_json = self.jira_client.getCreateMeta(
                project_key, issuetype_id)
            current_meta = json.loads(current_meta_json)
            targetFieldId = self.jira_client.getFieldIdFromKeyAndMeta(
                field_key, current_meta)
            value = None
            if not (isLiteralValue):
                sourceFieldKey = value_or_field_key[1:]
                sourceFieldId = self.jira_client.getFieldIdFromKeyAndMeta(
                    sourceFieldKey, current_meta)
                value = current_issue['fields'][sourceFieldId]
            else:
                value = value_or_field_key
            # Now get the field id from the field name
            current_issue['fields'][targetFieldId] = value
            rc, datas = self.jira_client.updateIssue(issue_keys, field_key,
                                                     value)
            self.processResults(rc, datas)

    def createMeta(self):
        project_key = self.options['<project_key>']
        issue_key = self.options['<issue_type>']
        rc, datas = self.jira_client.getCreateMeta(project_key, issue_key)
        self.processResults(rc, datas)