Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into dependabot/gradle/org.jetbrains.kotlin-kot…
…lin-gradle-plugin-api-1.7.10
- Loading branch information
Showing
2 changed files
with
75 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...ugin/src/main/java/io/freefair/gradle/plugins/github/dependencies/UploadSnapshotTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |