Skip to content

Commit

Permalink
Add migration logic to 5.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
robinshine committed Oct 22, 2021
1 parent 9c2102e commit 9a2779b
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1175,4 +1175,45 @@ private void migrate12(VersionedYamlDoc doc, Stack<Integer> versions) {
}
}

@SuppressWarnings("unused")
private void migrate13(VersionedYamlDoc doc, Stack<Integer> versions) {
for (NodeTuple specTuple: doc.getValue()) {
String specTupleKey = ((ScalarNode)specTuple.getKeyNode()).getValue();
if (specTupleKey.equals("jobs")) {
SequenceNode jobsNode = (SequenceNode) specTuple.getValueNode();
for (Node jobsNodeItem: jobsNode.getValue()) {
MappingNode jobNode = (MappingNode) jobsNodeItem;
for (Iterator<NodeTuple> itJobTuple = jobNode.getValue().iterator(); itJobTuple.hasNext();) {
NodeTuple jobTuple = itJobTuple.next();
String jobTupleKey = ((ScalarNode)jobTuple.getKeyNode()).getValue();
if (jobTupleKey.equals("projectDependencies")) {
SequenceNode projectDependenciesNode = (SequenceNode) jobTuple.getValueNode();
for (Node projectDependenciesItem: projectDependenciesNode.getValue()) {
MappingNode projectDependencyNode = (MappingNode) projectDependenciesItem;
for (Iterator<NodeTuple> itProjectDependencyTuple = projectDependencyNode.getValue().iterator();
itProjectDependencyTuple.hasNext();) {
NodeTuple projectDependencyTuple = itProjectDependencyTuple.next();
ScalarNode projectDependencyTupleKeyNode = ((ScalarNode)projectDependencyTuple.getKeyNode());
if (projectDependencyTupleKeyNode.getValue().equals("projectName"))
projectDependencyTupleKeyNode.setValue("projectPath");
}
}
}
}
}
} else if (specTupleKey.equals("imports")) {
SequenceNode importsNode = (SequenceNode) specTuple.getValueNode();
for (Node importsNodeItem: importsNode.getValue()) {
MappingNode importNode = (MappingNode) importsNodeItem;
for (Iterator<NodeTuple> itImportTuple = importNode.getValue().iterator(); itImportTuple.hasNext();) {
NodeTuple importTuple = itImportTuple.next();
ScalarNode importTupleKeyNode = (ScalarNode)importTuple.getKeyNode();
if (importTupleKeyNode.getValue().equals("projectName"))
importTupleKeyNode.setValue("projectPath");
}
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,9 @@ public boolean isValid(ConstraintValidatorContext context) {
}
}

Set<String> dependencyProjectNames = new HashSet<>();
Set<String> dependencyProjectPaths = new HashSet<>();
for (ProjectDependency dependency: projectDependencies) {
if (!dependencyProjectNames.add(dependency.getProjectPath())) {
if (!dependencyProjectPaths.add(dependency.getProjectPath())) {
isValid = false;
context.buildConstraintViolationWithTemplate("Duplicate dependency (" + dependency.getProjectPath() + ")")
.addPropertyNode("projectDependencies").addConstraintViolation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,11 @@ public void delete(Project project) {
public Project find(String path) {
List<String> names = Splitter.on("/").omitEmptyStrings().trimResults().splitToList(path);
Project project = null;
for (String name: names)
for (String name: names) {
project = find(project, name);
if (project == null)
break;
}
return project;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
import java.util.Set;
import java.util.Stack;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Nullable;
import javax.inject.Singleton;

import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -3113,4 +3116,100 @@ private void migrate67(File dataDir, Stack<Integer> versions) {
}
}

private void migrateAttachmentLinks(@Nullable Element element, Map<String, String> projectIds) {
if (element != null) {
String content = element.getText();
Pattern pattern = Pattern.compile("/projects/([\\w-\\.]+)/attachment/");
Matcher matcher = pattern.matcher(content);
StringBuffer buffer = new StringBuffer();

while (matcher.find()) {
String projectName = matcher.group(1);
String projectId = projectIds.get(projectName);
if (projectId != null)
matcher.appendReplacement(buffer, "/projects/" + projectId + "/attachment/");
else
matcher.appendReplacement(buffer, matcher.group());
}

matcher.appendTail(buffer);
element.setText(buffer.toString());
}
}

private void migrate68(File dataDir, Stack<Integer> versions) {
Map<String, String> projectIds = new HashMap<>();
for (File file: dataDir.listFiles()) {
if (file.getName().startsWith("Projects.xml")) {
VersionedXmlDoc dom = VersionedXmlDoc.fromFile(file);
for (Element element: dom.getRootElement().elements())
projectIds.put(element.elementTextTrim("name"), element.elementTextTrim("id"));
}
}

for (File file: dataDir.listFiles()) {
if (file.getName().startsWith("Projects.xml")) {
VersionedXmlDoc dom = VersionedXmlDoc.fromFile(file);
for (Element element: dom.getRootElement().elements())
element.addElement("codeManagementEnabled").setText("true");
dom.writeToFile(file, false);
} else if (file.getName().startsWith("Issues.xml")) {
VersionedXmlDoc dom = VersionedXmlDoc.fromFile(file);
for (Element element: dom.getRootElement().elements())
migrateAttachmentLinks(element.element("description"), projectIds);
dom.writeToFile(file, false);
} else if (file.getName().startsWith("IssueComments.xml")) {
VersionedXmlDoc dom = VersionedXmlDoc.fromFile(file);
for (Element element: dom.getRootElement().elements())
migrateAttachmentLinks(element.element("content"), projectIds);
dom.writeToFile(file, false);
} else if (file.getName().startsWith("PullRequests.xml")) {
VersionedXmlDoc dom = VersionedXmlDoc.fromFile(file);
for (Element element: dom.getRootElement().elements())
migrateAttachmentLinks(element.element("description"), projectIds);
dom.writeToFile(file, false);
} else if (file.getName().startsWith("PullRequestComments.xml")) {
VersionedXmlDoc dom = VersionedXmlDoc.fromFile(file);
for (Element element: dom.getRootElement().elements())
migrateAttachmentLinks(element.element("content"), projectIds);
dom.writeToFile(file, false);
} else if (file.getName().startsWith("CodeComments.xml")) {
VersionedXmlDoc dom = VersionedXmlDoc.fromFile(file);
for (Element element: dom.getRootElement().elements())
migrateAttachmentLinks(element.element("content"), projectIds);
dom.writeToFile(file, false);
} else if (file.getName().startsWith("CodeCommentReplys.xml")) {
VersionedXmlDoc dom = VersionedXmlDoc.fromFile(file);
for (Element element: dom.getRootElement().elements())
migrateAttachmentLinks(element.element("content"), projectIds);
dom.writeToFile(file, false);
} else if (file.getName().startsWith("Groups.xml")) {
VersionedXmlDoc dom = VersionedXmlDoc.fromFile(file);
for (Element element: dom.getRootElement().elements())
element.addElement("createRootProjects").setText("false");
dom.writeToFile(file, false);
} else if (file.getName().startsWith("Roles.xml")) {
VersionedXmlDoc dom = VersionedXmlDoc.fromFile(file);
for (Element element: dom.getRootElement().elements())
element.addElement("createChildren").setText("false");
dom.writeToFile(file, false);
} else if (file.getName().startsWith("Settings.xml")) {
VersionedXmlDoc dom = VersionedXmlDoc.fromFile(file);
for (Element element: dom.getRootElement().elements()) {
if (element.elementTextTrim("key").equals("JOB_EXECUTORS")) {
Element valueElement = element.element("value");
for (Element executorElement: valueElement.elements()) {
Element jobMatchElement = executorElement.element("jobMatch");
if (jobMatchElement.getTextTrim().equals("all"))
jobMatchElement.detach();
else
jobMatchElement.setName("jobRequirement");
}
}
}
dom.writeToFile(file, false);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<wicket:extend>
<div class="alert alert-notice alert-light-info mb-4">
<wicket:svg href="bulb" class="icon mr-2"></wicket:svg>
When authorize a project, all child projects will also be authorized with the role
</div>
<form wicket:id="form" class="group-authorizations leave-confirm">
<div wicket:id="feedback"></div>
<div wicket:id="editor"></div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<wicket:extend>
<div class="alert alert-notice alert-light-info mb-4">
<wicket:svg href="bulb" class="icon mr-2"></wicket:svg>
When authorize a project, all child projects will also be authorized with the role
</div>
<form wicket:id="form" class="user-authorizations leave-confirm">
<div wicket:id="feedback" class="mb-4"></div>
<div wicket:id="editor"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
}

.floating.get-code {
max-width: 480px !important;
width: 480px !important;
}
.floating.get-code .download {
padding: 0 2.25rem 2rem 2.25rem;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<wicket:extend>
<div class="alert alert-notice alert-light-info mb-4">
<wicket:svg href="bulb" class="icon mr-2"></wicket:svg>
When authorize a user, the user will also be authorized with the role for all child projects
</div>
<div class="card project-authorizations">
<div class="card-body">
<form wicket:id="form" class="authorization-list leave-confirm">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.onedev.server.entitymanager.RoleManager;
import io.onedev.server.model.Role;
import io.onedev.server.web.editable.annotation.Editable;
import io.onedev.server.web.editable.annotation.NameOfEmptyValue;
import io.onedev.server.web.editable.annotation.RoleChoice;

@Editable
Expand All @@ -17,8 +18,10 @@ public class DefaultRoleBean implements Serializable {

private String roleName;

@Editable(name="Default Role", description="Default role determines default permissions granted to everyone in the system")
@Editable(name="Default Role", description="Default role determines default permissions granted to everyone in the system. "
+ "All child projects will also have this default role")
@RoleChoice
@NameOfEmptyValue("No default role")
public String getRoleName() {
return roleName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.onedev.server.entitymanager.ProjectManager;
import io.onedev.server.model.Project;
import io.onedev.server.web.editable.annotation.Editable;
import io.onedev.server.web.editable.annotation.NameOfEmptyValue;
import io.onedev.server.web.editable.annotation.ParentChoice;

@Editable
Expand All @@ -17,8 +18,10 @@ public class ParentBean implements Serializable {

private String parentPath;

@Editable(name="Parent Project")
@Editable(name="Parent Project", description="Settings and permissions of parent project will be inherited "
+ "by this project")
@ParentChoice
@NameOfEmptyValue("No parent")
public String getParentPath() {
return parentPath;
}
Expand Down

0 comments on commit 9a2779b

Please sign in to comment.