Esempio n. 1
0
def find_bitbucket_repository(user, password, email):
    bitbucket = Client(
        BasicAuthenticator(user, password, email)
    )

    try:
        print("Finding run project in bitbucket...")
        repository = Repository.find_repository_by_name_and_owner(
            repository_name="run",
            client=bitbucket
        )
    except HTTPError:
        print("Project not found")
        print("Creating project run...")

        repository = Repository.create(
            payload=RepositoryPayload({
                "name": "run",
                "is_private": True,
                "fork_policy": RepositoryForkPolicy.NO_FORKS,
            }),
            client=bitbucket
        )

    for link in repository.links["clone"]:
        if link["name"] == "https":
            return link["href"]
Esempio n. 2
0
    def get_repos_for_organization(self, name):
        repos = list(Repository.find_repositories_by_owner_and_role(name, 'member', self.bb))

        if not len(repos) or not isinstance(repos [0], Repository):
            return []

        return [r.full_name for r in repos]
Esempio n. 3
0
 def test_response_is_a_repository_generator(self):
     httpretty.register_uri(httpretty.GET,
                            self.url,
                            content_type='application/json',
                            body=self.resource_list_data(),
                            status=200)
     response = Repository.find_public_repositories(client=self.test_client)
     assert isinstance(next(response), Repository)
Esempio n. 4
0
 def test_response_is_a_repository(self):
     httpretty.register_uri(httpretty.GET,
                            self.url,
                            content_type='application/json',
                            body=self.resource_data(),
                            status=200)
     response = Repository.find_repository_by_name_and_owner(
         repository_name=self.name, client=self.test_client)
     assert isinstance(response, Repository)
Esempio n. 5
0
 def test_response_from_role_only_is_a_repository_generator(self):
     httpretty.register_uri(httpretty.GET,
                            self.url,
                            content_type='application/json',
                            body=self.resource_list_data(),
                            status=200)
     response = Repository.find_repositories_by_owner_and_role(
         role=RepositoryRole.MEMBER, client=self.test_client)
     assert isinstance(next(response), Repository)
Esempio n. 6
0
 def setup_class(cls):
     cls.owner, cls.name = \
         cls.destination_repository_full_name.split('/', 1)
     cls.with_both = cls.builder \
         .add_destination_repository_owner(cls.owner) \
         .add_destination_repository_name(cls.name)
     cls.repository = Repository(
         json.loads(cls.resource_data('Repository')),
         client=cls.test_client)
     cls.with_repository = cls.builder \
         .add_destination_repository(cls.repository)
Esempio n. 7
0
 def test_response_is_a_repository(self):
     httpretty.register_uri(httpretty.POST,
                            self.url,
                            content_type='application/json',
                            body=self.resource_data(),
                            status=200)
     payload = RepositoryPayload() \
         .add_name(self.name) \
         .add_fork_policy(self.fork_policy) \
         .add_is_private(self.is_private)
     response = Repository.fork(payload, client=self.test_client)
     assert 'application/x-www-form-urlencoded' == \
         httpretty.last_request().headers.get('Content-Type')
     assert isinstance(response, Repository)
Esempio n. 8
0
    def __init__(self,
                 package_name,
                 owner_name,
                 repo_name,
                 client,
                 strip_v=True):
        """
        Arguments
        ---------
        repository_name: str
            of the form username/repo
        """
        # XXX test repository resolution
        Repository.find_repository_by_name_and_owner(repo_name,
                                                     owner_name,
                                                     client=client)

        self.name = package_name
        self.owner_name = owner_name
        self.repo_name = repo_name

        self.client = client

        self.do_strip_v = strip_v
Esempio n. 9
0
 def create(self, user, repo, add=False):
     try:
         repo = Repository.create(
                 RepositoryPayload(dict(
                     fork_policy=RepositoryForkPolicy.ALLOW_FORKS,
                     is_private=False
                 )),
                 repository_name=repo,
                 owner=user,
                 client=self.bb.client
         )
         if add:
             self.add(user=user, repo=repo.name, tracking=self.name)
     except HTTPError as err:
         if '400' in err.args[0].split(' '):
             raise ResourceExistsError('Project {} already exists on this account.'.format(repo)) from err
         raise ResourceError("Couldn't complete creation: {}".format(err)) from err
Esempio n. 10
0
 def create(self, user, repo, add=False):
     try:
         repo = Repository.create(
                 RepositoryPayload(dict(
                     fork_policy=RepositoryForkPolicy.ALLOW_FORKS,
                     is_private=False
                 )),
                 repository_name=repo,
                 owner=user,
                 client=self.bb.client
         )
         if add:
             self.add(user=user, repo=repo.name, tracking=self.name)
     except HTTPError as err:
         if '400' in err.args[0].split(' '):
             raise ResourceExistsError('Project {} already exists on this account.'.format(repo)) from err
         raise ResourceError("Couldn't complete creation: {}".format(err)) from err
