Skip to content

Commit

Permalink
issue: Support issue move to projects
Browse files Browse the repository at this point in the history
  • Loading branch information
doortts committed Mar 13, 2017
1 parent 9c08aea commit bb330c5
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 14 deletions.
69 changes: 56 additions & 13 deletions app/controllers/IssueApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@

import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.*;

@AnonymousCheck
public class IssueApp extends AbstractPostingApp {
Expand Down Expand Up @@ -436,7 +433,9 @@ public static Result newIssue(String ownerName, String projectName) {

newIssue.state = State.OPEN;

addLabels(newIssue, request());
if (newIssue.project.id.equals(Project.findByOwnerAndProjectName(ownerName, projectName).id)) {
addLabels(newIssue, request());
}
setMilestone(issueForm, newIssue);

newIssue.dueDate = JodaDateUtil.lastSecondOfDay(newIssue.dueDate);
Expand Down Expand Up @@ -468,6 +467,10 @@ public static Result editIssueForm(String ownerName, String projectName, Long nu
Project project = Project.findByOwnerAndProjectName(ownerName, projectName);
Issue issue = Issue.findByNumber(project, number);

if (issue == null) {
return notFound(ErrorViews.Forbidden.render("error.notfound", project));
}

if (!AccessControl.isAllowed(UserApp.currentUser(), issue.asResource(), Operation.READ)) {
return forbidden(ErrorViews.Forbidden.render("error.forbidden", project));
}
Expand Down Expand Up @@ -536,21 +539,27 @@ public static Result editIssue(String ownerName, String projectName, Long number

Issue originalIssue = Issue.findByNumber(project, number);
if(StringUtils.isNotEmpty(issue.targetProjectId)){
Project toAnotherProject = Project.find.byId(Long.valueOf(issue.targetProjectId));
if(toAnotherProject == null){
Project toOtherProject = Project.find.byId(Long.valueOf(issue.targetProjectId));
if(toOtherProject == null){
flash(Constants.WARNING, Messages.get("error.notfound.project"));
return badRequest(edit.render("error.validation", issueForm, Issue.findByNumber(project, number), project));
} else if (!project.id.equals(toAnotherProject.id)){
originalIssue.project = toAnotherProject;
originalIssue.setNumber(Project.increaseLastIssueNumber(toAnotherProject.id));
} else if (isRequestedToOtherProject(project, toOtherProject)) {
originalIssue.project = toOtherProject;
originalIssue.setNumber(Project.increaseLastIssueNumber(toOtherProject.id));
originalIssue.createdDate = JodaDateUtil.now();
originalIssue.updatedDate = JodaDateUtil.now();
originalIssue.milestone = null;
for(IssueComment comment: originalIssue.comments){
comment.projectId = originalIssue.project.id;
comment.update();
}
originalIssue.update();
if (UserApp.currentUser().isMemberOf(toOtherProject) && originalIssue.labels.size() > 0) {
play.Logger.warn("--- transfered");
transferLabels(originalIssue, toOtherProject);
} else {
originalIssue.labels = new HashSet<>();
originalIssue.update();
}
}
}
updateSubtaskRelation(issue, originalIssue);
Expand All @@ -567,7 +576,12 @@ public void run() {
// Do not replace it to 'issue.comments = originalIssue.comments;'
issue.voters.addAll(originalIssue.voters);
issue.comments = originalIssue.comments;
addLabels(issue, request());
if(originalIssue.project.id.equals(Project.findByOwnerAndProjectName(ownerName, projectName).id)){
addLabels(issue, request());
} else {
play.Logger.warn("originalIssue.labels: " + originalIssue.labels.size());
issue.labels = originalIssue.labels;
}

if(isSelectedToSendNotificationMail() || !originalIssue.isAuthoredBy(UserApp.currentUser())){
addAssigneeChangedNotification(issue, originalIssue);
Expand All @@ -580,14 +594,43 @@ public void run() {
return editPosting(originalIssue, issue, issueForm, redirectTo, preUpdateHook);
}

private static void transferLabels(Issue originalIssue, Project toProject) {
Set<IssueLabel> newLabels = new HashSet<>();

for (IssueLabel label : originalIssue.getLabels()) {
IssueLabel copiedLabel = IssueLabel.copyIssueLabel(toProject, label);
IssueLabel existedLabel = copiedLabel.findExistLabel();
if(existedLabel == null){
toProject.issueLabels.add(copiedLabel);
copiedLabel.issues.add(originalIssue);
copiedLabel.save();
toProject.update();
} else {
copiedLabel = existedLabel;
copiedLabel.issues.add(originalIssue);
copiedLabel.update();
}
newLabels.add(copiedLabel);
}

originalIssue.labels = new HashSet<>(newLabels);
originalIssue.update();
}

private static boolean isRequestedToOtherProject(Project project, Project toOtherProject) {
return !project.id.equals(toOtherProject.id);
}

private static void updateSubtaskRelation(Issue issue, Issue originalIssue) {
if(StringUtils.isEmpty(issue.parentIssueId)){
issue.parent = null;
} else {
issue.parent = Issue.finder.byId(Long.valueOf(issue.parentIssueId));
}
originalIssue.parent = issue.parent;
originalIssue.update();
if (originalIssue.parent != null) {
originalIssue.update();
}
}

private static void setAssignee(Form<Issue> issueForm, Issue issue, Project project) {
Expand Down
16 changes: 15 additions & 1 deletion app/models/IssueLabel.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static void copyIssueLabels(Project fromProject, Project toProject){
* @param fromLabel
* @return copied {@code IssueLabel}
*/
private static IssueLabel copyIssueLabel(@Nonnull Project toProject, @Nonnull IssueLabel fromLabel) {
public static IssueLabel copyIssueLabel(@Nonnull Project toProject, @Nonnull IssueLabel fromLabel) {
IssueLabel label = new IssueLabel();
label.name = fromLabel.name;
label.color = fromLabel.color;
Expand Down Expand Up @@ -145,6 +145,20 @@ public boolean exists() {
.findRowCount() > 0;
}

@Transient
public IssueLabel findExistLabel() {
List<IssueLabel> list = finder.where()
.eq("project.id", project.id)
.eq("category", category)
.eq("name", name)
.findList();
if (list != null && list.size() > 0) {
return list.get(0);
} else {
return null;
}
}

@Override
public void delete() {
for(Issue issue: issues) {
Expand Down

0 comments on commit bb330c5

Please sign in to comment.