refactor: support configuration settings and configmap name (#2299)

<!--  Thanks for sending a pull request!  Here are some tips for you:
1. 如果这是你的第一次,请阅读我们的贡献指南:<https://github.com/halo-dev/halo/blob/master/CONTRIBUTING.md>。
1. If this is your first time, please read our contributor guidelines: <https://github.com/halo-dev/halo/blob/master/CONTRIBUTING.md>.
2. 请根据你解决问题的类型为 Pull Request 添加合适的标签。
2. Please label this pull request according to what type of issue you are addressing, especially if this is a release targeted pull request.
3. 请确保你已经添加并运行了适当的测试。
3. Ensure you have added or ran the appropriate tests for your PR.
-->

#### What type of PR is this?
/kind improvement
/area core
/milestone 2.0
<!--
添加其中一个类别:
Add one of the following kinds:

/kind bug
/kind cleanup
/kind documentation
/kind feature
/kind improvement

适当添加其中一个或多个类别(可选):
Optionally add one or more of the following kinds if applicable:

/kind api-change
/kind deprecation
/kind failing-test
/kind flake
/kind regression
-->

#### What this PR does / why we need it:
主题支持配置 settingName 和 configMapName

#### Which issue(s) this PR fixes:

<!--
PR 合并时自动关闭 issue。
Automatically closes linked issue when PR is merged.

用法:`Fixes #<issue 号>`,或者 `Fixes (粘贴 issue 完整链接)`
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->
Fixes #2298

#### Special notes for your reviewer:
/cc @halo-dev/sig-halo 
#### Does this PR introduce a user-facing change?

<!--
如果当前 Pull Request 的修改不会造成用户侧的任何变更,在 `release-note` 代码块儿中填写 `NONE`。
否则请填写用户侧能够理解的 Release Note。如果当前 Pull Request 包含破坏性更新(Break Change),
Release Note 需要以 `action required` 开头。
If no, just write "NONE" in the release-note block below.
If yes, a release note is required:
Enter your extended release note in the block below. If the PR requires additional action from users switching to the new release, include the string "action required".
-->

```release-note
None
```
pull/2321/head
guqing 2022-08-03 23:14:15 +08:00 committed by GitHub
parent 3d38979d80
commit ebecef89c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 107 additions and 2 deletions

View File

@ -4,6 +4,8 @@ import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.NonNull;
import run.halo.app.extension.AbstractExtension;
import run.halo.app.extension.GVK;
@ -24,6 +26,7 @@ public class Theme extends AbstractExtension {
@Data
@ToString
public static class ThemeSpec {
private static final String WILDCARD = "*";
@Schema(required = true, minLength = 1)
private String displayName;
@ -39,9 +42,29 @@ public class Theme extends AbstractExtension {
private String repo;
private String version = "*";
private String version;
private String require = "*";
private String require;
private String settingName;
private String configMapName;
@NonNull
public String getVersion() {
if (StringUtils.isBlank(this.version)) {
return WILDCARD;
}
return version;
}
@NonNull
public String getRequire() {
if (StringUtils.isBlank(this.require)) {
return WILDCARD;
}
return require;
}
}
@Data

View File

@ -0,0 +1,82 @@
package run.halo.app.core.extension;
import static org.assertj.core.api.Assertions.assertThat;
import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import run.halo.app.extension.Metadata;
import run.halo.app.infra.utils.JsonUtils;
/**
* Tests for {@link Theme}.
*
* @author guqing
* @since 2.0.0
*/
class ThemeTest {
@Test
void constructor() throws JSONException {
Theme theme = new Theme();
Metadata metadata = new Metadata();
metadata.setName("test-theme");
theme.setMetadata(metadata);
Theme.ThemeSpec themeSpec = new Theme.ThemeSpec();
theme.setSpec(themeSpec);
themeSpec.setDisplayName("test-theme");
Theme.Author author = new Theme.Author();
author.setName("test-author");
author.setWebsite("https://test.com");
themeSpec.setAuthor(author);
themeSpec.setRepo("https://test.com");
themeSpec.setLogo("https://test.com");
themeSpec.setWebsite("https://test.com");
themeSpec.setDescription("test-description");
themeSpec.setConfigMapName("test-config-map");
themeSpec.setSettingName("test-setting");
themeSpec.setVersion(null);
themeSpec.setRequire(null);
JSONAssert.assertEquals("""
{
"spec": {
"displayName": "test-theme",
"author": {
"name": "test-author",
"website": "https://test.com"
},
"description": "test-description",
"logo": "https://test.com",
"website": "https://test.com",
"repo": "https://test.com",
"version": "*",
"require": "*",
"settingName": "test-setting",
"configMapName": "test-config-map"
},
"apiVersion": "theme.halo.run/v1alpha1",
"kind": "Theme",
"metadata": {
"name": "test-theme",
"labels": null,
"annotations": null,
"version": null,
"creationTimestamp": null,
"deletionTimestamp": null
}
}
""",
JsonUtils.objectToJson(theme),
true);
themeSpec.setVersion("1.0.0");
themeSpec.setRequire("2.0.0");
assertThat(themeSpec.getVersion()).isEqualTo("1.0.0");
assertThat(themeSpec.getRequire()).isEqualTo("2.0.0");
}
}