Exemplo n.º 1
0
Arquivo: views.py Projeto: tima/galaxy
    def post(self, request, *args, **kwargs):
        github_user = request.data.get('github_user', None)
        github_repo = request.data.get('github_repo', None)

        if not github_user or not github_repo:
            raise ValidationError(
                dict(detail=
                     "Invalid request. Missing one or more required values."))

        try:
            token = SocialToken.objects.get(account__user=request.user,
                                            account__provider='github')
        except Exception:
            msg = "Failed to get GitHub token for user {0} ".format(request.user.username) + \
                  "You must first authenticate with GitHub."
            raise ValidationError(dict(detail=msg))

        try:
            gh_api = Github(token.token)
            gh_api.get_api_status()
        except GithubException as e:
            msg = "Failed to connect to GitHub API. This is most likely a temporary error, " + \
                  "please try again in a few minutes. {0} - {1}".format(e.data, e.status)
            raise ValidationError(dict(detail=msg))

        try:
            gh_repo = gh_api.get_repo(github_user + '/' + github_repo)
        except GithubException as e:
            msg = "GitHub API failed to return repo for {0}/{1}. {2} - {3}".format(
                github_user, github_repo, e.data, e.status)
            raise ValidationError(dict(detail=msg))

        try:
            gh_user = gh_api.get_user()
        except GithubException as e:
            msg = "GitHub API failed to return authorized user. {0} - {1}".format(
                e.data, e.status)
            raise ValidationError(dict(detail=msg))

        try:
            gh_user.add_to_starred(gh_repo)
        except GithubException as e:
            msg = "GitHub API failed to add user {0} to stargazers ".format(request.user.username) + \
                "for {0}/{1}. {2} - {3}".format(github_user, github_repo, e.data, e.status)
            raise ValidationError(dict(detail=msg))

        repo = models.Repository.objects.get(github_user=github_user,
                                             github_repo=github_repo)
        star = repo.stars.create(owner=request.user)
        repo.stargazers_count = gh_repo.stargazers_count + 1
        repo.save()

        return Response(
            dict(result=dict(id=star.id,
                             github_user=repo.github_user,
                             github_repo=repo.github_repo,
                             stargazers_count=repo.stargazers_count)),
            status=status.HTTP_201_CREATED)
Exemplo n.º 2
0
Arquivo: views.py Projeto: tima/galaxy
    def destroy(self, request, *args, **kwargs):
        obj = super(StargazerDetail, self).get_object()

        try:
            token = SocialToken.objects.get(account__user=request.user,
                                            account__provider='github')
        except Exception:
            msg = ("Failed to connect to GitHub account for Galaxy user {}. "
                   "You must first authenticate with Github.".format(
                       request.user.username))
            raise ValidationError(dict(detail=msg))
        try:
            gh_api = Github(token.token)
            gh_api.get_api_status()
        except GithubException as e:
            msg = (
                "Failed to connect to GitHub API. This is most likely a "
                "temporary error, please try again in a few minutes. {} - {}".
                format(e.data, e.status))
            raise ValidationError(dict(detail=msg))

        try:
            gh_repo = gh_api.get_repo(obj.role.github_user + '/' +
                                      obj.role.github_repo)
        except GithubException as e:
            msg = (
                "GitHub API failed to return repo for {}/{}. {} - {}".format(
                    obj.github_user, obj.github_repo, e.data, e.status))
            raise ValidationError(dict(detail=msg))

        try:
            gh_user = gh_api.get_user()
        except GithubException as e:
            msg = (
                "GitHub API failed to return authorized user. {} - {}".format(
                    e.data, e.status))
            raise ValidationError(dict(detail=msg))

        try:
            gh_user.remove_from_starred(gh_repo)
        except GithubException as e:
            msg = ("GitHub API failed to remove user {} from stargazers "
                   "for {}/{}. {} - {}".format(request.user.username,
                                               obj.github_user,
                                               obj.github_repo, e.data,
                                               e.status))
            raise ValidationError(dict(detail=msg))

        obj.delete()

        repo = models.Repository.objects.get(github_user=obj.role.github_user,
                                             github_repo=obj.role.github_repo)
        repo.stargazers_count = max(0, gh_repo.stargazers_count - 1)
        repo.save()

        return Response(status=status.HTTP_202_ACCEPTED)
