Skip to content

Commit

Permalink
Merge branch 'main' into refactor/publishtime
Browse files Browse the repository at this point in the history
  • Loading branch information
ruibaby authored Oct 27, 2023
2 parents 59ef51a + 2dbfbd1 commit bfa9e38
Show file tree
Hide file tree
Showing 16 changed files with 327 additions and 87 deletions.
2 changes: 1 addition & 1 deletion application/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'org.springframework.boot' version '3.1.4'
id 'org.springframework.boot' version '3.1.5'
id 'io.spring.dependency-management' version '1.1.0'
id "com.gorylenko.gradle-git-properties" version "2.3.2"
id "checkstyle"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
import java.util.function.Predicate;
import java.util.function.Supplier;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.pf4j.PluginState;
import org.reactivestreams.Publisher;
import org.springdoc.webflux.core.fn.SpringdocRouteBuilder;
import org.springframework.beans.factory.DisposableBean;
Expand Down Expand Up @@ -201,6 +203,27 @@ public RouterFunction<ServerResponse> endpoint() {
.response(responseBuilder()
.implementation(Plugin.class))
)
.PUT("plugins/{name}/plugin-state", this::changePluginRunningState,
builder -> builder.operationId("ChangePluginRunningState")
.description("Change the running state of a plugin by name.")
.tag(tag)
.parameter(parameterBuilder()
.name("name")
.in(ParameterIn.PATH)
.required(true)
.implementation(String.class)
)
.requestBody(requestBodyBuilder()
.required(true)
.content(contentBuilder()
.mediaType(MediaType.APPLICATION_JSON_VALUE)
.schema(schemaBuilder()
.implementation(RunningStateRequest.class))
)
)
.response(responseBuilder()
.implementation(Plugin.class))
)
.GET("plugins", this::list, builder -> {
builder.operationId("ListPlugins")
.tag(tag)
Expand Down Expand Up @@ -255,6 +278,56 @@ public RouterFunction<ServerResponse> endpoint() {
.build();
}

Mono<ServerResponse> changePluginRunningState(ServerRequest request) {
final var name = request.pathVariable("name");
return request.bodyToMono(RunningStateRequest.class)
.flatMap(runningState -> {
final var enable = runningState.isEnable();
return client.get(Plugin.class, name)
.flatMap(plugin -> {
plugin.getSpec().setEnabled(enable);
return client.update(plugin);
})
.flatMap(plugin -> {
if (runningState.isAsync()) {
return Mono.just(plugin);
}
return waitForPluginToMeetExpectedState(name, p -> {
// when enabled = true,excepted phase = started || failed
// when enabled = false,excepted phase = !started
var phase = p.statusNonNull().getPhase();
if (enable) {
return PluginState.STARTED.equals(phase)
|| PluginState.FAILED.equals(phase);
}
return !PluginState.STARTED.equals(phase);
});
});
})
.flatMap(plugin -> ServerResponse.ok().bodyValue(plugin));
}

Mono<Plugin> waitForPluginToMeetExpectedState(String name, Predicate<Plugin> predicate) {
return Mono.defer(() -> client.get(Plugin.class, name)
.map(plugin -> {
if (predicate.test(plugin)) {
return plugin;
}
throw new IllegalStateException("Plugin " + name + " is not in expected state");
})
)
.retryWhen(Retry.backoff(10, Duration.ofMillis(100))
.filter(IllegalStateException.class::isInstance)
);
}

@Data
@Schema(name = "PluginRunningStateRequest")
static class RunningStateRequest {
private boolean enable;
private boolean async;
}

private Mono<ServerResponse> fetchJsBundle(ServerRequest request) {
Optional<String> versionOption = request.queryParam("v");
return versionOption.map(s ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Optional;
import java.util.Set;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.springframework.context.ApplicationEvent;
Expand Down Expand Up @@ -102,6 +103,10 @@ public Result reconcile(Request request) {
post.getMetadata().setAnnotations(annotations);
}

if (!annotations.containsKey(Post.PUBLISHED_LABEL)) {
labels.put(Post.PUBLISHED_LABEL, BooleanUtils.FALSE);
}

var status = post.getStatus();
if (status == null) {
status = new Post.PostStatus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ rules:
verbs: [ "create", "patch", "update", "delete", "deletecollection" ]
- apiGroups: [ "api.console.halo.run" ]
resources: [ "plugins/upgrade", "plugins/resetconfig", "plugins/config", "plugins/reload",
"plugins/install-from-uri", "plugins/upgrade-from-uri" ]
"plugins/install-from-uri", "plugins/upgrade-from-uri", "plugins/plugin-state" ]
verbs: [ "*" ]
- apiGroups: [ "api.console.halo.run" ]
resources: [ "plugin-presets" ]
Expand Down
1 change: 1 addition & 0 deletions console/packages/api-client/src/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ models/personal-access-token-list.ts
models/personal-access-token.ts
models/plugin-author.ts
models/plugin-list.ts
models/plugin-running-state-request.ts
models/plugin-spec.ts
models/plugin-status.ts
models/plugin.ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ import { Plugin } from "../models";
// @ts-ignore
import { PluginList } from "../models";
// @ts-ignore
import { PluginRunningStateRequest } from "../models";
// @ts-ignore
import { Setting } from "../models";
// @ts-ignore
import { UpgradeFromUriRequest } from "../models";
Expand All @@ -57,6 +59,75 @@ export const ApiConsoleHaloRunV1alpha1PluginApiAxiosParamCreator = function (
configuration?: Configuration
) {
return {
/**
* Change the running state of a plugin by name.
* @param {string} name
* @param {PluginRunningStateRequest} pluginRunningStateRequest
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
changePluginRunningState: async (
name: string,
pluginRunningStateRequest: PluginRunningStateRequest,
options: AxiosRequestConfig = {}
): Promise<RequestArgs> => {
// verify required parameter 'name' is not null or undefined
assertParamExists("changePluginRunningState", "name", name);
// verify required parameter 'pluginRunningStateRequest' is not null or undefined
assertParamExists(
"changePluginRunningState",
"pluginRunningStateRequest",
pluginRunningStateRequest
);
const localVarPath =
`/apis/api.console.halo.run/v1alpha1/plugins/{name}/plugin-state`.replace(
`{${"name"}}`,
encodeURIComponent(String(name))
);
// use dummy base URL string because the URL constructor only accepts absolute URLs.
const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
let baseOptions;
if (configuration) {
baseOptions = configuration.baseOptions;
}

const localVarRequestOptions = {
method: "PUT",
...baseOptions,
...options,
};
const localVarHeaderParameter = {} as any;
const localVarQueryParameter = {} as any;

// authentication BasicAuth required
// http basic authentication required
setBasicAuthToObject(localVarRequestOptions, configuration);

// authentication BearerAuth required
// http bearer authentication required
await setBearerAuthToObject(localVarHeaderParameter, configuration);

localVarHeaderParameter["Content-Type"] = "application/json";

setSearchParams(localVarUrlObj, localVarQueryParameter);
let headersFromBaseOptions =
baseOptions && baseOptions.headers ? baseOptions.headers : {};
localVarRequestOptions.headers = {
...localVarHeaderParameter,
...headersFromBaseOptions,
...options.headers,
};
localVarRequestOptions.data = serializeDataIfNeeded(
pluginRunningStateRequest,
localVarRequestOptions,
configuration
);

return {
url: toPathString(localVarUrlObj),
options: localVarRequestOptions,
};
},
/**
* Merge all CSS bundles of enabled plugins into one.
* @param {*} [options] Override http request option.
Expand Down Expand Up @@ -855,6 +926,33 @@ export const ApiConsoleHaloRunV1alpha1PluginApiFp = function (
const localVarAxiosParamCreator =
ApiConsoleHaloRunV1alpha1PluginApiAxiosParamCreator(configuration);
return {
/**
* Change the running state of a plugin by name.
* @param {string} name
* @param {PluginRunningStateRequest} pluginRunningStateRequest
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
async changePluginRunningState(
name: string,
pluginRunningStateRequest: PluginRunningStateRequest,
options?: AxiosRequestConfig
): Promise<
(axios?: AxiosInstance, basePath?: string) => AxiosPromise<Plugin>
> {
const localVarAxiosArgs =
await localVarAxiosParamCreator.changePluginRunningState(
name,
pluginRunningStateRequest,
options
);
return createRequestFunction(
localVarAxiosArgs,
globalAxios,
BASE_PATH,
configuration
);
},
/**
* Merge all CSS bundles of enabled plugins into one.
* @param {*} [options] Override http request option.
Expand Down Expand Up @@ -1194,6 +1292,24 @@ export const ApiConsoleHaloRunV1alpha1PluginApiFactory = function (
) {
const localVarFp = ApiConsoleHaloRunV1alpha1PluginApiFp(configuration);
return {
/**
* Change the running state of a plugin by name.
* @param {ApiConsoleHaloRunV1alpha1PluginApiChangePluginRunningStateRequest} requestParameters Request parameters.
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
changePluginRunningState(
requestParameters: ApiConsoleHaloRunV1alpha1PluginApiChangePluginRunningStateRequest,
options?: AxiosRequestConfig
): AxiosPromise<Plugin> {
return localVarFp
.changePluginRunningState(
requestParameters.name,
requestParameters.pluginRunningStateRequest,
options
)
.then((request) => request(axios, basePath));
},
/**
* Merge all CSS bundles of enabled plugins into one.
* @param {*} [options] Override http request option.
Expand Down Expand Up @@ -1397,6 +1513,27 @@ export const ApiConsoleHaloRunV1alpha1PluginApiFactory = function (
};
};

/**
* Request parameters for changePluginRunningState operation in ApiConsoleHaloRunV1alpha1PluginApi.
* @export
* @interface ApiConsoleHaloRunV1alpha1PluginApiChangePluginRunningStateRequest
*/
export interface ApiConsoleHaloRunV1alpha1PluginApiChangePluginRunningStateRequest {
/**
*
* @type {string}
* @memberof ApiConsoleHaloRunV1alpha1PluginApiChangePluginRunningState
*/
readonly name: string;

/**
*
* @type {PluginRunningStateRequest}
* @memberof ApiConsoleHaloRunV1alpha1PluginApiChangePluginRunningState
*/
readonly pluginRunningStateRequest: PluginRunningStateRequest;
}

/**
* Request parameters for fetchPluginConfig operation in ApiConsoleHaloRunV1alpha1PluginApi.
* @export
Expand Down Expand Up @@ -1635,6 +1772,26 @@ export interface ApiConsoleHaloRunV1alpha1PluginApiUpgradePluginFromUriRequest {
* @extends {BaseAPI}
*/
export class ApiConsoleHaloRunV1alpha1PluginApi extends BaseAPI {
/**
* Change the running state of a plugin by name.
* @param {ApiConsoleHaloRunV1alpha1PluginApiChangePluginRunningStateRequest} requestParameters Request parameters.
* @param {*} [options] Override http request option.
* @throws {RequiredError}
* @memberof ApiConsoleHaloRunV1alpha1PluginApi
*/
public changePluginRunningState(
requestParameters: ApiConsoleHaloRunV1alpha1PluginApiChangePluginRunningStateRequest,
options?: AxiosRequestConfig
) {
return ApiConsoleHaloRunV1alpha1PluginApiFp(this.configuration)
.changePluginRunningState(
requestParameters.name,
requestParameters.pluginRunningStateRequest,
options
)
.then((request) => request(this.axios, this.basePath));
}

/**
* Merge all CSS bundles of enabled plugins into one.
* @param {*} [options] Override http request option.
Expand Down
10 changes: 10 additions & 0 deletions console/packages/api-client/src/models/contributor-vo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
* Do not edit the class manually.
*/

// May contain unused imports in some cases
// @ts-ignore
import { Metadata } from "./metadata";

/**
*
* @export
Expand All @@ -36,6 +40,12 @@ export interface ContributorVo {
* @memberof ContributorVo
*/
displayName?: string;
/**
*
* @type {Metadata}
* @memberof ContributorVo
*/
metadata: Metadata;
/**
*
* @type {string}
Expand Down
1 change: 1 addition & 0 deletions console/packages/api-client/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export * from "./personal-access-token-list";
export * from "./plugin";
export * from "./plugin-author";
export * from "./plugin-list";
export * from "./plugin-running-state-request";
export * from "./plugin-spec";
export * from "./plugin-status";
export * from "./policy";
Expand Down
Loading

0 comments on commit bfa9e38

Please sign in to comment.