Пример #1
0
    def get_substage_name(self, command):
        """
        Resolve Travis CI substage name that corresponds to a cli command.

        Parameters:
        - command : cli command
        """
        if not tools.is_string(command):
            return ""

        if len(self.current_build_data) > 0 and \
                "config" in self.current_build_data:
            build_config = self.current_build_data["config"]
        else:
            logger.warning("Travis CI build config is not set")
            return ""

        # check if build_config collection is empty
        if build_config:
            for stage_name, commands in build_config.items():
                if tools.is_list(commands) and command in commands:
                    substage_number = commands.index(command) + 1
                    substage_name = "{stage}.{substage:d}".format(
                        stage=stage_name, substage=substage_number)
                    logger.debug("Substage %s corresponds to '%s'",
                                 substage_name, command)
                    return substage_name

        return ""
Пример #2
0
def check_authorization(repo, auth_header):
    """
    Check if Travis CI notification has a correct Authorization header.

    This check is enabled if travis_account_token is defined in settings.

    More information on the Authorization header :
    http://docs.travis-ci.com/user/notifications/#Authorization-for-Webhooks

    Returns true if Authorization header is valid, but also if
    travis_account_token is not defined.

    Parameters:
    - repo : git repo name
    - auth_header : Travis CI notification Authorization header
    """
    # get Travis account token from Settings
    token = Settings().get_setting("travis_account_token")

    # return True if token is not set
    if token is None:
        logger.info("Setting travis_account_token is not defined,"
                    " Travis CI notification Authorization header"
                    " is not checked.")
        return True

    # check if parameters are strings
    if is_string(repo) and is_string(auth_header) and is_string(token):
        # generate hash (encode string to bytes first)
        auth_hash = sha256((repo + token).encode('utf-8')).hexdigest()

        # compare hash with Authorization header
        if auth_hash == auth_header:
            logger.info("Travis CI notification Authorization header"
                        " is correct.")
            return True
        else:
            logger.error("Travis CI notification Authorization header"
                         " is incorrect.")
            return False
    else:
        logger.debug("repo, auth_header and travis_auth_token"
                     " should be strings.")
        return False
Пример #3
0
def process_notification_payload(payload):
    """
    Extract repo slug and build number from Travis notification payload.

    Returns a dictionary with "repo" and "build" information,
    or an empty dictionary if the payload could not be processed.

    Deprecated behaviour : Currently the repo and build information are
    also stored in the "settings" object,
    but this will be removed in the near future.

    Parameters:
    - payload : Travis CI notification payload
    """
    settings = Settings()
    parameters = {}

    if payload is None:
        logger.warning("Travis notification payload is not set")
        return parameters

    if not is_string(payload):
        logger.warning(
            "Travis notification payload is incorrect :"
            " string expected, got %s", type(payload))
        return parameters

    json_payload = json.loads(payload)
    logger.info("Travis Payload : %r.", json_payload)

    # get repo name from payload
    if ("repository" in json_payload
            and "owner_name" in json_payload["repository"]
            and "name" in json_payload["repository"]):

        repo = get_repo_slug(json_payload["repository"]["owner_name"],
                             json_payload["repository"]["name"])

        logger.info("Build repo : %s", repo)
        settings.set_project_name(repo)
        parameters["repo"] = repo

    # get build number from payload
    if "number" in json_payload:
        logger.info("Build number : %s", str(json_payload["number"]))
        settings.add_setting('build', json_payload['number'])
        parameters["build"] = json_payload['number']

    return parameters
Пример #4
0
def check_time_interval(interval=None):
    """
    Check time interval and returns corresponding parameters.

    Parameters :
    - interval : timeframe, possible values : 'week', 'month', 'year',
                 anything else defaults to 'week'
    """
    if is_string(interval):
        # convert to lowercase
        interval = interval.lower()

        if interval in TIME_INTERVALS:
            return TIME_INTERVALS[interval]

    return TIME_INTERVALS['week']
Пример #5
0
    def test_is_string(self):
        """Test is_string()"""
        self.assertRaises(TypeError, tools.is_string)

        # error is thrown when called with an invalid parameter
        with self.assertRaises(TypeError) as exc_msg:
            tools.is_string(None, "name")
        self.assertEqual("param name should be a string",
                         str(exc_msg.exception))

        # error is thrown when called with an invalid parameter
        with self.assertRaises(TypeError) as exc_msg:
            tools.is_string(123, "name")
        self.assertEqual("param name should be a string",
                         str(exc_msg.exception))

        # error is thrown when called with an invalid parameter
        with self.assertRaises(TypeError) as exc_msg:
            tools.is_string({}, "name")
        self.assertEqual("param name should be a string",
                         str(exc_msg.exception))

        # error is thrown when called with an invalid parameter
        with self.assertRaises(TypeError) as exc_msg:
            tools.is_string([], "name")
        self.assertEqual("param name should be a string",
                         str(exc_msg.exception))

        self.assertFalse(tools.is_string(None))
        self.assertFalse(tools.is_string(None, None))

        self.assertTrue(tools.is_string("string", "name"))
Пример #6
0
    def test_is_string(self):
        """Test is_string()"""
        self.assertRaises(TypeError, tools.is_string)

        # error is thrown when called with an invalid parameter
        with self.assertRaises(TypeError) as exc_msg:
            tools.is_string(None, "name")
        self.assertEqual(
            "param name should be a string",
            str(exc_msg.exception)
        )

        # error is thrown when called with an invalid parameter
        with self.assertRaises(TypeError) as exc_msg:
            tools.is_string(123, "name")
        self.assertEqual(
            "param name should be a string",
            str(exc_msg.exception)
        )

        # error is thrown when called with an invalid parameter
        with self.assertRaises(TypeError) as exc_msg:
            tools.is_string({}, "name")
        self.assertEqual(
            "param name should be a string",
            str(exc_msg.exception)
        )

        # error is thrown when called with an invalid parameter
        with self.assertRaises(TypeError) as exc_msg:
            tools.is_string([], "name")
        self.assertEqual(
            "param name should be a string",
            str(exc_msg.exception)
        )

        self.assertFalse(tools.is_string(None))
        self.assertFalse(tools.is_string(None, None))

        self.assertTrue(tools.is_string("string", "name"))