Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ graalvmNative.binaries {
named("main") {
imageName.set("justserve")
buildArgs.add("--color=always")
buildArgs.add("-march=native")
}
}

Expand Down
76 changes: 39 additions & 37 deletions src/main/java/org/justserve/cli/command/UnReassignProjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import static org.justserve.cli.util.JustServePrinter.printError;
Expand Down Expand Up @@ -58,7 +59,7 @@ public void run() {
return;
}

Map<String, UUID> projects;
Map<String, Set<UUID>> projects;
try {
projects = EmailParser.getProjects(emlContent);
} catch (MessagingException | IOException | JustServeEmailParserError e) {
Expand All @@ -76,45 +77,46 @@ public void run() {
ProjectClient client = projectClientProvider.get();
int successCount = 0;

for (Map.Entry<String, UUID> entry : projects.entrySet()) {
for (Map.Entry<String, Set<UUID>> entry : projects.entrySet()) {
String projectName = entry.getKey();
UUID projectId = entry.getValue();
GetProjectRequest getProjectRequest = new GetProjectRequest();
Project project;
try {
project = client.getProject(projectId, " ", getProjectRequest).body();
} catch (HttpClientResponseException | NullPointerException e) {
printError("Failed to get project " + projectName + " (" + projectId + ")");
log.atError().setCause(e).log("Error getting project");
continue;
}
if (null == project.getProjectOwnerUserId()) {
warning(String.format("Project %s (%s) has no owner", projectName, projectId));
log.warn("Project {} ({}) has no owner", projectName, projectId);
continue;
}
if (project.getProjectOwnerUserId().equals(userID)) {
log.warn("Project {} ({}) is already assigned to user {}", projectName, projectId, userID);
continue;
}

try {
ReassignProjectRequest reassignProjectRequest = new ReassignProjectRequest(userID, project.getProjectOwnerUserId());
log.atTrace().log("Reassigning project {} ({}) to user {}", projectName, projectId, userID);
HttpResponse<Object> reassignResponse = client.reassignProject(projectId, reassignProjectRequest);
if (reassignResponse.status() == HttpStatus.OK) {
printNormal("Successfully reassigned project %s (%s) to user %s", projectName, projectId, userID);
log.atTrace().log("received api response status: {}", reassignResponse.status());
successCount++;
Set<UUID> projectIds = entry.getValue();
for (UUID projectId : projectIds) {
GetProjectRequest getProjectRequest = new GetProjectRequest();
Project project;
try {
project = client.getProject(projectId, " ", getProjectRequest).body();
} catch (HttpClientResponseException | NullPointerException e) {
printError("Failed to get project " + projectName + " (" + projectId + ")");
log.atError().setCause(e).log("Error getting project");
continue;
}
printError("Failed to reassign project " + projectName + " (" + projectId + ") to user " + userID +
". Expected HTTP Status 'OK', but got " + reassignResponse.status());
log.atError().log("Failed to reassign project {} ({}) to user {}. Expected HTTP Status 'OK', but got {}",
projectName, projectId, userID, reassignResponse.status());
} catch (HttpClientResponseException e) {
printError("Failed to reassign project " + projectName + " (" + projectId + ") to user " + userID);
log.atError().setCause(e).log("Error response from API: {}", e.getResponse().body());
if (null == project.getProjectOwnerUserId()) {
warning(String.format("Project %s (%s) has no owner", projectName, projectId));
log.warn("Project {} ({}) has no owner", projectName, projectId);
continue;
}
if (project.getProjectOwnerUserId().equals(userID)) {
log.warn("Project {} ({}) is already assigned to user {}", projectName, projectId, userID);
continue;
}
try {
ReassignProjectRequest reassignProjectRequest = new ReassignProjectRequest(userID, project.getProjectOwnerUserId());
log.atTrace().log("Reassigning project {} ({}) to user {}", projectName, projectId, userID);
HttpResponse<Object> reassignResponse = client.reassignProject(projectId, reassignProjectRequest);
if (reassignResponse.status() == HttpStatus.OK) {
printNormal("Successfully reassigned project %s (%s) to user %s", projectName, projectId, userID);
log.atTrace().log("received api response status: {}", reassignResponse.status());
successCount++;
continue;
}
printError("Failed to reassign project " + projectName + " (" + projectId + ") to user " + userID +
". Expected HTTP Status 'OK', but got " + reassignResponse.status());
log.atError().log("Failed to reassign project {} ({}) to user {}. Expected HTTP Status 'OK', but got {}",
projectName, projectId, userID, reassignResponse.status());
} catch (HttpClientResponseException e) {
printError("Failed to reassign project " + projectName + " (" + projectId + ") to user " + userID);
log.atError().setCause(e).log("Error response from API: {}", e.getResponse().body());
}
}
}
printNormal("Successfully reassigned %d projects to user %s", successCount, userID);
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/org/justserve/util/EmailParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class EmailParser {
* @throws JustServeEmailParserError If the email does not contain an HTML body, is not a JustServe generated email,
* or the HTML structure does not conform to the expected format for extracting projects.
*/
public static Map<String, UUID> getProjects(String emlFileContent) throws MessagingException, IOException, JustServeEmailParserError {
public static Map<String, Set<UUID>> getProjects(String emlFileContent) throws MessagingException, IOException, JustServeEmailParserError {
return getProjects(parse(emlFileContent));
}

Expand Down Expand Up @@ -70,8 +70,8 @@ public static Document parse(String emlFileContent) throws MessagingException, I
* @return A map where keys are project names (String) and values are project UUIDs.
* @throws JustServeEmailParserError If the HTML structure does not conform to the expected format for extracting projects.
*/
public static Map<String, UUID> getProjects(Document doc) throws JustServeEmailParserError {
Map<String, UUID> projects = new HashMap<>();
public static Map<String, Set<UUID>> getProjects(Document doc) throws JustServeEmailParserError {
Map<String, Set<UUID>> projects = new HashMap<>();
Collection<String> errors = new ArrayList<>();

/*
Expand Down Expand Up @@ -102,7 +102,10 @@ public static Map<String, UUID> getProjects(Document doc) throws JustServeEmailP
if (uuid == null) {
errors.add(String.format("Expected link to contain a valid project ID, but found none. URL: %s", link.attr("href")));
} else {
projects.put(link.text(), uuid);
if (!projects.containsKey(link.text())) {
projects.put(link.text(), new HashSet<>());
}
projects.get(link.text()).add(uuid);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class UnReassignProjectsSpec extends BaseCommandSpec {

def setupSpec() {
testEmails = new HashMap<>()
Stream.of("sara-anderson-email.eml"/*, "test-with-automated-email.eml", "test-without-automated-email.eml"*/).forEach { file ->
Stream.of("sara-anderson-email.eml", "test-with-automated-email.eml", "test-without-automated-email.eml").forEach { file ->
def resource = resourceResolver.getResourceAsStream("classpath:$file")
resource.ifPresent { stream ->
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
Expand All @@ -40,23 +40,14 @@ class UnReassignProjectsSpec extends BaseCommandSpec {
}
}

def setup() {
// Create a temporary file for the test
tempEmlFile = File.createTempFile("test-reassign", ".eml")
println "Temp file created at: " + tempEmlFile.absolutePath
}

def cleanup() {
if (tempEmlFile != null && tempEmlFile.exists()) {
tempEmlFile.delete()
}
}

def "can make reassignments from #title to a user"(String title, String fileContent) {
given:

def args = ["unReassignProjects", "-u", readOnlyUser.uuid.toString(), "-f", "build\\resources\\test\\" + title + ".eml"]
def projectCount = EmailParser.getProjects(fileContent).size()
if (title.contains("without")) {
return
}
String testFile = new File(resourceResolver.getResource("classpath:${title}.eml").get().toURI()).absolutePath
def args = ["unReassignProjects", "-u", readOnlyUser.uuid.toString(), "-f", testFile]
def projectCount = EmailParser.getProjects(fileContent).values().flatten().size()

when:
def (outputStream, errorStream) = executeCommand(ctx, args as String[])
Expand All @@ -70,6 +61,5 @@ class UnReassignProjectsSpec extends BaseCommandSpec {

where:
[title, fileContent] << testEmails.collect { key, value -> [key, value] }

}
}
4 changes: 2 additions & 2 deletions src/test/groovy/org/justserve/util/EmailParserSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class EmailParserSpec extends Specification {
Document doc = EmailParser.parse(fileContent as String)

when:
Map<String, UUID> projects = EmailParser.getProjects(doc)
Map<String, Set<UUID>> projects = EmailParser.getProjects(doc)

then:
if (!(title as String).toLowerCase().contains("without")) {
Expand All @@ -105,7 +105,7 @@ class EmailParserSpec extends Specification {

def "Can use 'getProjects' to parse entire #title file"() {
when:
Map<String, UUID> projects = EmailParser.getProjects(fileContent as String)
Map<String, Set<UUID>> projects = EmailParser.getProjects(fileContent as String)

then:
if (!(title as String).toLowerCase().contains("without")) {
Expand Down
10 changes: 5 additions & 5 deletions src/test/resources/SpockConfig.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//runner {
// parallel {
// enabled true
// }
//}
runner {
parallel {
enabled true
}
}
Loading