|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2010, Kohsuke Kawaguchi |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | +package org.kohsuke.github; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.net.URL; |
| 28 | +import java.util.Collection; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.HashSet; |
| 31 | +import java.util.Set; |
| 32 | + |
| 33 | +import static java.util.Arrays.asList; |
| 34 | + |
| 35 | +/** |
| 36 | + * @author Kohsuke Kawaguchi |
| 37 | + */ |
| 38 | +public class GHRepository { |
| 39 | + /*package almost final*/ GitHub root; |
| 40 | + |
| 41 | + private String description, homepage, url, name, owner; |
| 42 | + private boolean has_issues, has_wiki, fork, _private, has_downloads; |
| 43 | + private int watchers,forks; |
| 44 | + |
| 45 | + public String getDescription() { |
| 46 | + return description; |
| 47 | + } |
| 48 | + |
| 49 | + public String getHomepage() { |
| 50 | + return homepage; |
| 51 | + } |
| 52 | + |
| 53 | + public String getUrl() { |
| 54 | + return url; |
| 55 | + } |
| 56 | + |
| 57 | + public String getName() { |
| 58 | + return name; |
| 59 | + } |
| 60 | + |
| 61 | + public GHUser getOwner() throws IOException { |
| 62 | + return root.getUser(owner); |
| 63 | + } |
| 64 | + |
| 65 | + public boolean hasIssues() { |
| 66 | + return has_issues; |
| 67 | + } |
| 68 | + |
| 69 | + public boolean hasWiki() { |
| 70 | + return has_wiki; |
| 71 | + } |
| 72 | + |
| 73 | + public boolean isFork() { |
| 74 | + return fork; |
| 75 | + } |
| 76 | + |
| 77 | + public int getForks() { |
| 78 | + return forks; |
| 79 | + } |
| 80 | + |
| 81 | + public boolean isPrivate() { |
| 82 | + return _private; |
| 83 | + } |
| 84 | + |
| 85 | + public boolean hasDownloads() { |
| 86 | + return has_downloads; |
| 87 | + } |
| 88 | + |
| 89 | + public int getWatchers() { |
| 90 | + return watchers; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Gets the collaborators on this repository. |
| 95 | + * This set always appear to include the owner. |
| 96 | + */ |
| 97 | + public Set<GHUser> getCollaborators() throws IOException { |
| 98 | + Set<GHUser> r = new HashSet<GHUser>(); |
| 99 | + for (String u : root.retrieve("/repos/show/"+owner+"/"+name+"/collaborators",JsonCollaborators.class).collaborators) |
| 100 | + r.add(root.getUser(u)); |
| 101 | + return Collections.unmodifiableSet(r); |
| 102 | + } |
| 103 | + |
| 104 | + public void addCollaborators(GHUser... users) throws IOException { |
| 105 | + addCollaborators(asList(users)); |
| 106 | + } |
| 107 | + |
| 108 | + public void addCollaborators(Collection<GHUser> users) throws IOException { |
| 109 | + modifyCollaborators(users, "/add/"); |
| 110 | + } |
| 111 | + |
| 112 | + public void removeCollaborators(GHUser... users) throws IOException { |
| 113 | + removeCollaborators(asList(users)); |
| 114 | + } |
| 115 | + |
| 116 | + public void removeCollaborators(Collection<GHUser> users) throws IOException { |
| 117 | + modifyCollaborators(users, "/remove/"); |
| 118 | + } |
| 119 | + |
| 120 | + private void modifyCollaborators(Collection<GHUser> users, String op) throws IOException { |
| 121 | + root.requireCredential(); |
| 122 | + verifyMine(); |
| 123 | + for (GHUser user : users) { |
| 124 | + new Poster(root).withCredential().to(root.getApiURL("/repos/collaborators/"+name+ op +user.getLogin())); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Deletes this repository. |
| 130 | + */ |
| 131 | + public void delete() throws IOException { |
| 132 | + root.requireCredential(); |
| 133 | + verifyMine(); |
| 134 | + Poster poster = new Poster(root).withCredential(); |
| 135 | + URL url = root.getApiURL("/repos/delete/" + name); |
| 136 | + |
| 137 | + DeleteToken token = poster.to(url, DeleteToken.class); |
| 138 | + poster.with("delete_token",token.delete_token).to(url); |
| 139 | + } |
| 140 | + |
| 141 | + private void verifyMine() throws IOException { |
| 142 | + if (!root.login.equals(owner)) |
| 143 | + throw new IOException("Operation not applicable to a repository owned by someone else: "+owner); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public String toString() { |
| 148 | + return "Repository:"+owner+":"+name; |
| 149 | + } |
| 150 | +} |
0 commit comments