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
Show file tree
Hide file tree
Changes from 12 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -178,38 +178,43 @@ public ThemeProperty fetchTheme(@RequestParam("uri") String uri) {
return themeService.fetch(uri);
}

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

@PostMapping("fetchingReleases")
@ApiOperation("Fetches all releases")
@Deprecated
public List<ThemeProperty> fetchReleases(@RequestParam("uri") String uri) {
return themeService.fetchReleases(uri);
}

@GetMapping("fetchingRelease")
@ApiOperation("Fetches a specific release")
@Deprecated
public ThemeProperty fetchRelease(@RequestParam("uri") String uri, @RequestParam("tag") String tagName) {
return themeService.fetchRelease(uri, tagName);
}

@GetMapping("fetchBranch")
@ApiOperation("Fetch specific branch")
@Deprecated
public ThemeProperty fetchBranch(@RequestParam("uri") String uri, @RequestParam("branch") String branchName) {
return themeService.fetchBranch(uri, branchName);
}

@GetMapping("fetchLatestRelease")
@ApiOperation("Fetch latest release")
@Deprecated
public ThemeProperty fetchLatestRelease(@RequestParam("uri") String uri) {
return themeService.fetchLatestRelease(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);
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/run/halo/app/core/ControllerLogAop.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@
@Slf4j
public class ControllerLogAop {

@Pointcut("@within(org.springframework.web.bind.annotation.RestController)")
public void restController() {
}

@Pointcut("@within(org.springframework.stereotype.Controller)")
public void controller() {
}

@Around("controller()")
@Around("controller() || restController()")
public Object controller(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
final Method method = signature.getMethod();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Theme property.
*
* @author ryanwang
* @author johnniang
* @date 2019-03-22
*/
@Data
Expand All @@ -32,13 +33,18 @@ public class ThemeProperty {
/**
* Theme remote branch.(default is master)
*/
private String branch;
private String branch = "master";

/**
* Theme repo url.
*/
private String repo;

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

/**
* Theme description.
*/
Expand Down Expand Up @@ -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.
Expand All @@ -134,4 +145,22 @@ private static class Author {
*/
private String avatar;
}

/**
* Theme update strategy.
*
* @author johnniang
*/
public enum UpdateStrategy {

/**
* Update from specific branch
*/
BRANCH,

/**
* Update from latest release, only available if the repo is a github repo
*/
RELEASE;
}
}
28 changes: 11 additions & 17 deletions src/main/java/run/halo/app/mail/AbstractMailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;

/**
* Abstract mail service.
Expand All @@ -29,10 +30,15 @@
public abstract class AbstractMailService implements MailService {

private static final int DEFAULT_POOL_SIZE = 5;

protected final OptionService optionService;

private JavaMailSender cachedMailSender;

private MailProperties cachedMailProperties;

private String cachedFromName;

@Nullable
private ExecutorService executorService;

Expand All @@ -48,7 +54,7 @@ public ExecutorService getExecutorService() {
return executorService;
}

public void setExecutorService(ExecutorService executorService) {
public void setExecutorService(@Nullable ExecutorService executorService) {
this.executorService = executorService;
}

Expand All @@ -73,7 +79,7 @@ public void testConnection() {
*
* @param callback mime message callback.
*/
protected void sendMailTemplate(@Nullable Callback callback) {
protected void sendMailTemplate(@Nullable Consumer<MimeMessageHelper> callback) {
if (callback == null) {
log.info("Callback is null, skip to send email");
return;
Expand All @@ -99,7 +105,7 @@ protected void sendMailTemplate(@Nullable Callback callback) {
// set from-name
messageHelper.setFrom(getFromAddress(mailSender));
// handle message set separately
callback.handle(messageHelper);
callback.accept(messageHelper);

// get mime message
MimeMessage mimeMessage = messageHelper.getMimeMessage();
Expand All @@ -121,9 +127,9 @@ protected void sendMailTemplate(@Nullable Callback callback) {
* @param callback callback message handler
* @param tryToAsync if the send procedure should try to asynchronous
*/
protected void sendMailTemplate(boolean tryToAsync, @Nullable Callback callback) {
protected void sendMailTemplate(boolean tryToAsync, @Nullable Consumer<MimeMessageHelper> callback) {
ExecutorService executorService = getExecutorService();
if (tryToAsync && executorService != null) {
if (tryToAsync) {
// send mail asynchronously
executorService.execute(() -> sendMailTemplate(callback));
} else {
Expand Down Expand Up @@ -222,16 +228,4 @@ protected void clearCache() {
log.debug("Cleared all mail caches");
}

/**
* Message callback.
*/
protected interface Callback {
/**
* Handle message set.
*
* @param messageHelper mime message helper
* @throws Exception if something goes wrong
*/
void handle(@NonNull MimeMessageHelper messageHelper) throws Exception;
}
}
45 changes: 31 additions & 14 deletions src/main/java/run/halo/app/mail/MailServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package run.halo.app.mail;

import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import run.halo.app.event.options.OptionUpdatedEvent;
import run.halo.app.service.OptionService;

import javax.mail.MessagingException;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
Expand All @@ -35,32 +39,46 @@ public MailServiceImpl(FreeMarkerConfigurer freeMarker,
@Override
public void sendTextMail(String to, String subject, String content) {
sendMailTemplate(true, messageHelper -> {
messageHelper.setSubject(subject);
messageHelper.setTo(to);
messageHelper.setText(content);
try {
messageHelper.setSubject(subject);
messageHelper.setTo(to);
messageHelper.setText(content);
} catch (MessagingException e) {
throw new RuntimeException("Failed to set message subject, to or test!", e);
}
});
}

@Override
public void sendTemplateMail(String to, String subject, Map<String, Object> content, String templateName) {
sendMailTemplate(true, messageHelper -> {
// build message content with freemarker
Template template = freeMarker.getConfiguration().getTemplate(templateName);
String contentResult = FreeMarkerTemplateUtils.processTemplateIntoString(template, content);
try {
Template template = freeMarker.getConfiguration().getTemplate(templateName);
String contentResult = FreeMarkerTemplateUtils.processTemplateIntoString(template, content);
messageHelper.setSubject(subject);
messageHelper.setTo(to);
messageHelper.setText(contentResult, true);
} catch (IOException | TemplateException e) {
throw new RuntimeException("Failed to convert template to html!", e);
} catch (MessagingException e) {
throw new RuntimeException("Failed to set message subject, to or test", e);
}

messageHelper.setSubject(subject);
messageHelper.setTo(to);
messageHelper.setText(contentResult, true);
});
}

@Override
public void sendAttachMail(String to, String subject, Map<String, Object> content, String templateName, String attachFilePath) {
sendMailTemplate(true, messageHelper -> {
messageHelper.setSubject(subject);
messageHelper.setTo(to);
Path attachmentPath = Paths.get(attachFilePath);
messageHelper.addAttachment(attachmentPath.getFileName().toString(), attachmentPath.toFile());
try {
messageHelper.setSubject(subject);
messageHelper.setTo(to);
Path attachmentPath = Paths.get(attachFilePath);
messageHelper.addAttachment(attachmentPath.getFileName().toString(), attachmentPath.toFile());
} catch (MessagingException e) {
throw new RuntimeException("Failed to set message subject, to or test", e);
}
});
}

Expand All @@ -69,9 +87,8 @@ public void testConnection() {
super.testConnection();
}


@Override
public void onApplicationEvent(OptionUpdatedEvent event) {
public void onApplicationEvent(@NonNull OptionUpdatedEvent event) {
// clear the cached java mail sender
clearCache();
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/run/halo/app/service/ThemeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ public interface ThemeService {
* @throws IOException IOException
*/
@NonNull
@Deprecated
ThemeProperty add(@NonNull Path themeTmpPath) throws IOException;

/**
Expand All @@ -321,6 +322,7 @@ public interface ThemeService {
* @return theme property
*/
@NonNull
@Deprecated
ThemeProperty fetchLatestRelease(@NonNull String uri);

/**
Expand All @@ -330,6 +332,7 @@ public interface ThemeService {
* @return list of theme properties
*/
@NonNull
@Deprecated
List<ThemeProperty> fetchBranches(@NonNull String uri);

/**
Expand All @@ -339,6 +342,7 @@ public interface ThemeService {
* @return list of theme properties
*/
@NonNull
@Deprecated
List<ThemeProperty> fetchReleases(@NonNull String uri);

/**
Expand All @@ -349,6 +353,7 @@ public interface ThemeService {
* @return theme property
*/
@NonNull
@Deprecated
ThemeProperty fetchRelease(@NonNull String uri, @NonNull String tagName);

/**
Expand All @@ -359,6 +364,7 @@ public interface ThemeService {
* @return theme property
*/
@NonNull
@Deprecated
ThemeProperty fetchBranch(@NonNull String uri, @NonNull String branchName);

/**
Expand Down
24 changes: 0 additions & 24 deletions src/main/java/run/halo/app/service/TraceService.java

This file was deleted.

Loading