The Unified and Autonomous Development Platform
Fix the issue that sometimes UI can not be updated automatically via web socket.
robin shinec13ae32456ea
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/asset/common.js b/commons.wicket/src/main/java/com/pmease/commons/wicket/asset/common.jsindex 0174ed8051..5fffb090ce 100644--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/asset/common.js+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/asset/common.js@@ -748,7 +748,15 @@ pmease.commons = {if (json.type == "RenderCallback")Wicket.WebSocket.send(message);});- }+ Wicket.Event.subscribe("/websocket/open", function(jqEvent, message) {+ // Request to render via web socket right away after a web socket is opened+ // in case we missed the message sent by WebSocketRender.requestToRender+ // at Java side if web socket connection is not established at that time+ for (var i in pmease.commons.websocket.renderTraits)+ Wicket.WebSocket.send(JSON.stringify(pmease.commons.websocket.renderTraits[i]));+ });+ },+ renderTraits:[]},setupClearableInput: function(inputId) {diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/websocket/WebSocketRenderBehavior.java b/commons.wicket/src/main/java/com/pmease/commons/wicket/websocket/WebSocketRenderBehavior.javaindex 93027fee4a..779725244e 100644--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/websocket/WebSocketRenderBehavior.java+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/websocket/WebSocketRenderBehavior.java@@ -6,6 +6,8 @@ import java.util.List;import java.util.Map;import org.apache.wicket.Component;+import org.apache.wicket.markup.head.IHeaderResponse;+import org.apache.wicket.markup.head.OnDomReadyHeaderItem;import org.apache.wicket.protocol.ws.api.IWebSocketConnection;import org.apache.wicket.protocol.ws.api.WebSocketBehavior;import org.apache.wicket.protocol.ws.api.WebSocketRequestHandler;@@ -24,7 +26,7 @@ public abstract class WebSocketRenderBehavior extends WebSocketBehavior {private final static Map<IWebSocketConnection, List<Object>> connections =new MapMaker().concurrencyLevel(16).weakKeys().makeMap();-+private Component component;@Override@@ -35,6 +37,13 @@ public abstract class WebSocketRenderBehavior extends WebSocketBehavior {this.component = component;}+ @Override+ public void renderHead(Component component, IHeaderResponse response) {+ super.renderHead(component, response);+ String script = String.format("pmease.commons.websocket.renderTraits.push(%s);", asMessage(getTrait()));+ response.render(OnDomReadyHeaderItem.forScript(script));+ }+@Overrideprotected void onConnect(ConnectedMessage message) {super.onConnect(message);@@ -74,23 +83,26 @@ public abstract class WebSocketRenderBehavior extends WebSocketBehavior {}}- public static void requestToRender(Object trait) {- String textMessage;+ private static String asMessage(Object trait) {try {ObjectMapper mapper = AppLoader.getInstance(ObjectMapper.class);WebSocketMessage webSocketMessage = new WebSocketMessage(WebSocketMessage.RENDER_CALLBACK, trait);- textMessage = mapper.writeValueAsString(webSocketMessage);+ return mapper.writeValueAsString(webSocketMessage);} catch (JsonProcessingException e) {throw new RuntimeException(e);}-+ }++ public static void requestToRender(Object trait) {+ String message = asMessage(trait);+for (Iterator<Map.Entry<IWebSocketConnection, List<Object>>> it = connections.entrySet().iterator(); it.hasNext();) {Map.Entry<IWebSocketConnection, List<Object>> entry = it.next();IWebSocketConnection connection = entry.getKey();if (connection != null && connection.isOpen()) {if (entry.getValue().contains(trait)) {try {- connection.sendMessage(textMessage);+ connection.sendMessage(message);} catch (IOException e) {throw new RuntimeException(e);}diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/hookcallback/GitPostReceiveCallback.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/hookcallback/GitPostReceiveCallback.javaindex 1b967572e5..c62cb8ff76 100644--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/hookcallback/GitPostReceiveCallback.java+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/hookcallback/GitPostReceiveCallback.java@@ -103,9 +103,11 @@ public class GitPostReceiveCallback extends HttpServlet {branch.setName(branchName);branch.setHeadCommitHash(newCommitHash);repository.getBranches().add(branch);- dao.persist(branch);- if (repository.getBranches().size() == 1)+ if (repository.getBranches().size() == 1) {repository.git().updateDefaultBranch(branchName);+ branch.setDefault(true);+ }+ dao.persist(branch);} else if (newCommitHash.equals(GitUtils.NULL_SHA1)) {Branch branch = branchManager.findBy(repository, branchName);Preconditions.checkNotNull(branch);diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultBranchManager.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultBranchManager.javaindex 6a5102fa4d..7775a8b0f6 100644--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultBranchManager.java+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultBranchManager.java@@ -53,9 +53,9 @@ public class DefaultBranchManager implements BranchManager {}for (PullRequest request: branch.getOutgoingRequests()) {- pullRequestManager.discard(request, null, "Source branch is deleted.");request.setSource(null);dao.persist(request);+ pullRequestManager.discard(request, null, "Source branch is deleted.");}dao.remove(branch);}diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/assets/css/page.css b/gitplex.web/src/main/java/com/pmease/gitplex/web/assets/css/page.cssindex 848ccb74b6..1fa9943cbd 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/assets/css/page.css+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/assets/css/page.css@@ -655,7 +655,6 @@ table.branches td {white-space: nowrap;}table.branches td.watch {width: 160px;}table.branches td.actions {width: 1px;}table.branches td.behind-ahead {width: 25%;}-table.branches td.behind-ahead a, table.branches td.behind-ahead em {font-size: 13px;}table.branches td.behind {padding-right: 0;}table.branches td.ahead {padding-left: 0;}table.branches td.behind>div {border-right: 1px solid #333;}diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/branches/BranchesPage.html b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/branches/BranchesPage.htmlindex a5d1fc355f..b4d94807f9 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/branches/BranchesPage.html+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/branches/BranchesPage.html@@ -27,8 +27,8 @@<div align="right"><div class="text"><a wicket:id="behindLink">+ <span class="fa fa-download" wicket:id="pullRequest"></span><span wicket:id="count"></span> behind- <span class="status" wicket:id="status"></span></a></div><div class="bar" wicket:id="behindBar"></div>@@ -39,7 +39,7 @@<div class="text"><a wicket:id="aheadLink"><span wicket:id="count"></span> ahead- <span class="status" wicket:id="status"></span>+ <span class="fa fa-upload" wicket:id="pullRequest"></span></a></div><div class="bar" wicket:id="aheadBar"></div>@@ -54,7 +54,7 @@</div></td><td class="actions">- <a wicket:id="delete" class="fa fa-times fa-2x" title="Delete this branch"></a>+ <a wicket:id="delete" class="fa fa-times fa-lg" title="Delete this branch"></a></td></tr></table>diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/branches/BranchesPage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/branches/BranchesPage.javaindex 155f209f05..90977e9902 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/branches/BranchesPage.java+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/branches/BranchesPage.java@@ -322,18 +322,27 @@ public class BranchesPage extends RepositoryPage {protected void onInitialize() {super.onInitialize();add(new Label("count", ab.getBehind()));- add(new Label("status", new LoadableDetachableModel<String>() {+ add(new WebMarkupContainer("pullRequest") {@Override- protected String load() {- PullRequest request = behindOpenRequestsModel.getObject().get(item.getModelObject().getName());- if (request != null)- return "- " + request.getStatus().toString().toLowerCase();- else- return "";+ protected void onConfigure() {+ super.onConfigure();+ setVisible(behindOpenRequestsModel.getObject().get(item.getModelObject().getName()) != null);}- }));+ });+ }++ @Override+ protected void onComponentTag(ComponentTag tag) {+ super.onComponentTag(tag);++ if (behindOpenRequestsModel.getObject().get(item.getModelObject().getName()) != null) {+ tag.put("title", "" + ab.getBehind() + " commits behind of base branch, and there is an "+ + "open pull request fetching those commits");+ } else {+ tag.put("title", "" + ab.getBehind() + " commits ahead of base branch");+ }}@Override@@ -372,18 +381,27 @@ public class BranchesPage extends RepositoryPage {protected void onInitialize() {super.onInitialize();add(new Label("count", ab.getAhead()));- add(new Label("status", new LoadableDetachableModel<String>() {+ add(new WebMarkupContainer("pullRequest") {@Override- protected String load() {- PullRequest request = aheadOpenRequestsModel.getObject().get(item.getModelObject().getName());- if (request != null)- return "- " + request.getStatus().toString().toLowerCase();- else- return "";+ protected void onConfigure() {+ super.onConfigure();+ setVisible(aheadOpenRequestsModel.getObject().get(item.getModelObject().getName()) != null);}- }).setEscapeModelStrings(false));+ });+ }++ @Override+ protected void onComponentTag(ComponentTag tag) {+ super.onComponentTag(tag);++ if (aheadOpenRequestsModel.getObject().get(item.getModelObject().getName()) != null) {+ tag.put("title", "" + ab.getAhead() + " commits ahead of base branch, and there is an "+ + "open pull request sending these commits to base branch");+ } else {+ tag.put("title", "" + ab.getAhead() + " commits ahead of base branch");+ }}@Overridediff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/compare/BranchComparePage.html b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/compare/BranchComparePage.htmlindex b0b331cff0..67911ece1d 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/compare/BranchComparePage.html+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/compare/BranchComparePage.html@@ -10,13 +10,11 @@<td class="actions"><a wicket:id="swap" class="swap btn btn-xs btn-default">swap</a><div class="direction"><span class="fa fa-arrow-left fa-lg"></span></div>+ <a wicket:id="createRequest" class="btn btn-primary btn-sm">Create Pull Request</a></td><td class="source"><div wicket:id="source"></div></td>- <td>- <a wicket:id="createRequest" class="btn btn-primary btn-lg">Create Pull Request</a>- </td></tr></table><div wicket:id="sameBranch" class="alert alert-warning">diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/compare/BranchComparePage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/compare/BranchComparePage.javaindex 04b9d8ecae..4b06cfcfab 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/compare/BranchComparePage.java+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/compare/BranchComparePage.java@@ -213,6 +213,13 @@ public class BranchComparePage extends RepositoryPage {Branch source = sourceModel.getObject();setResponsePage(NewRequestPage.class, NewRequestPage.paramsOf(target.getRepository(), source, target));}++ @Override+ protected void onConfigure() {+ super.onConfigure();+ PullRequest request = requestModel.getObject();+ setVisible((request == null || !request.isOpen()) && !commitsModel.getObject().isEmpty());+ }});diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/activity/DiscardPullRequest.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/activity/DiscardPullRequest.javaindex a0deaaf69d..b9c1dc4b9c 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/activity/DiscardPullRequest.java+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/activity/DiscardPullRequest.java@@ -2,6 +2,8 @@ package com.pmease.gitplex.web.page.repository.pullrequest.activity;import java.util.Date;+import javax.annotation.Nullable;+import org.apache.wicket.markup.html.panel.Panel;import org.apache.wicket.model.LoadableDetachableModel;@@ -16,8 +18,8 @@ public class DiscardPullRequest implements PullRequestActivity {private final Date date;- public DiscardPullRequest(User user, Date date) {- this.userId = user.getId();+ public DiscardPullRequest(@Nullable User user, Date date) {+ this.userId = user!=null?user.getId():null;this.date = date;}@@ -28,7 +30,10 @@ public class DiscardPullRequest implements PullRequestActivity {@Overridepublic User getUser() {- return GitPlex.getInstance(Dao.class).load(User.class, userId);+ if (userId != null)+ return GitPlex.getInstance(Dao.class).load(User.class, userId);+ else+ return null;}@Overridediff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/activity/IntegratePullRequest.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/activity/IntegratePullRequest.javaindex 43d09975fe..74f185f6b8 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/activity/IntegratePullRequest.java+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/activity/IntegratePullRequest.java@@ -17,7 +17,7 @@ public class IntegratePullRequest implements PullRequestActivity {private final Date date;public IntegratePullRequest(User user, Date date) {- this.userId = user.getId();+ this.userId = user!=null?user.getId():null;this.date = date;}@@ -28,7 +28,10 @@ public class IntegratePullRequest implements PullRequestActivity {@Overridepublic User getUser() {- return GitPlex.getInstance(Dao.class).load(User.class, userId);+ if (userId != null)+ return GitPlex.getInstance(Dao.class).load(User.class, userId);+ else+ return null;}@Override