Skip to content

The Unified and Autonomous Development Platform

chore: Dispatch branch created event for issue for UI auto update

Robin Shen2589832ca674
diff --git a/server-core/src/main/java/io/onedev/server/event/project/issue/IssueBranchCreated.java b/server-core/src/main/java/io/onedev/server/event/project/issue/IssueBranchCreated.java
new file mode 100644
index 0000000000..416e84cfd9
--- /dev/null
+++ b/server-core/src/main/java/io/onedev/server/event/project/issue/IssueBranchCreated.java
@@ -0,0 +1,42 @@
+package io.onedev.server.event.project.issue;
+
+import java.util.Date;
+
+import org.jspecify.annotations.Nullable;
+
+import io.onedev.server.OneDev;
+import io.onedev.server.model.Issue;
+import io.onedev.server.model.User;
+import io.onedev.server.service.UserService;
+
+public class IssueBranchCreated extends IssueEvent {
+
+ private static final long serialVersionUID = 1L;
+
+ private final String branch;
+
+ public IssueBranchCreated(@Nullable User user, Issue issue, String branch) {
+ super(user != null ? user : OneDev.getInstance(UserService.class).getSystem(), new Date(), issue);
+ this.branch = branch;
+ }
+
+ public String getBranch() {
+ return branch;
+ }
+
+ @Override
+ public boolean affectsListing() {
+ return false;
+ }
+
+ @Override
+ public String getActivity() {
+ return "branch '" + branch + "' created";
+ }
+
+ @Override
+ public boolean isMinor() {
+ return true;
+ }
+
+}
diff --git a/server-core/src/main/java/io/onedev/server/model/Issue.java b/server-core/src/main/java/io/onedev/server/model/Issue.java
index 69decdf8e9..dda9c22697 100644
--- a/server-core/src/main/java/io/onedev/server/model/Issue.java
+++ b/server-core/src/main/java/io/onedev/server/model/Issue.java
@@ -943,6 +943,36 @@ public class Issue extends ProjectBelonging implements AttachmentStorageSupport
return fieldInputs;
}
+ /**
+ * Parse issue number from given branch name. The branch is expected to
+ * follow the convention used by {@code IssueService#suggestBranch}, i.e.
+ * an optional path prefix, followed by {@code issue-<number>}, optionally
+ * followed by a {@code -<title>} suffix.
+ *
+ * @return the parsed issue number, or {@code null} if the branch name
+ * does not match the issue branch pattern
+ */
+ @Nullable
+ public static Long parseNumberFromBranch(String branchName) {
+ String prefix = "issue-";
+ int segmentStart = branchName.lastIndexOf('/') + 1;
+ if (!branchName.regionMatches(true, segmentStart, prefix, 0, prefix.length()))
+ return null;
+ int numberStart = segmentStart + prefix.length();
+ int numberEnd = numberStart;
+ while (numberEnd < branchName.length()) {
+ char c = branchName.charAt(numberEnd);
+ if (c < '0' || c > '9')
+ break;
+ numberEnd++;
+ }
+ if (numberEnd == numberStart)
+ return null;
+ if (numberEnd < branchName.length() && branchName.charAt(numberEnd) != '-')
+ return null;
+ return Long.parseLong(branchName.substring(numberStart, numberEnd));
+ }
+
public static String getDetailChangeObservable(Long issueId) {
return Issue.class.getName() + ":" + issueId;
}
@@ -1413,19 +1443,13 @@ public class Issue extends ProjectBelonging implements AttachmentStorageSupport
@Nullable
public String getBranch() {
if (branchOptional == null) {
- String fixedPart = "issue-" + getNumber();
for (var ref : getProject().getBranchRefs()) {
String branch = Preconditions.checkNotNull(GitUtils.ref2branch(ref.getName()));
- int index = branch.toLowerCase().indexOf(fixedPart);
- if (index < 0)
- continue;
- if (index > 0 && branch.charAt(index - 1) != '/')
- continue;
- int end = index + fixedPart.length();
- if (end < branch.length() && branch.charAt(end) != '-')
- continue;
- branchOptional = Optional.of(branch);
- break;
+ Long parsedNumber = parseNumberFromBranch(branch);
+ if (parsedNumber != null && parsedNumber == getNumber()) {
+ branchOptional = Optional.of(branch);
+ break;
+ }
}
if (branchOptional == null)
branchOptional = Optional.empty();
diff --git a/server-core/src/main/java/io/onedev/server/search/entity/issue/ReferencedInCurrentBranchCriteria.java b/server-core/src/main/java/io/onedev/server/search/entity/issue/ReferencedInCurrentBranchCriteria.java
index 8ba26d7165..b6d9fee918 100644
--- a/server-core/src/main/java/io/onedev/server/search/entity/issue/ReferencedInCurrentBranchCriteria.java
+++ b/server-core/src/main/java/io/onedev/server/search/entity/issue/ReferencedInCurrentBranchCriteria.java
@@ -1,8 +1,5 @@
package io.onedev.server.search.entity.issue;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.From;
@@ -20,27 +17,12 @@ public class ReferencedInCurrentBranchCriteria extends Criteria<Issue> {
private static final long serialVersionUID = 1L;
- private static final Pattern BRANCH_PATTERN = Pattern.compile("(?:.+/)?issue-(\\d+)(?:-.*)?");
-
- @Nullable
- private static Long parseIssueNumber(String branchName) {
- Matcher matcher = BRANCH_PATTERN.matcher(branchName);
- if (matcher.matches()) {
- try {
- return Long.parseLong(matcher.group(1));
- } catch (NumberFormatException e) {
- return null;
- }
- }
- return null;
- }
-
@Override
public Predicate getPredicate(@Nullable ProjectScope projectScope, CriteriaQuery<?> query, From<Issue, Issue> from, CriteriaBuilder builder) {
ProjectScopedBranch branch = ProjectScopedBranch.get();
if (branch == null)
throw new ExplicitException("No branch in query context");
- Long issueNumber = parseIssueNumber(branch.getBranchName());
+ Long issueNumber = Issue.parseNumberFromBranch(branch.getBranchName());
if (issueNumber == null)
return builder.disjunction();
return builder.and(
@@ -53,7 +35,7 @@ public class ReferencedInCurrentBranchCriteria extends Criteria<Issue> {
ProjectScopedBranch branch = ProjectScopedBranch.get();
if (branch == null)
throw new ExplicitException("No branch in query context");
- Long issueNumber = parseIssueNumber(branch.getBranchName());
+ Long issueNumber = Issue.parseNumberFromBranch(branch.getBranchName());
if (issueNumber == null)
return false;
return issue.getProject().equals(branch.getProject())
diff --git a/server-core/src/main/java/io/onedev/server/service/impl/DefaultIssueService.java b/server-core/src/main/java/io/onedev/server/service/impl/DefaultIssueService.java
index ce697985a8..0bb7ce9575 100644
--- a/server-core/src/main/java/io/onedev/server/service/impl/DefaultIssueService.java
+++ b/server-core/src/main/java/io/onedev/server/service/impl/DefaultIssueService.java
@@ -30,6 +30,7 @@ import javax.persistence.criteria.Root;
import javax.persistence.criteria.Subquery;
import org.apache.shiro.subject.Subject;
+import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
@@ -56,6 +57,8 @@ import io.onedev.server.event.Listen;
import io.onedev.server.event.ListenerRegistry;
import io.onedev.server.event.entity.EntityPersisted;
import io.onedev.server.event.entity.EntityRemoved;
+import io.onedev.server.event.project.RefUpdated;
+import io.onedev.server.event.project.issue.IssueBranchCreated;
import io.onedev.server.event.project.issue.IssueDeleted;
import io.onedev.server.event.project.issue.IssueEvent;
import io.onedev.server.event.project.issue.IssueOpened;
@@ -259,6 +262,22 @@ public class DefaultIssueService extends BaseEntityService<Issue> implements Iss
listenerRegistry.post(new IssueOpened(issue, notifiedEmailAddresses));
}
+ @Transactional
+ @Listen
+ public void on(RefUpdated event) {
+ if (!event.getOldCommitId().equals(ObjectId.zeroId()))
+ return;
+ String branchName = GitUtils.ref2branch(event.getRefName());
+ if (branchName == null)
+ return;
+ Long issueNumber = Issue.parseNumberFromBranch(branchName);
+ if (issueNumber == null)
+ return;
+ Issue issue = find(event.getProject(), issueNumber);
+ if (issue != null)
+ listenerRegistry.post(new IssueBranchCreated(event.getUser(), issue, branchName));
+ }
+
@Transactional
@Override
public void togglePin(Issue issue) {
diff --git a/server-core/src/main/java/io/onedev/server/xodus/DefaultCommitInfoService.java b/server-core/src/main/java/io/onedev/server/xodus/DefaultCommitInfoService.java
index 84485f8d88..b3787c5ccd 100644
--- a/server-core/src/main/java/io/onedev/server/xodus/DefaultCommitInfoService.java
+++ b/server-core/src/main/java/io/onedev/server/xodus/DefaultCommitInfoService.java
@@ -491,7 +491,8 @@ public class DefaultCommitInfoService extends AbstractEnvironmentService
}));
}
- if (GitUtils.branch2ref(project.getDefaultBranch()).equals(refName)) {
+ var defaultBranch = project.getDefaultBranch();
+ if (defaultBranch != null && GitUtils.branch2ref(defaultBranch).equals(refName)) {
collectLineStats(project, commitId);
collectContributions(project, commitId);
collectFiles(project, commitId);