def main(): parser = argparse.ArgumentParser( description= 'gl2csv - Command line utility to export Gitlab issues as CSV file') parser.add_argument('token', help='A token to access gitlab') parser.add_argument('--url', help='Gitlab URL', default='https://gitlab.fhnw.ch/') parser.add_argument( '--project', help='Gitlab project name (i.e. webteam/fhnw.webauftritt)', default='webteam/fhnw.webauftritt') args = parser.parse_args() gl = gitlab.Gitlab(args.url, args.token) print(args.project) project = gl.projects.get(args.project) issues = project.issues.list(all=True) pickle.dump(issues, open("save.p", "wb")) print('Saved issues') issues = pickle.load(open('save.p', 'rb')) fieldnames = [ 'iid', 'id', 'title', 'state', 'created_at', 'updated_at', 'closed_at', 'closed_week' ] f = open('issues.csv', 'w') writer = csv.DictWriter(f, fieldnames=fieldnames) writer.writeheader() issue_number = len(issues) all_issues = [] weeks = {} for i, issue in enumerate(issues): d = { key: enc(val) for key, val in issue.attributes.items() if key in fieldnames } print('{:4d}/{:4d} - ({})'.format(i, issue_number, d)) try: notes = issue.notes.list() except gitlab.exceptions.GitlabConnectionError: print('Can' 't get notes for issue {0!s}'.format(issue)) continue if d['state'] == 'closed': c = [x.created_at for x in notes if 'closed' in x.body.lower()] if c: closed_at = c[0].encode('utf-8') d['closed_at'] = closed_at closed_date = iso8601.parse_date(closed_at.decode('utf-8')) else: bodys = [ x.body.lower() for x in notes if isinstance(x.body, (str, bytes)) and 'moved' in x.body.lower() ] if bodys: continue # ignore all issues not ment to be here else: print('No closing date found {0!s}'.format(issue)) print print(bodys) d['closed_at'] = d['updated_at'] closed_date = iso8601.parse_date( d['updated_at'].decode('utf-8')) if True: # closed_date.replace(tzinfo=None) > datetime.datetime(2017,4,1): d['closed_week'] = '{:4d}-{:2d}'.format( closed_date.year, closed_date.month) # d['closed_week'] = '{:4d}-{:2d}'.format(*closed_date.isocalendar()[:2]) else: d['closed_week'] = '' else: d['closed_at'] = '' d['closed_week'] = '' writer.writerow(d) all_issues.append(d) if d['closed_week']: if d['closed_week'] not in weeks: weeks[d['closed_week']] = 0 weeks[d['closed_week']] += 1 issues_count = list(weeks.values()) print(weeks) # print('Minimum gelöst: {0} '.format(min([x for x in issues_count if x]))) # print('Maximum gelöst: {0} '.format(max(issues_count))) # print('Median: {0} '.format(numpy.median(issues_count))) # print('Mittelwert: {0} '.format(numpy.average(issues_count))) # 17 is because we started at week #36 in 2016 # 15 are the zero weeks 19.5.2017 # wX = weeks.keys() # wY = issues_count # plt.plot(wX, wY) # plt.xlabel(u'Monat') # plt.ylabel(u'gelöste Issues') # plt.show() f.close()
# Parse Command Line Arguments parser = argparse.ArgumentParser() parser.add_argument("-s", "--server", help="URL of gitlab host", type=str) parser.add_argument("-u", "--user", help="User name", type=str) parser.add_argument("-p", "--pw", help="Password", type=str) parser.add_argument("-r", "--repo", help="Git Repo to Create", type=str) args = parser.parse_args() print args server = args.server user = args.user repo = args.repo password = args.pw git = gitlab.Gitlab(server, verify_ssl=False) git.login(user, password) projectexists = False projects = git.getprojects() for project in projects: #print project['owner']['username'], project['name'] if user == project['owner']['username'] and repo == project['name']: projectexists = True if not projectexists: print "creating repo" git.createproject(name=repo, description="Ansible Repo", issues_enabled=1, merge_requests_enabled=1,
import json import urllib3 from configparser import ConfigParser from colored import fg, bg, attr urllib3.disable_warnings() config = ConfigParser() cfg_path = os.path.join(os.environ['HOME'], '.gitlab.cfg') if not os.path.exists(cfg_path): cfg_path = '.gitlab.cfg' config.read(cfg_path) domain = config['default']['domain'] private_token = config['default']['private_token'] gl = gitlab.Gitlab(domain, private_token=private_token) def color(text): return '{}{} {} {}'.format(fg('white'), bg('yellow'), text, attr('reset')) def replace(text, word): return text.replace(word, color(word)) class Searcher(object): def __init__(self, word, count, debug): self.word = word self.count = count self.debug = debug
import moviepy.editor as mpy import multiprocessing from functools import lru_cache, partial import logging import os import concurrent import time import random import requests import requests_cache requests_cache.install_cache() gl = gitlab.Gitlab('https://gitlab.jyu.fi', private_token=os.getenv("GITLAB_TOKEN")) # Some forks that are not interesting FORKS_DENIED = [2034, 1739, 1833, 1956, 2030] RESOLUTION = (1920, 1080) GRID = (5, 3) FPS = 30 logging.basicConfig(level=logging.DEBUG) def git_authors(path): """ Fetch git authors. Return list of email and name vectors """ git_output = subprocess.check_output(
import gitlab TOKEN = "CHANGEME" CLEAN_BEFORE = "2018-07" gl = gitlab.Gitlab("https://code.eliotberriot.com", private_token=TOKEN, per_page=100) project = gl.projects.get("funkwhale/funkwhale") jobs = project.jobs.list(as_list=False) total = jobs.total for job in jobs: if job.attributes["ref"] != "develop": continue if job.attributes["status"] != "success": continue if job.attributes["tag"] is True: continue if job.attributes["name"] not in ["build_api", "build_front", "pages"]: continue if job.attributes["created_at"].startswith(CLEAN_BEFORE): continue relevant = { "ref": job.attributes["ref"], "status": job.attributes["status"], "tag": job.attributes["tag"], "name": job.attributes["name"], "created_at": job.attributes["created_at"], } print("Deleting job {}!".format(job.id), relevant)
def _rpc_connection(self): # we use an access token so only the password field is required return gitlab.Gitlab(self.bug_system.api_url, private_token=self.bug_system.api_password)
def main(): argument_spec = basic_auth_argument_spec() argument_spec.update(dict( server_url=dict(type='str', removed_in_version="2.10"), login_user=dict(type='str', no_log=True, removed_in_version="2.10"), login_password=dict(type='str', no_log=True, removed_in_version="2.10"), api_token=dict(type='str', no_log=True, aliases=["login_token"]), name=dict(type='str', required=True), path=dict(type='str'), description=dict(type='str'), state=dict(type='str', default="present", choices=["absent", "present"]), parent=dict(type='str'), visibility=dict(type='str', default="private", choices=["internal", "private", "public"]), )) module = AnsibleModule( argument_spec=argument_spec, mutually_exclusive=[ ['api_url', 'server_url'], ['api_username', 'login_user'], ['api_password', 'login_password'], ['api_username', 'api_token'], ['api_password', 'api_token'], ['login_user', 'login_token'], ['login_password', 'login_token'] ], required_together=[ ['api_username', 'api_password'], ['login_user', 'login_password'], ], required_one_of=[ ['api_username', 'api_token', 'login_user', 'login_token'], ['server_url', 'api_url'] ], supports_check_mode=True, ) deprecation_warning(module) server_url = module.params['server_url'] login_user = module.params['login_user'] login_password = module.params['login_password'] api_url = module.params['api_url'] validate_certs = module.params['validate_certs'] api_user = module.params['api_username'] api_password = module.params['api_password'] gitlab_url = server_url if api_url is None else api_url gitlab_user = login_user if api_user is None else api_user gitlab_password = login_password if api_password is None else api_password gitlab_token = module.params['api_token'] group_name = module.params['name'] group_path = module.params['path'] description = module.params['description'] state = module.params['state'] parent_identifier = module.params['parent'] group_visibility = module.params['visibility'] if not HAS_GITLAB_PACKAGE: module.fail_json(msg=missing_required_lib("python-gitlab"), exception=GITLAB_IMP_ERR) try: gitlab_instance = gitlab.Gitlab(url=gitlab_url, ssl_verify=validate_certs, email=gitlab_user, password=gitlab_password, private_token=gitlab_token, api_version=4) gitlab_instance.auth() except (gitlab.exceptions.GitlabAuthenticationError, gitlab.exceptions.GitlabGetError) as e: module.fail_json(msg="Failed to connect to Gitlab server: %s" % to_native(e)) except (gitlab.exceptions.GitlabHttpError) as e: module.fail_json(msg="Failed to connect to Gitlab server: %s. \ Gitlab remove Session API now that private tokens are removed from user API endpoints since version 10.2" % to_native(e)) # Define default group_path based on group_name if group_path is None: group_path = group_name.replace(" ", "_") gitlab_group = GitLabGroup(module, gitlab_instance) parent_group = None if parent_identifier: parent_group = findGroup(gitlab_instance, parent_identifier) if not parent_group: module.fail_json(msg="Failed create Gitlab group: Parent group doesn't exists") group_exists = gitlab_group.existsGroup(parent_group.full_path + '/' + group_path) else: group_exists = gitlab_group.existsGroup(group_path) if state == 'absent': if group_exists: gitlab_group.deleteGroup() module.exit_json(changed=True, msg="Successfully deleted group %s" % group_name) else: module.exit_json(changed=False, msg="Group deleted or does not exists") if state == 'present': if gitlab_group.createOrUpdateGroup(group_name, parent_group, { "path": group_path, "description": description, "visibility": group_visibility}): module.exit_json(changed=True, msg="Successfully created or updated the group %s" % group_name, group=gitlab_group.groupObject._attrs) else: module.exit_json(changed=False, msg="No need to update the group %s" % group_name, group=gitlab_group.groupObject._attrs)
def __init__(self, host: str, project_id: int, token: str): self._instance = gitlab.Gitlab(host, private_token=token) self.project = self._instance.projects.get(project_id)
import gitlab import time, timeit import sys from datetime import timedelta gl = gitlab.Gitlab("https://your_gitlab_instance.com/", private_token="cujzCzpTQ5W2xBymrq5T") project = gl.projects.get('restExampleTest') create_pipeline = project.pipelines.create({'ref': 'master'}) # Set default status = "pending" start_time = timeit.default_timer() while (status == "running" or status == "pending"): pipeline = project.pipelines.get(create_pipeline.id) status = pipeline.status elapsed_time = timeit.default_timer() - start_time formated_time = str(timedelta(seconds=elapsed_time)) sys.stderr.write("Still running pipeline... ({})\n".format(formated_time)) if status == "success": sys.stderr.write("\nPipeline success\n") break elif status == "failed": raise Exception elif status == "canceled":
def login(): config = get_config() return gitlab.Gitlab(config[0], config[1])
def call(cmd): code = os.system(cmd) if code != 0: raise Exception('"{}"" returned {}'.format(cmd, code)) def call_bash(cmd): call("bash -c " + shlex.quote(cmd)) GIT_REPOS_DIRECTORY = sys.argv[1] GITLAB_TOKEN = sys.argv[2] if len(sys.argv) == 3 else None if GITLAB_TOKEN: gl = gitlab.Gitlab('https://git.regardscitoyens.org/', private_token=GITLAB_TOKEN) group = gl.groups.list(search='parlement')[0] # delete existing bills projects = group.projects.list() for project in projects: print('delete', project.id) gl.projects.delete(project.id) for procedure_file in sorted( glob.glob("data/**/procedure.json", recursive=True)): procedure = open_json(procedure_file) if len(procedure["steps"]) < 5: continue if procedure["stats"]["total_amendements"] < 5:
def get_auth_token(cls, login, password, prompt=None): gl = gitlab.Gitlab(url='https://{}'.format(cls.fqdn), email=login, password=password) gl.auth() return gl.user.private_token
repo_url=r.html_url, description=emoji.emojize(r.description, use_aliases=True) if r.description else None, homepage_url=r.homepage, forks_count=r.forks_count, # watchers_count=r.subscribers_count, issues_count=r.open_issues_count, # downloads_count=r.get_downloads().totalCount, releases_count=r.get_releases().totalCount, # readme=r.get_readme().decoded_content, doi=v["doi"] or get_doi(r.html_url), objectID=f"github:{r.id}", ) else: with gitlab.Gitlab(m.group(1)) as gl: p = gl.projects.get( m.group(2) ) # https://gitlab.nektar.info/api/v4/projects/2/ repo = dict( name=p.attributes["name"], owner_name=p.namespace["name"], owner_uri=f"gitlab.nektar.info:{p.namespace['path']}", created_at=datetime.datetime.fromisoformat(p.created_at[:-1]), updated_at=datetime.datetime.fromisoformat(p.last_activity_at[:-1]), language=max(p.languages().items(), key=operator.itemgetter(1))[0], topics=p.tag_list, # is_fork=False, # TODO stars_count=p.star_count, license=v["licence"], commits_count=p.commits.list(as_list=False).total,
def __init__(self): self.gl = gitlab.Gitlab(_config.GIT_URL, _config.GIT_PRIVATE_TOKEN)
def gitlab_create_remote_repo(git_remote): git = gitlab.Gitlab(git_remote["url"], git_remote["token"]) git.auth() command = "git.projects.create(" + str(git_remote["repo_config"]) + ")" # pylint: disable=exec-used exec(command)
def main(): argument_spec = basic_auth_argument_spec() argument_spec.update( dict( url=dict(type='str', removed_in_version="2.10"), api_token=dict(type='str', no_log=True, aliases=["private_token"]), description=dict(type='str', required=True, aliases=["name"]), active=dict(type='bool', default=True), tag_list=dict(type='list', default=[]), run_untagged=dict(type='bool', default=True), locked=dict(type='bool', default=False), access_level=dict(type='str', default='ref_protected', choices=["not_protected", "ref_protected"]), maximum_timeout=dict(type='int', default=3600), registration_token=dict(type='str', required=True), state=dict(type='str', default="present", choices=["absent", "present"]), )) module = AnsibleModule( argument_spec=argument_spec, mutually_exclusive=[ ['api_url', 'url'], ['api_username', 'api_token'], ['api_password', 'api_token'], ], required_together=[ ['api_username', 'api_password'], ['login_user', 'login_password'], ], required_one_of=[['api_username', 'api_token'], ['api_url', 'url']], supports_check_mode=True, ) deprecation_warning(module) if module.params['url'] is not None: url = re.sub('/api.*', '', module.params['url']) api_url = module.params['api_url'] validate_certs = module.params['validate_certs'] gitlab_url = url if api_url is None else api_url gitlab_user = module.params['api_username'] gitlab_password = module.params['api_password'] gitlab_token = module.params['api_token'] state = module.params['state'] runner_description = module.params['description'] runner_active = module.params['active'] tag_list = module.params['tag_list'] run_untagged = module.params['run_untagged'] runner_locked = module.params['locked'] access_level = module.params['access_level'] maximum_timeout = module.params['maximum_timeout'] registration_token = module.params['registration_token'] if not HAS_GITLAB_PACKAGE: module.fail_json(msg=missing_required_lib("python-gitlab"), exception=GITLAB_IMP_ERR) try: gitlab_instance = gitlab.Gitlab(url=gitlab_url, ssl_verify=validate_certs, email=gitlab_user, password=gitlab_password, private_token=gitlab_token, api_version=4) gitlab_instance.auth() except (gitlab.exceptions.GitlabAuthenticationError, gitlab.exceptions.GitlabGetError) as e: module.fail_json(msg="Failed to connect to GitLab server: %s" % to_native(e)) except (gitlab.exceptions.GitlabHttpError) as e: module.fail_json(msg="Failed to connect to GitLab server: %s. \ GitLab remove Session API now that private tokens are removed from user API endpoints since version 10.2" % to_native(e)) gitlab_runner = GitLabRunner(module, gitlab_instance) runner_exists = gitlab_runner.existsRunner(runner_description) if state == 'absent': if runner_exists: gitlab_runner.deleteRunner() module.exit_json(changed=True, msg="Successfully deleted runner %s" % runner_description) else: module.exit_json(changed=False, msg="Runner deleted or does not exists") if state == 'present': if gitlab_runner.createOrUpdateRunner( runner_description, { "active": runner_active, "tag_list": tag_list, "run_untagged": run_untagged, "locked": runner_locked, "access_level": access_level, "maximum_timeout": maximum_timeout, "registration_token": registration_token }): module.exit_json( changed=True, runner=gitlab_runner.runnerObject._attrs, msg="Successfully created or updated the runner %s" % runner_description) else: module.exit_json(changed=False, runner=gitlab_runner.runnerObject._attrs, msg="No need to update the runner %s" % runner_description)
def init_gitlab(git_url=f'http://{gitlab_url}', git_token=gitlab_token): gl = gitlab.Gitlab(git_url, private_token=git_token) assert gl.version()[0] != 'unknown', 'gitlab connection failure' return gl
def __init__(self, url, private_token, project_id=''): self._gitlab = gitlab.Gitlab(url, private_token=private_token) if len(project_id) > 0: self.set_project_by_id(project_id)
def __init__(self, token, project): super(GitLabBackend, self).__init__(token, project) self.client = gitlab.Gitlab(GITLAB_URL, token, api_version=str(GITLAB_API_VERSION))
def create(self): try: print('Data: {}..'.format(self.today)) URL = 'https://gitlab.com' session = requests.Session() session.headers.update({'Private-Token': self.token}) print('Entrando no Gitlab...') gl = gitlab.Gitlab(URL, api_version=4, session=session) print('Buscando eventos do projeto...') user = gl.users.list(username=self.username)[0] project = gl.projects.get(self.project_id) events = project.events.list(per_page=50) if not os.path.exists(self.path): os.makedirs(self.path) print('Criando arquivo de daily...') daily_file = open(os.path.join(self.path, self.filename), 'w+') daily_file.write("O que fiz hoje?\r\n\r\n") print('Listando os eventos no arquivo...') for event in events: if event.attributes['author_username'] == self.username: created = datetime.strptime(event.attributes['created_at'], '%Y-%m-%dT%H:%M:%S.%fZ') if created.date() == self.today: if event.attributes['action_name'] == 'accepted': target_type = event.attributes['target_type'] target_title = event.attributes['target_title'] daily_file.write("[X] Aceito {} do {}\r\n".format( target_type, target_title)) elif event.attributes['action_name'] == 'opened': target_type = event.attributes['target_type'] target_title = event.attributes['target_title'] daily_file.write("[X] Aberto {}: {}\r\n".format( target_type, target_title)) elif event.attributes['action_name'] == 'pushed to': commit = event.attributes['push_data'] commit_title = commit['commit_title'] daily_file.write("[X] {}\r\n".format(commit_title)) elif event.attributes['action_name'] == 'commented on': target_title = event.attributes['target_title'] daily_file.write("[X] Comentado em: {}\r\n".format( target_title)) daily_file.write( "\r\nQuais foram minhas dificuldades ou impedimentos? (se houveram)?\r\n" ) daily_file.write("O que vou fazer no próximo dia?\r\n") daily_file.close() # Abre o arquivo para verificação subprocess.Popen( ["subl", "-w", os.path.join(self.path, self.filename)]).wait() except Exception as e: print('Erro ao criar o arquivo de daily: {} \n'.format(e)) sys.exit()
def init_gitlab(self): # https://python-gitlab.readthedocs.io/en/stable/api-usage.html import gitlab if os.environ.get("GITLAB_PRIVATE_TOKEN"): logger.info("Using GITLAB_PRIVATE_TOKEN") self.gl = gitlab.Gitlab( "http://gitlab.com", private_token=os.environ.get("GITLAB_PRIVATE_TOKEN"), ) elif os.environ.get("CI_JOB_TOKEN"): logger.info("Using CI_JOB_TOKEN") self.gl = gitlab.Gitlab("http://gitlab.com", job_token=os.environ["CI_JOB_TOKEN"]) else: raise Exception( "Can't authenticate against Gitlab ( export GITLAB_PRIVATE_TOKEN )" ) if os.environ.get("CI_PROJECT_ROOT_NAMESPACE"): project_root_namespace = os.environ.get( "CI_PROJECT_ROOT_NAMESPACE") logger.info( f"Using project_root_namespace: {project_root_namespace} ( export CI_PROJECT_ROOT_NAMESPACE={project_root_namespace} )" ) else: raise Exception( "no CI_PROJECT_ROOT_NAMESPACE given ( export CI_PROJECT_ROOT_NAMESPACE=k9ert )" ) if os.environ.get("CI_PROJECT_ID"): self.project_id = os.environ.get("CI_PROJECT_ID") self.github_project = f"{project_root_namespace}/specter-desktop" else: logger.error("No Project given. choose one:") for project in self.gl.projects.list(search="specter-desktop"): logger.info( f" export CI_PROJECT_ID={project.id} # {project.name_with_namespace}" ) exit(1) logger.info(f"Using project_id: {self.project_id} ") logger.info(f"Using github_project: {self.github_project}") try: from gitlab.v4.objects import Project self.project: Project = self.gl.projects.get(self.project_id) except gitlab.exceptions.GitlabAuthenticationError as e: logger.fatal(e) logger.error( "Your token might be expired or wrong. Get a new one here:") logger.error( " https://gitlab.com/-/profile/personal_access_tokens") exit(2) if self.project.attributes["namespace"][ "path"] != project_root_namespace: logger.fatal( f"project_root_namespace ({ project_root_namespace }) does not match namespace of Project ({self.project.attributes['namespace']['path']}) " ) logger.error("You might want to: unset CI_PROJECT_ID") exit(2) if os.environ.get("CI_COMMIT_TAG"): self.tag = os.environ.get("CI_COMMIT_TAG") else: raise Exception( "no tag given ( export CI_COMMIT_TAG=v0.0.0.0-pre13 )") logger.info(f"Using tag: {self.tag}") if os.environ.get("CI_PIPELINE_ID"): self.pipeline_id = os.environ.get("CI_PIPELINE_ID") self.pipeline = self.project.pipelines.get(self.pipeline_id) else: logger.info( "no CI_PIPELINE_ID given, trying to find an appropriate one ..." ) pipelines = self.project.pipelines.list() for pipeline in pipelines: if pipeline.ref == self.tag: self.pipeline = pipeline logger.info(f"Found matching pipeline: {pipeline}") if not hasattr(self, "pipeline"): logger.error( f"Could not find tag {self.tag} in the pipeline-refs:") for pipeline in self.project.pipelines.list(): logger.error(pipeline.ref) raise Exception( "no CI_PIPELINE_ID given ( export CI_PIPELINE_ID= ) or maybe you're on the wrong project ( export CI_PROJECT_ROOT_NAMESPACE= )" ) logger.info(f"Using pipeline_id: {self.pipeline.id}") Path(self.target_dir).mkdir(parents=True, exist_ok=True)
#sshpubkey = join(home, ".ssh/id_rsa.pub") glurl = "http://gl.kanripo.org" #get the token config = ConfigParser.ConfigParser() cfgfile = os.path.join(mdbase, "user/mandoku-settings.cfg") config.readfp(open(cfgfile)) gltok = config.get("Gitlab", "Private Token") #get the key path sshpubkey = os.path.join(home, ".ssh/glkanripo.pub") sshprivkey = os.path.join(home, ".ssh/glkanripo") # Connect to get the current user gl = gitlab.Gitlab(glurl, gltok) #print gl.getsshkeys() user = getpass.getuser() fn = codecs.open(sshpubkey, 'r', 'utf-8') #print user key = fn.read() #print "key length", len(key), key #print sshpubkey title = u"%s %s" % ("mandoku-key", datetime.now().strftime("%Y-%m-%d %H:%M:%S")) if gl.addsshkey(title, key): print "200 Success: Successfully uploaded the key." else: print "400 Failed: Could not upload the key. Something went wrong. Maybe the key exists already?" ssh = os.path.join(gitpath, "ssh.exe")
#!/usr/bin/env python import json import gitlab # read config file config_file = open("test_config2.json", "r") config = json.load(config_file) config_file.close() gl = gitlab.Gitlab(config["url"], config["token"]) try: project = gl.projects.get('test-master-group/testex1') project.files.create({'file_path': 'README.md', 'branch_name': 'master', 'content': "This is a test file to publish.", 'commit_message': 'Create testfile'}) print "OK" except ValueError: print "ERROR: ", ValueError
repo.log.error("%s: raised exception: %s: %s", context.name, e, traceback.format_exc()) context.blockage += 1 finally: context_global.errors += context.errors context_global.warnings += context.warnings context_global.blockage += context.blockage # Are we working with gitlab? if args.gitlab_repo: assert args.use == "HEAD", \ "Gitlab can be only used with --use-head" gl_url = urllib.parse.urlparse(args.gitlab_repo) gitlab_baseurl = "https://" + gl_url.hostname gl = gitlab.Gitlab(gitlab_baseurl, private_token=args.gitlab_token) gl.auth() gl_project = gl.projects.get(gl_url.path[1:]) context_c.gl_commit = gl_project.commits.get(changedfile_c.gitrev_blame) changedfile_c.gl_commit = context_c.gl_commit context_c.gl_mr = gl_project.mergerequests.get(args.gitlab_mergerequest) changedfile_c.gl_mr = context_c.gl_mr print("DEBUG discussions gl_mr id %08x" % id(context_c.gl_mr)) else: gl = None gl_commit = None gl_mr = None gl_project = None cfs_all = {}
def main(): argument_spec = basic_auth_argument_spec() argument_spec.update( dict( server_url=dict(type='str', required=True, removed_in_version="2.10"), login_user=dict(type='str', no_log=True, removed_in_version="2.10"), login_password=dict(type='str', no_log=True, removed_in_version="2.10"), api_token=dict(type='str', no_log=True, aliases=["login_token"]), group=dict(type='str'), name=dict(type='str', required=True), path=dict(type='str'), description=dict(type='str'), issues_enabled=dict(type='bool', default=True), merge_requests_enabled=dict(type='bool', default=True), wiki_enabled=dict(type='bool', default=True), snippets_enabled=dict(default=True, type='bool'), visibility=dict(type='str', default="private", choices=["internal", "private", "public"], aliases=["visibility_level"]), import_url=dict(type='str'), state=dict(type='str', default="present", choices=["absent", "present"]), )) module = AnsibleModule( argument_spec=argument_spec, mutually_exclusive=[['api_url', 'server_url'], ['api_username', 'login_user'], ['api_password', 'login_password'], ['api_username', 'api_token'], ['api_password', 'api_token'], ['login_user', 'login_token'], ['login_password', 'login_token']], required_together=[ ['api_username', 'api_password'], ['login_user', 'login_password'], ], required_one_of=[[ 'api_username', 'api_token', 'login_user', 'login_token' ]], supports_check_mode=True, ) deprecation_warning(module) server_url = module.params['server_url'] login_user = module.params['login_user'] login_password = module.params['login_password'] api_url = module.params['api_url'] validate_certs = module.params['validate_certs'] api_user = module.params['api_username'] api_password = module.params['api_password'] gitlab_url = server_url if api_url is None else api_url gitlab_user = login_user if api_user is None else api_user gitlab_password = login_password if api_password is None else api_password gitlab_token = module.params['api_token'] group_identifier = module.params['group'] project_name = module.params['name'] project_path = module.params['path'] project_description = module.params['description'] issues_enabled = module.params['issues_enabled'] merge_requests_enabled = module.params['merge_requests_enabled'] wiki_enabled = module.params['wiki_enabled'] snippets_enabled = module.params['snippets_enabled'] visibility = module.params['visibility'] import_url = module.params['import_url'] state = module.params['state'] if not HAS_GITLAB_PACKAGE: module.fail_json(msg=missing_required_lib("python-gitlab"), exception=GITLAB_IMP_ERR) try: gitlab_instance = gitlab.Gitlab(url=gitlab_url, ssl_verify=validate_certs, email=gitlab_user, password=gitlab_password, private_token=gitlab_token, api_version=4) gitlab_instance.auth() except (gitlab.exceptions.GitlabAuthenticationError, gitlab.exceptions.GitlabGetError) as e: module.fail_json(msg="Failed to connect to GitLab server: %s" % to_native(e)) except (gitlab.exceptions.GitlabHttpError) as e: module.fail_json(msg="Failed to connect to GitLab server: %s. \ GitLab remove Session API now that private tokens are removed from user API endpoints since version 10.2." % to_native(e)) # Set project_path to project_name if it is empty. if project_path is None: project_path = project_name.replace(" ", "_") gitlab_project = GitLabProject(module, gitlab_instance) if group_identifier: group = findGroup(gitlab_instance, group_identifier) if group is None: module.fail_json( msg="Failed to create project: group %s doesn't exists" % group_identifier) namespace = gitlab_instance.namespaces.get(group.id) project_exists = gitlab_project.existsProject(namespace, project_path) else: user = gitlab_instance.users.list( username=gitlab_instance.user.username)[0] namespace = gitlab_instance.namespaces.get(user.id) project_exists = gitlab_project.existsProject(namespace, project_path) if state == 'absent': if project_exists: gitlab_project.deleteProject() module.exit_json(changed=True, msg="Successfully deleted project %s" % project_name) else: module.exit_json(changed=False, msg="Project deleted or does not exists") if state == 'present': if gitlab_project.createOrUpdateProject( project_name, namespace, { "path": project_path, "description": project_description, "issues_enabled": issues_enabled, "merge_requests_enabled": merge_requests_enabled, "wiki_enabled": wiki_enabled, "snippets_enabled": snippets_enabled, "visibility": visibility, "import_url": import_url }): module.exit_json( changed=True, msg="Successfully created or updated the project %s" % project_name, project=gitlab_project.projectObject._attrs) else: module.exit_json(changed=False, msg="No need to update the project %s" % project_name, project=gitlab_project.projectObject._attrs)
def nginx_gitlab(): url = 'http://172.31.37.14' token = 'Ji2yGiPs4mYWA7zU_8yL' login = gitlab.Gitlab(url, token) projects = login.projects.list(all=True) # 获取所有的projects # print(projects) name = load_dict["initialize"]["a,站点标识"] nodes = load_dict["initialize"]["nodes"] app_path_dir = '/tmp/appApi_move_' + nodes + '.conf' web_path_dir = '/tmp/webUI_' + nodes + '.conf' with open(app_path_dir, 'r', encoding='utf-8')as f1: with open('appApi' + "_" + name + '.conf', 'w', encoding='utf-8')as f3: for line in f1.readlines(): if 'server_name' in line: line = line.replace('server_name', 'server_name' + " " + load_dict["domain"]["appApi"] + ";") elif 'access_log' in line: line = line.replace('access_log', 'access_log /opt/lnmp/nginx/logs/apiApi' + "_" + name + ".log main buffer=32k flush=1m;") elif 'set $pwd' in line: line = line.replace('set $pwd', 'set $pwd "/opt/project/code/' + name + '/webUI";') elif 'alias /opt/project/code' in line: line = line.replace('alias /opt/project/code', 'alias /opt/project/code/' + name + "/webUI/appTemp") elif 'server a_1' in line: line = line.replace('server a_1', 'server' + " " + balance_a + ":" + name_ascii + " " + "max_fails=3 fail_timeout=30s;") elif 'server a_2' in line: line = line.replace('server a_2', 'server' + " " + balance_a + ":" + name_ascii_2 + " " + "max_fails=3 fail_timeout=30s;") elif 'server b_1' in line: line = line.replace('server b_1', 'server' + " " + balance_b + ":" + name_ascii + " " + "max_fails=3 fail_timeout=30s;") elif 'server b_2' in line: line = line.replace('server b_2', 'server' + " " + balance_b + ":" + name_ascii_2 + " " + "max_fails=3 fail_timeout=30s;") elif 'server c_1' in line: line = line.replace('server c_1', 'server' + " " + balance_c + ":" + name_ascii + " " + "max_fails=3 fail_timeout=30s;") elif 'server c_2' in line: line = line.replace('server c_2', 'server' + " " + balance_c + ":" + name_ascii_2 + " " + "max_fails=3 fail_timeout=30s;") f3.write(line) with open('appApi_' + name + '.conf', 'r', encoding='utf-8')as f4: appApi_conf = f4.read() with open(web_path_dir, 'r', encoding='utf-8')as f: with open('webUI' + "_" + name + '.conf', 'w', encoding='utf-8')as f5: for line in f.readlines(): # if "server_name" in line: # print(line) if 'server_name' in line: line = line.replace('server_name', 'server_name' + " " + load_dict["domain"]["webUI"] + ";") elif 'access_log' in line: line = line.replace('access_log', 'access_log /opt/lnmp/nginx/logs/webUI' + "_" + name + ".log main buffer=32k flush=1m;") elif 'access_kkk' in line: line = line.replace('access_kkk', 'access_log /opt/lnmp/nginx/logs/webUI' + "_" + name + "_" + "greate" + "_" + "10" + '.log main_greater_10 buffer=32k flush=1m;') elif 'set $pwd' in line: line = line.replace('set $pwd', 'set $pwd "/opt/project/code/' + name + '/webUI";') elif 'server a_1' in line: line = line.replace('server a_1', 'server' + " " + balance_a + ":" + name_ascii + " " + "max_fails=3 fail_timeout=30s;") elif 'server a_2' in line: line = line.replace('server a_2', 'server' + " " + balance_a + ":" + name_ascii_2 + " " + "max_fails=3 fail_timeout=30s;") elif 'server b_1' in line: line = line.replace('server b_1', 'server' + " " + balance_b + ":" + name_ascii + " " + "max_fails=3 fail_timeout=30s;") elif 'server b_2' in line: line = line.replace('server b_2', 'server' + " " + balance_b + ":" + name_ascii_2 + " " + "max_fails=3 fail_timeout=30s;") elif 'server c_1' in line: line = line.replace('server c_1', 'server' + " " + balance_c + ":" + name_ascii + " " + "max_fails=3 fail_timeout=30s;") elif 'server c_2' in line: line = line.replace('server c_2', 'server' + " " + balance_c + ":" + name_ascii_2 + " " + "max_fails=3 fail_timeout=30s;") f5.write(line) with open('webUI' + "_" + name + '.conf', 'r', encoding='utf-8')as f6: webUI_conf = f6.read() data = { 'branch': name, # v3 'commit_message': name, 'actions': [ { 'action': 'create', 'file_path': 'appApi' + "_" + name + '.conf', 'content': appApi_conf, }, { 'action': 'create', 'file_path': 'webUI' + "_" + name + '.conf', 'content': webUI_conf, } ] } branch_list = [] for p in projects: # 获取所有的project的name,id # print(p.name, p.id) if p.name == 'cloud_site_nginx_config' + "_" + load_dict["initialize"]["cloud"]: num = p.id result = login.projects.get(num) if result: for p1 in result.branches.list(): branch_list.append(p1.name) if name in branch_list: branch = result.branches.delete(name) branch = result.branches.create({'branch': name, 'ref': 'master'}) commit = result.commits.create(data) print("%s has existed...pls create node..." % name) else: branch = result.branches.create({'branch': name, 'ref': 'master'}) commit = result.commits.create(data) print("%s has created...pls create node..." % name) else: print('can not find cloud_site_nginx_config' + "_" + load_dict["initialize"]["cloud"] + "pls check...")
def gl_trailing(): return gitlab.Gitlab("http://localhost/", private_token="private_token", api_version=4)
import gitlab from urllib.parse import urlparse from nx_project.client import Client from jenkinsapi.jenkins import Jenkins proj = Client(username='******', password='') git = gitlab.Gitlab('https://gitlab.nxin.com', private_token='') jen = Jenkins('http://jenkins.p.nxin.com', username='******', password='') def get_gitlab_project(name): project = proj.get_project(name) repo = project['buildInfo']['repo'] if not repo: raise ValueError('project [{}] not config repo on proj.'.format(name)) # repo 'https://gitlab.nxin.com/yiyoutao/passport.git' parsed = urlparse(repo) if not parsed.netloc.startswith('gitlab.nxin.com'): raise ValueError( 'project [{}] repo is not a gitlab repo.'.format(name)) # remove '/' and '.git' yiyoutao/passport project_space_and_name = parsed.path[1:-4] git_project = git.projects.get(project_space_and_name) return git_project def get_git_project_master_head_commit(name): project = get_gitlab_project(name) # latest commit on master commit = project.commits.get('master')
def main(): argument_spec = basic_auth_argument_spec() argument_spec.update( dict( api_token=dict(type='str', no_log=True, aliases=["private_token", "access_token"]), state=dict(type='str', default="present", choices=["absent", "present"]), project=dict(type='str', required=True), hook_url=dict(type='str', required=True), push_events=dict(type='bool', default=True), issues_events=dict(type='bool', default=False), merge_requests_events=dict(type='bool', default=False), tag_push_events=dict(type='bool', default=False), note_events=dict(type='bool', default=False), job_events=dict(type='bool', default=False), pipeline_events=dict(type='bool', default=False), wiki_page_events=dict(type='bool', default=False), hook_validate_certs=dict(type='bool', default=False, aliases=['enable_ssl_verification']), token=dict(type='str', no_log=True), )) module = AnsibleModule( argument_spec=argument_spec, mutually_exclusive=[['api_username', 'api_token'], ['api_password', 'api_token']], required_together=[['api_username', 'api_password']], required_one_of=[['api_username', 'api_token']], supports_check_mode=True, ) deprecation_warning(module) gitlab_url = re.sub('/api.*', '', module.params['api_url']) validate_certs = module.params['validate_certs'] gitlab_user = module.params['api_username'] gitlab_password = module.params['api_password'] gitlab_token = module.params['api_token'] state = module.params['state'] project_identifier = module.params['project'] hook_url = module.params['hook_url'] push_events = module.params['push_events'] issues_events = module.params['issues_events'] merge_requests_events = module.params['merge_requests_events'] tag_push_events = module.params['tag_push_events'] note_events = module.params['note_events'] job_events = module.params['job_events'] pipeline_events = module.params['pipeline_events'] wiki_page_events = module.params['wiki_page_events'] enable_ssl_verification = module.params['hook_validate_certs'] hook_token = module.params['token'] if not HAS_GITLAB_PACKAGE: module.fail_json(msg=missing_required_lib("python-gitlab"), exception=GITLAB_IMP_ERR) try: gitlab_instance = gitlab.Gitlab(url=gitlab_url, ssl_verify=validate_certs, email=gitlab_user, password=gitlab_password, private_token=gitlab_token, api_version=4) gitlab_instance.auth() except (gitlab.exceptions.GitlabAuthenticationError, gitlab.exceptions.GitlabGetError) as e: module.fail_json(msg="Failed to connect to GitLab server: %s" % to_native(e)) except (gitlab.exceptions.GitlabHttpError) as e: module.fail_json(msg="Failed to connect to GitLab server: %s. \ GitLab remove Session API now that private tokens are removed from user API endpoints since version 10.2." % to_native(e)) gitlab_hook = GitLabHook(module, gitlab_instance) project = findProject(gitlab_instance, project_identifier) if project is None: module.fail_json( msg="Failed to create hook: project %s doesn't exists" % project_identifier) hook_exists = gitlab_hook.existsHook(project, hook_url) if state == 'absent': if hook_exists: gitlab_hook.deleteHook() module.exit_json(changed=True, msg="Successfully deleted hook %s" % hook_url) else: module.exit_json(changed=False, msg="Hook deleted or does not exists") if state == 'present': if gitlab_hook.createOrUpdateHook( project, hook_url, { "push_events": push_events, "issues_events": issues_events, "merge_requests_events": merge_requests_events, "tag_push_events": tag_push_events, "note_events": note_events, "job_events": job_events, "pipeline_events": pipeline_events, "wiki_page_events": wiki_page_events, "enable_ssl_verification": enable_ssl_verification, "token": hook_token }): module.exit_json( changed=True, msg="Successfully created or updated the hook %s" % hook_url, hook=gitlab_hook.hookObject._attrs) else: module.exit_json(changed=False, msg="No need to update the hook %s" % hook_url, hook=gitlab_hook.hookObject._attrs)
#[Getting started with the API — python-gitlab 2.4.0 documentation](https://python-gitlab.readthedocs.io/en/stable/api-usage.html#gitlab-gitlab-class) import gitlab import json import os private_token = os.environ['private_token'] gl = gitlab.Gitlab('https://gitlab.com', private_token) ''' # Get a project by ID project_id = 851 project = gl.projects.get(project_id) [Projects — python-gitlab 2.4.0 documentation](https://python-gitlab.readthedocs.io/en/stable/gl_objects/projects.html) ''' project_id = 16345223 project = gl.projects.get(project_id) ''' ‘By default GitLab does not return the complete list of items. Use the all parameter to get all the items when using listing methods - [Getting started with the API — python-gitlab 2.4.0 documentation](https://python-gitlab.readthedocs.io/en/stable/api-usage.html#pagination) ''' branches = project.branches.list(all=True) #print(len(branches)) (output:36 ) ''' [Issues — python-gitlab 2.4.0 documentation](https://python-gitlab.readthedocs.io/en/stable/gl_objects/issues.html#project-issues) ''' issues = project.issues.list(all=True) #print('The number of Tasks respository issues:', + len(issues)) #output: The number of Tasks respository issues: 144