Example #1
0
 def _github_client(self):
     if hasattr(settings, "GITHUB_ACCOUNT") and hasattr(
             settings, "GITHUB_KEY"):
         github = Github(username=settings.GITHUB_ACCOUNT,
                         api_token=settings.GITHUB_KEY)
     else:
         github = Github()
     return github
    def handle(self, *args, **options):

        print >> stdout, "Commencing package updating now at %s " % strftime(
            "%a, %d %b %Y %H:%M:%S +0000", gmtime())

        # set up various useful bits
        github_repo = Repo.objects.get(title__icontains="github")
        bitbucket_repo = Repo.objects.get(title__icontains="bitbucket")
        launchpad_repo = Repo.objects.get(title__icontains="launchpad")

        # instantiate the github connection
        if hasattr(settings, "GITHUB_ACCOUNT") and hasattr(
                settings, "GITHUB_KEY"):
            github = Github(username=settings.GITHUB_ACCOUNT,
                            api_token=settings.GITHUB_KEY)
            authed = True
        else:
            github = Github()
            authed = False

        for index, package in enumerate(Package.objects.all()):
            zzz = 5

            try:
                if package.repo == github_repo:
                    # Do github
                    try:
                        package.fetch_metadata()
                    except socket_error, e:
                        print >> stdout, "For '%s', threw a socket.error: %s" % (
                            package.title, e)
                        continue
                    for commit in github.commits.list(package.repo_name(),
                                                      "master"):
                        commit, created = Commit.objects.get_or_create(
                            package=package, commit_date=commit.committed_date)
                    zzz += 1
                elif package.repo == bitbucket_repo:
                    zzz = 1
                    # do bitbucket
                    try:
                        package.fetch_metadata()
                    except socket_error, e:
                        print >> stdout, "For '%s', threw a socket.error: %s" % (
                            package.title, e)
                        continue
                    for commit in get_bitbucket_commits(package):
                        commit, created = Commit.objects.get_or_create(
                            package=package, commit_date=commit["timestamp"])
Example #3
0
def get_repos(user='******'):
    print("Executing task id %r, args: %r kwargs: %r" %
          (add.request.id, add.request.args, add.request.kwargs))
    time.sleep(5)
    github = Github()
    repos = github.repos.list(user)
    return repos
 def __init__(self, username=None, api_user=None, api_token=None):
     self.api_user = api_user or self.git_config_get("github.user")
     self.api_token = api_token or self.git_config_get("github.token")
     self.username = username or self.api_user
     print("U:(%s) T:(%s) F:(%s)" % (self.api_user, self.api_token,
         self.username))
     self.client = Github(self.api_user, self.api_token)
Example #5
0
 def create_remote(self, name, description='', homepage=''):
     from github2.client import Github
     github = Github(username=settings.GITHUB_USER,
                     api_token=settings.GITHUB_API_TOKEN)
     new_repo = github.repo.create(name, description, homepage, public=True)
     commit_url = new_repo.url.replace('http://', 'git@') + '.git'
     return commit_url
Example #6
0
    def test_delays(self, sleep):
        """Test calls in quick succession are delayed."""
        client = Github(requests_per_second=.5)
        client.users.show('defunkt')
        client.users.show('mojombo')

        # 0.5 requests per second, means a two second delay
        sleep.assert_called_once_with(2.0)
def test_issue_50():
    """Erroneous init of ``Http`` with proxy setup.

    See https://github.com/ask/python-github2/pull/50
    """
    client = Github(proxy_host="my.proxy.com", proxy_port=9000)
    proxy_info = client.request._http.proxy_info
    eq_(type(proxy_info), httplib2.ProxyInfo)
    eq_(proxy_info.proxy_host, 'my.proxy.com')
    eq_(proxy_info.proxy_port, 9000)
Example #8
0
def main():
  github = Github() #create the object
  all_repos = github.repos.list(USERNAME)
  for repo in all_repos:
    if repo.fork == False:
      issues = github.issues.list(USERNAME+"/"+repo.name, state="open")
      if len(issues)<>0:
        for issue in issues:
          print repo.name
          print "       #"+str(issue.number)+" | "+issue.title
  print "---------------------done---------------------"
