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

Fix theme updation error #1217

Merged
merged 40 commits into from
Jan 26, 2021
Merged
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
3dd0c21
Make rest controller loggable
Jan 3, 2021
ab87bfc
Refactor pull from git process
Jan 3, 2021
b3ec001
Replace Callback interface with Consumer
Jan 6, 2021
0bf6e76
Tag theme fetch apis and services deprecated
Jan 6, 2021
ab65ec8
Add getAllBranchesTest
Jan 6, 2021
a322e67
Refactor theme fetcher partially
Jan 17, 2021
efaa5eb
Refactor theme property scanner
Jan 17, 2021
f621bff
Add ThemeFetcherComposite
Jan 17, 2021
c5aff98
Add InputStreamThemeFetcher
Jan 17, 2021
01ecb23
Accomplish multipart zip file theme fetcher
JohnNiang Jan 18, 2021
dc339a5
Reformat ThemeServiceImpl
JohnNiang Jan 19, 2021
f6f2faf
Reformat codes
Jan 19, 2021
25b6447
Provide ThemeRepository
Jan 19, 2021
2a30a15
Complete MultipartFileThemeUpdater
JohnNiang Jan 20, 2021
250ebcf
Make CommonsMultipartResolver support put request method
JohnNiang Jan 20, 2021
3789af3
Replace some methods with ThemeRepository
JohnNiang Jan 20, 2021
920b3af
Add GitThemeUpdater
Jan 20, 2021
3bc4038
Add merge two local repo test
Jan 20, 2021
8bdcfbf
Refine merge process with two repos
Jan 21, 2021
eda523c
Add more test entry point in GitTest
JohnNiang Jan 22, 2021
6920c95
Add shutdown hook after creating temporary directory
Jan 22, 2021
30ffad7
Add test: find commit by tag
Jan 22, 2021
ef0d07d
Refactor git clone process in GitThemeFetcher
Jan 22, 2021
a10af60
Refine merge process of two repo
Jan 22, 2021
8fbe80a
Merge remote-tracking branch 'upstream/master' into 1133-bug/theme-up…
Jan 24, 2021
2dfa722
Make sure that RevWalk closed
Jan 24, 2021
6b4eecc
Fix FileUtils#findRootPath bug
Jan 24, 2021
77b71f4
Add clean task before gradle check
Jan 24, 2021
5cbe85d
Add fallback theme fetcher
Jan 24, 2021
c4a6159
Disable logback-test.xml
Jan 24, 2021
998e81a
Set testLogging.showStandardStreams with true
Jan 24, 2021
22e479e
Fix test error while missing halo-test folder
Jan 24, 2021
ae18205
Enhance git theme fetcher
Jan 24, 2021
dbbec2b
Add copy hidden folder test
JohnNiang Jan 25, 2021
a9b7957
Refine GitThemeFetcherTest
Jan 25, 2021
fe757e6
Accomplish GitThemeUpdater
Jan 25, 2021
b57381b
Accomplish theme update
JohnNiang Jan 26, 2021
3469aa8
Fix checkstyle error
JohnNiang Jan 26, 2021
714e14f
Add more deprecated details
JohnNiang Jan 26, 2021
5fcd146
Merge remote-tracking branch 'upstream/master' into 1133-bug/theme-up…
JohnNiang Jan 26, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor pull from git process
johnniang committed Jan 3, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit ab87bfcc99090ebca56b96e2b5e252e201657c9a
Original file line number Diff line number Diff line change
@@ -178,7 +178,7 @@ public ThemeProperty fetchTheme(@RequestParam("uri") String uri) {
return themeService.fetch(uri);
}

