Example #1
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]
Example #2
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)
Example #3
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)
Example #4
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
        '',  # 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'])
Example #6
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"))