Пример #1
0
 def list_remote_branches():
     try:
         gh = get_github_repo(app.GIT_ORG, app.GIT_REPO)
         return [x.name for x in gh.get_branches() if x]
     except GithubException as error:
         log.warning(u"Unable to contact github, can't check for update: {0!r}", error)
         return []
Пример #2
0
    def submit_github_issue(self, version_checker, max_issues=500):
        """Submit errors to github.

        :param version_checker:
        :type version_checker: CheckVersion
        :param max_issues:
        :type max_issues: int
        :return: user message and issue number
        :rtype: list of tuple(str, str)
        """
        if app.GIT_AUTH_TYPE == 0:
            if not app.DEBUG or not app.GIT_USERNAME or not app.GIT_PASSWORD:
                log.warning(IssueSubmitter.INVALID_CONFIG)
                return [(IssueSubmitter.INVALID_CONFIG, None)]
        else:
            if not app.DEBUG or not app.GIT_TOKEN:
                log.warning(IssueSubmitter.INVALID_CONFIG)
                return [(IssueSubmitter.INVALID_CONFIG, None)]
        if not ErrorViewer.errors:
            log.info(IssueSubmitter.NO_ISSUES)
            return [(IssueSubmitter.NO_ISSUES, None)]

        if not app.DEVELOPER and version_checker.need_update():
            log.warning(IssueSubmitter.UNSUPPORTED_VERSION)
            return [(IssueSubmitter.UNSUPPORTED_VERSION, None)]

        if self.running:
            log.warning(IssueSubmitter.ALREADY_RUNNING)
            return [(IssueSubmitter.ALREADY_RUNNING, None)]

        self.running = True
        try:
            if app.GIT_AUTH_TYPE == 0:
                github = authenticate(app.GIT_USERNAME, app.GIT_PASSWORD)
            else:
                github = token_authenticate(app.GIT_TOKEN)
            if not github:
                return [(IssueSubmitter.BAD_CREDENTIALS, None)]

            github_repo = get_github_repo(app.GIT_ORG, app.GIT_REPO, gh=github)
            loglines = ErrorViewer.errors[:max_issues]
            similar_issues = IssueSubmitter.find_similar_issues(
                github_repo, loglines)

            return IssueSubmitter.submit_issues(github, github_repo, loglines,
                                                similar_issues)
        except RateLimitExceededException:
            log.warning(IssueSubmitter.RATE_LIMIT)
            return [(IssueSubmitter.RATE_LIMIT, None)]
        except (GithubException, IOError):
            log.warning(IssueSubmitter.GITHUB_EXCEPTION)
            return [(IssueSubmitter.GITHUB_EXCEPTION, None)]
        finally:
            self.running = False
Пример #3
0
    def submit_github_issue(self, version_checker, max_issues=500):
        """Submit errors to github."""
        def result(message, level=logging.WARNING):
            log.log(level, message)
            return [(message, None)]

        if not app.DEBUG:
            return result(self.DEBUG_NOT_ENABLED)

        if app.GIT_AUTH_TYPE == 1 and not app.GIT_TOKEN:
            return result(self.MISSING_CREDENTIALS_TOKEN)

        if app.GIT_AUTH_TYPE == 0 and not (app.GIT_USERNAME
                                           and app.GIT_PASSWORD):
            return result(self.MISSING_CREDENTIALS)

        if not ErrorViewer.errors:
            return result(self.NO_ISSUES, logging.INFO)

        if not app.DEVELOPER and version_checker.need_update():
            return result(self.UNSUPPORTED_VERSION)

        if self.running:
            return result(self.ALREADY_RUNNING)

        self.running = True
        try:
            if app.GIT_AUTH_TYPE:
                github = token_authenticate(app.GIT_TOKEN)
            else:
                github = authenticate(app.GIT_USERNAME, app.GIT_PASSWORD)
            if not github:
                return result(self.BAD_CREDENTIALS)

            github_repo = get_github_repo(app.GIT_ORG, app.GIT_REPO, gh=github)
            loglines = ErrorViewer.errors[:max_issues]
            similar_issues = self.find_similar_issues(github_repo, loglines)

            return self.submit_issues(github, github_repo, loglines,
                                      similar_issues)
        except RateLimitExceededException:
            return result(self.RATE_LIMIT)
        except (GithubException, IOError) as error:
            # If the api return http status 404, authentication or permission issue(token right to create gists)
            if isinstance(error, UnknownObjectException):
                return result(self.GITHUB_UNKNOWNOBJECTEXCEPTION)
            return result(self.GITHUB_EXCEPTION)
        finally:
            self.running = False