Exemplo n.º 3
0
    def get(self, request, *args, **kwargs):
        # Return a the list of user's repositories directly from GitHub
        try:
            token = SocialToken.objects.get(
                account__user=request.user, account__provider='github'
            )
        except Exception:
            msg = (
                "Failed to connect to GitHub account for Galaxy user {} "
                "You must first authenticate with GitHub."
                .format(request.user.username)
            )
            logger.error(msg)
            return HttpResponseBadRequest({'detail': msg})

        try:
            gh_api = Github(token.token)
            gh_api.get_api_status()
        except GithubException as e:
            msg = (
                "Failed to connect to GitHub API. This is most likely a "
                "temporary error, please try again in a few minutes. {} - {}"
                .format(e.data, e.status)
            )
            logger.error(msg)
            return HttpResponseBadRequest({'detail': msg})

        try:
            ghu = gh_api.get_user()
        except Exception:
            msg = "Failed to get GitHub authorized user."
            logger.error(msg)
            return HttpResponseBadRequest({'detail': msg})
        try:
            user_repos = ghu.get_repos()
        except Exception:
            msg = "Failed to get user repositories from GitHub."
            logger.error(msg)
            return HttpResponseBadRequest({'detail': msg})

        try:
            celerytasks.refresh_existing_user_repos(token.token, ghu)
        except Exception as exc:
            logger.error("Error: refresh_user_repos - {0}".format(exc.message))
            raise

        try:
            celerytasks.update_user_repos(user_repos, request.user)
        except Exception as exc:
            logger.error("Error: update_user_repos - {0}".format(exc.message))
            raise

        qs = request.user.repositories.all()
        serializer = serializers.RepositorySerializer(qs, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)
Exemplo n.º 4
0
    def destroy(self, request, *args, **kwargs):
        obj = super(StargazerDetail, self).get_object()

        try:
            token = SocialToken.objects.get(account__user=request.user, account__provider='github')
        except:
            msg = "Failed to connect to GitHub account for Galaxy user {0}. ".format(request.user.username) + \
                  "You must first authenticate with Github."
            raise ValidationError(dict(detail=msg))
        try:
            gh_api = Github(token.token)
            gh_api.get_api_status()
        except GithubException, e:
            msg = "Failed to connect to GitHub API. This is most likely a temporary " + \
                "error, please try again in a few minutes. {0} - {1}".format(e.data, e.status)
            raise ValidationError(dict(detail=msg))
Exemplo n.º 5
0
    def get(self, request, *args, **kwargs):
        # Return a the list of user's repositories directly from GitHub
        try:
            token = SocialToken.objects.get(account__user=request.user, account__provider='github')
        except:
            msg = "Failed to connect to GitHub account for Galaxy user {0} ".format(request.user.username) + \
                  "You must first authenticate with GitHub."
            raise ValidationError(dict(detail=msg))

        try:
            gh_api = Github(token.token)
            gh_api.get_api_status()
        except GithubException, e:
            msg = "Failed to connect to GitHub API. This is most likely a temporary error, " + \
                  "please try again in a few minutes. {0} - {1}".format(e.data, e.status)
            raise ValidationError(dict(detail=msg))
Exemplo n.º 6
0
    def post(self, request, *args, **kwargs):
        github_user = request.data.get('github_user', None)
        github_repo = request.data.get('github_repo', None)

        if not github_user or not github_repo:
            raise ValidationError(dict(detail="Invalid request. Missing one or more required values."))

        try:
            token = SocialToken.objects.get(account__user=request.user, account__provider='github')
        except:
            msg = "Failed to connect to GitHub account for Galaxy user {0}. ".format(request.user.username) + \
                  "You must first authenticate with Github."
            raise ValidationError(dict(detail=msg))

        try:
            gh_api = Github(token.token)
            gh_api.get_api_status()
        except GithubException, e:
            msg = "Failed to connect to GitHub API. This is most likely a temporary error, please try " + \
                  "again in a few minutes. {0} - {1}".format(e.data, e.status)
            raise ValidationError(dict(detail=msg))
Exemplo n.º 7
0
    def delete(self, request, *args, **kwargs):

        gh_user = request.query_params.get('github_user',None)
        gh_repo = request.query_params.get('github_repo', None)
        
        if not gh_user or not gh_repo:
            raise ValidationError(dict(detail="Invalid request."))

        if not request.user.is_staff:
            # Verify via GitHub API that user has access to requested role
            try:
                token = SocialToken.objects.get(account__user=request.user, account__provider='github')
            except:
                msg = "Failed to get Github account for Galaxy user {0}. ".format(request.user.username) + \
                      "You must first authenticate with Github."
                raise ValidationError(dict(detail=msg))

            try:
                gh_api = Github(token.token)
                gh_api.get_api_status()
            except GithubException, e:
                msg = "Failed to connect to GitHub API. This is most likely a temporary error, " + \
                      "please try again in a few minutes. {0} - {1}".format(e.data, e.status)
                raise ValidationError(dict(detail=msg))
        
            try:
                ghu = gh_api.get_user()
            except:
                raise ValidationError(dict(detail="Failed to get Github authorized user."))

            allowed = False
            repo_full_name = "%s/%s" % (gh_user, gh_repo)
            for r in ghu.get_repos():
                if r.full_name == repo_full_name:
                    allowed = True
                    continue
            if not allowed:
                msg = "Galaxy user {0} does not have access to repo {1}".format(
                    request.user.username, repo_full_name)
                raise ValidationError(dict(detail=msg))
