Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1e3f1288af5b4ed85e7a51da609d673d8f87b0b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/******************************************************************************
 *  Copyright (c) 2011 GitHub Inc.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  Contributors:
 *    Kevin Sawicki (GitHub Inc.) - initial API and implementation
 *****************************************************************************/
package org.eclipse.egit.github.core.service;

import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_COLLABORATORS;
import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_REPOS;

import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;

import org.eclipse.egit.github.core.IRepositoryIdProvider;
import org.eclipse.egit.github.core.User;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.client.PagedRequest;

/**
 * Service for interacting with the collaborators on a GitHub repository
 *
 * @see <a href="http://developer.github.com/v3/repos/collaborators/">GitHub
 *      collaborator API documentation</a>
 */
public class CollaboratorService extends GitHubService {

	/**
	 * Create collaborator service
	 */
	public CollaboratorService() {
		super();
	}

	/**
	 * Create collaborator service
	 *
	 * @param client
	 */
	public CollaboratorService(GitHubClient client) {
		super(client);
	}

	/**
	 * Get collaborators for given repository
	 *
	 * @param repository
	 * @return non-null list of collaborators
	 * @throws IOException
	 */
	public List<User> getCollaborators(IRepositoryIdProvider repository)
			throws IOException {
		String id = getId(repository);
		StringBuilder uri = new StringBuilder(SEGMENT_REPOS);
		uri.append('/').append(id);
		uri.append(SEGMENT_COLLABORATORS);
		PagedRequest<User> request = createPagedRequest();
		request.setUri(uri);
		request.setType(new TypeToken<List<User>>() {
		}.getType());
		return getAll(request);
	}

	/**
	 * Create URI for updating collaborators
	 *
	 * @param repository
	 * @param user
	 * @return URI
	 */
	protected String createUpdateUri(IRepositoryIdProvider repository,
			String user) {
		String id = getId(repository);
		if (user == null)
			throw new IllegalArgumentException("User cannot be null"); //$NON-NLS-1$
		if (user.length() == 0)
			throw new IllegalArgumentException("User cannot be empty"); //$NON-NLS-1$

		StringBuilder uri = new StringBuilder(SEGMENT_REPOS);
		uri.append('/').append(id);
		uri.append(SEGMENT_COLLABORATORS);
		uri.append('/').append(user);
		return uri.toString();
	}

	/**
	 * Is given user a collaborator on the given repository?
	 *
	 * @param repository
	 * @param user
	 * @return true if collaborator, false otherwise
	 * @throws IOException
	 */
	public boolean isCollaborator(IRepositoryIdProvider repository, String user)
			throws IOException {
		return check(createUpdateUri(repository, user));
	}

	/**
	 * Add given user as a collaborator on the given repository
	 *
	 * @param repository
	 * @param user
	 * @throws IOException
	 */
	public void addCollaborator(IRepositoryIdProvider repository, String user)
			throws IOException {
		client.put(createUpdateUri(repository, user));
	}

	/**
	 * Remove given user as a collaborator on the given repository
	 *
	 * @param repository
	 * @param user
	 * @throws IOException
	 */
	public void removeCollaborator(IRepositoryIdProvider repository, String user)
			throws IOException {
		client.delete(createUpdateUri(repository, user));
	}
}

Back to the top