def test_feedstock_token_raises(gh_mock, git_mock, tmp_mock, tmpdir, repo,
                                project):
    gh_mock.return_value = "abc123"
    tmp_mock.TemporaryDirectory.return_value.__enter__.return_value = str(
        tmpdir)

    git_mock.Repo.clone_from.side_effect = ValueError("blarg")
    user = "******"
    os.makedirs(os.path.join(tmpdir, "tokens"), exist_ok=True)
    with open(os.path.join(tmpdir, "tokens", "%s.json" % project), "w") as fp:
        fp.write("blarg")

    with pytest.raises(RuntimeError) as e:
        feedstock_token_exists(user, project, repo)

    assert "Testing for the feedstock token for" in str(e.value)

    git_mock.Repo.clone_from.assert_called_once_with(
        "abc123",
        str(tmpdir),
        depth=1,
    )
Exemple #2
0
    def __call__(self, args):
        from conda_smithy.feedstock_tokens import (
            register_feedstock_token_with_proviers,
            register_feedstock_token,
            feedstock_token_exists,
        )
        from conda_smithy.ci_register import drone_default_endpoint

        drone_endpoints = args.drone_endpoints
        if drone_endpoints is None:
            drone_endpoints = [drone_default_endpoint]

        owner = args.user or args.organization
        repo = os.path.basename(os.path.abspath(args.feedstock_directory))

        if args.token_repo is None:
            token_repo = (
                "https://${GITHUB_TOKEN}@github.com/%s/feedstock-tokens" %
                owner)
        else:
            token_repo = args.token_repo

        if feedstock_token_exists(owner, repo, token_repo):
            raise RuntimeError("Token for repo %s/%s already exists!" %
                               (owner, repo))

        print("Registering the feedstock tokens. Can take up to ~30 seconds.")

        # do all providers first
        register_feedstock_token_with_proviers(
            owner,
            repo,
            drone=args.drone,
            circle=args.circle,
            travis=args.travis,
            azure=args.azure,
            github_actions=args.github_actions,
            drone_endpoints=drone_endpoints,
        )

        # then if that works do the github repo
        register_feedstock_token(owner, repo, token_repo)

        print("Successfully registered the feedstock token!")
Exemple #3
0
    def migrate(self, feedstock, branch):

        yaml = YAML()
        cfg = _read_conda_forge_yaml(yaml)

        if ("conda_forge_output_validation" in cfg
                and cfg["conda_forge_output_validation"]
                and feedstock_token_exists(
                    "conda-forge", feedstock + "-feedstock", TOKENS_REPO)):
            # migration done, no commits, no API calls
            return True, False, False

        if branch == "master":
            # register a feedstock token
            # this call is idempotent if the token already exists
            _register_feedstock_token(feedstock)
            print("    registered feedstock token")

            # register the staging binstar token
            subprocess.run(
                "conda smithy update-binstar-token "
                "--without-appveyor --token_name STAGING_BINSTAR_TOKEN",
                shell=True,
                check=True)
            print("    added staging binstar token")

            # register the outputs
            _register_feedstock_outputs(feedstock)
            print("    added output to outputs repo")

        # set the param and write
        cfg["conda_forge_output_validation"] = True
        with open("conda-forge.yml", "w") as fp:
            yaml.dump(cfg, fp)
        subprocess.run(
            ["git", "add", "conda-forge.yml"],
            check=True,
        )
        print("    updated conda-forge.yml")

        # migration done, make a commit, lots of API calls
        return True, True, True
def test_feedstock_token_exists(gh_mock, git_mock, tmp_mock, tmpdir, repo,
                                project, retval):
    gh_mock.return_value = "abc123"
    tmp_mock.TemporaryDirectory.return_value.__enter__.return_value = str(
        tmpdir)

    user = "******"
    os.makedirs(os.path.join(tmpdir, "tokens"), exist_ok=True)
    if retval:
        with open(os.path.join(tmpdir, "tokens", "%s.json" % project),
                  "w") as fp:
            fp.write("blarg")

    assert feedstock_token_exists(user, project, repo) is retval

    git_mock.Repo.clone_from.assert_called_once_with(
        "abc123",
        str(tmpdir),
        depth=1,
    )
Exemple #5
0
def _register_feedstock_token(feedstock):
    """Generate and register feedstock tokens."""

    if feedstock_token_exists("conda-forge", feedstock + "-feedstock",
                              TOKENS_REPO):
        print("    feedstock token already exists")
        return

    try:
        subprocess.run(
            ["conda-smithy", "generate-feedstock-token"],
            check=True,
        )

        subprocess.run(
            ["conda-smithy", "register-feedstock-token"],
            check=True,
        )
    except subprocess.CalledProcessError as e:
        print("    feedstock token registration failed")
        raise e
    finally:
        # remove both paths due to change in smithy
        try:
            if feedstock.endswith("-feedstock"):
                feedstock_name = feedstock[:-len("-feedstock")]
            else:
                feedstock_name = feedstock
            token_path = os.path.expanduser(
                "~/.conda-smithy/conda-forge_%s_feedstock.token" %
                feedstock_name)
            os.remove(token_path)
        except Exception:
            pass

        try:
            token_path = os.path.expanduser(
                "~/.conda-smithy/conda-forge_%s.token" % feedstock)
            os.remove(token_path)
        except Exception:
            pass
def register_feedstock_token_handler(feedstock):
    """Generate and register feedstock tokens.

    Parameters
    ----------
    feedstock : str
        The name of the feedstock.

    Returns
    -------
    error : bool
        True if there is an error, False otherwise.
    """

    feedstock_url = "https://github.com/conda-forge/%s.git" % feedstock

    tmpdir = None
    try:
        if feedstock_token_exists("conda-forge", feedstock, TOKENS_REPO):
            LOGGER.info("    feedstock token already exists")
            return False

        tmpdir = tempfile.mkdtemp('_recipe')
        fspath = os.path.join(tmpdir, feedstock)
        try:
            _run_git_command(
                "clone", "--depth=1",
                feedstock_url, fspath,
            )
        except subprocess.CalledProcessError:
            LOGGER.info("    could not clone the feedstock")
            return True

        try:
            _run_smithy_command("generate-feedstock-token", cwd=fspath)
        except subprocess.CalledProcessError:
            LOGGER.info("    could not generate feedstock token")
            return True

        try:
            _run_smithy_command("register-feedstock-token", cwd=fspath)
        except subprocess.CalledProcessError:
            LOGGER.info("    could not register feedstock token")
            return True
    finally:
        if tmpdir is not None:
            shutil.rmtree(tmpdir)

        # remove both paths due to change in smithy
        try:
            if feedstock.endswith("-feedstock"):
                feedstock_name = feedstock[:-len("-feedstock")]
            else:
                feedstock_name = feedstock
            token_path = os.path.expanduser(
                "~/.conda-smithy/conda-forge_%s_feedstock.token" % feedstock_name
            )
            os.remove(token_path)
        except Exception:
            pass

        try:
            token_path = os.path.expanduser(
                "~/.conda-smithy/conda-forge_%s.token" % feedstock)
            os.remove(token_path)
        except Exception:
            pass

    return False