diff --git a/github-plugin/src/main/java/io/freefair/gradle/plugins/github/dependencies/DependencySubmissionPlugin.java b/github-plugin/src/main/java/io/freefair/gradle/plugins/github/dependencies/DependencySubmissionPlugin.java index 8444740c..c7742f36 100644 --- a/github-plugin/src/main/java/io/freefair/gradle/plugins/github/dependencies/DependencySubmissionPlugin.java +++ b/github-plugin/src/main/java/io/freefair/gradle/plugins/github/dependencies/DependencySubmissionPlugin.java @@ -1,19 +1,10 @@ package io.freefair.gradle.plugins.github.dependencies; -import com.google.gson.Gson; import io.freefair.gradle.plugins.github.GithubBasePlugin; -import io.freefair.gradle.plugins.github.internal.GithubService; -import io.freefair.gradle.plugins.github.internal.Snapshot; -import io.freefair.gradle.plugins.github.internal.UploadSnapshotResponse; import io.freefair.gradle.util.GitUtil; import org.gradle.api.Plugin; import org.gradle.api.Project; import org.gradle.api.tasks.TaskProvider; -import retrofit2.Call; -import retrofit2.Response; - -import java.io.FileReader; -import java.io.IOException; public class DependencySubmissionPlugin implements Plugin { @@ -58,27 +49,12 @@ else if (GitUtil.isCircleCi()) { project.allprojects(this::configureProject); - project.getTasks().register("uploadGithubDependenciesSnapshot", t -> { - t.dependsOn(githubDependencySnapshot); - t.getInputs().file(githubDependencySnapshot.map(DependencySnapshotTask::getOutputFile)); - t.doLast(t2 -> { - GithubService githubService = basePlugin.getGithubClient().getGithubService(); - - try (FileReader fileReader = new FileReader(githubDependencySnapshot.get().getOutputFile().getAsFile().get())) { - Snapshot snapshot = new Gson().fromJson(fileReader, Snapshot.class); - t.getLogger().lifecycle("Job: {}", snapshot.getJob()); - t.getLogger().lifecycle("Ref: {}, Sha: {}", snapshot.getRef(), snapshot.getSha()); - t.getLogger().info("Detector: {}", snapshot.getDetector()); - - Call stringCall = githubService.uploadDependencySnapshot(basePlugin.getGithubExtension().getOwner().get(), basePlugin.getGithubExtension().getRepo().get(), snapshot); - - Response execute = stringCall.execute(); - t.getLogger().lifecycle(execute.body().getMessage()); - } catch (IOException e) { - throw new RuntimeException(e); - } + TaskProvider uploadGithubDependenciesSnapshot = project.getTasks().register("uploadGithubDependenciesSnapshot", UploadSnapshotTask.class, basePlugin.getGithubClient()); + uploadGithubDependenciesSnapshot.configure(t -> { + t.getSnapshotFile().set(githubDependencySnapshot.flatMap(DependencySnapshotTask::getOutputFile)); - }); + t.getOwner().convention(basePlugin.getGithubExtension().getOwner()); + t.getRepo().convention(basePlugin.getGithubExtension().getRepo()); }); } diff --git a/github-plugin/src/main/java/io/freefair/gradle/plugins/github/dependencies/UploadSnapshotTask.java b/github-plugin/src/main/java/io/freefair/gradle/plugins/github/dependencies/UploadSnapshotTask.java new file mode 100644 index 00000000..bbd10561 --- /dev/null +++ b/github-plugin/src/main/java/io/freefair/gradle/plugins/github/dependencies/UploadSnapshotTask.java @@ -0,0 +1,70 @@ +package io.freefair.gradle.plugins.github.dependencies; + + +import com.google.gson.Gson; +import io.freefair.gradle.plugins.github.internal.GithubClient; +import io.freefair.gradle.plugins.github.internal.GithubService; +import io.freefair.gradle.plugins.github.internal.Snapshot; +import io.freefair.gradle.plugins.github.internal.UploadSnapshotResponse; +import org.gradle.api.DefaultTask; +import org.gradle.api.file.RegularFileProperty; +import org.gradle.api.provider.Property; +import org.gradle.api.tasks.Input; +import org.gradle.api.tasks.InputFile; +import org.gradle.api.tasks.TaskAction; +import retrofit2.Call; +import retrofit2.Response; + +import javax.inject.Inject; +import java.io.FileReader; +import java.io.IOException; + +public abstract class UploadSnapshotTask extends DefaultTask { + + private final GithubClient githubClient; + + @InputFile + public abstract RegularFileProperty getSnapshotFile(); + + @Input + public abstract Property getOwner(); + + @Input + public abstract Property getRepo(); + + @Inject + public UploadSnapshotTask(GithubClient githubClient) { + this.githubClient = githubClient; + } + + @TaskAction + public void upload() { + GithubService githubService = githubClient.getGithubService(); + + try (FileReader fileReader = new FileReader(getSnapshotFile().getAsFile().get())) { + Gson gson = new Gson(); + Snapshot snapshot = gson.fromJson(fileReader, Snapshot.class); + getLogger().lifecycle("Job: {}", snapshot.getJob()); + getLogger().lifecycle("Ref: {}, Sha: {}", snapshot.getRef(), snapshot.getSha()); + getLogger().info("Detector: {}", snapshot.getDetector()); + + Call stringCall = githubService.uploadDependencySnapshot(getOwner().get(), getRepo().get(), snapshot); + + Response execute = stringCall.execute(); + if (execute.isSuccessful()) { + getLogger().lifecycle(execute.body().getMessage()); + } + else if (execute.code() == 403) { + getLogger().warn("Upload Failed due to missing permissions"); + getLogger().info(execute.errorBody().string()); + } + else { + getLogger().error("{} {}", execute.code(), execute.message()); + getLogger().error(execute.errorBody().string()); + throw new RuntimeException("Upload Failed"); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } +}