Search in sources :

Example 1 with GitOriginPlugin

use of org.shipkit.internal.gradle.git.GitOriginPlugin in project shipkit by mockito.

the class UpgradeDependencyPlugin method apply.

@Override
public void apply(final Project project) {
    IncubatingWarning.warn("upgrade-dependency plugin");
    final ShipkitConfiguration conf = project.getPlugins().apply(ShipkitConfigurationPlugin.class).getConfiguration();
    final GitOriginPlugin gitOriginPlugin = project.getRootProject().getPlugins().apply(GitOriginPlugin.class);
    project.getPlugins().apply(GitConfigPlugin.class);
    upgradeDependencyExtension = project.getExtensions().create("upgradeDependency", UpgradeDependencyExtension.class);
    // set defaults
    upgradeDependencyExtension.setBuildFile(project.file("build.gradle"));
    upgradeDependencyExtension.setBaseBranch("master");
    String dependency = (String) project.findProperty(DEPENDENCY_PROJECT_PROPERTY);
    new DependencyNewVersionParser(dependency).fillVersionUpgradeExtension(upgradeDependencyExtension);
    TaskMaker.task(project, CHECKOUT_BASE_BRANCH, GitCheckOutTask.class, new Action<GitCheckOutTask>() {

        @Override
        public void execute(final GitCheckOutTask task) {
            task.setDescription("Checks out the base branch.");
            deferredConfiguration(project, new Runnable() {

                @Override
                public void run() {
                    task.setRev(upgradeDependencyExtension.getBaseBranch());
                }
            });
        }
    });
    TaskMaker.task(project, PULL_UPSTREAM, GitPullTask.class, new Action<GitPullTask>() {

        @Override
        public void execute(final GitPullTask task) {
            task.setDescription("Performs git pull from upstream repository.");
            task.mustRunAfter(CHECKOUT_BASE_BRANCH);
            task.setDryRun(conf.isDryRun());
            deferredConfiguration(project, new Runnable() {

                @Override
                public void run() {
                    task.setRev(upgradeDependencyExtension.getBaseBranch());
                }
            });
            gitOriginPlugin.provideOriginRepo(task, new Action<String>() {

                public void execute(String originRepo) {
                    GitUrlInfo info = new GitUrlInfo(conf);
                    task.setUrl(info.getGitUrl());
                    task.setSecretValue(info.getWriteToken());
                }
            });
        }
    });
    final FindOpenPullRequestTask findOpenPullRequestTask = TaskMaker.task(project, FIND_OPEN_PULL_REQUEST, FindOpenPullRequestTask.class, new Action<FindOpenPullRequestTask>() {

        @Override
        public void execute(final FindOpenPullRequestTask task) {
            task.setDescription("Find an open pull request with version upgrade, if such exists.");
            task.mustRunAfter(PULL_UPSTREAM);
            task.setGitHubApiUrl(conf.getGitHub().getApiUrl());
            task.setAuthToken(conf.getLenient().getGitHub().getReadOnlyAuthToken());
            task.setUpstreamRepositoryName(conf.getGitHub().getRepository());
            task.setVersionBranchRegex(getVersionBranchName(upgradeDependencyExtension.getDependencyName(), ReplaceVersionTask.VERSION_REGEX));
        }
    });
    TaskMaker.task(project, CHECKOUT_VERSION_BRANCH, GitCheckOutTask.class, new Action<GitCheckOutTask>() {

        public void execute(final GitCheckOutTask task) {
            task.setDescription("Creates a new version branch and checks it out.");
            task.mustRunAfter(FIND_OPEN_PULL_REQUEST);
            findOpenPullRequestTask.provideOpenPullRequest(task, new Action<Optional<PullRequest>>() {

                @Override
                public void execute(Optional<PullRequest> pullRequest) {
                    if (pullRequest.isPresent()) {
                        // don't create a new branch if there is already a branch with open pull request with version upgrade
                        task.setNewBranch(false);
                    } else {
                        task.setNewBranch(true);
                    }
                    task.setRev(getCurrentVersionBranchName(upgradeDependencyExtension.getDependencyName(), upgradeDependencyExtension.getNewVersion(), pullRequest));
                }
            });
        }
    });
    final ReplaceVersionTask replaceVersionTask = TaskMaker.task(project, REPLACE_VERSION, ReplaceVersionTask.class, new Action<ReplaceVersionTask>() {

        @Override
        public void execute(final ReplaceVersionTask task) {
            task.setDescription("Replaces dependency version in build file.");
            task.mustRunAfter(CHECKOUT_VERSION_BRANCH);
            deferredConfiguration(project, new Runnable() {

                @Override
                public void run() {
                    task.setDependencyGroup(upgradeDependencyExtension.getDependencyGroup());
                    task.setNewVersion(upgradeDependencyExtension.getNewVersion());
                    task.setDependencyName(upgradeDependencyExtension.getDependencyName());
                    task.setBuildFile(upgradeDependencyExtension.getBuildFile());
                }
            });
        }
    });
    final ShipkitExecTask shipkitExecTask = TaskMaker.task(project, COMMIT_VERSION_UPGRADE, ShipkitExecTask.class, new Action<ShipkitExecTask>() {

        @Override
        public void execute(final ShipkitExecTask exec) {
            exec.setDescription("Commits updated build file.");
            exec.mustRunAfter(REPLACE_VERSION);
            exec.dependsOn(GitConfigPlugin.SET_EMAIL_TASK, GitConfigPlugin.SET_USER_TASK);
            deferredConfiguration(project, new Runnable() {

                @Override
                public void run() {
                    String message = String.format("%s version upgraded to %s", upgradeDependencyExtension.getDependencyName(), upgradeDependencyExtension.getNewVersion());
                    exec.execCommand(execCommand("Committing build file", "git", "commit", "--author", GitUtil.getGitGenericUserNotation(conf.getGit().getUser(), conf.getGit().getEmail()), "-m", message, upgradeDependencyExtension.getBuildFile().getAbsolutePath()));
                }
            });
            exec.onlyIf(wasBuildFileUpdatedSpec(replaceVersionTask));
        }
    });
    final GitPushTask gitPushTask = TaskMaker.task(project, PUSH_VERSION_UPGRADE, GitPushTask.class, new Action<GitPushTask>() {

        @Override
        public void execute(final GitPushTask task) {
            task.setDescription("Pushes updated config file to an update branch.");
            task.mustRunAfter(COMMIT_VERSION_UPGRADE);
            task.setDryRun(conf.isDryRun());
            gitOriginPlugin.provideOriginRepo(task, new Action<String>() {

                public void execute(String originRepo) {
                    GitUrlInfo info = new GitUrlInfo(conf);
                    task.setUrl(info.getGitUrl());
                    task.setSecretValue(info.getWriteToken());
                }
            });
            findOpenPullRequestTask.provideOpenPullRequest(task, new Action<Optional<PullRequest>>() {

                @Override
                public void execute(Optional<PullRequest> pullRequest) {
                    task.getTargets().add(getCurrentVersionBranchName(upgradeDependencyExtension.getDependencyName(), upgradeDependencyExtension.getNewVersion(), pullRequest));
                }
            });
            task.onlyIf(wasBuildFileUpdatedSpec(replaceVersionTask));
        }
    });
    final CreatePullRequestTask createPullRequestTask = TaskMaker.task(project, CREATE_PULL_REQUEST, CreatePullRequestTask.class, new Action<CreatePullRequestTask>() {

        @Override
        public void execute(final CreatePullRequestTask task) {
            task.setDescription("Creates a pull request from branch with version upgraded to master");
            task.mustRunAfter(PUSH_VERSION_UPGRADE);
            task.setGitHubApiUrl(conf.getGitHub().getApiUrl());
            task.setDryRun(conf.isDryRun());
            task.setAuthToken(conf.getLenient().getGitHub().getWriteAuthToken());
            task.setPullRequestTitle(getPullRequestTitle(upgradeDependencyExtension));
            task.setPullRequestDescription(getPullRequestDescription(upgradeDependencyExtension));
            task.setUpstreamRepositoryName(conf.getGitHub().getRepository());
            gitOriginPlugin.provideOriginRepo(task, new Action<String>() {

                @Override
                public void execute(String originRepoName) {
                    task.setForkRepositoryName(originRepoName);
                }
            });
            findOpenPullRequestTask.provideOpenPullRequest(task, new Action<Optional<PullRequest>>() {

                @Override
                public void execute(Optional<PullRequest> pullRequest) {
                    task.setVersionBranch(getCurrentVersionBranchName(upgradeDependencyExtension.getDependencyName(), upgradeDependencyExtension.getNewVersion(), pullRequest));
                }
            });
            deferredConfiguration(project, new Runnable() {

                @Override
                public void run() {
                    task.setBaseBranch(upgradeDependencyExtension.getBaseBranch());
                }
            });
            task.onlyIf(wasOpenPullRequestNotFound(findOpenPullRequestTask));
            task.onlyIf(wasBuildFileUpdatedSpec(replaceVersionTask));
        }
    });
    TaskMaker.task(project, MERGE_PULL_REQUEST, MergePullRequestTask.class, new Action<MergePullRequestTask>() {

        @Override
        public void execute(final MergePullRequestTask task) {
            task.setDescription("Merge pull request when all checks will be passed");
            task.mustRunAfter(CREATE_PULL_REQUEST);
            task.setGitHubApiUrl(conf.getGitHub().getApiUrl());
            task.setDryRun(conf.isDryRun());
            task.setAuthToken(conf.getLenient().getGitHub().getWriteAuthToken());
            task.setBaseBranch(upgradeDependencyExtension.getBaseBranch());
            task.setUpstreamRepositoryName(conf.getGitHub().getRepository());
            gitOriginPlugin.provideOriginRepo(task, new Action<String>() {

                @Override
                public void execute(String originRepoName) {
                    task.setForkRepositoryName(originRepoName);
                }
            });
            createPullRequestTask.provideCreatedPullRequest(task, new Action<PullRequest>() {

                @Override
                public void execute(PullRequest pullRequest) {
                    setPullRequestDataToTask(Optional.ofNullable(pullRequest), task);
                }
            });
            deferredConfiguration(project, new Runnable() {

                @Override
                public void run() {
                    task.setBaseBranch(upgradeDependencyExtension.getBaseBranch());
                }
            });
            task.onlyIf(wasOpenPullRequestNotFound(findOpenPullRequestTask));
            task.onlyIf(wasBuildFileUpdatedSpec(replaceVersionTask));
        }
    });
    TaskMaker.task(project, PERFORM_VERSION_UPGRADE, new Action<Task>() {

        @Override
        public void execute(Task task) {
            task.setDescription("Checkouts new version branch, updates Shipkit dependency in config file, commits and pushes.");
            task.dependsOn(CHECKOUT_BASE_BRANCH);
            task.dependsOn(FIND_OPEN_PULL_REQUEST);
            task.dependsOn(PULL_UPSTREAM);
            task.dependsOn(CHECKOUT_VERSION_BRANCH);
            task.dependsOn(REPLACE_VERSION);
            task.dependsOn(COMMIT_VERSION_UPGRADE);
            task.dependsOn(PUSH_VERSION_UPGRADE);
            task.dependsOn(CREATE_PULL_REQUEST);
            task.dependsOn(MERGE_PULL_REQUEST);
        }
    });
    addLazyDependencyValidation(dependency, createPullRequestTask, gitPushTask, findOpenPullRequestTask, replaceVersionTask, shipkitExecTask);
}
Also used : Action(org.gradle.api.Action) Task(org.gradle.api.Task) GitCheckOutTask(org.shipkit.internal.gradle.git.tasks.GitCheckOutTask) GitPushTask(org.shipkit.gradle.git.GitPushTask) ShipkitExecTask(org.shipkit.gradle.exec.ShipkitExecTask) GitPullTask(org.shipkit.internal.gradle.git.tasks.GitPullTask) ShipkitConfiguration(org.shipkit.gradle.configuration.ShipkitConfiguration) PullRequest(org.shipkit.internal.gradle.git.domain.PullRequest) GitPushTask(org.shipkit.gradle.git.GitPushTask) ShipkitConfigurationPlugin(org.shipkit.internal.gradle.configuration.ShipkitConfigurationPlugin) Optional(java.util.Optional) GitUrlInfo(org.shipkit.internal.gradle.git.GitUrlInfo) ShipkitExecTask(org.shipkit.gradle.exec.ShipkitExecTask) GitOriginPlugin(org.shipkit.internal.gradle.git.GitOriginPlugin) GitPullTask(org.shipkit.internal.gradle.git.tasks.GitPullTask) GitCheckOutTask(org.shipkit.internal.gradle.git.tasks.GitCheckOutTask)

