def test_is_repo_allowed_set_allowed_multi(self):
        """Test is_repo_allowed() by using multiple 'allowed_repo' settings"""
        # test multiple allowed repos
        self.settings.add_setting("allowed_repo", {"test1", "test2"})

        self.assertFalse(service.is_repo_allowed("name/repo"))
        self.assertTrue(service.is_repo_allowed("name/test1"))
        self.assertTrue(service.is_repo_allowed("name/test2"))
Example #2
0
    def test_is_repo_allowed_set_allowed_multi(self):
        """Test is_repo_allowed() by using multiple 'allowed_repo' settings"""
        # test multiple allowed repos
        self.settings.add_setting("allowed_repo", {"test1", "test2"})

        self.assertFalse(service.is_repo_allowed("name/repo"))
        self.assertTrue(service.is_repo_allowed("name/test1"))
        self.assertTrue(service.is_repo_allowed("name/test2"))
    def test_is_repo_allowed_set_allowed(self):
        """Test is_repo_allowed() by using the 'allowed_repo' setting"""
        # test allowed repo
        self.settings.add_setting("allowed_repo", {"test1"})

        self.assertFalse(service.is_repo_allowed("name/repo"))
        self.assertTrue(service.is_repo_allowed("name/test1"))
        self.assertFalse(service.is_repo_allowed("name/test2"))
Example #4
0
    def test_is_repo_allowed_set_allowed(self):
        """Test is_repo_allowed() by using the 'allowed_repo' setting"""
        # test allowed repo
        self.settings.add_setting("allowed_repo", {"test1"})

        self.assertFalse(service.is_repo_allowed("name/repo"))
        self.assertTrue(service.is_repo_allowed("name/test1"))
        self.assertFalse(service.is_repo_allowed("name/test2"))
    def test_is_repo_allowed(self):
        """Test is_repo_allowed()"""
        # error is thrown when called without parameters
        self.assertRaises(TypeError, service.is_repo_allowed)

        # error is thrown when called with an invalid parameter
        self.assertFalse(service.is_repo_allowed(None))

        # repo is allowed by default
        self.assertTrue(service.is_repo_allowed("name/repo"))
Example #6
0
    def test_is_repo_allowed(self):
        """Test is_repo_allowed()"""
        # error is thrown when called without parameters
        self.assertRaises(TypeError, service.is_repo_allowed)

        # error is thrown when called with an invalid parameter
        self.assertFalse(service.is_repo_allowed(None))

        # repo is allowed by default
        self.assertTrue(service.is_repo_allowed("name/repo"))
    def test_is_repo_allowed_set_denied_allowed(self):
        """
        Test is_repo_allowed() by using both
        'allowed_repo' and 'denied_repo' settings
        """
        # set denied repo
        self.settings.add_setting("denied_repo", {"test1"})
        # set allowed repo
        self.settings.add_setting("allowed_repo", {"name"})

        self.assertTrue(service.is_repo_allowed("name/repo"))
        self.assertFalse(service.is_repo_allowed("name/test1"))
        self.assertTrue(service.is_repo_allowed("name/test2"))
        self.assertFalse(service.is_repo_allowed("owner/repo"))
Example #8
0
    def test_is_repo_allowed_set_denied_allowed(self):
        """
        Test is_repo_allowed() by using both
        'allowed_repo' and 'denied_repo' settings
        """
        # set denied repo
        self.settings.add_setting("denied_repo", {"test1"})
        # set allowed repo
        self.settings.add_setting("allowed_repo", {"name"})

        self.assertTrue(service.is_repo_allowed("name/repo"))
        self.assertFalse(service.is_repo_allowed("name/test1"))
        self.assertTrue(service.is_repo_allowed("name/test2"))
        self.assertFalse(service.is_repo_allowed("owner/repo"))