Example #9
0
    def setUp(self):
        """Prepare test fixtures.

        :see: :class:`HttpMockTestCase`

        :attr:`client` is an authenticated :obj:`Github` object for easy use
        in tests.

        """
        httplib2.Http.request = Mock(spec_set=httplib2.Http.request,
                                     side_effect=request_mock)
        self.client = Github(access_token='xxx')
Example #10
0
def pull(package):

    if hasattr(settings, "GITHUB_ACCOUNT") and hasattr(settings, "GITHUB_KEY"):
        github = Github(username=settings.GITHUB_ACCOUNT,
                        api_token=settings.GITHUB_KEY)
    else:
        github = Github()

    repo_name = package.repo_name()
    repo = github.repos.show(repo_name)
    package.repo_watchers = repo.watchers
    package.repo_forks = repo.forks
    package.repo_description = repo.description

    collaborators = github.repos.list_collaborators(repo_name) + [
        x['login'] for x in github.repos.list_contributors(repo_name)
    ]
    if collaborators:
        package.participants = ','.join(uniquer(collaborators))

    return package
Example #11
0
 def __init__(self, username=None, api_user=None, api_token=None):
     """
     A recommendation engine which polls the Github API
     to find users with similar interests in repositories.
     """
     self.api_user = api_user or \
         self.git_config_get('github.user') or \
         self.git_config_get('user.name')
     self.api_token = api_token or self.git_config_get('github.token')
     self.username = username or self.api_user
     self.client = Github(self.api_user, self.api_token)
     self.sleep_interval = 65
Example #12
0
    def setUp(self):
        """Prepare test fixtures.

        `httplib2.Http` is patched to return cached entries via
        :class:`HttpMock`.

        :attr:`client` is an unauthenticated :obj:`Github` object for easy use
        in tests.

        """
        httplib2.Http.request = Mock(spec_set=httplib2.Http.request,
                                     side_effect=request_mock)
        self.client = Github()
Example #13
0
    def publish(self, args):
        """Publish repos to github"""

        #import logging
        #logging.basicConfig(level=logging.DEBUG)

        gh = Github(username=args.username, api_token=args.api_token)

        config = Config(args.projects_file)
        for repo in config.get_repos():
            if args.repos is None or \
               repo.name in args.repos.split(','):
                repo.publish(gh)
Example #14
0
 def test_delays(self):
     import datetime
     USERNAME = ''
     API_KEY = ''
     client = Github(username=USERNAME,
                     api_token=API_KEY,
                     requests_per_second=.5)
     client.users.show('defunkt')
     start = datetime.datetime.now()
     client.users.show('mojombo')
     end = datetime.datetime.now()
     self.assertGreaterEqual(
         (end - start).total_seconds(), 2.0,
         "Expected .5 reqs per second to require a 2 second delay between "
         "calls.")
def post_to_github(issue, sync_comments=True):
    logging.info('should post %s', issue)
    github = Github(username=options.github_user_name,
                    api_token=options.github_api_token,
                    requests_per_second=1)
    if issue.status.lower(
    ) in "invalid closed fixed wontfix verified worksforme duplicate done".lower(
    ):
        issue.status = 'closed'
    else:
        issue.status = 'open'
    try:
        git_issue = github.issues.show(options.github_project, int(issue.id))
        logging.warn("skipping issue : %s" % (issue))
    except RuntimeError:
        title = "%s" % issue.summary
        logging.info('will post issue:%s' % issue)
        logging.info("issue did not exist")
        git_issue = github.issues.open(options.github_project,
                                       title=title,
                                       body=issue.body)
    if issue.status == 'closed':
        github.issues.close(options.github_project, git_issue.number)
    if sync_comments is False:
        return git_issue
    old_comments = github.issues.comments(options.github_project,
                                          git_issue.number)
    for i, comment in enumerate(issue.comments):

        exists = False
        for old_c in old_comments:
            # issue status changes have empty bodies in google code , exclude those:
            if bool(old_c.body) or old_c.body == comment.body:
                exists = True
                logging.info("Found comment there, skipping")
                break
        if not exists:
            #logging.info('posting comment %s', comment.body.encode('utf-8'))
            try:
                github.issues.comment(options.github_project, git_issue.number,
                                      comment)
            except:
                logging.exception("Failed to post comment %s for issue %s" %
                                  (i, issue))

    return git_issue