Exemplo n.º 8
0
class GitWrapper:
    """Class wrapping PyGithub API.

    The purpose of this class is to wrap methods from PyGithub API used in Watchdog, for less error-prone and
    more convenient use. Docs for used API, including wrapped methods can be found at:
    https://pygithub.readthedocs.io/en/latest/introduction.html

        :param git_token:       Token used for GitHub
        :param repository:      GitHub repository name
        :param project:         GitHub project name
        :type git_token:        String
        :type repository:       String
        :type project:          String
    """
    def __init__(self, git_token, repository, project):
        self.git = Github(git_token)
        self.repository = repository
        self.project = project

    @retry(stop_max_attempt_number=_RETRY_LIMIT, wait_fixed=_RETRY_COOLDOWN_MS)
    def get_git_time(self):
        """Retrieve time from GitHub.

        Used to reliably determine time during Watchdog run.

            :return:                    Datetime object describing current time
            :rtype:                     datetime
        """
        datetime_string = self.git.get_api_status().raw_headers['date']
        try:
            datetime_object = datetime.strptime(datetime_string,
                                                '%a, %d %b %Y %H:%M:%S %Z')
        except ValueError:
            log.exception('Failed to parse date retrieved from GitHub: %s',
                          str(datetime_string))
            raise
        return datetime_object

    @retry(stop_max_attempt_number=_RETRY_LIMIT, wait_fixed=_RETRY_COOLDOWN_MS)
    def get_pull_requests(self):
        return self.git.get_organization(self.repository).get_repo(
            self.project).get_pulls()
Exemplo n.º 9
0
class GithubAPIWrapper:
    def __init__(self, user, password):
        self.g = Github(user, password)

    def get_rate_limit(self):
        rate = self.g.get_rate_limit()
        return (rate.rate.remaining, rate.rate.limit)

    def get_api_status(self):
        return self.g.get_api_status().status

    def query_by_stars(self, low, high):
        return self.g.search_repositories("stars:%d..%d" % (low, high))

    def issues_by_date(self, label, low, high, sort="created", order="asc"):
        return self.g.search_issues("created:%s..%s type:issue label:%s"
                % (low, high, label), sort, order)

    def load_repo(self, raw):
        return self.g.create_from_raw_data(Repository, raw)

    def sleep(self, secs):
        time.sleep(secs)
