Exemplo n.º 1
0
def retry_on_server_failure(cutpoint: Callable[..., Any], *args: Any,
                            **kwargs: Any) -> Any:
    # yes, I know about aspectlib.contrib.retry(), but this one logs
    e = None  # type: GithubException
    for i in range(4):
        if i < 3:
            try:
                yield aspectlib.Proceed
                break
            except GithubException as e:
                if e.status >= 500:
                    print_info(
                        "Received a server error %s from Github. Retry %s/3" %
                        (str(e.status), str(i + 1)))
                    time.sleep(1)
                    continue
                elif e.status == 404:
                    raise
                else:
                    print_error(
                        "Received server error %s from Github. Won't retry." %
                        str(e.status))
                    raise
        else:
            raise ErrorMessage("3 retries didn't yield results. Exiting.")
Exemplo n.º 2
0
 def execute_review_protection(
         change: Change[str], branch: Branch,
         existing_protection: Optional[BranchProtection],
         review_count: int) -> Change[str]:
     try:
         if branch.protected and existing_protection and existing_protection.required_pull_request_reviews:
             if review_count > 0:
                 print_debug(
                     "Replacing review protection on branch %s (%s reviews)"
                     % (highlight(branch.name), str(review_count)))
                 branch.edit_required_pull_request_reviews(
                     required_approving_review_count=review_count)
             else:
                 print_debug("Removing review protection on branch: %s" %
                             highlight(branch.name))
                 branch.remove_required_pull_request_reviews()
         elif review_count > 0:
             print_debug(
                 "Adding review protection on branch: %s (%s reviews)" %
                 (highlight(branch.name), str(review_count)))
             safe_branch_edit_protection(
                 branch, required_approving_review_count=review_count)
     except GithubException as e:
         print_error("Can't set review protection on branch %s to %s: %s" %
                     (highlight(branch.name), str(review_count), str(e)))
         return change.failure()
     return change.success()
Exemplo n.º 3
0
 def execute_dismiss_reviews(
         change: Change[str], branch: Branch,
         required_reviews: Optional[RequiredPullRequestReviews],
         dismiss_approvals: bool) -> Change[str]:
     try:
         if branch.protected and required_reviews:
             print_debug(
                 "Setting already protected branch %s to %s stale reviews" %
                 (highlight(branch.name),
                  highlight("dismiss" if dismiss_approvals else "allow")))
             branch.edit_required_pull_request_reviews(
                 dismiss_stale_reviews=dismiss_approvals)
         else:
             print_debug(
                 "Changing branch %s to %s stale reviews" %
                 (highlight(branch.name),
                  highlight("dismiss" if dismiss_approvals else "allow")))
             safe_branch_edit_protection(
                 branch, dismiss_stale_reviews=dismiss_approvals)
     except GithubException as e:
         print_error(
             "Can't set review dismissal on branch %s to %s: %s" %
             (highlight(branch.name), str(dismiss_approvals), str(e)))
         return change.failure()
     return change.success()
Exemplo n.º 4
0
def app() -> None:
    try:
        main()
    except utils.ErrorMessage as e:
        print_error("%s" % e.ansi_msg)
        if utils.enable_debug_output:
            print(
                utils.highlight(
                    "********** VERBOSE OUTPUT Full Exception Follows **********"
                ))
            raise
        else:
            sys.exit(e.exitcode)
Exemplo n.º 5
0
 def execute_remove_all_status_checks(
         change: Change[str], branch: Branch,
         existing_checks: Set[str]) -> Change[str]:
     print_debug("Removing all status checks from branch %s" %
                 highlight(branch.name))
     try:
         if existing_checks:
             branch.remove_required_status_checks()
     except GithubException as e:
         print_error(str(e))
         return change.failure()
     else:
         return change.success()