@PostMapping("fetchingBranches")
@PostMapping(value = {"fetchingBranches", "/fetching/git/branches"})
@ApiOperation("Fetches all branches")
public List<ThemeProperty> fetchBranches(@RequestParam("uri") String uri) {
return themeService.fetchBranches(uri);
@@ -209,7 +209,7 @@ public ThemeProperty fetchLatestRelease(@RequestParam("uri") String uri) {
}

@PutMapping("fetching/{themeId}")
@ApiOperation("Upgrades theme by remote")
@ApiOperation("Upgrades theme from remote")
public ThemeProperty updateThemeByFetching(@PathVariable("themeId") String themeId) {
return themeService.update(themeId);
}
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
* Theme property.
*
* @author ryanwang
* @author johnniang
* @date 2019-03-22
*/
@Data
@@ -39,6 +40,11 @@ public class ThemeProperty {
*/
private String repo;

/**
* Theme update strategy. Default is branch.
*/
private UpdateStrategy updateStrategy = UpdateStrategy.BRANCH;

/**
* Theme description.
*/
@@ -116,8 +122,13 @@ public int hashCode() {
return Objects.hash(id);
}

/**
* Theme author info.
*
* @author johnniang
*/
@Data
private static class Author {
public static class Author {

/**
* Author name.
@@ -134,4 +145,21 @@ private static class Author {
*/
private String avatar;
}

/**
* Theme update strategy.
*
* @author johnniang
*/
public enum UpdateStrategy {
/**
* Update from specific branch
*/
BRANCH,

/**
* Update from latest release
*/
RELEASE;
}
}
27 changes: 19 additions & 8 deletions src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java
Original file line number Diff line number Diff line change
@@ -683,7 +683,7 @@ public ThemeProperty update(String themeId, MultipartFile file) {
// Create temp directory
tempPath = FileUtils.createTempDirectory();

String basename = FilenameUtils.getBasename(file.getOriginalFilename());
String basename = FilenameUtils.getBasename(Objects.requireNonNull(file.getOriginalFilename()));
Path themeTempPath = tempPath.resolve(basename);

// Check directory traversal
@@ -704,8 +704,9 @@ public ThemeProperty update(String themeId, MultipartFile file) {
}

// Not support current halo version.
if (StringUtils.isNotEmpty(prepareThemeProperty.getRequire()) && !VersionUtil.compareVersion(HaloConst.HALO_VERSION, prepareThemeProperty.getRequire())) {
throw new ThemeNotSupportException("新版本主题仅支持 Halo " + prepareThemeProperty.getRequire() + " 以上的版本");
if (StringUtils.isNotEmpty(prepareThemeProperty.getRequire())
&& !VersionUtil.compareVersion(HaloConst.HALO_VERSION, prepareThemeProperty.getRequire())) {
throw new ThemeNotSupportException("新版本主题仅支持 Halo " + prepareThemeProperty.getRequire() + " 及以上的版本,请更新 Halo 后重试。");
}

// Coping new theme files to old theme folder.
@@ -734,6 +735,7 @@ private void pullFromGit(@NonNull ThemeProperty themeProperty) throws
DEFAULT_REMOTE_BRANCH : themeProperty.getBranch();

Git git = null;
RevCommit lastCommit = null;

try {
git = GitUtils.openOrInit(Paths.get(themeProperty.getThemePath()));
@@ -745,15 +747,19 @@ private void pullFromGit(@NonNull ThemeProperty themeProperty) throws
.addFilepattern(".")
.call();
// Commit the changes
git.commit().setMessage("Commit by halo automatically").call();
git.commit()
.setAllowEmpty(true)
.setSign(false)
.setMessage("Commit by halo automatically")
.call();

RevWalk revWalk = new RevWalk(repository);

Ref ref = repository.findRef(Constants.HEAD);

Assert.notNull(ref, Constants.HEAD + " ref was not found!");

RevCommit lastCommit = revWalk.parseCommit(ref.getObjectId());
lastCommit = revWalk.parseCommit(ref.getObjectId());

// Force to set remote name
git.remoteRemove().setRemoteName(THEME_PROVIDER_REMOTE_NAME).call();
@@ -798,14 +804,19 @@ private void pullFromGit(@NonNull ThemeProperty themeProperty) throws
ThemeProperty updatedThemeProperty = getProperty(Paths.get(themeProperty.getThemePath()));

// Not support current halo version.
if (StringUtils.isNotEmpty(updatedThemeProperty.getRequire()) && !VersionUtil.compareVersion(HaloConst.HALO_VERSION, updatedThemeProperty.getRequire())) {
// reset theme version
if (StringUtils.isNotEmpty(updatedThemeProperty.getRequire())
&& !VersionUtil.compareVersion(HaloConst.HALO_VERSION, updatedThemeProperty.getRequire())) {
throw new ThemeNotSupportException("新版本主题仅支持 Halo " + updatedThemeProperty.getRequire() + " 以上的版本");
}
} catch (Exception e) {
if (git != null && lastCommit != null) {
log.warn("Rollback theme files to [{}]", lastCommit.getFullMessage());
git.reset()
.setMode(ResetCommand.ResetType.HARD)
.setRef(lastCommit.getName())
.call();
throw new ThemeNotSupportException("新版本主题仅支持 Halo " + updatedThemeProperty.getRequire() + " 以上的版本");
}
throw e;
} finally {
GitUtils.closeQuietly(git);
}