Пример #4
0
    def submit_github_issue(self, version_checker, max_issues=500):
        """Submit errors to github."""
        def result(message, level=logging.WARNING):
            log.log(level, message)
            return [(message, None)]

        if not app.DEBUG:
            return result(self.DEBUG_NOT_ENABLED)

        if app.GIT_AUTH_TYPE == 1 and not app.GIT_TOKEN:
            return result(self.MISSING_CREDENTIALS_TOKEN)

        if app.GIT_AUTH_TYPE == 0 and not (app.GIT_USERNAME and app.GIT_PASSWORD):
            return result(self.MISSING_CREDENTIALS)

        if not ErrorViewer.errors:
            return result(self.NO_ISSUES, logging.INFO)

        if not app.DEVELOPER and version_checker.need_update():
            return result(self.UNSUPPORTED_VERSION)

        if self.running:
            return result(self.ALREADY_RUNNING)

        self.running = True
        try:
            if app.GIT_AUTH_TYPE:
                github = token_authenticate(app.GIT_TOKEN)
            else:
                github = authenticate(app.GIT_USERNAME, app.GIT_PASSWORD)
            if not github:
                return result(self.BAD_CREDENTIALS)

            github_repo = get_github_repo(app.GIT_ORG, app.GIT_REPO, gh=github)
            loglines = ErrorViewer.errors[:max_issues]
            similar_issues = self.find_similar_issues(github_repo, loglines)

            return self.submit_issues(github, github_repo, loglines, similar_issues)
        except RateLimitExceededException:
            return result(self.RATE_LIMIT)
        except (GithubException, IOError) as error:
            log.debug('Issue submitter failed with error: {0!r}', error)
            # If the api return http status 404, authentication or permission issue(token right to create gists)
            if isinstance(error, UnknownObjectException):
                return result(self.GITHUB_UNKNOWNOBJECTEXCEPTION)
            return result(self.GITHUB_EXCEPTION)
        finally:
            self.running = False
Пример #5
0
    def _check_github_for_update(self):
        """Use pygithub to ask github if there is a newer version..

        If there is a newer version it sets application's version text.

        commit_hash: hash that we're checking against
        """

        self._num_commits_behind = 0
        self._newest_commit_hash = None

        gh = get_github_repo(app.GIT_ORG, app.GIT_REPO)
        # try to get newest commit hash and commits behind directly by comparing branch and current commit
        if self._cur_commit_hash:
            try:
                branch_compared = gh.compare(base=self.branch,
                                             head=self._cur_commit_hash)
                self._newest_commit_hash = branch_compared.base_commit.sha
                self._num_commits_behind = branch_compared.behind_by
                self._num_commits_ahead = branch_compared.ahead_by
            except Exception:  # UnknownObjectException
                self._newest_commit_hash = ""
                self._num_commits_behind = 0
                self._num_commits_ahead = 0
                self._cur_commit_hash = ""

        # fall back and iterate over last 100 (items per page in gh_api) commits
        if not self._newest_commit_hash:

            for curCommit in gh.get_commits():
                if not self._newest_commit_hash:
                    self._newest_commit_hash = curCommit.sha
                    if not self._cur_commit_hash:
                        break

                if curCommit.sha == self._cur_commit_hash:
                    break

                # when _cur_commit_hash doesn't match anything _num_commits_behind == 100
                self._num_commits_behind += 1

        log.debug(
            u'cur_commit = {0}, newest_commit = {1}, num_commits_behind = {2}',
            self._cur_commit_hash, self._newest_commit_hash,
            self._num_commits_behind)
Пример #6
0
    def check_for_update(self):
        """Use pygithub to ask github if there is a newer version..

        If there is a newer version it sets application's version text.

        commit_hash: hash that we're checking against
        """
        gh = get_github_repo(app.GIT_ORG, app.GIT_REPO)

        # try to get the newest commit hash and commits by comparing branch and current commit
        if self._cur_commit_hash:
            try:
                branch_compared = gh.compare(base=self.branch, head=self._cur_commit_hash)
                self._newest_commit_hash = branch_compared.base_commit.sha
                self._num_commits_behind = branch_compared.behind_by
                self._num_commits_ahead = branch_compared.ahead_by
            except Exception:
                self._newest_commit_hash = None
                self._num_commits_behind = 0
                self._num_commits_ahead = 0

        # fall back and iterate over last 100 (items per page in gh_api) commits
        if not self._newest_commit_hash:
            for cur_commit in gh.get_commits():
                if not self._newest_commit_hash:
                    self._newest_commit_hash = cur_commit.sha
                    if not self._cur_commit_hash:
                        break

                if cur_commit.sha == self._cur_commit_hash:
                    break

                # when _cur_commit_hash doesn't match anything _num_commits_behind == 100
                self._num_commits_behind += 1

        log.debug(u'cur_commit = {0}, newest_commit = {1}, num_commits_behind = {2}',
                  self._cur_commit_hash, self._newest_commit_hash, self._num_commits_behind)
Пример #7
0
 def list_remote_branches():
     gh = get_github_repo(app.GIT_ORG, app.GIT_REPO)
     return [x.name for x in gh.get_branches() if x]