Exemplo n.º 1
0
    def from_url(cls, url, token):
        parts = urlsplit(url)
        if not parts.scheme:
            raise RepoError("{} is missing a scheme.".format(url))
        if not parts.netloc:
            raise RepoError("{} is missing a domain.".format(url))

        if parts.scheme != "https":
            logging.warning("Using scheme %s instead of https.", parts.scheme)

        match = cls.PATH_RE.match(parts.path)
        if not match:
            raise RepoError(
                "Bad path. Can't separate namespace "
                + "from repo name {}.".format(parts.path)
            )

        namespace = match.group("namespace")
        name = match.group("name")

        config = {
            "host": urlunsplit((parts.scheme, parts.netloc, "", "", "")),
            "token": token,
            "name": "gitlab",
        }

        self = cls(config, namespace, name, url)

        logging.debug(json.dumps(self.info))
        logging.debug("%s is valid.", self.name_with_namespace)

        return self
Exemplo n.º 2
0
    def from_url(cls, url, token):
        parts = urlsplit(url)
        if not parts.scheme:
            raise RepoError("{} is missing a scheme.".format(url))
        if not parts.netloc:
            raise RepoError("{} is missing a domain.".format(url))

        if parts.scheme != "https":
            logging.warning("Using scheme %s instead of https.", parts.scheme)

        return cls("https://basemock.com/api4/", "namespace", "HW1", "token")
Exemplo n.º 3
0
    def get_head(self, branch):
        if self.repo is None:
            raise RepoError("No repo to get head from")

        for head in self.repo.heads:
            if head.name == branch:
                return head

        return self.repo.create_head(branch, "origin/{}".format(branch))
Exemplo n.º 4
0
    def get_user_id(cls, username, config):
        data = cls._cls_gl_get(config, "/users", params={"search": username})

        if not data:
            logging.warning("Did not find any users matching %s.", username)
            raise RepoError("No user {}.".format(username))

        for result in data:
            if result["username"] == username:
                logging.info("Got id %s for user %s.", data[0]["id"], username)
                return result["id"]

        # Fall back to first result if all else fails
        logging.warning("Got %s users for %s.", len(data), username)
        logging.warning("Failed to find an exact match for %s.", username)
        logging.info("Got id %s for user %s.", data[0]["id"], data[0]["username"])
        return data[0]["id"]
Exemplo n.º 5
0
    def clone_to(self, dir_name, branch, attempts=1):
        logging.debug("Cloning %s...", self.ssh_url)
        for attempt in range(0, attempts):
            if attempts > 1:
                logging.debug("Attempt %d of %d...", attempt + 1, attempts)

            try:  # for exp. backoff
                try:
                    self._repo = git.Repo.clone_from(self.ssh_url, dir_name)
                    if branch:
                        for b in branch:
                            try:
                                self._repo.create_head(b, "origin/{}".format(b))
                            # pylint: disable=no-member
                            except git.exc.BadName as e:
                                raise BranchNotFound(b, e) from e

                        logging.debug(self._repo.heads)

                    logging.debug("Cloned %s.", self.name)
                # pylint: disable=no-member
                except git.exc.GitCommandError as e:
                    # GitPython may delete this directory
                    # and the caller may have opinions about that,
                    # so go ahead and re-create it just to be safe.
                    os.makedirs(dir_name, exist_ok=True)
                    raiseRetryableGitError(e)
                    raise RepoError(e) from e

                # if we got this far, we succeeded!
                break

            except RetryableGitError as e:
                if attempt == (attempts - 1):
                    raise
                logging.debug(e)

                duration = 0.5 * 2 ** attempt
                logging.debug("Retrying after %.1f seconds...", duration)
                sleep(duration)

        return self._repo
Exemplo n.º 6
0
    def id(self):
        if not self.already_exists():
            raise RepoError("Repo {} does not exist on Mock".format(self.name))

        return self.info["id"]
Exemplo n.º 7
0
 def pull(self, branch):
     if self.repo is None:
         raise RepoError("No repo to pull to")
Exemplo n.º 8
0
    def ssh_url(self):
        if not self.already_exists():
            raise RepoError("Repo {} does not exist on Mock".format(self.name))

        return self.info["ssh_url_to_repo"]
Exemplo n.º 9
0
    def get_index(self):
        if self.repo is None:
            raise RepoError("No repo to get index from")

        return self.repo.index
Exemplo n.º 10
0
    def namespace_id(self):
        if not self.already_exists():
            raise RepoError("Repo {} does not exist on Gitlab".format(self.name))

        return self.info["namespace"]["id"]