Example #9
0
    def config_js(self, repo_owner=None, repo_name=None):
        """
        Config file for dashboard.

        Parameters :
        - repo_owner : name of the Github repo owner, fe. `buildtimetrend`
        - repo_name : name of the Github repo, fe. `service`
        """
        # define extra settings
        extra = {
            'serviceUrl': "/"  # use this service instance for badge generation
        }

        repo = get_repo_slug(repo_owner, repo_name)

        # Check if repo is allowed
        if repo is not None and not is_repo_allowed(repo):
            message = "Project '{}' is not allowed."
            self.logger.info(message.format(repo))
            extra['message'] = message.format(cgi.escape(repo))
            repo = None
        else:
            self.logger.info("Generated dashboard config for project '%s'",
                             repo)

        # add project list
        extra.update(get_config_project_list())

        # return config file
        return get_config_string(repo, extra)
Example #10
0
def get_config_project_list():
    """
    Return a list of repoNames of projects hosted on this website.

    Create list of only allowed project repos
    and convert values from unicode to UTF8
    """
    allowed_projects = [
        str(x) for x in get_all_projects() if is_repo_allowed(x)
    ]

    # check if collection is empty
    if allowed_projects:
        return {'projectList': allowed_projects}
    else:
        return {}
Example #11
0
    def default(self,
                repo_owner=None,
                repo_name=None,
                badge_type="avg",
                interval=None):
        """
        Generate a shield badge.

        Parameters :
        - repo_owner : name of the Github repo owner, fe. `buildtimetrend`
        - repo_name : name of the Github repo, fe. `service`
        - badge_type : type of badge, options :
          - latest : buildtime of last build job
          - avg : average buildtime of buildjobs in period set by `interval`
                  (default)
          - jobs : number of build jobs in period set by `interval`
          - builds : number of builds in period set by `interval`
          - passed : percentage of successful build jobs during `interval`
        - interval : time interval, options :
          - week (default) : events of last week (last 7 days)
          - month : events of last month (last 30 days)
          - year : events of last year (last 52 weeks)
        """
        # parameter check
        repo = get_repo_slug(repo_owner, repo_name)
        badge_type = str(badge_type).lower()
        interval = check_time_interval(interval)["name"]

        badge_subject = "buildtime"
        badge_status = "trend"
        badge_colour = "blue"
        format_string = "{:d}"

        if repo is not None and is_repo_allowed(repo):
            if badge_type == "latest":
                # get last duration
                value = get_latest_buildtime(repo)
                badge_status = format_duration(value)
            elif badge_type == "jobs":
                badge_subject = "{0!s}_({1!s})".format(badge_type, interval)
                value = get_total_build_jobs(repo, interval)
                format_string = "{:d}"
                badge_status = format_string.format(value)
            elif badge_type == "builds":
                badge_subject = "{0!s}_({1!s})".format(badge_type, interval)
                value = get_total_builds(repo, interval)
                format_string = "{:d}"
                badge_status = format_string.format(value)
            elif badge_type == "passed":
                badge_subject = "{0!s}_({1!s})".format(badge_type, interval)
                value = get_pct_passed_build_jobs(repo, interval)
                badge_colour = get_result_color(value, 100, 75)
                format_string = "{:d}%"
                badge_status = format_string.format(value)
            elif badge_type == "last_fail":
                badge_subject = "{0!s}".format((badge_type))
                value = keenio.get_days_since_fail(repo)
                badge_colour = get_result_color(value, 3, 0)
                format_string = "{:d} days"
                badge_status = format_string.format(value)
            else:
                # calculate average
                badge_subject = "{0!s}_({1!s})".format(badge_subject, interval)
                value = get_avg_buildtime(repo, interval)
                badge_status = format_duration(value)

            # valid duration is 0 or greater int or float
            if type(value) not in (float, int) or value < 0:
                badge_status = "unknown"
                badge_colour = "lightgrey"

            self.logger.info(
                "Badge type %s (interval : %s) for %s, value : %s", badge_type,
                interval, repo, badge_status)

        # Redirect to shields.io API to generate badge
        raise cherrypy.HTTPRedirect(
            "https://img.shields.io/badge/{0!s}-{1!s}-{2!s}.svg".format(
                badge_subject, badge_status, badge_colour))