Skip to content

Commit

Permalink
Added latest tag option
Browse files Browse the repository at this point in the history
  • Loading branch information
faab007nl committed Oct 13, 2021
1 parent a71f52b commit 05aaa57
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
.gradle/
out/
build/
/config.json
/config.json
artifacts/
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ repositories {
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-web:2.5.5'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
compile group: 'commons-io', name: 'commons-io', version: '2.7'
compile 'com.jcraft:jsch:0.1.55'
implementation 'org.kohsuke:github-api:1.133'
}

jar {
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/me/TechsCode/ReleaseServer/WebRequestsHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.TechsCode.ReleaseServer;

import com.sun.javafx.scene.text.HitInfo;
import me.TechsCode.ReleaseServer.github.GitHubUtil;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
Expand All @@ -12,6 +14,7 @@
import java.net.MalformedURLException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -41,8 +44,17 @@ public Object download(@RequestParam(value = "token", required = false) String t
return "Could not find any artifact for project '"+project+"'";
}

if(tag.equals("latest")){
String ghToken = "";
if(artifactsOfProject.stream().findFirst().get().getRelease().getProject().getGithubToken().isPresent())
ghToken = artifactsOfProject.stream().findFirst().get().getRelease().getProject().getGithubToken().get();
String repo = artifactsOfProject.stream().findFirst().get().getRelease().getProject().getGithubRepository();
tag = Objects.requireNonNull(GitHubUtil.getLatestRelease(ghToken, repo)).getRelease().getTagName();
}

String finalTag = tag;
Optional<Artifact> artifact = artifactsOfProject.stream()
.filter(x -> x.getReleaseTag().equals(tag)).findFirst();
.filter(x -> x.getReleaseTag().equals(finalTag)).findFirst();

if(artifact.isPresent()){
if(artifact.get().getAssets().length == 0){
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/me/TechsCode/ReleaseServer/github/GitHubUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package me.TechsCode.ReleaseServer.github;

import org.kohsuke.github.*;

import java.io.IOException;
import java.util.Objects;

public class GitHubUtil {

private static GitHub github = null;

public static GithubRelease getLatestRelease(String token, String repo) {
try {
GHRepository ghrepo = Objects.requireNonNull(getGithub(token)).getRepository(repo);
GHRelease release = ghrepo.getLatestRelease();

if(release == null) return null;

return new GithubRelease(release);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

private static GitHub getGithub(String token) {
try {
if(github == null) github = new GitHubBuilder().withOAuthToken(token).build();
return github;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
30 changes: 30 additions & 0 deletions src/main/java/me/TechsCode/ReleaseServer/github/GithubRelease.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package me.TechsCode.ReleaseServer.github;

import org.kohsuke.github.GHAsset;
import org.kohsuke.github.GHRelease;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class GithubRelease {

private final GHRelease release;
private GHAsset asset = null;

public GithubRelease(GHRelease release) throws IOException {
List<GHAsset> releaseAsset = release.listAssets().toList();
if(releaseAsset.size() > 0) {
this.asset = releaseAsset.get(0);
}
this.release = release;
}

public GHRelease getRelease() {
return release;
}

public GHAsset getAsset() {
return asset;
}
}

0 comments on commit 05aaa57

Please sign in to comment.