Skip to content
Browse files

Added some privilege mgmt operations

  • Loading branch information...
1 parent 2dc7e13 commit 7f316fdb039c1312069d6215cf0fbf82adcc9a6b @ericbottard committed Jan 16, 2013
View
5 .settings/org.eclipse.wst.common.component
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
+ <wb-module deploy-name="spring-social-bitbucket">
+ <wb-resource deploy-path="/" source-path="/src/main/java"/>
+ </wb-module>
+</project-modules>
View
5 .settings/org.eclipse.wst.common.project.facet.core.xml
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<faceted-project>
+ <installed facet="java" version="1.6"/>
+ <installed facet="jst.utility" version="1.0"/>
+</faceted-project>
View
6 src/main/java/org/springframework/social/bitbucket/api/BitBucket.java
@@ -36,4 +36,10 @@
* accounts.
*/
UserOperations userOperations();
+
+ /**
+ * Returns the portion of the BitBucket API that allow messing with
+ * privileges.
+ */
+ PrivilegeOperations privelegesOperations();
}
View
12 src/main/java/org/springframework/social/bitbucket/api/BitBucketPrivilege.java
@@ -0,0 +1,12 @@
+package org.springframework.social.bitbucket.api;
+
+/**
+ * The type of action that can be taken on a repository.
+ *
+ * @author Eric Bottard
+ */
+public enum BitBucketPrivilege {
+
+ read, write, admin;
+
+}
View
45 src/main/java/org/springframework/social/bitbucket/api/PrivilegeOperations.java
@@ -0,0 +1,45 @@
+package org.springframework.social.bitbucket.api;
+
+import java.util.List;
+
+public interface PrivilegeOperations {
+
+ /**
+ * Get the privileges on a given repository.
+ *
+ * @param user
+ * repository owner
+ * @param repoSlug
+ * name of the repository
+ */
+ public List<RepoPrivilege> getRepoPrivileges(String user, String repoSlug);
+
+ /**
+ * Set or change the privilege for the given recipient on some repository.
+ *
+ * @param owner
+ * the repository owner
+ * @param repoSlug
+ * the repository name
+ * @param recipient
+ * user for which to set the privilege
+ * @param privilege
+ * new privilege value to set
+ */
+ public RepoPrivilege setPrivilege(String owner, String repoSlug,
+ String recipient, BitBucketPrivilege privilege);
+
+ /**
+ * Removes the current privilege of the given recipient from some
+ * repository.
+ *
+ * @param owner
+ * the repository owner
+ * @param repoSlug
+ * the repository name
+ * @param recipient
+ * user for which to remove the privilege
+ */
+ public void removePrivilege(String owner, String repoSlug, String recipient);
+
+}
View
30 src/main/java/org/springframework/social/bitbucket/api/RepoPrivilege.java
@@ -0,0 +1,30 @@
+package org.springframework.social.bitbucket.api;
+
+import org.codehaus.jackson.annotate.JsonIgnoreProperties;
+import org.codehaus.jackson.annotate.JsonProperty;
+
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class RepoPrivilege {
+
+ @JsonProperty("repo")
+ private String repository;
+
+ @JsonProperty
+ private BitBucketPrivilege privilege;
+
+ @JsonProperty
+ private BitBucketUser user;
+
+ public String getRepository() {
+ return repository;
+ }
+
+ public BitBucketPrivilege getPrivilege() {
+ return privilege;
+ }
+
+ public BitBucketUser getUser() {
+ return user;
+ }
+
+}
View
8 src/main/java/org/springframework/social/bitbucket/api/impl/AbstractBitBucketOperations.java
@@ -15,14 +15,20 @@
*/
package org.springframework.social.bitbucket.api.impl;
+import org.springframework.web.client.RestTemplate;
+
public class AbstractBitBucketOperations {
protected final boolean authorized;
+ protected final RestTemplate restTemplate;
+
private static final String BASE_API_URL = "https://api.bitbucket.org/1.0";
- public AbstractBitBucketOperations(boolean authorized) {
+ public AbstractBitBucketOperations(RestTemplate restTemplate,
+ boolean authorized) {
this.authorized = authorized;
+ this.restTemplate = restTemplate;
}
protected String buildUrl(String string) {
View
10 src/main/java/org/springframework/social/bitbucket/api/impl/BitBucketTemplate.java
@@ -16,6 +16,7 @@
package org.springframework.social.bitbucket.api.impl;
import org.springframework.social.bitbucket.api.BitBucket;
+import org.springframework.social.bitbucket.api.PrivilegeOperations;
import org.springframework.social.bitbucket.api.RepoOperations;
import org.springframework.social.bitbucket.api.UserOperations;
import org.springframework.social.oauth1.AbstractOAuth1ApiBinding;
@@ -27,6 +28,8 @@
private RepoOperations repoOperations;
+ private PrivilegeOperations privilegeOperations;
+
public BitBucketTemplate(String consumerKey, String consumerSecret,
String accessToken, String accessTokenSecret) {
super(consumerKey, consumerSecret, accessToken, accessTokenSecret);
@@ -41,6 +44,8 @@ public BitBucketTemplate() {
private void initSubApis() {
userOperations = new UserTemplate(getRestTemplate(), isAuthorized());
repoOperations = new RepoTemplate(getRestTemplate(), isAuthorized());
+ privilegeOperations = new PrivilegeTemplate(getRestTemplate(),
+ isAuthorized());
}
@Override
@@ -53,4 +58,9 @@ public UserOperations userOperations() {
return userOperations;
}
+ @Override
+ public PrivilegeOperations privelegesOperations() {
+ return privilegeOperations;
+ }
+
}
View
45 src/main/java/org/springframework/social/bitbucket/api/impl/PrivilegeTemplate.java
@@ -0,0 +1,45 @@
+package org.springframework.social.bitbucket.api.impl;
+
+import static java.util.Arrays.*;
+
+import java.util.List;
+
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpMethod;
+import org.springframework.social.bitbucket.api.BitBucketPrivilege;
+import org.springframework.social.bitbucket.api.PrivilegeOperations;
+import org.springframework.social.bitbucket.api.RepoPrivilege;
+import org.springframework.web.client.RestTemplate;
+
+public class PrivilegeTemplate extends AbstractBitBucketOperations implements
+ PrivilegeOperations {
+
+ public PrivilegeTemplate(RestTemplate restTemplate, boolean authorized) {
+ super(restTemplate, authorized);
+ }
+
+ @Override
+ public List<RepoPrivilege> getRepoPrivileges(String user, String repoSlug) {
+ return asList(restTemplate.getForObject(
+ buildUrl("/privileges/{user}/{repo_slug}"),
+ RepoPrivilege[].class, user, repoSlug));
+ }
+
+ @Override
+ public RepoPrivilege setPrivilege(String owner, String repoSlug,
+ String recipient, BitBucketPrivilege privilege) {
+
+ return restTemplate.exchange(
+ buildUrl("/privileges/{user}/{repo_slug}/{recipient}"),
+ HttpMethod.PUT, new HttpEntity<String>(privilege.toString()),
+ RepoPrivilege[].class, owner, repoSlug, recipient).getBody()[0];
+ }
+
+ @Override
+ public void removePrivilege(String owner, String repoSlug, String recipient) {
+ restTemplate.delete(
+ buildUrl("/privileges/{user}/{repo_slug}/{recipient}"), owner,
+ repoSlug, recipient);
+ }
+
+}
View
5 src/main/java/org/springframework/social/bitbucket/api/impl/RepoTemplate.java
@@ -36,11 +36,8 @@
public class RepoTemplate extends AbstractBitBucketOperations implements
RepoOperations {
- private final RestTemplate restTemplate;
-
public RepoTemplate(RestTemplate restTemplate, boolean authorized) {
- super(authorized);
- this.restTemplate = restTemplate;
+ super(restTemplate, authorized);
}
@Override
View
5 src/main/java/org/springframework/social/bitbucket/api/impl/UserTemplate.java
@@ -27,11 +27,8 @@
class UserTemplate extends AbstractBitBucketOperations implements
UserOperations {
- private final RestTemplate restTemplate;
-
public UserTemplate(RestTemplate restTemplate, boolean authorized) {
- super(authorized);
- this.restTemplate = restTemplate;
+ super(restTemplate, authorized);
}
@Override
View
62 src/test/java/org/springframework/social/bitbucket/api/impl/PrivilegeTemplateTest.java
@@ -0,0 +1,62 @@
+package org.springframework.social.bitbucket.api.impl;
+
+import static org.junit.Assert.*;
+import static org.springframework.http.HttpMethod.*;
+import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
+import static org.springframework.test.web.client.response.MockRestResponseCreators.*;
+
+import java.util.List;
+
+import org.junit.Test;
+import org.springframework.http.MediaType;
+import org.springframework.social.bitbucket.api.BitBucketPrivilege;
+import org.springframework.social.bitbucket.api.RepoPrivilege;
+
+public class PrivilegeTemplateTest extends BaseTemplateTest {
+ @Test
+ public void testGetRepoPrivileges() {
+
+ mockServer
+ .expect(requestTo("https://api.bitbucket.org/1.0/privileges/evzijst/test"))
+ .andExpect(method(GET))
+ .andRespond(
+ withSuccess(jsonResource("get-repo-privileges"),
+ MediaType.APPLICATION_JSON));
+
+ List<RepoPrivilege> privs = bitBucket.privelegesOperations()
+ .getRepoPrivileges("evzijst", "test");
+
+ assertEquals(4, privs.size());
+ assertEquals("evzijst/test", privs.get(0).getRepository());
+ assertEquals("jespern", privs.get(0).getUser().getUsername());
+ assertEquals(BitBucketPrivilege.read, privs.get(0).getPrivilege());
+
+ }
+
+ @Test
+ public void testDeletePrivilege() {
+ mockServer
+ .expect(requestTo("https://api.bitbucket.org/1.0/privileges/evzijst/test/jespern"))
+ .andExpect(method(DELETE)).andRespond(withNoContent());
+
+ bitBucket.privelegesOperations().removePrivilege("evzijst", "test",
+ "jespern");
+ }
+
+ @Test
+ public void testSetPrivilege() {
+ mockServer
+ .expect(requestTo("https://api.bitbucket.org/1.0/privileges/tutorials/mydvcsproject/atlassian_tutorial"))
+ .andExpect(method(PUT))
+ .andExpect(content().string("write"))
+ .andRespond(
+ withSuccess(jsonResource("set-repo-privilege"),
+ MediaType.APPLICATION_JSON));
+
+ RepoPrivilege priv = bitBucket.privelegesOperations().setPrivilege(
+ "tutorials", "mydvcsproject", "atlassian_tutorial",
+ BitBucketPrivilege.write);
+
+ assertEquals("Atlassian Tutorials", priv.getUser().getFirstName());
+ }
+}
View
38 src/test/resources/org/springframework/social/bitbucket/api/impl/get-repo-privileges.json
@@ -0,0 +1,38 @@
+[
+ {
+ "repo": "evzijst/test",
+ "privilege": "read",
+ "user": {
+ "username": "jespern",
+ "first_name": "Jesper",
+ "last_name": "Noehr"
+ }
+ },
+ {
+ "repo": "evzijst/test",
+ "privilege": "read",
+ "user": {
+ "username": "detkin",
+ "first_name": "Dylan",
+ "last_name": "Etkin"
+ }
+ },
+ {
+ "repo": "evzijst/test",
+ "privilege": "write",
+ "user": {
+ "username": "davidchambers",
+ "first_name": "David",
+ "last_name": "Chambers"
+ }
+ },
+ {
+ "repo": "evzijst/test",
+ "privilege": "admin",
+ "user": {
+ "username": "nvenegas",
+ "first_name": "Nicolas",
+ "last_name": "Venegas"
+ }
+ }
+]
View
24 src/test/resources/org/springframework/social/bitbucket/api/impl/set-repo-privilege.json
@@ -0,0 +1,24 @@
+[
+ {
+ "repo": "tutorials/mydvcsproject",
+ "privilege": "write",
+ "user": {
+ "username": "atlassian_tutorial",
+ "first_name": "Atlassian Tutorials",
+ "last_name": "",
+ "avatar": "https://secure.gravatar.com/avatar/eb4e0ad6934518b3e335345a4ceeef21?d=https%3A%2F%2Fdwz7u9t8u8usb.cloudfront.net%2Fm%2F8319cb1efac9%2Fimg%2Fteam_no_avatar_32.png&s=32",
+ "is_team": true
+ },
+ "repository": {
+ "owner": {
+ "username": "tutorials",
+ "first_name": "",
+ "last_name": "",
+ "avatar": "https://secure.gravatar.com/avatar/0bc5bd490000b8e63c35c0f54e667b9e?d=identicon&s=32",
+ "is_team": false
+ },
+ "name": "mydvcsproject",
+ "slug": "mydvcsproject"
+ }
+ }
+]

0 comments on commit 7f316fd

Please sign in to comment.
Something went wrong with that request. Please try again.