Example #16
0
    def test_delays(self):
        """Test call delay is at least one second"""
        import datetime
        USERNAME = ''
        API_KEY = ''
        client = Github(username=USERNAME, api_token=API_KEY,
            requests_per_second=.5)
        client.users.show('defunkt')
        start = datetime.datetime.now()
        client.users.show('mojombo')
        end = datetime.datetime.now()

        delta = end - start
        delta_seconds = delta.days * 24 * 60 * 60 + delta.seconds

        self.assertTrue(delta_seconds >= 2,
            "Expected .5 reqs per second to require a 2 second delay between "
            "calls.")
Example #17
0
def _gen_release_notes(version):
    def _convert(i):
        return { \
                'number': i.number, \
                'html_url': i.html_url, \
                'title': i.title, \
            }

    github = Github(_keychain_get_username(gitub_keychain_item),
                    _keychain_get_password(gitub_keychain_item))

    issues = github.issues.list('fikovnik/ShiftIt', state='closed')
    issues = [e for e in issues if 'v' + version in e.labels]
    issues.sort(key=lambda i: i.closed_at)

    return pystache.render(release_notes_template, \
                           version=version, \
                           issues=[_convert(e) for e in issues])
Example #18
0
def run_csv():
    """
    Export github issues into a csv format
    """
    output_csv = csv.writer(open(csv_name, 'wb'), delimiter=',')
    github = Github(username=git_username, api_token=git_api_token)

    # csv headers
    headers = [
        'id',
        'title',
        'body',
        'state',
        'creator',
        'labels',
        'created_at',
        'updated_at',
        'closed_at',
    ]

    # write header rows
    output_csv.writerow(headers)

    # get the git issues and write the rows to the csv
    git_issues = github.issues.list(git_repo)
    for git_issue in git_issues:
        print git_issue.title
        labels = ' '.join(git_issue.labels)

        # alot of these are blank because they are not really
        # needed but if you need them just fill them out
        issue = [
            git_issue.number,
            git_issue.title.encode('utf8'),
            git_issue.body.encode('utf8'),
            git_issue.state,
            git_issue.user,
            labels,
            git_issue.created_at,
            git_issue.updated_at,
            git_issue.closed_at,
        ]
        output_csv.writerow(issue)
Example #19
0
def update_github():
    logging.info("updating github")
    github = Github(username=settings.GITHUB_USER,
                    api_token=settings.GITHUB_API_KEY)
    commits = []
    recent_commits = []
    try:
        for r in github.repos.list(for_user=settings.GITHUB_USER):
            new_commits = github.commits.list(
                "%s/%s" % (settings.GITHUB_USER, r.name), 'master')
            for c in new_commits:
                c.repo = r
            commits.extend(new_commits)
        all_commits = sorted(commits, key=lambda c: c.committed_date)
        commits = reversed(all_commits[-settings.NUM_GITHUB_COMMITS:])
        for c in commits:
            minute = str(c.committed_date.minute)
            if len(minute) < 2:
                minute = "0" + minute
            recent_commits.append({
                'repo_name': c.repo.name,
                'repo_url': c.repo.url,
                'time': {
                    'day': c.committed_date.day,
                    'month': c.committed_date.month,
                    'year': c.committed_date.year,
                    'hour': c.committed_date.hour,
                    'minute': minute
                },
                'message': c.message,
                'url': c.url
            })

        delete_all(RecentCommits)
        RecentCommits(commitsJson=encode_commits(recent_commits)).put()
    except DownloadError:
        # Some query to Github took too long
        logging.info("unable to download from github")

    return recent_commits
