Search in sources :

Example 1 with GHReleaseUpdater

use of org.kohsuke.github.GHReleaseUpdater in project jreleaser by jreleaser.

the class GithubReleaser method createRelease.

@Override
protected void createRelease() throws ReleaseException {
    org.jreleaser.model.Github github = context.getModel().getRelease().getGithub();
    context.getLogger().info(RB.$("git.releaser.releasing"), github.getResolvedRepoUrl(context.getModel()));
    String tagName = github.getEffectiveTagName(context.getModel());
    try {
        String branch = github.getBranch();
        List<String> branchNames = GitSdk.of(context).getRemoteBranches();
        if (!branchNames.contains(branch)) {
            throw new ReleaseException(RB.$("ERROR_git_release_branch_not_exists", branch, branchNames));
        }
        String changelog = context.getChangelog();
        Github api = new Github(context.getLogger(), github.getApiEndpoint(), github.getResolvedToken(), github.getConnectTimeout(), github.getReadTimeout());
        context.getLogger().debug(RB.$("git.releaser.release.lookup"), tagName, github.getCanonicalRepoName());
        GHRelease release = api.findReleaseByTag(github.getCanonicalRepoName(), tagName);
        boolean snapshot = context.getModel().getProject().isSnapshot();
        if (null != release) {
            context.getLogger().debug(RB.$("git.releaser.release.exists"), tagName);
            if (github.isOverwrite() || snapshot) {
                context.getLogger().debug(RB.$("git.releaser.release.delete"), tagName);
                if (!context.isDryrun()) {
                    release.delete();
                }
                context.getLogger().debug(RB.$("git.releaser.release.create"), tagName);
                createRelease(api, tagName, changelog, github.isMatch());
            } else if (github.getUpdate().isEnabled()) {
                context.getLogger().debug(RB.$("git.releaser.release.update"), tagName);
                if (!context.isDryrun()) {
                    GHReleaseUpdater updater = release.update();
                    updater.prerelease(github.getPrerelease().isEnabled());
                    updater.draft(github.isDraft());
                    if (github.getUpdate().getSections().contains(UpdateSection.TITLE)) {
                        context.getLogger().info(RB.$("git.releaser.release.update.title"), github.getEffectiveReleaseName());
                        updater.name(github.getEffectiveReleaseName());
                    }
                    if (github.getUpdate().getSections().contains(UpdateSection.BODY)) {
                        context.getLogger().info(RB.$("git.releaser.release.update.body"));
                        updater.body(changelog);
                    }
                    updater.update();
                    if (github.getUpdate().getSections().contains(UpdateSection.ASSETS)) {
                        api.uploadAssets(release, assets);
                    }
                    linkDiscussion(tagName, release);
                }
            } else {
                if (context.isDryrun()) {
                    context.getLogger().debug(RB.$("git.releaser.release.create"), tagName);
                    createRelease(api, tagName, changelog, false);
                    return;
                }
                throw new IllegalStateException(RB.$("ERROR_git_releaser_cannot_release", "GitHub", tagName));
            }
        } else {
            context.getLogger().debug(RB.$("git.releaser.release.not.found"), tagName);
            context.getLogger().debug(RB.$("git.releaser.release.create"), tagName);
            createRelease(api, tagName, changelog, snapshot && github.isMatch());
        }
    } catch (RestAPIException e) {
        context.getLogger().trace(e.getStatus() + " " + e.getReason());
        context.getLogger().trace(e);
        throw new ReleaseException(e);
    } catch (IOException | IllegalStateException e) {
        context.getLogger().trace(e);
        throw new ReleaseException(e);
    }
}
Also used : IOException(java.io.IOException) GHRelease(org.kohsuke.github.GHRelease) GHReleaseUpdater(org.kohsuke.github.GHReleaseUpdater) RestAPIException(org.jreleaser.sdk.commons.RestAPIException) ReleaseException(org.jreleaser.model.releaser.spi.ReleaseException)

Aggregations

IOException (java.io.IOException)1 ReleaseException (org.jreleaser.model.releaser.spi.ReleaseException)1 RestAPIException (org.jreleaser.sdk.commons.RestAPIException)1 GHRelease (org.kohsuke.github.GHRelease)1 GHReleaseUpdater (org.kohsuke.github.GHReleaseUpdater)1