Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add api for online version update #1882

Closed
wants to merge 11 commits into from
5 changes: 5 additions & 0 deletions src/main/java/run/halo/app/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package run.halo.app;

import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import run.halo.app.utils.VmUtils;

/**
* Halo main class.
Expand All @@ -13,6 +15,9 @@
public class Application {

public static void main(String[] args) {
// Store the program args to construct launch command in version switch.
VmUtils.PROGRAM_ARGS.addAll(List.of(args));

// Customize the spring config location
System.setProperty("spring.config.additional-location",
"optional:file:${user.home}/.halo/,optional:file:${user.home}/halo-dev/");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package run.halo.app.controller.admin.api;

import io.swagger.annotations.ApiOperation;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import run.halo.app.model.dto.VersionInfoDTO;
import run.halo.app.service.HaloVersionCtrlService;

@Slf4j
@RestController
@RequestMapping("/api/admin/version")
public class VersionCtrlController {
@Autowired
HaloVersionCtrlService versionCtrlService;

@GetMapping("releases")
@ApiOperation("Lists all release info of halo jar")
List<VersionInfoDTO> getAllReleaseInfo() {
return versionCtrlService.getAllReleasesInfo();
}

@GetMapping("releases/latest")
@ApiOperation("Lists the latest release info of halo")
VersionInfoDTO getLatestReleaseInfo() {
return versionCtrlService.getLatestReleaseInfo();
}

@GetMapping("releases/tags/{tagName:.+}")
@ApiOperation("Lists the release info with specified version")
VersionInfoDTO getReleaseInfo(@PathVariable(name = "tagName") String tagName) {
return versionCtrlService.getReleaseInfoByTag(tagName);
}

@GetMapping("download/latest")
@ApiOperation("Downloads the latest release jar")
String downloadLatest() {
versionCtrlService.downloadLatestJar();
return "success";
}

@GetMapping("download/tags/{tagName:.+}")
@ApiOperation("Downloads the specified jar")
String download(@PathVariable(name = "tagName") String tagName) {
versionCtrlService.downloadSpecifiedJarToRepo(tagName);
return "success";
}

@GetMapping("switch/latest")
@ApiOperation("Switch halo to latest version")
String switchLatestVersion() {
versionCtrlService.switchLatest();
return "success";
}

@GetMapping("switch/tags/{tagName:.+}")
@ApiOperation("Switch halo to specified version")
String switchVersion(@PathVariable(name = "tagName") String tagName) {
versionCtrlService.switchVersion(tagName);
return "success";
}

@GetMapping("downloadswitch/latest")
@ApiOperation("Downloads latest version to local and switch halo to it")
String downloadSwitchLatestVersion() {
versionCtrlService.downloadAndSwitchLatest();
return "success";
}

@GetMapping("downloadswitch/tags/{tagName:.+}")
@ApiOperation("Downloads specified version to local and switch halo to it")
String downLoadSwitchVersion(@PathVariable(name = "tagName") String tagName) {
versionCtrlService.downloadAndSwitch(tagName);
return "success";
}


}
41 changes: 41 additions & 0 deletions src/main/java/run/halo/app/model/dto/VersionInfoDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package run.halo.app.model.dto;

import lombok.Builder;
import lombok.Data;
import lombok.ToString;
import run.halo.app.model.entity.Assets;
import run.halo.app.model.entity.GithubApiVersionJson;

/**
* Version information of a release.
*
* <p>This is a simplified representation of
* {@linkplain run.halo.app.model.entity.GithubApiVersionJson}.
*
* @author Chen_Kunqiu
*/
@Data
@ToString
@Builder
public class VersionInfoDTO {
private String version;
private String jarName;
private String desc;
private String githubUrl;
private String downloadUrl;
private Boolean inLocal;
private Long size;

/**
* Initially convert the JSON given by Github into VO.
*
* @param json the json data given by github api
* @return the simplified VO object
*/
public static VersionInfoDTO convertFrom(GithubApiVersionJson json) {
final Assets asset = json.getAssets().get(0);
return VersionInfoDTO.builder().version(json.getTagName()).desc(json.getBody())
.githubUrl(json.getHtmlUrl()).jarName(asset.getName()).size(asset.getSize())
.downloadUrl(asset.getBrowserDownloadUrl()).build();
}
}
34 changes: 34 additions & 0 deletions src/main/java/run/halo/app/model/entity/Assets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

package run.halo.app.model.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Date;
import lombok.Data;

/**
* The Java class relevant to the Json returned by github api.
*
* @author Chen_Kunqiu
*/
@Data
public class Assets {
private String url;
private int id;
@JsonProperty("node_id")
private String nodeId;
private String name;
private String label;
private Uploader uploader;
@JsonProperty("content_type")
private String contentType;
private String state;
private long size;
@JsonProperty("download_count")
private int downloadCount;
@JsonProperty("created_at")
private Date createdAt;
@JsonProperty("updated_at")
private Date updatedAt;
@JsonProperty("browser_download_url")
private String browserDownloadUrl;
}
45 changes: 45 additions & 0 deletions src/main/java/run/halo/app/model/entity/Author.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package run.halo.app.model.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

