Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 37622ada9bb3c04641fc5a8956d5b0684915d0bd (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
/*******************************************************************************
 *  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.mylyn.github.internal;

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

import org.eclipse.core.runtime.Assert;

/**
 * @author Kevin Sawicki (kevin@github.com)
 */
public class PullRequestService {

	/**
	 * Pull request wrapper
	 */
	private static class PullRequestWrapper {

		private PullRequest pull;

		public PullRequest getPull() {
			return this.pull;
		}

	}

	/**
	 * Pull requests wrapper
	 */
	private static class PullRequestsWrapper {

		private List<PullRequest> pulls;

		public List<PullRequest> getPulls() {
			return this.pulls;
		}

	}

	private GitHubClient client;

	/**
	 * Create pull request service
	 * 
	 * @param client
	 *            cannot be null
	 */
	public PullRequestService(GitHubClient client) {
		Assert.isNotNull(client, "Client cannot be null"); //$NON-NLS-1$
		this.client = client;
	}

	/**
	 * Get pull request from repository with id
	 * 
	 * @param repository
	 * @param id
	 * @return pull request
	 * @throws IOException
	 */
	public PullRequest getPullRequest(Repository repository, String id)
			throws IOException {
		Assert.isNotNull(repository, "Repository cannot be null"); //$NON-NLS-1$
		Assert.isNotNull(id, "Id cannot be null"); //$NON-NLS-1$
		String repositoryId = repository.getId();
		Assert.isNotNull(repositoryId, "Repository id cannot be null"); //$NON-NLS-1$
		StringBuilder uri = new StringBuilder(IGitHubConstants.SEGMENT_V2_API);
		uri.append(IGitHubConstants.SEGMENT_PULLS);
		uri.append('/').append(repositoryId);
		uri.append('/').append(id);
		PullRequestWrapper wrapper = this.client.get(uri.toString(),
				PullRequestWrapper.class);
		return wrapper.getPull();
	}

	/**
	 * Get pull requests from repository matching state
	 * 
	 * @param repository
	 * @param state
	 * @return list of pull requests
	 * @throws IOException
	 */
	public List<PullRequest> getPullRequests(Repository repository, String state)
			throws IOException {
		Assert.isNotNull(repository, "Repository cannot be null"); //$NON-NLS-1$
		Assert.isNotNull(state, "State cannot be null"); //$NON-NLS-1$
		StringBuilder uri = new StringBuilder(IGitHubConstants.SEGMENT_V2_API);
		uri.append(IGitHubConstants.SEGMENT_PULLS);
		uri.append('/').append(repository.getId());
		uri.append('/').append(state);
		PullRequestsWrapper wrapper = this.client.get(uri.toString(),
				PullRequestsWrapper.class);
		return wrapper.getPulls();
	}
}

Back to the top