Example #20
0
def init_github(settings):
    api_user = settings.get('github.api.user')
    api_token = settings.get('github.api.token')
    username = settings.get('github.username') or api_user
    return Github(api_user, api_token, requests_per_second=1)
Example #21
0
 def test_default_host(self):
     client = Github()
     eq_(client.request.github_url, 'https://github.com')
Example #22
0
 def get(self):
     logging.info('generating ranking...')
     network = Github().repos.network('icefox/git-achievements')
     users = [repo['owner'] for repo in network]
     for user in users:
         create_record_for(user)
Example #23
0
def test_project_for_user_repo():
    client = Github()
    eq_(client.project_for_user_repo('JNRowe', 'misc-overlay'),
        'JNRowe/misc-overlay')
Example #24
0
    (opts, args) = parser.parse_args()

    if opts.test:
        import doctest
        doctest.testmod()

    if len(args) > 0:
        gitorious_master_path = args[0]
        submodules = list(
            collect_submodules(
                open(os.path.join(gitorious_master_path,
                                  '.gitmodules'))).items())
        # For whatever reason, this makes them come out alphabetically in GitHub's index
        submodules.sort(reverse=True)

        if opts.token and opts.user:
            gh = Github(username=opts.user, api_token=opts.token)
        else:
            gh = Github()

        for x, attrs in submodules.items():
            url_path = urlparse.urlparse(attrs['url'])[2]
            name = rdrop(url_path.split('/')[-1], '.git')
            print '**', name
            new_path = 'boost-cpp/' + name
            new_repo = gh.repos.create(name, 'Boost.org %s module' % name,
                                       'http://boost.org/libs/' + name)
            print new_repo
            mirror_repo(attrs['url'], '[email protected]:' + new_path + '.git')
Example #25
0
 def test_non_standard_host(self):
     client = Github(github_url="http://your-github-enterprise-url.com/")
     eq_(client.request.github_url,
         'http://your-github-enterprise-url.com/')
Example #26
0
 def __init__(self, *args, **kwargs):
     super(GithubContributionBackend, self).__init__(*args, **kwargs)
     self.gh = Github(username=GITHUB_USERNAME, api_token=GITHUB_TOKEN)
Example #27
0
items = pn_client.items.list(pn_project.project_id, detail="full")
print "%d items are ready to be exported from project %s\n" % (len(items),
                                                               pn_project.name)

# list all items
#for item in items:
#    print "    %s (ID: %d)" % (item.subject, item.item_id)

#####################
#                   #
#  get github bits  #
#                   #
#####################

gh_client = Github(username=settings.github_user,
                   api_token=settings.github_token)
gh_repos = gh_client.repos.list(settings.github_user)

for i, p in enumerate(gh_repos):
    print "    [%d] %s" % (i + 1, p.name)

while True:
    #get the repo you want to import to:
    gh_repo_id = raw_input(
        "\nPlease select the project you want to import to: ")

    #make sure input is valid
    try:
        gh_repo_id = int(gh_repo_id)
        gh_repo_id_test = gh_repo_id - 1
    except ValueError:
Example #28
0
def increment_api_call():
    global github_api_count
    github_api_count += 1

    if github_api_count > 40:
        global start_date
        sleep_time = (
            (start_date + timedelta(minutes=1)) - datetime.now()).seconds + 5
        print "Waiting for", sleep_time
        time.sleep(sleep_time)
        start_date = datetime.now()
        github_api_count = 0


# Login in to github and create account object
github = Github(api_token=options.github_api_token,
                username=options.github_username)
start = 0
issue_counts = 0
while True:
    url = "https://api.bitbucket.org/1.0/repositories/%s/%s/issues/?start=%d" % (
        options.bitbucket_username, options.bitbucket_repo, start)
    response = urllib2.urlopen(url)
    result = json.loads(response.read())
    if not result['issues']:
        # Check to see if there is issues to process if not break out.
        break

    for issue in result['issues']:
        comments = scrape_comments(issue)
        if options.dry_run:
            print "Title:", issue.get('title')