/**
* The Java class relevant to the Json returned by github api.
*
* @author Chen_Kunqiu
*/
@Data
public class Author {
private String login;
private int id;
@JsonProperty("node_id")
private String nodeId;
@JsonProperty("avatar_url")
private String avatarUrl;
@JsonProperty("gravatar_id")
private String gravatarId;
private String url;
@JsonProperty("html_url")
private String htmlUrl;
@JsonProperty("followers_url")
private String followersUrl;
@JsonProperty("following_url")
private String followingUrl;
@JsonProperty("gists_url")
private String gistsUrl;
@JsonProperty("starred_url")
private String starredUrl;
@JsonProperty("subscriptions_url")
private String subscriptionsUrl;
@JsonProperty("organizations_url")
private String organizationsUrl;
@JsonProperty("repos_url")
private String reposUrl;
@JsonProperty("events_url")
private String eventsUrl;
@JsonProperty("received_events_url")
private String receivedEventsUrl;
private String type;
@JsonProperty("site_admin")
private boolean siteAdmin;
}
53 changes: 53 additions & 0 deletions src/main/java/run/halo/app/model/entity/GithubApiVersionJson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package run.halo.app.model.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Date;
import java.util.List;
import lombok.Data;
import lombok.ToString;

/**
* The Java class relevant to the Json returned by github api.
*
* <p>The json structure refers to the response of
* <a href="https://api.github.com/repos/halo-dev/halo/releases/latest">
* https://api.github.com/repos/halo-dev/halo/releases/latest
* </a>.
*
* @author Chen_Kunqiu
*/
@Data
@ToString
public class GithubApiVersionJson {
private String url;
@JsonProperty("assets_url")
private String assetsUrl;
@JsonProperty("upload_url")
private String uploadUrl;
@JsonProperty("html_url")
private String htmlUrl;
private int id;
private Author author;
@JsonProperty("node_id")
private String nodeId;
@JsonProperty("tag_name")
private String tagName;
@JsonProperty("target_commitish")
private String targetCommitish;
private String name;
private boolean draft;
private boolean prerelease;
@JsonProperty("created_at")
private Date createdAt;
@JsonProperty("published_at")
private Date publishedAt;
private List<Assets> assets;
@JsonProperty("tarball_url")
private String tarballUrl;
@JsonProperty("zipball_url")
private String zipballUrl;
private String body;
private Reactions reactions;
@JsonProperty("mentions_count")
private int mentionsCount;
}
26 changes: 26 additions & 0 deletions src/main/java/run/halo/app/model/entity/Reactions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package run.halo.app.model.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

/**
* The Java class relevant to the Json returned by github api.
*
* @author Chen_Kunqiu
*/
@Data
public class Reactions {
private String url;
@JsonProperty("total_count")
private int totalCount;
@JsonProperty("+1")
private int plusOne;
@JsonProperty("-1")
private int minusOne;
private int laugh;
private int hooray;
private int confused;
private int heart;
private int rocket;
private int eyes;
}
45 changes: 45 additions & 0 deletions src/main/java/run/halo/app/model/entity/Uploader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package run.halo.app.model.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

/**
* The Java class relevant to the Json returned by github api.
*
* @author Chen_Kunqiu
*/
@Data
public class Uploader {
private String login;
private int id;
@JsonProperty("node_id")
private String nodeId;
@JsonProperty("avatar_url")
private String avatarUrl;
@JsonProperty("gravatar_id")
private String gravatarId;
private String url;
@JsonProperty("html_url")
private String htmlUrl;
@JsonProperty("followers_url")
private String followersUrl;
@JsonProperty("following_url")
private String followingUrl;
@JsonProperty("gists_url")
private String gistsUrl;
@JsonProperty("starred_url")
private String starredUrl;
@JsonProperty("subscriptions_url")
private String subscriptionsUrl;
@JsonProperty("organizations_url")
private String organizationsUrl;
@JsonProperty("repos_url")
private String reposUrl;
@JsonProperty("events_url")
private String eventsUrl;
@JsonProperty("received_events_url")
private String receivedEventsUrl;
private String type;
@JsonProperty("site_admin")
private boolean siteAdmin;
}
10 changes: 10 additions & 0 deletions src/main/java/run/halo/app/model/enums/SystemType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package run.halo.app.model.enums;

/**
* Represents the type of operating system.
*
* @author Chen_Kunqiu
*/
public enum SystemType {
WINDOWS, LINUX, MACOS, ELSE
}
Loading