Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use stream to look for error message prefixes #331

Merged
merged 2 commits into from
Mar 15, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.google.common.collect.Multimap;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.kohsuke.github.*;
import org.slf4j.Logger;
Expand All @@ -21,6 +22,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Arrays;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong lexicographical order for 'java.util.Arrays' import. Should be before 'org.slf4j.LoggerFactory'.

import java.util.concurrent.TimeUnit;

/**
Expand Down Expand Up @@ -110,18 +112,18 @@ public int createPullReq(GHRepository origRepo, String branch,
log.warn("Handling error with pull request creation... {}", e.getMessage());
JsonElement root = JsonParser.parseString(e.getMessage());
JsonArray errorJson = root.getAsJsonObject().get("errors").getAsJsonArray();
String error = "";
if (errorJson.get(0).getAsJsonObject().has("message")) {
error = errorJson.get(0).getAsJsonObject().get("message").getAsString();
} else {
// This case usually happens when the PR head has invalid data. Deleting the forked repo resolves it.
error = PR_INVALID_CODE;
String error = PR_INVALID_CODE;
JsonObject errorMessage = errorJson.get(0).getAsJsonObject();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there ever more than 1 error in this array? Any reason not to foreach on them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about that. This piece of code has been there for a while.

if (errorMessage.has("message")) {
error = errorMessage.get("message").getAsString();
}
log.info("error: {}", error);
String finalError = error;
List<String> errorPrefixes = Arrays.asList(PR_INVALID_CODE, "No commits between");
if (error.startsWith("A pull request already exists")) {
Comment on lines +121 to 123
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why you need finalError instead of just using error but you should probably use either one or the other in both if statements.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I tried using the variable error I got the following warning on IDE
Variable used in lambda expression should be final or effectively final

log.info("NOTE: {} New commits may have been added to the pull request.", error);
return 0;
} else if (error.startsWith("No commits between") || error.startsWith(PR_INVALID_CODE)) {
} else if (errorPrefixes.stream().anyMatch(prefix -> finalError.startsWith(prefix))) {
log.warn("NOTE: {} Pull request was not created.", error);
return 1;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void testCreatePullReq_errorCase1_withInvalidCode() throws Exception {
GHRepository forkRepo = mock(GHRepository.class);
when(forkRepo.getOwnerName()).thenReturn("owner");
assertEquals(gitHubUtil.createPullReq(origRepo, "branch", forkRepo, "title", "body"), 1);
verify(origRepo, times(1)).createPullRequest(eq("title"), eq("owner:branch"), eq("master"), eq("body"));
verify(origRepo).createPullRequest(eq("title"), eq("owner:branch"), eq("master"), eq("body"));
}

@Test
Expand Down