Skip to content

Commit

Permalink
add GithubDeploymentTask
Browse files Browse the repository at this point in the history
  • Loading branch information
LCLPYT committed Jun 10, 2023
1 parent a4300b5 commit 64df07e
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 13 deletions.
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ plugins {

repositories {
mavenCentral()
gradlePluginPortal()
}

dependencies {
implementation("org.kohsuke:github-api:1.135")

testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.2")
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/work/lclpnet/build/ext/BuildUtilsExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

public interface BuildUtilsExtension {

String latestTag();

String gitVersion();

Property<String> getVersionPattern();
Expand Down
39 changes: 26 additions & 13 deletions src/main/java/work/lclpnet/build/ext/BuildUtilsExtensionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,29 @@ public class BuildUtilsExtensionImpl implements BuildUtilsExtension {
private final Project project;
private final Property<String> versionPattern;
private GitVersionResolver gitVersionResolver = null;
private String latestTag = null;

public BuildUtilsExtensionImpl(Project project) {
this.project = project;
this.versionPattern = project.getObjects().property(String.class).convention(GitVersionResolver.VERSION_PATTERN);
}

@Override
public String gitVersion() {
final String version;

public String latestTag() {
Map<String, String> env = System.getenv();

if (env.containsKey("CI_VERSION")) {
version = env.get("CI_VERSION");
} else {
File pwd = project.getProjectDir();
return env.get("CI_VERSION");
}

synchronized (this) {
if (gitVersionResolver == null) {
gitVersionResolver = new GitVersionResolver(project.getLogger());
}
}
if (latestTag != null) return latestTag;

version = gitVersionResolver.getGitVersion(pwd);
}
return fetchLatestTag();
}

@Override
public String gitVersion() {
final String version = latestTag();

String versionPattern = getVersionPattern().get();
if (!version.matches(versionPattern)) {
Expand Down Expand Up @@ -86,4 +85,18 @@ public Properties loadProperties(Object src) {
public Property<String> getVersionPattern() {
return versionPattern;
}

private synchronized String fetchLatestTag() {
if (latestTag != null) return latestTag;

File pwd = project.getProjectDir();

synchronized (this) {
if (gitVersionResolver == null) {
gitVersionResolver = new GitVersionResolver(project.getLogger());
}
}

return gitVersionResolver.getGitVersion(pwd);
}
}
109 changes: 109 additions & 0 deletions src/main/java/work/lclpnet/build/task/GithubDeploymentTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package work.lclpnet.build.task;

import org.gradle.api.Action;
import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.file.RegularFile;
import org.gradle.api.provider.SetProperty;
import org.gradle.api.publish.plugins.PublishingPlugin;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.TaskAction;
import org.kohsuke.github.GHRelease;
import org.kohsuke.github.GHReleaseBuilder;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;

import java.io.File;
import java.io.IOException;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.util.Objects;

public abstract class GithubDeploymentTask extends DefaultTask {

@InputFiles
public abstract SetProperty<RegularFile> getAssets();

private final Config config = new Config();
private final ReleaseConfig releaseConfig = new ReleaseConfig();

public GithubDeploymentTask() {
this.onlyIf(task -> config.token != null && config.repository != null);
this.setGroup(PublishingPlugin.PUBLISH_TASK_GROUP);
}

@TaskAction
public void deploy() throws IOException {
final GitHub github = GitHub.connectUsingOAuth(config.token);
final GHRepository repository = github.getRepository(config.repository);

final Project project = getProject();
final String version = project.getVersion().toString();

final GHReleaseBuilder releaseBuilder = new GHReleaseBuilder(repository, version);

releaseBuilder.name(Objects.requireNonNull(releaseConfig.title, "Release title is null"));

if (releaseConfig.description != null) {
releaseBuilder.body(releaseConfig.description);
}

if (releaseConfig.commitish != null) {
releaseBuilder.commitish(releaseConfig.commitish);
}

releaseBuilder.prerelease(releaseConfig.preRelease);

final GHRelease release = releaseBuilder.create();

FileNameMap fileNameMap = URLConnection.getFileNameMap();

for (RegularFile regularFile : getAssets().get()) {
File file = regularFile.getAsFile();

String contentType = fileNameMap.getContentTypeFor(file.getName());

if (contentType == null) {
contentType = "application/octet-stream"; // fallback content-type
}

release.uploadAsset(file, contentType);
}
}

public void release(Action<ReleaseConfig> action) {
action.execute(releaseConfig);
}

public void config(Action<Config> action) {
action.execute(config);
}

public static class ReleaseConfig {
public String tag = null;
public String title = null;
public String description = null;
private String commitish = null;
public boolean preRelease = false;

/**
* Configures to create a new tag on the specified branch with the creation of the release.
* @param name The name of the tag.
* @param branch The branch to create the tag on. If null, the repositories default branch is used.
*/
public void createNewTag(String name, String branch) {
tag = name;
commitish = branch;
}

private ReleaseConfig() {}
}

public static class Config {

public String token = null;
public String repository = null;

private Config() {}
}
}

0 comments on commit 64df07e

Please sign in to comment.