|
| 1 | +/* |
| 2 | + * Copyright 2002-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.gradle.github.release; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.nio.file.Paths; |
| 23 | + |
| 24 | +import org.gradle.api.Action; |
| 25 | +import org.gradle.api.DefaultTask; |
| 26 | +import org.gradle.api.Project; |
| 27 | +import org.gradle.api.tasks.Input; |
| 28 | +import org.gradle.api.tasks.Optional; |
| 29 | +import org.gradle.api.tasks.TaskAction; |
| 30 | + |
| 31 | +import org.springframework.gradle.github.RepositoryRef; |
| 32 | +import org.springframework.gradle.github.changelog.GitHubChangelogPlugin; |
| 33 | + |
| 34 | +/** |
| 35 | + * @author Steve Riesenberg |
| 36 | + */ |
| 37 | +public class CreateGitHubReleaseTask extends DefaultTask { |
| 38 | + @Input |
| 39 | + private RepositoryRef repository = new RepositoryRef(); |
| 40 | + |
| 41 | + @Input @Optional |
| 42 | + private String gitHubAccessToken; |
| 43 | + |
| 44 | + @Input |
| 45 | + private String version; |
| 46 | + |
| 47 | + @Input @Optional |
| 48 | + private String branch = "main"; |
| 49 | + |
| 50 | + @Input |
| 51 | + private boolean createRelease = false; |
| 52 | + |
| 53 | + @TaskAction |
| 54 | + public void createGitHubRelease() { |
| 55 | + String body = readReleaseNotes(); |
| 56 | + Release release = Release.tag(this.version) |
| 57 | + .commit(this.branch) |
| 58 | + .name(this.version) |
| 59 | + .body(body) |
| 60 | + .preRelease(this.version.contains("-")) |
| 61 | + .build(); |
| 62 | + |
| 63 | + System.out.printf("%sCreating GitHub release for %s/%s@%s\n", |
| 64 | + this.createRelease ? "" : "[DRY RUN] ", |
| 65 | + this.repository.getOwner(), |
| 66 | + this.repository.getName(), |
| 67 | + this.version |
| 68 | + ); |
| 69 | + System.out.printf(" Release Notes:\n\n----\n%s\n----\n\n", body.trim()); |
| 70 | + |
| 71 | + if (this.createRelease) { |
| 72 | + GitHubReleaseApi github = new GitHubReleaseApi(this.gitHubAccessToken); |
| 73 | + github.publishRelease(this.repository, release); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private String readReleaseNotes() { |
| 78 | + Project project = getProject(); |
| 79 | + File inputFile = project.file(Paths.get(project.getBuildDir().getPath(), GitHubChangelogPlugin.RELEASE_NOTES_PATH)); |
| 80 | + try { |
| 81 | + return Files.readString(inputFile.toPath()); |
| 82 | + } catch (IOException ex) { |
| 83 | + throw new RuntimeException("Unable to read release notes from " + inputFile, ex); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public RepositoryRef getRepository() { |
| 88 | + return repository; |
| 89 | + } |
| 90 | + |
| 91 | + public void repository(Action<RepositoryRef> repository) { |
| 92 | + repository.execute(this.repository); |
| 93 | + } |
| 94 | + |
| 95 | + public void setRepository(RepositoryRef repository) { |
| 96 | + this.repository = repository; |
| 97 | + } |
| 98 | + |
| 99 | + public String getGitHubAccessToken() { |
| 100 | + return gitHubAccessToken; |
| 101 | + } |
| 102 | + |
| 103 | + public void setGitHubAccessToken(String gitHubAccessToken) { |
| 104 | + this.gitHubAccessToken = gitHubAccessToken; |
| 105 | + } |
| 106 | + |
| 107 | + public String getVersion() { |
| 108 | + return version; |
| 109 | + } |
| 110 | + |
| 111 | + public void setVersion(String version) { |
| 112 | + this.version = version; |
| 113 | + } |
| 114 | + |
| 115 | + public String getBranch() { |
| 116 | + return branch; |
| 117 | + } |
| 118 | + |
| 119 | + public void setBranch(String branch) { |
| 120 | + this.branch = branch; |
| 121 | + } |
| 122 | + |
| 123 | + public boolean isCreateRelease() { |
| 124 | + return createRelease; |
| 125 | + } |
| 126 | + |
| 127 | + public void setCreateRelease(boolean createRelease) { |
| 128 | + this.createRelease = createRelease; |
| 129 | + } |
| 130 | +} |
0 commit comments