class jira: def __init__(self): # load admin credentials with open(".credentials.json") as credentials: self.creds = json.load(credentials) # load new user information with open(".new_user.json") as new_user: self.user = json.load(new_user) self.JIRA_SERVER = self.creds["jira"]["server"] self.JIRA_USERNAME = self.creds["jira"]["username"] self.JIRA_PASSWORD = self.creds["jira"]["password"] self.jc = JIRA(server=self.JIRA_SERVER, basic_auth=(self.JIRA_USERNAME, self.JIRA_PASSWORD)) # add new user to jira (keyprprojects.atlassian.net) def add_user(self): # contractors do not get confluence access, on an as-needed basis if self.user["contractor"].lower() == "f": # username, email, directory(?), password, full name, notification, activation self.jc.add_user(self.user["email"], self.user["email"], 1, self.user["password"], self.user["fullName"], False, True) print("Jira/Confluence: User created - %s" % self.user["email"]) # set users groups def add_group(self): # contractors do not get confluence access, on an as-needed basis if self.user["contractor"].lower() == "f": # Default groups # username, group name self.jc.add_user_to_group(self.user["email"], "internal-dev") self.jc.add_user_to_group(self.user["email"], "confluence-users") print("Jira/Confluence: Groups added - internal-dev, jira-users, confluence-users") def remove_group(self): self.jc.remove_user_from_group(self.user["email"], "internal-dev") self.jc.remove_user_from_group(self.user["email"], "confluence-users") self.jc.remove_user_from_group(self.user["email"], "jira-users") print("Jira/Confluence: Removed user from groups [internal-dev, confluence-users, jira-users]")
def create_user(user_config, service_config): user_config = \ utils.convert_config_file(user_config)['jira'] service_config = \ utils.convert_config_file(service_config)['jira'] jira = JIRA(options=service_config['jira_options'], basic_auth=( service_config['username'], service_config['password'] )) return jira.add_user(**user_config)
def add_member_jira_individual(email, service): options = {'server': 'https://%s.atlassian.net' % service.org_name} jira = JIRA(options, basic_auth=('admin', service.token)) jira.add_user(email, email)
class Strategy: def __init__(self, account_name, user_name, user_password): self._account = account_name self._user = user_name self._password = user_password self._server = 'https://{}.atlassian.net'.format(self._account) self._jira_connection = JIRA(server=self._server, basic_auth=(self._user, self._password)) self._makelog = makelog.Makelog('output', 'errorlog') def execute(self, key): if key == 1: self._doreporting() elif key == 2: self._domailing() elif key == 3: self._dogenerating() else: return False def _doreporting(self): data_peruser = {} data_percomponent = {} # getting all users users_all = self._jira_connection.search_users('%', maxResults=False, includeInactive=True) for user in users_all: data_peruser[user.name] = { 'time_total': 0, 'time_perissue': {}, 'actual_name': user.displayName, 'components': set() } # getting all components components_all = set() projects_all = self._jira_connection.projects() for project in projects_all: try: comps = self._jira_connection.project_components(project) components_all.update(comps) except: outstr = "Unexpected error with getting components from project: {}\n".format( project.key) self._makelog.putto_console(outstr) self._makelog.putto_errorlog(outstr, traceback.format_exc()) for comp in components_all: try: component_data = self._jira_connection.component(comp.id) data_percomponent[component_data.id] = { 'name': component_data.name, 'projectkey': component_data.project, 'time_total': 0, 'time_perissue': {}, 'lead': '' if not hasattr(component_data, 'lead') else component_data.lead.name } if hasattr(component_data, 'lead'): data_peruser[component_data.lead.name]['components'].add( component_data.id) except: outstr = "Unexpected error with getting data of component id: {}\n".format( comp.id) self._makelog.putto_console(outstr) self._makelog.putto_errorlog(outstr, traceback.format_exc()) # counting hours logic issues_all = self._jira_connection.search_issues('', maxResults=False) for iss in issues_all: try: iss_works = self._jira_connection.worklogs(iss) for work in iss_works: # per user data_peruser[work.author. name]['time_total'] += work.timeSpentSeconds if iss.key not in data_peruser[ work.author.name]['time_perissue']: data_peruser[work.author.name]['time_perissue'][ iss.key] = 0 data_peruser[work.author.name]['time_perissue'][ iss.key] += work.timeSpentSeconds # per valid component (with lead) for comp in iss.fields.components: if data_percomponent[ comp.id]['lead'] == work.author.name: data_percomponent[ comp.id]['time_total'] += work.timeSpentSeconds if iss.key not in data_percomponent[ comp.id]['time_perissue']: data_percomponent[comp.id]['time_perissue'][ iss.key] = 0 data_percomponent[comp.id]['time_perissue'][ iss.key] += work.timeSpentSeconds except: outstr = "Unexpected error counting hours with issue: {}\n".format( iss.key) self._makelog.putto_console(outstr) self._makelog.putto_errorlog(outstr, traceback.format_exc()) # outputting data outstr = "" outstr += "\t\t\tReport on the spent hours:\n" outstr += "\n\t\tPer programmer:\n\n" for user_name, user_dat in data_peruser.iteritems(): outstr += "-> Name: {} ({})\n".format(user_dat['actual_name'], user_name) outstr += " Total time: {} hour(s)\n".format( str(user_dat['time_total'] / 3600)) outstr += " Time per issue:\n" for iss_key, time_val in user_dat['time_perissue'].iteritems(): outstr += "\t{} is: {} hour(s)\n".format( iss_key, str(time_val / 3600)) outstr += "\n" outstr += "\n\t\tPer component (with lead only):\n\n" for comp_id, comp_dat in data_percomponent.iteritems(): outstr += "-> Name: {} ({})\n".format(comp_dat['name'], comp_dat['projectkey']) outstr += " Lead: {}\n".format(comp_dat['lead']) outstr += " Total time: {} hour(s)\n".format( str(comp_dat['time_total'] / 3600)) outstr += " Time per issue:\n" for iss_key, time_val in comp_dat['time_perissue'].iteritems(): outstr += "\t{} is: {} hour(s)\n".format( iss_key, str(time_val / 3600)) outstr += "\n" outstr += "\n-----> END REPORT <-----\n\n" self._makelog.putto_console(outstr, iscln=True) self._makelog.putto_file(outstr) def _domailing(self): issues_tonotify = [] issues_all = self._jira_connection.search_issues('', maxResults=False) for iss in issues_all: try: iss_data = self._jira_connection.issue(iss) if (iss_data.fields.timeestimate is None) or (len( iss_data.fields.components) == 0): issues_tonotify.append({ 'name': iss_data.fields.assignee.name, 'dispname': iss_data.fields.assignee.displayName, 'email': iss_data.fields.assignee.emailAddress, 'isskey': iss.key }) except: outstr = "Unexpected error with getting issue: {}\n".format( iss.key) self._makelog.putto_console(outstr) self._makelog.putto_errorlog(outstr, traceback.format_exc()) for data in issues_tonotify: try: url = "{}/rest/api/2/issue/{}/notify".format( self._server, data['isskey']) notify_data = { "subject": "You have some incomplete fields in issue {}".format( data['isskey']), "textBody": "Your got this notification because have one or couple incomplete fields in {} issue. Note, that 'estimates' \ and 'component' fields are mandatory. Please, check this fields and fill its in if need." .format(data['isskey']), "to": { "users": [{ "name": data['name'] }] }, } requests.post(url, auth=(self._user, self._password), json=notify_data) outstr = "Successfully sending notification to:\n-> {} {} about incomplete fields in {} issue\n".format( data['dispname'], data['email'], data['isskey']) self._makelog.putto_console(outstr) self._makelog.putto_file(outstr) except: outstr = "Unexpected error with sending notification to:\n-> {} {} about: {}\n".format( data['dispname'], data['email'], data['isskey']) self._makelog.putto_console(outstr) self._makelog.putto_errorlog(outstr, traceback.format_exc()) if len(issues_tonotify) == 0: self._makelog.putto_console( "All tested issues were filed in correct") def _dogenerating(self): names_base = namebase.Namebase() maxlen_projname = 10 content_count = { 'project': 1, 'user': 1, 'component': 2, 'issue': 10, 'worklog': 20 } # making projects for i in xrange(content_count['project']): newname = names_base.getname_project() parts = newname.split()[::2] newkey = string.join( (parts[0][:(maxlen_projname - len(parts[1]))], parts[1]), '') try: self._jira_connection.create_project(newkey, name=newname) outstr = "Project {} was successfully created\n".format(newkey) self._makelog.putto_console(outstr) self._makelog.putto_file(outstr) except: outstr = "Some problem with project {} creation\n".format( newkey) self._makelog.putto_console(outstr) self._makelog.putto_errorlog(outstr, traceback.format_exc()) # making users for i in xrange(content_count['user']): newname = names_base.getname_user() try: self._jira_connection.add_user(newname, "{}@mail.net".format(newname),\ fullname="Name {}{}".format(string.upper(newname[:1]), newname[1:])) outstr = "User {} was successfully created\n".format(newname) self._makelog.putto_console(outstr) self._makelog.putto_file(outstr) except: outstr = "Some problem with user {} creation\n".format(newname) self._makelog.putto_console(outstr) self._makelog.putto_errorlog(outstr, traceback.format_exc()) # getting all valid project keys projects_keys = [] projects_all = self._jira_connection.projects() for project in projects_all: projects_keys.append(project.key) # getting all valid user names users_keys = [] users_all = self._jira_connection.search_users('%', maxResults=False, includeInactive=True) for user in users_all: users_keys.append(user.name) # making components for i in xrange(content_count['component']): newname = names_base.getname_component() try: self._jira_connection.create_component( newname, random.choice(projects_keys), leadUserName=random.choice(users_keys)) outstr = "Component {} was successfully created\n".format( newname) self._makelog.putto_console(outstr) self._makelog.putto_file(outstr) except: outstr = "Some problem with component {} creation\n".format( newname) self._makelog.putto_console(outstr) self._makelog.putto_errorlog(outstr, traceback.format_exc()) # making issues for i in xrange(content_count['issue']): newname = names_base.getname_issue() fields = { "project": { "key": random.choice(projects_keys) }, "summary": "Here should be some random text summary for issue {}".format( newname), "description": "Here should be some random text description for issue {}". format(newname), "issuetype": { "name": random.choice( ("Bug", "Improvement", "Task", "Epic", "New Feature")) }, "assignee": { "name": random.choice(users_keys) }, "timetracking": { "originalEstimate": "{}w {}d {}h".format(random.randint(1, 3), random.randint(1, 4), random.randint(1, 7)), "remainingEstimate": "{}d {}h".format(random.randint(1, 4), random.randint(1, 7)) } } try: self._jira_connection.create_issue(fields=fields) outstr = "Issue {} was successfully created\n".format(newname) self._makelog.putto_console(outstr) self._makelog.putto_file(outstr) except: outstr = "Some problem with issue {} creation\n".format( newname) self._makelog.putto_console(outstr) self._makelog.putto_errorlog(outstr, traceback.format_exc()) # making worklogs issues_all = self._jira_connection.search_issues('', maxResults=False) for i in xrange(content_count['worklog']): iss = random.choice(issues_all) try: self._jira_connection.add_worklog(iss, timeSpent="{}h".format(random.randint(1, 3)), user=random.choice(users_keys),\ comment="Here should be some random text about work on this issue") outstr = "Worklog for issue {} was successfully created\n".format( iss.key) self._makelog.putto_console(outstr) self._makelog.putto_file(outstr) except: outstr = "Some problem with worklog creation for issue {}\n".format( iss.key) self._makelog.putto_console(outstr) self._makelog.putto_errorlog(outstr, traceback.format_exc())
class jira(): def __init__(self): pass def auth(self): PWD = os.getenv("HOME") with open("%s/.python_auth_cfg.yml" % PWD, 'r') as auth: auth_conf = yaml.load(auth, Loader=yaml.FullLoader) jira_conf = auth_conf['jira'] jira_admin_user = jira_conf['admin_user'] jira_admin_api_token = jira_conf['admin_api_token'] jira_basic_url = jira_conf['jira_basic_url'] self.jira_basic_url = jira_basic_url self.jira_options = { 'server' : jira_basic_url, 'max_retries' : 1 } try: self._session = JIRA(self.jira_options, basic_auth=(jira_admin_user, jira_admin_api_token)) except JIRAError and TypeError and KeyError as error: print('jira.init.auth <%s>' % error) return self._session def user_invite(self, email): self.USERNAME = email.split('@')[0] try: self._session.add_user(self.USERNAME, email, active=True) except JIRAError as error: print('jira.init.user_invite <%s>' % error.text) def user_delete(self, username): try: DELETE_USER = self._session.delete_user(username) except JIRAError as error: print('jira.init.user_delete <%s>' % error.text) def user_blocked(self, username): DEACTIVATE_USER = self._session.deactivate_user(username) try: DELETE_USER = self._session.deactivate_user(username) except JIRAError and TypeError and KeyError as error: print('jira_api_user.init.user_blocked <%s>' % error) def user_add_group(self, groupName): time.sleep(15) with open("config/jira_group_access.yml", 'r') as jira_group_access: jira_group_access = yaml.load(jira_group_access, Loader=yaml.FullLoader) try: group_list = jira_group_access[groupName] for group in group_list: try: self._session.add_user_to_group(self.USERNAME , group) except JIRAError and TypeError and KeyError as error: print('jira_api_user.init.user_add_group <%s>' % error) except KeyError as error: print('init.user_add_group | add default group | Error < groupName: %s is not defined >' % groupName)