Exemplo n.º 10
0
def import_role(task_id):
    try:
        logger.info(u"Starting task: %d" % int(task_id))
        import_task = ImportTask.objects.get(id=task_id)
        import_task.state = "RUNNING"
        import_task.started = timezone.now()
        import_task.save()
        transaction.commit()
    except:
        fail_import_task(None, u"Failed to get task id: %d" % int(task_id))

    try:
        role = Role.objects.get(id=import_task.role.id)
    except:
        fail_import_task(import_task,
                         u"Failed to get role for task id: %d" % int(task_id))

    user = import_task.owner
    repo_full_name = role.github_user + "/" + role.github_repo
    add_message(
        import_task, u"INFO", u"Starting import %d: role_name=%s repo=%s" %
        (import_task.id, role.name, repo_full_name))
    try:
        token = SocialToken.objects.get(account__user=user,
                                        account__provider='github')
    except:
        fail_import_task(import_task, (
            u"Failed to get Github account for Galaxy user %s. You must first "
            u"authenticate with Github." % user.username))

    # create an API object and get the repo
    try:
        gh_api = Github(token.token)
        gh_api.get_api_status()
    except:
        fail_import_task(import_task, (
            u'Failed to cfonnect to Github API. This is most likely a temporary error, '
            u'please retry your import in a few minutes.'))

    try:
        gh_user = gh_api.get_user()
    except:
        fail_import_task(import_task, u"Failed to get Github authorized user.")

    repo = None
    add_message(import_task, u"INFO",
                u"Retrieving Github repo %s" % repo_full_name)
    for r in gh_user.get_repos():
        if r.full_name == repo_full_name:
            repo = r
            continue
    if repo is None:
        fail_import_task(
            import_task, u"Galaxy user %s does not have access to repo %s" %
            (user.username, repo_full_name))

    update_namespace(repo)

    # determine which branch to use
    if import_task.github_reference:
        branch = import_task.github_reference
    elif role.github_branch:
        branch = role.github_branch
    else:
        branch = repo.default_branch

    add_message(import_task, u"INFO", u"Accessing branch: %s" % branch)

    # parse meta data
    add_message(import_task, u"INFO", u"Parsing and validating meta data.")
    meta_data = decode_file(import_task,
                            repo,
                            branch,
                            'meta/main.yml',
                            return_yaml=True)
    if not meta_data:
        meta_data = decode_file(import_task,
                                repo,
                                branch,
                                'ansible/meta.yml',
                                return_yaml=True)
    if not meta_data:
        fail_import_task(
            import_task,
            u"Failed to get meta data. Did you forget to add meta/main.yml or "
            u"ansible/meta.yml?")

    # validate meta/main.yml
    galaxy_info = meta_data.get("galaxy_info", None)
    if galaxy_info is None:
        add_message(import_task, u"ERROR",
                    u"Key galaxy_info not found in meta data")
        galaxy_info = {}

    if import_task.alternate_role_name:
        add_message(
            import_task, u"INFO",
            u"Setting role name to %s" % import_task.alternate_role_name)
        role.name = import_task.alternate_role_name

    role.description = strip_input(
        galaxy_info.get("description", repo.description))
    role.author = strip_input(galaxy_info.get("author", ""))
    role.company = strip_input(galaxy_info.get("company", ""))
    role.license = strip_input(galaxy_info.get("license", ""))
    if galaxy_info.get('min_ansible_version'):
        role.min_ansible_version = strip_input(
            galaxy_info.get("min_ansible_version", ""))
    if galaxy_info.get('min_ansible_container_version'):
        role.min_ansible_container_version = strip_input(
            galaxy_info.get("min_ansible_container_version", ""))
    role.issue_tracker_url = strip_input(
        galaxy_info.get("issue_tracker_url", ""))
    role.github_branch = strip_input(galaxy_info.get("github_branch", ""))
    role.github_default_branch = repo.default_branch

    # check if meta/container.yml exists
    container_yml = decode_file(import_task,
                                repo,
                                branch,
                                'meta/container.yml',
                                return_yaml=False)
    ansible_container_yml = decode_file(import_task,
                                        repo,
                                        branch,
                                        'ansible/container.yml',
                                        return_yaml=False)
    if container_yml and ansible_container_yml:
        add_message(import_task, u"ERROR",
                    (u"Found ansible/container.yml and meta/container.yml. "
                     u"A role can only have only one container.yml file."))
    elif container_yml:
        add_message(import_task, u"INFO", u"Found meta/container.yml")
        add_message(import_task, u"INFO", u"Setting role type to Container")
        role.role_type = Role.CONTAINER
        role.container_yml = container_yml
    elif ansible_container_yml:
        add_message(import_task, u"INFO", u"Found ansible/container.yml")
        add_message(import_task, u"INFO",
                    u"Setting role type to Container App")
        role.role_type = Role.CONTAINER_APP
        role.container_yml = ansible_container_yml
    else:
        role.type = role.ANSIBLE
        role.container_yml = None

    if role.issue_tracker_url == "" and repo.has_issues:
        role.issue_tracker_url = repo.html_url + '/issues'

    if role.company != "" and len(role.company) > 50:
        add_message(
            import_task, u"WARNING",
            u"galaxy_info.company exceeds max length of 50 in meta data")
        role.company = role.company[:50]

    if not role.description:
        add_message(
            import_task, u"ERROR",
            u"missing description. Add a description to GitHub repo or meta data."
        )
    elif len(role.description) > 255:
        add_message(
            import_task, u"WARNING",
            u"galaxy_info.description exceeds max length of 255 in meta data")
        role.description = role.description[:255]

    if not role.license:
        add_message(import_task, u"ERROR",
                    u"galaxy_info.license missing value in meta data")
    elif len(role.license) > 50:
        add_message(
            import_task, u"WARNING",
            u"galaxy_info.license exceeds max length of 50 in meta data")
        role.license = role.license[:50]

    if role.role_type in (role.CONTAINER,
                          role.ANSIBLE) and not role.min_ansible_version:
        add_message(
            import_task, u"WARNING",
            u"Minimum Ansible version missing in meta data. Defaulting to 1.9."
        )
        role.min_ansible_version = u'1.9'

    if role.role_type == role.CONTAINER_APP and not role.min_ansible_container_version:
        add_message(
            import_task, u"WARNING",
            u"Minimum Ansible Container version missing in meta data. "
            u"Defaulting to 0.2.0")
        role.min_ansible_container_version = u'0.2.0'

    if not role.issue_tracker_url:
        add_message(import_task, u"WARNING", (
            u"No issue tracker defined. Enable issue tracker in repo settings, "
            u"or provide an issue tracker in meta data."))
    else:
        parsed_url = urlparse(role.issue_tracker_url)
        if parsed_url.scheme == '' or parsed_url.netloc == '' or parsed_url.path == '':
            add_message(import_task, u"WARNING",
                        u"Invalid URL found in meta data for issue tracker ")
            role.issue_tracker_url = ""

    # Update role attributes from repo
    sub_count = 0
    for sub in repo.get_subscribers():
        sub_count += 1  # only way to get subscriber count via pygithub
    role.stargazers_count = repo.stargazers_count
    role.watchers_count = sub_count
    role.forks_count = repo.forks_count
    role.open_issues_count = repo.open_issues_count

    last_commit = repo.get_commits(sha=branch)[0].commit
    role.commit = last_commit.sha
    role.commit_message = last_commit.message[:255]
    role.commit_url = last_commit.html_url
    role.commit_created = last_commit.committer.date.replace(tzinfo=pytz.UTC)

    # Update the import task in the event the role is left in an invalid state.
    import_task.stargazers_count = repo.stargazers_count
    import_task.watchers_count = sub_count
    import_task.forks_count = repo.forks_count
    import_task.open_issues_count = repo.open_issues_count

    import_task.commit = last_commit.sha
    import_task.commit_message = last_commit.message[:255]
    import_task.commit_url = last_commit.html_url
    import_task.github_branch = branch

    add_tags(import_task, galaxy_info, role)
    if role.role_type in (role.CONTAINER, role.ANSIBLE):
        if not galaxy_info.get('platforms'):
            add_message(import_task, u"ERROR",
                        u"No platforms found in meta data")
        else:
            add_platforms(import_task, galaxy_info, role)

    if role.role_type in (role.CONTAINER,
                          role.ANSIBLE) and meta_data.get('dependencies'):
        add_dependencies(import_task, meta_data['dependencies'], role)

    readme, readme_html, readme_type = get_readme(import_task, repo, branch,
                                                  token)
    if readme:
        role.readme = readme
        role.readme_html = readme_html
        role.readme_type = readme_type
    else:
        fail_import_task(
            import_task,
            u"Failed to get README. All roles must include a README.")

    # iterate over repo tags and create version objects
    add_message(import_task, u"INFO", u"Adding repo tags as role versions")
    try:
        git_tag_list = repo.get_tags()
        for tag in git_tag_list:
            rv, created = RoleVersion.objects.get_or_create(name=tag.name,
                                                            role=role)
            rv.release_date = tag.commit.commit.author.date.replace(
                tzinfo=pytz.UTC)
            rv.save()
    except Exception as exc:
        add_message(
            import_task, u"ERROR",
            u"An error occurred while importing repo tags: %s" % unicode(exc))

    try:
        role.validate_char_lengths()
    except Exception as exc:
        add_message(import_task, u"ERROR", unicode(exc))

    try:
        import_task.validate_char_lengths()
    except Exception as exc:
        add_message(import_task, u"ERROR", unicode(exc))

    # determine state of import task
    error_count = import_task.messages.filter(message_type="ERROR").count()
    warning_count = import_task.messages.filter(message_type="WARNING").count()
    import_state = u"SUCCESS" if error_count == 0 else u"FAILED"
    add_message(import_task, u"INFO", u"Import completed")
    add_message(
        import_task, import_state, u"Status %s : warnings=%d errors=%d" %
        (import_state, warning_count, error_count))

    try:
        import_task.state = import_state
        import_task.finished = timezone.now()
        import_task.save()
        role.imported = timezone.now()
        role.is_valid = True
        role.save()
        transaction.commit()
    except Exception, e:
        fail_import_task(import_task, u"Error saving role: %s" % e.message)
