-
Notifications
You must be signed in to change notification settings - Fork 711
/
GHPreReceiveHooks.java
142 lines (114 loc) · 4.06 KB
/
GHPreReceiveHooks.java
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package org.kohsuke.github;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class GHPreReceiveHooks {
static abstract class Context extends GitHubInteractiveObject {
private Context(GitHub root) {
super(root);
}
/**
* Collection.
*
* @return the string
*/
abstract String collection();
/**
* Collection class.
*
* @return the class<? extends GH pre-receive hook[]>
*/
abstract Class<? extends GHPreReceiveHook[]> collectionClass();
/**
* Clazz.
*
* @return the class<? extends GH pre-receive hook>
*/
abstract Class<? extends GHPreReceiveHook> clazz();
/**
* Wrap.
*
* @param hook
* the hook
* @return the GH hook
*/
abstract GHPreReceiveHook wrap(GHPreReceiveHook hook);
abstract Map<String, Object> hookExtraParameters(GHPreReceiveHook hook);
public GHPreReceiveHook configurePreReceiveHook(GHPreReceiveHook hook) throws IOException {
GHPreReceiveHook updatedHook = root().createRequest()
.method("PATCH")
.with("enforcement", hook.getEnforcement())
.with(hookExtraParameters(hook))
.withUrlPath(collection(), Long.toString(hook.getId()))
.fetch(clazz());
return wrap(updatedHook);
}
}
private static class OrgContext extends Context {
private final GHOrganization organization;
private OrgContext(GHOrganization organization) {
super(organization.root());
this.organization = organization;
}
@Override
String collection() {
return String.format("/orgs/%s/pre-receive-hooks", organization.login);
}
@Override
Class<? extends GHPreReceiveHook[]> collectionClass() {
return GHOrgPreReceiveHook[].class;
}
@Override
Class<? extends GHPreReceiveHook> clazz() {
return GHOrgPreReceiveHook.class;
}
@Override
GHPreReceiveHook wrap(GHPreReceiveHook hook) {
return ((GHOrgPreReceiveHook) hook).wrap(organization);
}
@Override
Map<String, Object> hookExtraParameters(GHPreReceiveHook hook) {
final Map<String, Object> params = new HashMap<>();
if (hook instanceof GHOrgPreReceiveHook) {
params.put("allow_downstream_configuration",
Boolean.valueOf(((GHOrgPreReceiveHook) hook).allowDownstreamConfiguration));
}
return params;
}
}
private static class RepositoryContext extends Context {
private final GHRepository repository;
private final GHUser owner;
private RepositoryContext(GHRepository repository, GHUser owner) {
super(repository.root());
this.repository = repository;
this.owner = owner;
}
String collection() {
return String.format("/repos/%s/%s/pre-receive-hooks", owner.getLogin(), repository.getName());
}
@Override
Class<? extends GHPreReceiveHook[]> collectionClass() {
return GHRepoPreReceiveHook[].class;
}
@Override
Class<? extends GHPreReceiveHook> clazz() {
return GHRepoPreReceiveHook.class;
}
@Override
GHPreReceiveHook wrap(GHPreReceiveHook hook) {
return ((GHRepoPreReceiveHook) hook).wrap(repository);
}
@Override
Map<String, Object> hookExtraParameters(GHPreReceiveHook hook) {
return Collections.emptyMap();
}
}
static Context repoContext(GHRepository repository, GHUser owner) {
return new RepositoryContext(repository, owner);
}
static Context orgContext(GHOrganization organization) {
return new OrgContext(organization);
}
}