Example 2 with GitOriginPlugin

use of org.shipkit.internal.gradle.git.GitOriginPlugin in project shipkit by mockito.

the class InitPlugin method apply.

@Override
public void apply(final Project project) {
    project.getPlugins().apply(VersioningPlugin.class);
    project.getPlugins().apply(ShipkitConfigurationPlugin.class);
    final GitOriginPlugin gitOriginPlugin = project.getRootProject().getPlugins().apply(GitOriginPlugin.class);
    TaskMaker.task(project, INIT_TRAVIS_TASK, InitTravisTask.class, new Action<InitTravisTask>() {

        public void execute(InitTravisTask t) {
            t.setDescription("Creates '.travis.yml' file if not already present.");
            t.setOutputFile(new File(project.getRootDir(), ".travis.yml"));
        }
    });
    final BumpVersionFileTask bump = (BumpVersionFileTask) project.getTasks().getByName(VersioningPlugin.BUMP_VERSION_FILE_TASK);
    TaskMaker.task(project, INIT_VERSIONING_TASK, InitVersioningTask.class, new Action<InitVersioningTask>() {

        @Override
        public void execute(InitVersioningTask t) {
            t.setDescription("Creates version.properties file if it doesn't exist");
            t.setVersionFile(bump.getVersionFile());
        }
    });
    TaskMaker.task(project, INIT_SHIPKIT_FILE_TASK, InitShipkitFileTask.class, new Action<InitShipkitFileTask>() {

        @Override
        public void execute(final InitShipkitFileTask t) {
            final File shipkitFile = ShipkitConfigurationPlugin.getShipkitFile(project);
            t.setDescription("Creates Shipkit configuration file unless it already exists");
            t.setShipkitFile(shipkitFile);
            gitOriginPlugin.provideOriginRepo(t, new Action<String>() {

                public void execute(String originRepo) {
                    t.setOriginRepoName(originRepo);
                }
            });
        }
    });
    TaskMaker.task(project, INIT_SHIPKIT_TASK, new Action<Task>() {

        public void execute(Task t) {
            t.setDescription("Initializes Shipkit");
            t.dependsOn(INIT_TRAVIS_TASK, INIT_VERSIONING_TASK, INIT_SHIPKIT_FILE_TASK);
            t.doLast(new Action<Task>() {

                @Override
                public void execute(Task task) {
                    LOG.lifecycle("  Initialization complete. Thank you for using Shipkit!\n" + "  Please review generated files before checking them in.\n" + "  Guide: https://github.com/mockito/shipkit/blob/master/docs/getting-started.md");
                }
            });
        }
    });
    // The user might configure the 'group' later, after the plugin is applied
    DeferredConfiguration.deferredConfiguration(project, new Runnable() {

        @Override
        public void run() {
            if (project.getGroup() == null) {
                LOG.info("  Gradle project does not have 'group' property configured yet." + "\n  Shipkit is setting the group to 'org.shipkit.bootstrap' so that it has a reasonable default." + "\n  It is recommended that you configure the 'group' to your choice.");
                project.setGroup("org.shipkit.bootstrap");
            }
        }
    });
}
Also used : Action(org.gradle.api.Action) InitTravisTask(org.shipkit.gradle.init.InitTravisTask) Task(org.gradle.api.Task) InitVersioningTask(org.shipkit.gradle.init.InitVersioningTask) InitShipkitFileTask(org.shipkit.gradle.init.InitShipkitFileTask) BumpVersionFileTask(org.shipkit.gradle.version.BumpVersionFileTask) InitTravisTask(org.shipkit.gradle.init.InitTravisTask) BumpVersionFileTask(org.shipkit.gradle.version.BumpVersionFileTask) GitOriginPlugin(org.shipkit.internal.gradle.git.GitOriginPlugin) InitShipkitFileTask(org.shipkit.gradle.init.InitShipkitFileTask) InitVersioningTask(org.shipkit.gradle.init.InitVersioningTask) File(java.io.File)