Exemplo n.º 11
0
brorepo.size

# <codecell>

gepy.get_public_members()

# <codecell>

# <codecell>

gepy.location

# <codecell>

searchbleh = g.get_api_status()

# <codecell>

print searchbleh.status
print searchbleh.last_modified

# <codecell>

searchpy.totalCount

# <codecell>

import geopy

# <codecell>
Exemplo n.º 12
0
from github import Github

g = Github('<usuario>','<contraseña>')

p = g.get_api_status()

#Status
print p.status

#Last_updated
print p.last_updated

#Last_modified
print p.last_modified
Exemplo n.º 13
0
brorepo.size

# <codecell>

gepy.get_public_members()

# <codecell>


# <codecell>

gepy.location

# <codecell>

searchbleh = g.get_api_status()

# <codecell>

print searchbleh.status
print searchbleh.last_modified

# <codecell>

searchpy.totalCount

# <codecell>

import geopy

# <codecell>
from github import Github
from github_token import GITHUB_TOKEN, user, password

g1 = Github(GITHUB_TOKEN)
g2 = Github(user, password)

print (g1.get_last_api_status_message())
print (g1.get_api_status_messages())
print (g1.get_api_status())

#print (g2.get_last_api_status_message())
#print (g2.get_api_status_messages())
#print (g2.get_api_status())
Exemplo n.º 15
0
Arquivo: views.py Projeto: tima/galaxy
    def delete(self, request, *args, **kwargs):

        gh_user = request.query_params.get('github_user', None)
        gh_repo = request.query_params.get('github_repo', None)

        if not gh_user or not gh_repo:
            raise ValidationError(dict(detail="Invalid request."))

        if not request.user.is_staff:
            # Verify via GitHub API that user has access to requested role
            try:
                token = SocialToken.objects.get(account__user=request.user,
                                                account__provider='github')
            except Exception:
                msg = ("Failed to get Github account for Galaxy user {}. "
                       "You must first authenticate with Github.".format(
                           request.user.username))
                raise ValidationError(dict(detail=msg))

            try:
                gh_api = Github(token.token)
                gh_api.get_api_status()
            except GithubException as e:
                msg = (
                    "Failed to connect to GitHub API. This is most likely a "
                    "temporary error, please try again in a few minutes. {} - {}"
                    .format(e.data, e.status))
                raise ValidationError(dict(detail=msg))

            try:
                ghu = gh_api.get_user()
            except Exception:
                raise ValidationError(
                    dict(detail="Failed to get Github authorized user."))

            allowed = False
            repo_full_name = "{}/{}".format(gh_user, gh_repo)
            for r in ghu.get_repos():
                if r.full_name == repo_full_name:
                    allowed = True
                    continue

            if not allowed:
                msg = "Galaxy user {0} does not have access to repo {1}".format(
                    request.user.username, repo_full_name)
                raise ValidationError(dict(detail=msg))

        # User has access. Delete requested role and associated bits.
        response = OrderedDict([('deleted_roles', []), ('status', '')])

        roles = models.Content.objects.filter(
            repository__provider_namespace__name=gh_user,
            repository__name=gh_repo)
        cnt = len(roles)
        if cnt == 0:
            response['status'] = (
                "Role {}.{} not found. Maybe it was deleted previously?".
                format(gh_user, gh_repo))
            return Response(response)
        elif cnt == 1:
            response['status'] = "Role {}.{} deleted".format(gh_user, gh_repo)
        else:
            response['status'] = (
                "Deleted {:d} roles associated with {}/{}".format(
                    len(roles), gh_user, gh_repo))

        for role in roles:
            response['deleted_roles'].append({
                "id": role.id,
                "namespace": role.namespace.name,
                "name": role.name,
                "github_user": role.github_user,
                "github_repo": role.github_repo
            })

        repo = models.Repository.objects.get(provider_namespace__name=gh_user,
                                             name=gh_repo)

        models.Notification.objects.filter(repository=repo).delete()
        models.Content.objects.filter(repository=repo).delete()
        models.ImportTask.objects.filter(repository=repo).delete()
        repo.delete()

        return Response(response)