Example #29
0
import pickle
import pprint
import time
from github2.client import Github

github = Github()

f = open('data.txt', 'r')
pickles = open('pickled.pic', 'w')
repdataset = {}
for line in f:
    try:
        repo = line.split('https://github.com/')[1].strip()
        print "repo <%s>" % repo
        repodata = dict(github.repos.show(str(repo)))
        repdataset[repo] = repodata
        time.sleep(2)
    except (IndexError, RuntimeError), ex:
        print ex
        time.sleep(2)

f.close()
pickle.dump(repdataset, pickles)
pickles.close()

pp = pprint.PrettyPrinter(indent=4)
pp.pprint(repdataset)
def test_project_for_user_repo():
    client = Github()
    assert_equals(client.project_for_user_repo('JNRowe', 'misc-overlay'),
                  'JNRowe/misc-overlay')
Example #31
0
def run():

    # cli flags
    upstream_on = args.flags.contains('--upstream')
    only_type = args.grouped.get('--only', False)
    organization = args[0]

    os.chdir(GHSYNC_DIR)

    # API Object
    github = Github(username=GITHUB_USER, api_token=GITHUB_TOKEN)

    # repo slots
    repos = {}

    if not organization:
        repos['watched'] = [r for r in github.repos.watching(GITHUB_USER)]
    repos['private'] = []
    repos['mirrors'] = []
    repos['public'] = []
    repos['forks'] = []

    # Collect GitHub repos via API
    for repo in github.repos.list(organization):

        if repo.private:
            repos['private'].append(repo)
        elif repo.fork:
            repos['forks'].append(repo)
        elif 'mirror' in repo.description.lower():
            # mirrors owned by self if mirror in description...
            repos['mirrors'].append(repo)
        else:
            repos['public'].append(repo)

    for org, repos in repos.iteritems():
        for repo in repos:

            # create org directory (safely)
            try:
                os.makedirs(org)
            except OSError:
                pass

            # enter org dir
            os.chdir(org)

            # I own the repo
            is_private = (org in ('private', 'forks', 'mirror', 'public'))
            is_fork = (org == 'forks')

            if is_fork:
                _url = 'http://github.com/api/v2/json/repos/show/{repo.owner}/{repo.name}'.format(
                    repo=repo)
                repo.parent = json.loads(requests.get(
                    _url, ).content)['repository'].get('parent')

            if not only_type or (org in only_type):

                # just `git pull` if it's already there
                if os.path.exists(repo.name):

                    os.chdir(repo.name)
                    puts(
                        colored.red(
                            'Updating repo: {repo.name}'.format(repo=repo)))
                    os.system('git pull')

                    if is_fork and upstream_on:
                        print repo.__dict__
                        puts(
                            colored.red(
                                'Adding upstream: {repo.parent}'.format(
                                    repo=repo)))
                        os.system(
                            'git remote add upstream [email protected]:{repo.parent}.git'
                            .format(repo=repo))

                    os.chdir('..')

                else:
                    if is_private:
                        puts(
                            colored.red(
                                'Cloning private repo: {repo.name}'.format(
                                    repo=repo)))
                        os.system(
                            'git clone [email protected]:{repo.owner}/{repo.name}.git'
                            .format(repo=repo))
                        print('git clone [email protected]:%s/%s.git' %
                              (repo.owner, repo.name))

                        if is_fork and upstream_on:
                            os.chdir(repo.name)
                            puts(
                                colored.red(
                                    'Adding upstream: {repo.parent}'.format(
                                        repo=repo)))
                            os.system(
                                'git remote add upstream [email protected]:{repo.parent}.git'
                                .format(repo=repo))
                            os.chdir('..')

                    else:
                        puts(
                            colored.red(
                                'Cloning repo: {repo.name}'.format(repo=repo)))
                        os.system('git clone git://github.com/%s/%s.git' %
                                  (repo.owner, repo.name))
                        print('git clone git://github.com/%s/%s.git' %
                              (repo.owner, repo.name))

            # return to base
            os.chdir('..')