Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6b243713d590b6f82695062c68f75ab3ae4aaadd (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
/*******************************************************************************
 *  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 com.google.gson.reflect.TypeToken;

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

import org.eclipse.core.runtime.Assert;

/**
 * Label service class for listing {@link Label} objects in use for a given user
 * and repository.
 */
public class LabelService {

	private GitHubClient client;

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

	/**
	 * Get labels
	 * 
	 * @param user
	 * @param repository
	 * @return list of labels
	 * @throws IOException
	 */
	public List<Label> getLabels(String user, String repository)
			throws IOException {
		Assert.isNotNull(user, "User cannot be null"); //$NON-NLS-1$
		Assert.isNotNull(repository, "Repository cannot be null"); //$NON-NLS-1$
		StringBuilder uri = new StringBuilder(IGitHubConstants.SEGMENT_REPOS);
		uri.append('/').append(user).append('/').append(repository);
		uri.append(IGitHubConstants.SEGMENT_LABELS).append(
				IGitHubConstants.SUFFIX_JSON);
		TypeToken<List<Label>> labelToken = new TypeToken<List<Label>>() {
		};
		return this.client.get(uri.toString(), labelToken.getType());
	}

	/**
	 * Set the labels for an issue
	 * 
	 * @param user
	 * @param repository
	 * @param issueId
	 * @param labels
	 * @return list of labels
	 * @throws IOException
	 */
	public List<Label> setLabels(String user, String repository,
			String issueId, List<Label> labels) throws IOException {
		Assert.isNotNull(user, "User cannot be null"); //$NON-NLS-1$
		Assert.isNotNull(repository, "Repository cannot be null"); //$NON-NLS-1$
		Assert.isNotNull(issueId, "Issue id cannot be null"); //$NON-NLS-1$
		StringBuilder uri = new StringBuilder(IGitHubConstants.SEGMENT_REPOS);
		uri.append('/').append(user).append('/').append(repository);
		uri.append(IGitHubConstants.SEGMENT_ISSUES).append('/').append(issueId);
		uri.append(IGitHubConstants.SEGMENT_LABELS).append(
				IGitHubConstants.SUFFIX_JSON);

		TypeToken<List<Label>> labelToken = new TypeToken<List<Label>>() {
		};
		return this.client.put(uri.toString(), labels, labelToken.getType());
	}

	/**
	 * Create label
	 * 
	 * @param user
	 * @param repository
	 * @param label
	 * @return created label
	 * @throws IOException
	 */
	public Label createLabel(String user, String repository, Label label)
			throws IOException {
		Assert.isNotNull(user, "User cannot be null"); //$NON-NLS-1$
		Assert.isNotNull(repository, "Repository cannot be null"); //$NON-NLS-1$
		Assert.isNotNull(label, "Label cannot be null"); //$NON-NLS-1$
		StringBuilder uri = new StringBuilder(IGitHubConstants.SEGMENT_REPOS);
		uri.append('/').append(user).append('/').append(repository);
		uri.append(IGitHubConstants.SEGMENT_LABELS).append(
				IGitHubConstants.SUFFIX_JSON);
		return this.client.post(uri.toString(), label, Label.class);
	}

}

Back to the top