Exemplo n.º 16
0
Arquivo: views.py Projeto: tima/galaxy
    def destroy(self, request, *args, **kwargs):
        obj = super(SubscriptionDetail, self).get_object()

        try:
            token = SocialToken.objects.get(account__user=request.user,
                                            account__provider='github')
        except Exception:
            msg = ("Failed to access GitHub account for Galaxy user {}. "
                   "You must first authenticate with GitHub.".format(
                       request.user.username))
            raise ValidationError(dict(detail=msg))

        try:
            gh_api = Github(token.token)
            gh_api.get_api_status()
        except GithubException as e:
            msg = (
                "Failed to connect to GitHub API. This is most likely a "
                "temporary error, please try again in a few minutes. {} - {}".
                format(e.data, e.status))
            raise ValidationError(dict(detail=msg))

        try:
            gh_repo = gh_api.get_repo(obj.github_user + '/' + obj.github_repo)
        except GithubException as e:
            msg = (
                "GitHub API failed to return repo for {}/{}. {} - {}".format(
                    obj.github_user, obj.github_repo, e.data, e.status))
            raise ValidationError(dict(detail=msg))

        try:
            gh_user = gh_api.get_user()
        except GithubException as e:
            msg = (
                "GitHub API failed to return authorized user. {} - {}".format(
                    e.data, e.status))
            raise ValidationError(dict(detail=msg))

        try:
            gh_user.remove_from_subscriptions(gh_repo)
        except GithubException as e:
            msg = ("GitHub API failed to unsubscribe {} from {}/{}. {} - {}".
                   format(request.user.username, obj.github_user,
                          obj.github_repo, e.data, e.status))
            raise ValidationError(dict(detail=msg))

        obj.delete()

        sub_count = 0
        for sub in gh_repo.get_subscribers():
            sub_count += 1  # only way to get subscriber count via pygithub

        repo = models.Repository.objects.get(github_user=obj.github_user,
                                             github_repo=obj.github_repo)
        repo.watchers_count = sub_count
        repo.save()

        result = ("unsubscribed {} from {}/{}.".format(request.user.username,
                                                       obj.github_user,
                                                       obj.github_repo))

        return Response(dict(detail=result), status=status.HTTP_202_ACCEPTED)
