|
| 1 | +package org.kohsuke.github; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.URL; |
| 5 | +import java.util.Locale; |
| 6 | + |
| 7 | +/** |
| 8 | + * Represents a membership of a user in an organization. |
| 9 | + * |
| 10 | + * @author Kohsuke Kawaguchi |
| 11 | + * @see GHMyself#listOrgMemberships() |
| 12 | + */ |
| 13 | +public class GHMembership /* extends GHObject --- but it doesn't have id, created_at, etc. */ { |
| 14 | + GitHub root; |
| 15 | + |
| 16 | + String url; |
| 17 | + String state; |
| 18 | + String role; |
| 19 | + GHUser user; |
| 20 | + GHOrganization organization; |
| 21 | + |
| 22 | + public URL getUrl() { |
| 23 | + return GitHub.parseURL(url); |
| 24 | + } |
| 25 | + |
| 26 | + public State getState() { |
| 27 | + return Enum.valueOf(State.class, state.toUpperCase(Locale.ENGLISH)); |
| 28 | + } |
| 29 | + |
| 30 | + public Role getRole() { |
| 31 | + return Enum.valueOf(Role.class, role.toUpperCase(Locale.ENGLISH)); |
| 32 | + } |
| 33 | + |
| 34 | + public GHUser getUser() { |
| 35 | + return user; |
| 36 | + } |
| 37 | + |
| 38 | + public GHOrganization getOrganization() { |
| 39 | + return organization; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Accepts a pending invitation to an organization. |
| 44 | + * |
| 45 | + * @see GHMyself#getMembership(GHOrganization) |
| 46 | + */ |
| 47 | + public void activate() throws IOException { |
| 48 | + root.retrieve().method("PATCH").with("state",State.ACTIVE).to(url,this); |
| 49 | + } |
| 50 | + |
| 51 | + /*package*/ GHMembership wrap(GitHub root) { |
| 52 | + this.root = root; |
| 53 | + if (user!=null) user = root.getUser(user.wrapUp(root)); |
| 54 | + if (organization!=null) organization.wrapUp(root); |
| 55 | + return this; |
| 56 | + } |
| 57 | + |
| 58 | + /*package*/ static void wrap(GHMembership[] page, GitHub root) { |
| 59 | + for (GHMembership m : page) |
| 60 | + m.wrap(root); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Role of a user in an organization. |
| 65 | + */ |
| 66 | + public enum Role { |
| 67 | + /** |
| 68 | + * Organization owner. |
| 69 | + */ |
| 70 | + ADMIN, |
| 71 | + /** |
| 72 | + * Non-owner organization member. |
| 73 | + */ |
| 74 | + MEMBER; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Whether a role is currently active or waiting for acceptance (pending) |
| 79 | + */ |
| 80 | + public enum State { |
| 81 | + ACTIVE, |
| 82 | + PENDING; |
| 83 | + } |
| 84 | +} |
0 commit comments