Skip to content
Permalink
Browse files
forge: add methods addCollaborator and canPush
Reviewed-by: rwestberg
  • Loading branch information
edvbld committed Mar 10, 2021
1 parent 2e89f01 commit a1337d80d8aa6e5d7ccc48aa73d00a22b60f66b1
@@ -23,6 +23,7 @@
package org.openjdk.skara.bots.tester;

import org.openjdk.skara.forge.*;
import org.openjdk.skara.host.HostUser;
import org.openjdk.skara.json.JSONValue;
import org.openjdk.skara.vcs.*;

@@ -194,4 +195,13 @@ public URI createPullRequestUrl(HostedRepository target, String sourceRef, Strin
public URI webUrl(Branch branch) {
return null;
}

@Override
public void addCollaborator(HostUser user, boolean canPush) {
}

@Override
public boolean canPush(HostUser user) {
return false;
}
}
@@ -22,6 +22,7 @@
*/
package org.openjdk.skara.forge;

import org.openjdk.skara.host.HostUser;
import org.openjdk.skara.json.JSONValue;
import org.openjdk.skara.vcs.*;

@@ -82,6 +83,8 @@ default List<CommitComment> recentCommitComments() {
URI createPullRequestUrl(HostedRepository target,
String targetRef,
String sourceRef);
void addCollaborator(HostUser user, boolean canPush);
boolean canPush(HostUser user);

default PullRequest createPullRequest(HostedRepository target,
String targetRef,
@@ -23,6 +23,7 @@
package org.openjdk.skara.forge.github;

import org.openjdk.skara.forge.*;
import org.openjdk.skara.host.HostUser;
import org.openjdk.skara.json.*;
import org.openjdk.skara.network.*;
import org.openjdk.skara.vcs.*;
@@ -513,4 +514,22 @@ public URI createPullRequestUrl(HostedRepository target, String targetRef, Strin
var endpoint = "/" + target.name() + "/" + targetRef + "..." + sourceGroup + ":" + sourceRef;
return gitHubHost.getWebURI(endpoint);
}

@Override
public void addCollaborator(HostUser user, boolean canPush) {
var query = JSON.object().put("permission", canPush ? "push" : "pull");
request.put("collaborators/" + user.username())
.body(query)
.execute();

}

@Override
public boolean canPush(HostUser user) {
var permission = request.get("collaborators/" + user.username())
.execute()
.get("permission")
.asString();
return permission.equals("admin") || permission.equals("write");
}
}
@@ -547,4 +547,22 @@ public URI createPullRequestUrl(HostedRepository target, String targetRef, Strin
"&merge_request[target_branch]=" + targetRef;
return gitLabHost.getWebUri(endpoint);
}

@Override
public void addCollaborator(HostUser user, boolean canPush) {
var accessLevel = canPush ? "30" : "20";
var data = "user_id=" + user.id() + "&access_level=" + accessLevel;
request.post("members")
.body(data)
.execute();
}

@Override
public boolean canPush(HostUser user) {
var accessLevel = request.get("members/" + user.id())
.execute()
.get("access_level")
.asInt();
return accessLevel >= 30;
}
}
@@ -23,6 +23,7 @@
package org.openjdk.skara.test;

import org.openjdk.skara.forge.*;
import org.openjdk.skara.host.HostUser;
import org.openjdk.skara.issuetracker.Issue;
import org.openjdk.skara.json.JSONValue;
import org.openjdk.skara.vcs.*;
@@ -41,6 +42,7 @@ public class TestHostedRepository extends TestIssueProject implements HostedRepo
private final Repository localRepository;
private final Pattern pullRequestPattern;
private final Map<Hash, List<CommitComment>> commitComments;
private Map<String, Boolean> collaborators = new HashMap<>();

public TestHostedRepository(TestHost host, String projectName, Repository localRepository) {
super(host, projectName);
@@ -275,6 +277,16 @@ public URI webUrl(Branch branch) {
return URI.create(webUrl() + "/branch/" + branch.name());
}

@Override
public void addCollaborator(HostUser user, boolean canPush) {
collaborators.put(user.username(), canPush);
}

@Override
public boolean canPush(HostUser user) {
return collaborators.getOrDefault(user.username(), false);
}

Repository localRepository() {
return localRepository;
}

1 comment on commit a1337d8

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on a1337d8 Mar 10, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.