Exemplo n.º 17
0
Arquivo: views.py Projeto: tima/galaxy
    def post(self, request, *args, **kwargs):
        github_user = request.data.get('github_user', None)
        github_repo = request.data.get('github_repo', None)

        if not github_user or not github_repo:
            raise ValidationError(
                dict(detail=
                     "Invalid request. Missing one or more required values."))

        try:
            token = SocialToken.objects.get(account__user=request.user,
                                            account__provider='github')
        except Exception:
            msg = ("Failed to connect to GitHub account for Galaxy user {}. "
                   "You must first authenticate with Github.".format(
                       request.user.username))
            raise ValidationError(dict(detail=msg))

        try:
            gh_api = Github(token.token)
            gh_api.get_api_status()
        except GithubException as e:
            msg = (
                "Failed to connect to GitHub API. This is most likely a "
                "temporary error, please try again in a few minutes. {} - {}".
                format(e.data, e.status))
            raise ValidationError(dict(detail=msg))

        try:
            gh_repo = gh_api.get_repo(github_user + '/' + github_repo)
        except GithubException as e:
            msg = (
                "GitHub API failed to return repo for {}/{}. {} - {}".format(
                    github_user, github_repo, e.data, e.status))
            raise ValidationError(dict(detail=msg))

        try:
            gh_user = gh_api.get_user()
        except GithubException as e:
            msg = (
                "GitHub API failed to return authorized user. {} - {}".format(
                    e.data, e.status))
            raise ValidationError(dict(detail=msg))

        try:
            gh_user.add_to_subscriptions(gh_repo)
        except GithubException as e:
            msg = (
                "GitHub API failed to subscribe user {} to for {}/{}".format(
                    request.user.username, github_user, github_repo))
            raise ValidationError(dict(detail=msg))

        new_sub, created = models.Subscription.objects.get_or_create(
            owner=request.user,
            github_user=github_user,
            github_repo=github_repo,
            defaults={
                'owner': request.user,
                'github_user': github_user,
                'github_repo': github_repo
            })

        sub_count = 0
        for s in gh_repo.get_subscribers():
            sub_count += 1  # only way to get subscriber count via pygithub

        repo = models.Repository.objects.get(github_user=github_user,
                                             github_repo=github_repo)
        repo.watchers_count = sub_count
        repo.save()

        return Response(dict(result=dict(id=new_sub.id,
                                         github_user=new_sub.github_user,
                                         github_repo=new_sub.github_repo,
                                         watchers_count=sub_count)),
                        status=status.HTTP_201_CREATED)