Aggregations

Action (org.gradle.api.Action)2 Task (org.gradle.api.Task)2 GitOriginPlugin (org.shipkit.internal.gradle.git.GitOriginPlugin)2 File (java.io.File)1 Optional (java.util.Optional)1 ShipkitConfiguration (org.shipkit.gradle.configuration.ShipkitConfiguration)1 ShipkitExecTask (org.shipkit.gradle.exec.ShipkitExecTask)1 GitPushTask (org.shipkit.gradle.git.GitPushTask)1 InitShipkitFileTask (org.shipkit.gradle.init.InitShipkitFileTask)1 InitTravisTask (org.shipkit.gradle.init.InitTravisTask)1 InitVersioningTask (org.shipkit.gradle.init.InitVersioningTask)1 BumpVersionFileTask (org.shipkit.gradle.version.BumpVersionFileTask)1 ShipkitConfigurationPlugin (org.shipkit.internal.gradle.configuration.ShipkitConfigurationPlugin)1 GitUrlInfo (org.shipkit.internal.gradle.git.GitUrlInfo)1 PullRequest (org.shipkit.internal.gradle.git.domain.PullRequest)1 GitCheckOutTask (org.shipkit.internal.gradle.git.tasks.GitCheckOutTask)1 GitPullTask (org.shipkit.internal.gradle.git.tasks.GitPullTask)1