|
| 1 | +package org.kohsuke.github; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.Collection; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Locale; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +/** |
| 12 | + * Utility class for creating and retrieving webhooks; removes duplication between GHOrganization and GHRepository |
| 13 | + * functionality |
| 14 | + */ |
| 15 | +class GHHooks { |
| 16 | + static abstract class Context { |
| 17 | + private final GitHub root; |
| 18 | + |
| 19 | + private Context(GitHub root) { |
| 20 | + this.root = root; |
| 21 | + } |
| 22 | + |
| 23 | + public List<GHHook> getHooks() throws IOException { |
| 24 | + List<GHHook> list = new ArrayList<GHHook>(Arrays.asList( |
| 25 | + root.retrieve().to(collection(), collectionClass()))); |
| 26 | + for (GHHook h : list) |
| 27 | + wrap(h); |
| 28 | + return list; |
| 29 | + } |
| 30 | + |
| 31 | + public GHHook getHook(int id) throws IOException { |
| 32 | + GHHook hook = root.retrieve().to(collection() + "/" + id, clazz()); |
| 33 | + return wrap(hook); |
| 34 | + } |
| 35 | + |
| 36 | + public GHHook createHook(String name, Map<String, String> config, Collection<GHEvent> events, boolean active) throws IOException { |
| 37 | + List<String> ea = null; |
| 38 | + if (events!=null) { |
| 39 | + ea = new ArrayList<String>(); |
| 40 | + for (GHEvent e : events) |
| 41 | + ea.add(e.name().toLowerCase(Locale.ENGLISH)); |
| 42 | + } |
| 43 | + |
| 44 | + GHHook hook = new Requester(root) |
| 45 | + .with("name", name) |
| 46 | + .with("active", active) |
| 47 | + ._with("config", config) |
| 48 | + ._with("events", ea) |
| 49 | + .to(collection(), clazz()); |
| 50 | + |
| 51 | + return wrap(hook); |
| 52 | + } |
| 53 | + |
| 54 | + abstract String collection(); |
| 55 | + |
| 56 | + abstract Class<? extends GHHook[]> collectionClass(); |
| 57 | + |
| 58 | + abstract Class<? extends GHHook> clazz(); |
| 59 | + |
| 60 | + abstract GHHook wrap(GHHook hook); |
| 61 | + } |
| 62 | + |
| 63 | + private static class RepoContext extends Context { |
| 64 | + private final GHRepository repository; |
| 65 | + private final GHUser owner; |
| 66 | + |
| 67 | + private RepoContext(GHRepository repository, GHUser owner) { |
| 68 | + super(repository.root); |
| 69 | + this.repository = repository; |
| 70 | + this.owner = owner; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + String collection() { |
| 75 | + return String.format("/repos/%s/%s/hooks", owner.getLogin(), repository.getName()); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + Class<? extends GHHook[]> collectionClass() { |
| 80 | + return GHRepoHook[].class; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + Class<? extends GHHook> clazz() { |
| 85 | + return GHRepoHook.class; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + GHHook wrap(GHHook hook) { |
| 90 | + return ((GHRepoHook)hook).wrap(repository); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private static class OrgContext extends Context { |
| 95 | + private final GHOrganization organization; |
| 96 | + |
| 97 | + private OrgContext(GHOrganization organization) { |
| 98 | + super(organization.root); |
| 99 | + this.organization = organization; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + String collection() { |
| 104 | + return String.format("/orgs/%s/hooks", organization.getLogin()); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + Class<? extends GHHook[]> collectionClass() { |
| 109 | + return GHOrgHook[].class; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + Class<? extends GHHook> clazz() { |
| 114 | + return GHOrgHook.class; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + GHHook wrap(GHHook hook) { |
| 119 | + return ((GHOrgHook)hook).wrap(organization); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + static Context repoContext(GHRepository repository, GHUser owner) { |
| 124 | + return new RepoContext(repository, owner); |
| 125 | + } |
| 126 | + |
| 127 | + static Context orgContext(GHOrganization organization) { |
| 128 | + return new OrgContext(organization); |
| 129 | + } |
| 130 | +} |
0 commit comments