All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jdroid.github.service.CollaboratorService Maven / Gradle / Ivy

There is a newer version: 0.9.4
Show newest version
/******************************************************************************
 *  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 com.jdroid.github.service;

import static com.jdroid.github.client.IGitHubConstants.SEGMENT_COLLABORATORS;
import static com.jdroid.github.client.IGitHubConstants.SEGMENT_REPOS;

import com.google.gson.reflect.TypeToken;

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

import com.jdroid.github.IRepositoryIdProvider;
import com.jdroid.github.User;
import com.jdroid.github.client.GitHubClient;
import com.jdroid.github.client.PagedRequest;

/**
 * Service for interacting with the collaborators on a GitHub repository
 *
 * @see GitHub
 *      collaborator API documentation
 */
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 getCollaborators(IRepositoryIdProvider repository)
			throws IOException {
		String id = getId(repository);
		StringBuilder uri = new StringBuilder(SEGMENT_REPOS);
		uri.append('/').append(id);
		uri.append(SEGMENT_COLLABORATORS);
		PagedRequest request = createPagedRequest();
		request.setUri(uri);
		request.setType(new TypeToken>() {
		}.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));
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy