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

Make Host::user return Optional<String> #435

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -288,7 +288,9 @@ public void handleCommits(HostedRepository repository, Repository localRepositor
var username = findIssueUsername(commit);
if (username.isPresent()) {
var assignee = issueProject.issueTracker().user(username.get());
issue.setAssignees(List.of(assignee));
if (assignee.isPresent()) {
issue.setAssignees(List.of(assignee.get()));
}
}
}
}
@@ -59,8 +59,8 @@ public boolean isValid() {
}

@Override
public HostUser user(String username) {
return users.get(username);
public Optional<HostUser> user(String username) {
return Optional.ofNullable(users.get(username));
}

@Override
@@ -42,8 +42,8 @@ public Optional<HostedRepository> repository(String name) {
}

@Override
public HostUser user(String username) {
return null;
public Optional<HostUser> user(String username) {
return Optional.empty();
}

@Override
@@ -801,6 +801,8 @@ public static void main(String[] args) throws IOException, InterruptedException
var usernames = Arrays.asList(arguments.get("assignees").asString().split(","));
var assignees = usernames.stream()
.map(u -> host.user(u))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
pr.setAssignees(assignees);
}
@@ -1058,6 +1060,8 @@ public static void main(String[] args) throws IOException, InterruptedException
var usernames = Arrays.asList(assigneesOption.split(","));
var assignees = usernames.stream()
.map(u -> host.user(u))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
pr.setAssignees(assignees);
}
@@ -1371,6 +1375,8 @@ public static void main(String[] args) throws IOException, InterruptedException
var usernames = Arrays.asList(assigneesOption.split(","));
var assignees = usernames.stream()
.map(u -> host.user(u))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
pr.setAssignees(assignees);
}
@@ -128,7 +128,7 @@ Optional<String> getInstallationToken() {

private String getFullName(String userName) {
var details = user(userName);
return details.fullName();
return details.get().fullName();
}

// Most GitHub API's return user information in this format
@@ -182,9 +182,15 @@ public Optional<HostedRepository> repository(String name) {
}

@Override
public HostUser user(String username) {
var details = request.get("users/" + URLEncoder.encode(username, StandardCharsets.UTF_8)).execute().asObject();
return asHostUser(details);
public Optional<HostUser> user(String username) {
var details = request.get("users/" + URLEncoder.encode(username, StandardCharsets.UTF_8))
.onError(r -> JSON.of())
.execute();
if (details.isNull()) {
return Optional.empty();
}

return Optional.of(asHostUser(details.asObject()));
}

private static HostUser asHostUser(JSONObject details) {
@@ -205,7 +211,7 @@ public HostUser currentUser() {
if (application != null) {
var appDetails = application.getAppDetails();
var appName = appDetails.get("name").asString() + "[bot]";
currentUser = user(appName);
currentUser = user(appName).get();
} else if (pat != null) {
// Cannot always trust username in PAT, e.g. Git Credential Manager
// on Windows always return "PersonalAccessToken" as username.
@@ -118,13 +118,22 @@ private HostUser parseUserDetails(JSONObject details) {
}

@Override
public HostUser user(String username) {
var details = request.get("users").param("username", username).execute().asArray();
if (details.size() != 1) {
throw new RuntimeException("Couldn't find user: " + username);
public Optional<HostUser> user(String username) {
var details = request.get("users")
.param("username", username)
.onError(r -> JSON.of())
.execute();

if (details.isNull()) {
return Optional.empty();
}

var users = details.asArray();
if (users.size() != 1) {
return Optional.empty();
}

return parseUserDetails(details.get(0).asObject());
return Optional.of(parseUserDetails(users.get(0).asObject()));
}

@Override
@@ -66,7 +66,7 @@ public String id() {

@Override
public HostUser author() {
return repository.forge().user(json.get("author").get("username").asString());
return repository.forge().user(json.get("author").get("username").asString()).get();
}

@Override
@@ -122,7 +122,7 @@ class CommitDate {
}
}
var id = obj.get("id").asInt();
return new Review(createdAt, reviewer, verdict, hash, id, null);
return new Review(createdAt, reviewer.get(), verdict, hash, id, null);
})
.collect(Collectors.toList());
}
@@ -624,7 +624,7 @@ public List<HostUser> assignees() {
var assignee = json.get("assignee").asObject();
if (assignee != null) {
var user = repository.forge().user(assignee.get("username").asString());
return List.of(user);
return List.of(user.get());
}
return Collections.emptyList();
}
@@ -22,9 +22,11 @@
*/
package org.openjdk.skara.host;

import java.util.Optional;

public interface Host {
boolean isValid();
HostUser user(String username);
Optional<HostUser> user(String username);
HostUser currentUser();
boolean isMemberOf(String groupId, HostUser user);
}
@@ -93,14 +93,19 @@ public IssueProject project(String name) {
}

@Override
public HostUser user(String username) {
public Optional<HostUser> user(String username) {
var data = request.get("user")
.param("username", username)
.onError(r -> JSON.of())
.execute();
if (data.isNull()) {
return Optional.empty();
}

var user = new HostUser(data.get("name").asString(),
data.get("name").asString(),
data.get("displayName").asString());
return user;
return Optional.of(user);
}

@Override
@@ -50,7 +50,7 @@ void simple(TestInfo info) throws IOException {

var userName = project.issueTracker().currentUser().userName();
var user = project.issueTracker().user(userName);
assertEquals(userName, user.userName());
assertEquals(userName, user.get().userName());

var issue = credentials.createIssue(project, "Test issue");
issue.setTitle("Updated title");
@@ -84,7 +84,7 @@ void addLink(TestInfo info) throws IOException {

var userName = project.issueTracker().currentUser().userName();
var user = project.issueTracker().user(userName);
assertEquals(userName, user.userName());
assertEquals(userName, user.get().userName());

var issue = credentials.createIssue(project, "Test issue");
issue.setBody("This is now the body");
@@ -115,11 +115,13 @@ public IssueProject project(String name) {
}

@Override
public HostUser user(String username) {
return data.users.stream()
.filter(user -> user.userName().equals(username))
.findAny()
.orElseThrow();
public Optional<HostUser> user(String username) {
return data.users
.stream()
.filter(user -> user.userName().equals(username))
.map(user -> Optional.of(user))
.findAny()
.orElseThrow();
}

@Override