Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/gradle/org.jetbrains.kotlin-kot…
Browse files Browse the repository at this point in the history
…lin-gradle-plugin-1.7.10
  • Loading branch information
larsgrefer committed Jul 9, 2022
2 parents 5c84b7c + 8218b2f commit ae198bb
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 29 deletions.
@@ -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<Project> {

Expand Down Expand Up @@ -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<UploadSnapshotResponse> stringCall = githubService.uploadDependencySnapshot(basePlugin.getGithubExtension().getOwner().get(), basePlugin.getGithubExtension().getRepo().get(), snapshot);

Response<UploadSnapshotResponse> execute = stringCall.execute();
t.getLogger().lifecycle(execute.body().getMessage());
} catch (IOException e) {
throw new RuntimeException(e);
}
TaskProvider<UploadSnapshotTask> 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());
});
}

Expand Down
@@ -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<String> getOwner();

@Input
public abstract Property<String> 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<UploadSnapshotResponse> stringCall = githubService.uploadDependencySnapshot(getOwner().get(), getRepo().get(), snapshot);

Response<UploadSnapshotResponse> 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);
}
}
}

0 comments on commit ae198bb

Please sign in to comment.