Exemplo n.º 18
0
            local_gh = Github(login_or_token=credential['pass'], client_id=credential['client_id'],
                              client_secret=credential['client_secret'], user_agent=credential['login'],
                              timeout=timeout)
            github_clients.append(local_gh)
            github_clients_ids.append(credential['login'])
            scream.say(local_gh.rate_limiting)
        else:
            local_gh = Github(credential['login'], credential['pass'])
            github_clients.append(local_gh)
            scream.say(local_gh.rate_limiting)

    scream.cout('How many Github objects in github_clients: ' + str(len(github_clients)))
    scream.cout('Assigning current github client to the first object in a list')

    github_client = github_clients[0]
    lapis = local_gh.get_api_status()
    scream.say('Current status of GitHub API...: ' + lapis.status + ' (last update: ' + str(lapis.last_updated) + ')')

    if intelli_no_of_threads:
        scream.say('Adjusting no of threads to: ' + str(len(github_clients)))
        no_of_threads = len(github_clients)
        scream.say('No of threads is currently: ' + str(no_of_threads))

    is_gc_turned_on = 'turned on' if str(gc.isenabled()) else 'turned off'
    scream.ssay('Garbage collector is ' + is_gc_turned_on)

    scream.say('WORKING WITH INPUT FILE : ' + input_filename)  # simply 'result_stargazers_2013_final_mature.csv'
    scream.say('This can take a while, max aprox. 2 minutes...')
    filename_ = 'data/' if sys.platform == 'linux2' else 'data\\'
    filename__ = filename_ + input_filename  # remember it is in a /data subdir
    with open(filename__, 'rb') as source_csvfile:
                              timeout=timeout)
            github_clients.append(local_gh)
            github_clients_ids.append(credential['login'])
            scream.say(local_gh.rate_limiting)
        else:
            local_gh = Github(credential['login'], credential['pass'])
            github_clients.append(local_gh)
            scream.say(local_gh.rate_limiting)

    scream.cout('How many Github objects in github_clients: ' +
                str(len(github_clients)))
    scream.cout(
        'Assigning current github client to the first object in a list')

    github_client = github_clients[0]
    lapis = local_gh.get_api_status()
    scream.say('Current status of GitHub API...: ' + lapis.status +
               ' (last update: ' + str(lapis.last_updated) + ')')

    if intelli_no_of_threads:
        scream.say('Adjusting no of threads to: ' + str(len(github_clients)))
        no_of_threads = len(github_clients)
        scream.say('No of threads is currently: ' + str(no_of_threads))

    is_gc_turned_on = 'turned on' if str(gc.isenabled()) else 'turned off'
    scream.ssay('Garbage collector is ' + is_gc_turned_on)

    scream.say(
        'WORKING WITH INPUT FILE : ' +
        input_filename)  # simply 'result_stargazers_2013_final_mature.csv'
    scream.say('This can take a while, max aprox. 2 minutes...')
class GitWrapper:
    """Class wrapping PyGithub API.

    The purpose of this class is to wrap methods from PyGithub API used in Watchdog, for less error-prone and
    more convenient use. Docs for used API, including wrapped methods can be found at:
    https://pygithub.readthedocs.io/en/latest/introduction.html

    :param github_credentials:       Credentials used for GitHub
    :param repository:            GitHub repository name
    :param project:               GitHub project name
    :type github_credentials:        String
    :type repository:             String
    :type project:                String
    """

    def __init__(self, github_credentials, repository, project):
        self.git = Github(*github_credentials)
        self.repository = repository
        self.project = project
        self.github_credentials = github_credentials

    @retry(stop_max_attempt_number=_RETRY_LIMIT, wait_fixed=_RETRY_COOLDOWN_MS)
    def get_git_time(self):
        """Retrieve time from GitHub.

        Used to reliably determine time during Watchdog run.

        :return:                    Datetime object describing current time
        :rtype:                     datetime
        """
        try:
            datetime_object = self._get_git_time()
        except ValueError as e:
            raise GitWrapperError(str(e))
        except GithubException as e:
            message = 'GitHub Exception during API status retrieval. Exception: {}'.format(str(e))
            raise GitWrapperError(message)
        except timeout_decorator.TimeoutError:
            message = 'GitHub Exception during API status retrieval. Timeout during API request.'
            raise GitWrapperError(message)
        return datetime_object

    @retry(stop_max_attempt_number=_RETRY_LIMIT, wait_fixed=_RETRY_COOLDOWN_MS)
    def get_pull_requests(self):
        """Retrieve paginated list of pull requests from GitHub.

        :return:                    Paginated list of Pull Requests in GitHub repo
        :rtype:                     github.PaginatedList.PaginatedList of github.PullRequest.PullRequest
        """
        try:
            prs = self._get_pull_requests()
        except GithubException as e:
            message = 'GitHub Exception during API status retrieval. Exception: {}'.format(str(e))
            raise GitWrapperError(message)
        return prs

    @timeout_decorator.timeout(_REQUEST_TIMEOUT_S)
    def _get_git_time(self):
        """Private method retrieving time from GitHub.

        :return:                    Datetime object describing current time
        :rtype:                     datetime
        """
        datetime_string = self.git.get_api_status().raw_headers.get('date', '')
        datetime_format = '%a, %d %b %Y %H:%M:%S %Z'
        datetime_object = datetime.strptime(datetime_string, datetime_format)
        return datetime_object

    @timeout_decorator.timeout(_REQUEST_TIMEOUT_S)
    def _get_pull_requests(self):
        """Private method retrieving pull requests from GitHub.

        :return:                    Paginated list of Pull Requests in GitHub repo
        :rtype:                     github.PaginatedList.PaginatedList of github.PullRequest.PullRequest
        """
        return self.git.get_organization(self.repository).get_repo(self.project).get_pulls()
Exemplo n.º 21
0
def github_api():
    g = Github()
    print(g.get_api_status())