Exemplo n.º 6
0
def checked_weave(*args: Any, **kwargs: Any) -> None:
    for i in range(4):
        if i < 3:
            try:
                aspectlib.weave(*args, **kwargs)
                # every call to weave can load properties which then can make network requests, so we need to check
                # rate limits every time :'(
                check_rate_limits()
            except RateLimitExceededException:
                # sometimes weaving accesses properties on objects which then trigger network requests (yes, really),
                # and then sometimes the buffer in check_rate_limits isn't enough and we get rate limited. In that
                # case, we end up here. You could ask: "Why don't you just handle this exception instead of calling
                # check_rate_limits() all the time?" and the answer is: Github seems to penalize tokens that run
                # into the API limit instead of throttling beforehand, so we try to be good.
                if kwargs.get("_nested", False):
                    check_rate_limits()
                    checked_weave(*args, _nested=True, **kwargs)
                else:
                    raise ErrorMessage(
                        "We seem to have been rate-limited after trying to outwait the rate limit"
                    )
            except GithubException as e:
                if e.status >= 500:
                    print_info(
                        "Received a server error %s from Github. Retry %s/3" %
                        (str(e.status), str(i + 1)))
                    time.sleep(1)
                    continue
                elif e.status == 404:
                    raise
                else:
                    print_error(
                        "Received server error %s from Github. Won't retry." %
                        str(e.status))
                    raise
            else:
                break
        else:
            raise ErrorMessage("3 retries didn't yield results. Exiting.")
Exemplo n.º 7
0
 def execute_test_protection(change: Change[str], branch: Branch,
                             existing_checks: Set[str],
                             known_checks: Set[str]) -> Change[str]:
     print_debug("[%s] Changing status checks on branch '%s' to [%s]" %
                 (highlight(repo.name), highlight(branch.name),
                  highlight(", ".join(list(known_checks)))))
     try:
         if existing_checks:
             branch.edit_required_status_checks(strict=True,
                                                contexts=list(known_checks))
         else:
             safe_branch_edit_protection(
                 branch,
                 strict=True,
                 contexts=list(known_checks),
             )
     except GithubException as e:
         print_error(
             "Can't edit required status checks on repo %s branch %s: %s" %
             (repo.name, branch.name, str(e)))
         return change.failure()
     return change.success()
Exemplo n.º 8
0
    def apply_team_change(change: Change[Team],
                          org: Organization) -> Change[Team]:
        if change.action not in [ChangeActions.ADD, ChangeActions.REMOVE]:
            print_warning("Unsupported change action for teams: %s" %
                          change.action)
            return change.skipped()

        if change.action == ChangeActions.ADD and change.after is not None:
            to_create = change.after  # type: Team
            if not isinstance(to_create, Team):
                raise ErrorMessage("Create action without team to create")

            created = org.create_team(to_create.name,
                                      permission=to_create.default_permission,
                                      privacy=to_create.privacy)
            created.edit(created.name, description=to_create.description)
            to_create.id = created.id
            return change.success()
        elif change.action == ChangeActions.REMOVE and change.before is not None:
            try:
                print_debug("Retrieving team id %s for deletion" %
                            highlight(str(change.before.id)))
                to_delete = org.get_team(change.before.id)  # type: GithubTeam
            except GithubException:
                # if the team is already gone... ok
                return change.success()

            try:
                print_debug("Deleting team id %s" %
                            highlight(str(change.before.id)))
                to_delete.delete()
            except GithubException as e:
                print_error("Can't delete team id %s: %s" %
                            (highlight(str(change.before.id)), str(e)))
                return change.failure()
            change.before.id = None
        return change.success()
Exemplo n.º 9
0
    def apply_team_access(self, change: Change[str], repo: Repository,
                          role: str) -> Change[str]:
        if change.action not in [ChangeActions.ADD, ChangeActions.REMOVE]:
            return change.skipped()

        if change.action == ChangeActions.REMOVE and change.before is not None:
            if change.before in self.org_teams:
                try:
                    self.org_teams[change.before].remove_from_repos(repo)
                except GithubException as e:
                    print_error("Can't remove team %s from repo %s (%s)" %
                                (highlight(change.before), highlight(
                                    repo.name), str(e)))
                    return change.failure()
                return change.success()
            else:
                # the team was probably removed by another change
                print_debug("Unknown team %s to remove from repo %s" %
                            (highlight(change.before), highlight(repo.name)))
                return change.success()
        elif change.action == ChangeActions.ADD and change.after is not None:
            if change.after in self.org_teams:
                try:
                    self.org_teams[change.after].set_repo_permission(
                        repo, role)
                except GithubException as e:
                    print_error(
                        "Can't set permission %s for team %s on repo %s (%s)" %
                        (highlight(role), highlight(change.after),
                         highlight(repo.name), highlight(str(e))))
                    return change.failure()
                return change.success()
            else:
                print_error("Unknown team %s to add to repo %s" %
                            (highlight(change.after), highlight(repo.name)))
                return change.failure()
        return change.success()