示例#1
0
    def parse_mr_event(event) -> Optional[MergeRequestGitlabEvent]:
        """ Look into the provided event and see if it's one for a new gitlab MR. """
        if event.get("object_kind") != "merge_request":
            return None

        state = event["object_attributes"]["state"]
        if state != "opened":
            return None
        action = nested_get(event, "object_attributes", "action")
        if action not in {"reopen", "update"}:
            action = state

        username = event["user"]["username"]
        if not username:
            logger.warning("No Gitlab username from event.")
            return None

        object_id = event["object_attributes"]["id"]
        if not object_id:
            logger.warning("No object id from the event.")
            return None

        object_iid = event["object_attributes"]["iid"]
        if not object_iid:
            logger.warning("No object iid from the event.")
            return None

        source_project_url = nested_get(event, "object_attributes", "source",
                                        "web_url")
        if not source_project_url:
            logger.warning("Source project url not found in the event.")
            return None
        parsed_source_url = parse_git_repo(potential_url=source_project_url)
        logger.info(f"Source: "
                    f"repo={parsed_source_url.repo} "
                    f"namespace={parsed_source_url.namespace} "
                    f"url={source_project_url}.")

        target_project_url = nested_get(event, "project", "web_url")
        if not target_project_url:
            logger.warning("Target project url not found in the event.")
            return None
        parsed_target_url = parse_git_repo(potential_url=target_project_url)
        logger.info(f"Target: "
                    f"repo={parsed_target_url.repo} "
                    f"namespace={parsed_target_url.namespace} "
                    f"url={target_project_url}.")

        commit_sha = nested_get(event, "object_attributes", "last_commit",
                                "id")

        return MergeRequestGitlabEvent(
            action=GitlabEventAction[action],
            username=username,
            object_id=object_id,
            object_iid=object_iid,
            source_repo_namespace=parsed_source_url.namespace,
            source_repo_name=parsed_source_url.repo,
            target_repo_namespace=parsed_target_url.namespace,
            target_repo_name=parsed_target_url.repo,
            project_url=target_project_url,
            commit_sha=commit_sha,
        )
示例#2
0
    def parse_mr_event(event) -> Optional[MergeRequestGitlabEvent]:
        """ Look into the provided event and see if it's one for a new gitlab MR. """
        if event.get("object_kind") != "merge_request":
            return None

        state = event["object_attributes"]["state"]
        if state != "opened":
            return None
        action = nested_get(event, "object_attributes", "action")
        if action not in {"reopen", "update"}:
            action = state

        username = event["user"]["username"]
        if not username:
            logger.warning("No Gitlab username from event.")
            return None

        object_id = event["object_attributes"]["id"]
        if not object_id:
            logger.warning("No object id from the event.")
            return None

        object_iid = event["object_attributes"]["iid"]
        if not object_iid:
            logger.warning("No object iid from the event.")
            return None

        source_repo_path_with_namespace = nested_get(event,
                                                     "object_attributes",
                                                     "source",
                                                     "path_with_namespace")
        if not source_repo_path_with_namespace:
            logger.warning("No source path from the event.")
            return None
        source_repo_namespace, source_repo_name = source_repo_path_with_namespace.split(
            "/")

        target_repo_path_with_namespace = nested_get(event,
                                                     "object_attributes",
                                                     "target",
                                                     "path_with_namespace")
        if not target_repo_path_with_namespace:
            logger.warning("No target path from the event.")
            return None
        target_repo_namespace, target_repo_name = target_repo_path_with_namespace.split(
            "/")

        logger.info(f"Target repo: {target_repo_path_with_namespace}.")

        https_url = nested_get(event, "object_attributes", "target", "web_url")

        commit_sha = nested_get(event, "object_attributes", "last_commit",
                                "id")

        return MergeRequestGitlabEvent(
            action=GitlabEventAction[action],
            username=username,
            object_id=object_id,
            object_iid=object_iid,
            source_repo_name=source_repo_name,
            source_repo_namespace=source_repo_namespace,
            target_repo_namespace=target_repo_namespace,
            target_repo_name=target_repo_name,
            https_url=https_url,
            commit_sha=commit_sha,
        )