Esempio n. 11
0
def clone_repos(client: Client, path: Path):
    repos = Repository.find_repositories_by_owner_and_role(client=client,
                                                           role="member")

    path.mkdir(parents=True, exist_ok=True)
    with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
        future_to_repo = {
            executor.submit(clone_repo, path, repo.clone['https']): repo
            for repo in repos
        }

        for future in concurrent.futures.as_completed(future_to_repo):
            repo: str = future_to_repo[future]
            try:
                data = future.result()
                print(f'Cloned: {repo.name} ')
            except Exception as exc:
                if exc.status == 128:
                    print(f'{repo.name} already exists')
            else:
                print(data)
Esempio n. 12
0
def process_repository(repository, url):
    table_name = "bitbucket_repository"
    user_name = get_config("bitbucket.user_name")
    user_name, repo_name = repository.split("/")
    repository = Repository.find_repository_by_name_and_owner(
        repository_name=repo_name, owner=user_name, client=bitbucket_client)
    logger.debug("Bitbucket repository - {}".format(repository))
    repository_info = vars(get_repository_info(repository.data))
    logger.debug("BitBucket repository owner - {}".format(
        repository_info['owner_uuid']))
    process_user(repository_info['owner_uuid'], url)
    print(f'https://{url}/{table_name}')
    r = requests.post(
        f'https://{url}/{table_name}',
        json={
            "repository": json.dumps(repository_info,
                                     sort_keys=True,
                                     default=str)
        },
        headers={"AUTH_TOKEN": get_config("azimu_api.auth_token")})
    if r.status_code != 200:
        raise Exception(r.status_code)
    return r.json()
Esempio n. 13
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--clone_dir', default='/tmp')
    parser.add_argument('--authfile', default='credentials.json')
    args = parser.parse_args()

    with open(args.authfile, 'r') as f:
        creds = json.load(f)

    # log into bitbucket and github
    github = Github(creds['github']['auth_token'])
    github_user = github.get_user()

    # verify github credentials by running a dummy command
    try:
        data = [(s.name, s.name) for s in github.get_user().get_repos()]
    except:
        print('Invalid GitHub token!')
        exit(1)

    bitbucket = Client(
        BasicAuthenticator(creds['bitbucket']['user'],
                           creds['bitbucket']['auth_token'],
                           creds['bitbucket']['mail']))

    # update the git command shell environment with SSH agent stuff
    global_git = git.Git()
    global_git.update_environment(
        **{k: os.environ[k]
           for k in os.environ if k.startswith('SSH')})

    # list bitbucket repos
    repos = list()
    failed = list()
    for repo in Repository.find_repositories_by_owner_and_role(
            role='owner', client=bitbucket):
        item = {
            'name': repo.name,
            'description': repo.description,
            'private': repo.is_private,
            'link': repo.links['clone'][1]['href'],
            'copy': False
        }
        if repo.scm != 'git':
            print(
                'Warning: repository {} will be ignored as it does not use git as SCM'
                .format(repo.name))
            item['reason'] = 'Non-git SCM: {}'.format(repo.scm)
            failed.append(item)
            continue
        repos.append(item)

    # ask the user which repos to copy
    proceed = False
    while not proceed:
        print('BitBucket Repositories to copy:')
        print(tabulate(repos, headers="keys"))
        print(
            'Type a name of a repo to toggle it, "all" to toggle all or "go" to proceed with the current selection'
        )

        while True:
            choice = raw_input('Choice [go|all]: ')
            if choice == 'go':
                proceed = True
                break
            elif choice == 'all':
                for r in repos:
                    r['copy'] = not r['copy']
                break

            item = next((item for item in repos if choice == item["name"]),
                        None)
            if item is not None:
                item['copy'] = not item['copy']
                break
            print('{} not found!'.format(choice))

    # fliter repos with copy=False
    copy_repos = [it for it in repos if it['copy']]

    print('Final list to copy:')
    print(tabulate(copy_repos, headers="keys"))

    # do the copying
    for repo in copy_repos:
        print('[{}]'.format(repo['name']))

        # fetch or clone the bitbucket repository
        destdir = os.path.join(args.clone_dir, repo['name'])
        if os.path.exists(destdir):
            local_repo = git.Repo(destdir)
            print('{} exists and is a valid repo, fetching updates'.format(
                destdir))
            local_repo.remotes.origin.fetch()
        else:
            print('-- Cloning {}'.format(repo['link']))
            try:
                local_repo = git.Repo.clone_from(repo['link'], destdir)
            except:
                print('Clone failed')
                repo['reason'] = 'Clone failed'
                failed.append(repo)
                continue

        # try to create the repo on github
        try:
            print('-- Creating a GitHub repo')
            github_user.create_repo(repo['name'],
                                    description=repo['description'],
                                    private=repo['private'])
        except GithubException as e:
            print(e.data['message'])
            repo['reason'] = e.data['message']
            failed.append(repo)
            continue
        github_repo = github_user.get_repo(repo['name'])

        # push to github repo
        print('-- Pushing')
        try:
            if not 'github' in local_repo.remotes:
                remote = local_repo.create_remote('github',
                                                  github_repo.ssh_url)
            else:
                remote = local_repo.remotes.github
            remote.push()
        except git.exc.GitCommandError as e:
            print('Failed to push')
            print(e)
            repo['reason'] = 'Push failed'
            failed.append(repo)
            continue

    if len(failed) > 0:
        print('Migration failed for: ')
        print(tabulate(failed, headers="keys"))
