Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b7de435

Browse filesBrowse files
committed
Alternative proposal
The guts of this version are a bit ugly but they result reasonable API code without a ton of extra code needed.
1 parent 2607d6a commit b7de435
Copy full SHA for b7de435

File tree

Expand file treeCollapse file tree

3 files changed

+122
-57
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+122
-57
lines changed

‎src/main/java/org/kohsuke/github/GHLabel.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHLabel.java
+117-51Lines changed: 117 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package org.kohsuke.github;
22

33
import com.fasterxml.jackson.annotation.JacksonInject;
4-
import com.fasterxml.jackson.annotation.JsonCreator;
54

65
import java.io.IOException;
76
import java.util.ArrayList;
87
import java.util.Collection;
98
import java.util.List;
109
import java.util.Objects;
11-
import java.util.function.Consumer;
1210

1311
import javax.annotation.Nonnull;
1412

@@ -68,11 +66,11 @@ public String getDescription() {
6866
* 6-letter hex color code, like "f29513"
6967
* @throws IOException
7068
* the io exception
71-
* @deprecated use {@link #update(Consumer)} instead
69+
* @deprecated use {@link #set()} instead
7270
*/
7371
@Deprecated
7472
public void setColor(String newColor) throws IOException {
75-
this.update(i -> i.color(newColor));
73+
this.set().color(newColor);
7674
}
7775

7876
/**
@@ -82,11 +80,11 @@ public void setColor(String newColor) throws IOException {
8280
* Description of label
8381
* @throws IOException
8482
* the io exception
85-
* @deprecated use {@link #update(Consumer)} instead
83+
* @deprecated use {@link #set()} instead
8684
*/
8785
@Deprecated
8886
public void setDescription(String newDescription) throws IOException {
89-
this.update(i -> i.description(newDescription));
87+
this.set().description(newDescription);
9088
}
9189

9290
static Collection<String> toNames(Collection<GHLabel> labels) {
@@ -99,16 +97,20 @@ static Collection<String> toNames(Collection<GHLabel> labels) {
9997

10098
// NEW IMPLEMENTATION STARTS HERE
10199

100+
@JacksonInject
102101
@Nonnull
103-
private final GitHub root;
102+
protected final GitHub root;
104103

105104
@Nonnull
106105
private final String url, name, color, description;
107106

108-
private GHRepository repository;
107+
protected GHRepository repository;
109108

110-
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
111-
private GHLabel(@Nonnull Builder builder) {
109+
GHLabel() {
110+
this(new GHLabelBuilder());
111+
}
112+
113+
private GHLabel(@Nonnull GHLabelBuilder builder) {
112114
this.root = builder.root;
113115
this.url = builder.url;
114116
this.name = builder.name;
@@ -122,18 +124,8 @@ private GHLabel(@Nonnull Builder builder) {
122124
* @throws IOException
123125
* the io exception
124126
*/
125-
public static GHLabel create(GHRepository repository, Consumer<Builder> initializer) throws IOException {
126-
Builder builder = new Builder();
127-
initializer.accept(builder);
128-
return repository.root.createRequest()
129-
.withUrlPath(repository.getApiTailUrl("labels"))
130-
.method("POST")
131-
.with("name", builder.name)
132-
.with("color", builder.color)
133-
.with("description", builder.description)
134-
.fetch(GHLabel.class)
135-
.lateBind(repository);
136-
127+
public static Creator create(GHRepository repository) throws IOException {
128+
return new Creator(repository);
137129
}
138130

139131
/**
@@ -212,18 +204,18 @@ GHLabel lateBind(GHRepository repo) {
212204
* @throws IOException
213205
* the io exception
214206
*/
215-
public GHLabel update(Consumer<Builder> updater) throws IOException {
216-
Builder builder = new Builder(this);
217-
updater.accept(builder);
207+
public BatchUpdater update() throws IOException {
208+
return new BatchUpdater(this);
209+
}
218210

219-
return repository.root.createRequest()
220-
.method("PATCH")
221-
.with("name", builder.name)
222-
.with("color", builder.color)
223-
.with("description", builder.description)
224-
.setRawUrlPath(url)
225-
.fetch(GHLabel.class)
226-
.lateBind(repository);
211+
/**
212+
* Modifies a label in a repository.
213+
*
214+
* @throws IOException
215+
* the io exception
216+
*/
217+
public SingleUpdater set() throws IOException {
218+
return new SingleUpdater(this);
227219
}
228220

229221
/**
@@ -253,41 +245,115 @@ public int hashCode() {
253245
return Objects.hash(url, name, color, repository);
254246
}
255247

256-
public static class Builder {
248+
public static class SingleUpdater extends Builder<GHLabel> {
249+
private SingleUpdater(GHLabel base) throws IOException {
250+
super(base, true);
251+
requester.method("PATCH").setRawUrlPath(base.url());
252+
}
253+
}
254+
255+
public static class BatchUpdater extends Builder<BatchUpdater> {
256+
private BatchUpdater(GHLabel base) throws IOException {
257+
super(base, false);
258+
requester.method("PATCH").setRawUrlPath(base.url());
259+
}
260+
}
261+
262+
public static class Creator extends Builder<Creator> {
263+
private Creator(GHRepository repository) throws IOException {
264+
super(repository);
265+
requester.method("POST").withUrlPath(repository.getApiTailUrl("labels"));
266+
}
267+
}
268+
269+
public static class GHLabelBuilder {
257270
private String url, name, color, description;
258271

259272
@JacksonInject
260273
private GitHub root;
261274

262-
public Builder() {
275+
public GHLabelBuilder() {
276+
root = null;
263277
url = "";
264278
name = "";
265279
color = "";
266280
description = "";
267281
}
282+
}
283+
284+
public static class Builder<U> extends BaseBuilder<GHLabel, U> {
285+
286+
final GHRepository repository;
287+
288+
public Builder(GHLabel label, boolean immediate) throws IOException {
289+
super(label.root, GHLabel.class, label, immediate);
290+
repository = label.repository;
291+
}
292+
293+
public Builder(GHRepository repository) throws IOException {
294+
super(repository.root, GHLabel.class, new GHLabel(new GHLabelBuilder()), false);
295+
this.repository = repository;
296+
}
297+
298+
public U name(String value) throws IOException {
299+
return with("name", value);
300+
}
301+
302+
public U color(String value) throws IOException {
303+
return with("color", value);
304+
}
268305

269-
public Builder(GHLabel label) {
270-
this.root = label.root;
271-
// Url is maintained on the mutator but cannot be changed locally.
272-
url = label.url();
273-
name = label.name();
274-
color = label.color();
275-
description = label.description();
306+
public U description(String value) throws IOException {
307+
return with("description", value);
276308
}
277309

278-
public Builder name(String value) {
279-
name = value;
280-
return this;
310+
@Override
311+
protected void initialize(GHLabel base) throws IOException {
312+
// Set initial values
313+
name(base.name());
314+
color(base.color());
315+
description(base.description());
281316
}
282317

283-
public Builder color(String value) {
284-
color = value;
285-
return this;
318+
@Override
319+
public GHLabel done() throws IOException {
320+
return requester.fetch(returnType).lateBind(repository);
286321
}
322+
}
323+
324+
/**
325+
*
326+
* @param <T>
327+
* @param <U>
328+
*/
329+
public abstract static class BaseBuilder<T, U> {
330+
331+
private final boolean initialized;
332+
private final boolean immediate;
333+
protected final Class<T> returnType;
334+
protected final Requester requester;
335+
336+
protected BaseBuilder(GitHub root, Class<T> returnType, T base, boolean immediate) throws IOException {
337+
this.requester = root.createRequest();
338+
this.immediate = immediate;
339+
this.returnType = returnType;
340+
initialize(base);
341+
this.initialized = true;
342+
}
343+
344+
public abstract T done() throws IOException;
345+
346+
protected abstract void initialize(T base) throws IOException;
287347

288-
public Builder description(String value) {
289-
description = value;
290-
return this;
348+
protected U with(String name, Object value) throws IOException {
349+
requester.with(name, value);
350+
if (initialized) {
351+
if (immediate) {
352+
return (U) done();
353+
}
354+
return (U) this;
355+
}
356+
return null;
291357
}
292358
}
293359
}

‎src/main/java/org/kohsuke/github/GHRepository.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHRepository.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1900,7 +1900,7 @@ public GHLabel getLabel(String name) throws IOException {
19001900
* the io exception
19011901
*/
19021902
public GHLabel createLabel(String name, String color) throws IOException {
1903-
return GHLabel.create(this, l -> l.name(name).color(color));
1903+
return GHLabel.create(this).name(name).color(color).done();
19041904
}
19051905

19061906
/**
@@ -1917,7 +1917,7 @@ public GHLabel createLabel(String name, String color) throws IOException {
19171917
* the io exception
19181918
*/
19191919
public GHLabel createLabel(String name, String color, String description) throws IOException {
1920-
return GHLabel.create(this, l -> l.name(name).color(color).description(description));
1920+
return GHLabel.create(this).name(name).color(color).description(description).done();
19211921
}
19221922

19231923
/**

‎src/test/java/org/kohsuke/github/AppTest.java

Copy file name to clipboardExpand all lines: src/test/java/org/kohsuke/github/AppTest.java
+3-4Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -814,8 +814,8 @@ public void testRepoLabel() throws IOException {
814814
assertEquals(t.url(), t2.url());
815815

816816
// update works on multiple changes in one call
817-
t.setColor("");
818-
t2 = t.update(i -> i.color("000000").description("It is dark!"));
817+
// t.setColor("");
818+
t2 = t.update().color("000000").description("It is dark!").done();
819819

820820
// instances are immutable, but update returns a new updated instance.
821821
assertEquals(t.color(), "123456");
@@ -824,9 +824,8 @@ public void testRepoLabel() throws IOException {
824824
assertEquals(t2.description(), "It is dark!");
825825

826826
t = r.getLabel("test");
827-
t.update(i -> i.description("this is also a test"));
827+
GHLabel t3 = t.set().description("this is also a test");
828828

829-
GHLabel t3 = r.getLabel("test");
830829
assertEquals(t3.color(), "000000");
831830
assertEquals(t3.description(), "this is also a test");
832831
t.delete();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.