Esempio n. 14
0
        '',  # Username
        '',  # Password/API Key
        '',  # E-mail
    ))


def get_user_for_activity(activity):
    for value in activity.values():
        if 'user' in value:
            return value['user']
        elif 'author' in value:
            return value['author']


repositories = [
    repo.slug for repo in Repository.find_repositories_by_owner_and_role(
        role='owner', client=bitbucket)
]

for repo in repositories:
    print("------- {repo} -------".format(repo=repo))
    for pr in PullRequest.find_pullrequests_for_repository_by_state(
            repo, client=bitbucket):
        print("...")
        if type(pr) == dict:
            continue
        activity = list(pr.activity())

        # Get approvals
        approvals = filter(lambda a: 'approval' in a, activity)
        print("Approvers:", [(a['approval']['user']['display_name'],
                              a['approval']['user']['links']['avatar'])
def main():

  # secrets from ENV
  parser = ArgumentParser(
        'web_hook_me_up.py',
        description="Tool to copy add a WebHook to all of your bitbucket repos."
  )

  parser.add_argument(
        '-username', required=True,
        action=EnvDefault, envvar='BITBUCKET_USERNAME',
        help='Bitbucket username (or BITBUCKET_USERNAME environment variable)'
  )

  parser.add_argument(
        '-password', required=True,
        action=EnvDefault, envvar='BITBUCKET_PASSWORD',
        help='Bitbucket password (or BITBUCKET_PASSWORD environment variable)'
  )

  parser.add_argument(
        '-email', required=True,
        action=EnvDefault, envvar='BITBUCKET_EMAIL',
        help='Bitbucket email (or BITBUCKET_EMAIL environment variable)'
  )

  opts = parser.parse_args()

  bitbucket = Client(
            BasicAuthenticator( opts.username, opts.password, opts.email))

  # things to change
  description='Hook for slack'
  url="https://hooks.slack.com/services/XXXXXXXXXXXXXX"
  owner="XXXXXXX"
  events=['issue:created','issue:updated', 'pullrequest:created', 'pullrequest:approved']

  repositories = Repository.find_repositoris_by_owner_and_role(client=bitbucket, role=RepositoryRole.ADMIN, owner=owner)

  for repo in repositories:
    print (repo.name)

    allhooks = Hook.find_hooks_in_repository(
      owner=owner,
      repository_name=repo.name.lower(),
      client=bitbucket
      )

    for hook in allhooks:
      if 'description' in hook and hook.description == description:
        print("existing hook deleted!")
        hook.delete()

    # print(allhooks)
    Hook.create_hook(
        repository_name=repo.name.lower(),
        description=description,
        callback_url=url,
        active=events,
        username=owner,
        client=bitbucket
    )
Esempio n. 16
0
  teamname = username
if not password:
  password = getpass(prompt='Enter your bitbucket password: '******'Username and password must be provided.')
  exit(0)

bitbucket = Client(
    BasicAuthenticator(
        username,
        password,
        ''
    )
)

for repo in Repository.find_repositories_by_owner_and_role(owner=teamname, role=RepositoryRole.MEMBER.value, client=bitbucket):
  if repo.scm != RepositoryType.HG.value:
    continue
  if repo.name.startswith(hgprefix):
    continue

  hg_clone_https = ''
  for clone in repo.links['clone']:
    if clone['name'] == 'https':
      hg_clone_https = clone['href']
      break
  hg_clone_https = hg_clone_https.replace('@', ':%s@' % password)

  print('\n\n================================================================================================')
  print('Cloning remote hg repo: %s' % repo.name)
  hg_clone_dir = 'hg-repos/%s' % repo.slug
Esempio n. 17
0
 def test_passes_the_type_check(self):
     assert Repository.is_type(self.data)
Esempio n. 18
0
 def example_object(cls):
     return Repository(json.loads(cls.resource_data()),
                       client=cls.test_client)