Skip to content

Commit a115f34

Browse files
authored
Merge pull request hub4j#637 from PauloMigAlmeida/marketplace_endpoints_patch2
[Patch 2/2] :: Add support to Marketplace endpoints
2 parents 9da487d + cd66c1e commit a115f34

9 files changed

Lines changed: 356 additions & 34 deletions

File tree

src/main/java/org/kohsuke/github/GHMarketplaceAccount.java

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@
33
import java.net.URL;
44

55
/**
6-
* A Github Marketplace Account.
6+
* Base class for Github Marketplace Account.
77
*
88
* @author Paulo Miguel Almeida
9+
* @see GitHub#getMyMarketplacePurchases()
910
* @see GHMarketplaceListAccountBuilder#createRequest()
1011
*/
1112
public class GHMarketplaceAccount {
1213

13-
private GitHub root;
14+
protected GitHub root;
1415
private String url;
1516
private long id;
1617
private String login;
1718
private String email;
1819
private String organizationBillingEmail;
1920
private GHMarketplaceAccountType type;
20-
private GHMarketplacePendingChange marketplacePendingChange;
21-
private GHMarketplacePurchase marketplacePurchase;
2221

2322
/**
2423
* Wrap up gh marketplace account.
@@ -29,12 +28,6 @@ public class GHMarketplaceAccount {
2928
*/
3029
GHMarketplaceAccount wrapUp(GitHub root) {
3130
this.root = root;
32-
if (this.marketplacePendingChange != null)
33-
this.marketplacePendingChange.wrapUp(this.root);
34-
35-
if (this.marketplacePurchase != null)
36-
this.marketplacePurchase.wrapUp(this.root);
37-
3831
return this;
3932
}
4033

@@ -92,21 +85,4 @@ public GHMarketplaceAccountType getType() {
9285
return type;
9386
}
9487

95-
/**
96-
* Gets marketplace pending change.
97-
*
98-
* @return the marketplace pending change
99-
*/
100-
public GHMarketplacePendingChange getMarketplacePendingChange() {
101-
return marketplacePendingChange;
102-
}
103-
104-
/**
105-
* Gets marketplace purchase.
106-
*
107-
* @return the marketplace purchase
108-
*/
109-
public GHMarketplacePurchase getMarketplacePurchase() {
110-
return marketplacePurchase;
111-
}
11288
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.kohsuke.github;
2+
3+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
4+
5+
/**
6+
* A Github Marketplace Account Plan.
7+
*
8+
* @author Paulo Miguel Almeida
9+
* @see GHMarketplaceListAccountBuilder#createRequest()
10+
*/
11+
public class GHMarketplaceAccountPlan extends GHMarketplaceAccount {
12+
13+
@SuppressFBWarnings(value = "UWF_UNWRITTEN_FIELD", justification = "Field comes from JSON deserialization")
14+
private GHMarketplacePendingChange marketplacePendingChange;
15+
@SuppressFBWarnings(value = "UWF_UNWRITTEN_FIELD", justification = "Field comes from JSON deserialization")
16+
private GHMarketplacePurchase marketplacePurchase;
17+
18+
/**
19+
* Wrap up gh marketplace account.
20+
*
21+
* @param root
22+
* the root
23+
* @return an instance of the GHMarketplaceAccount class
24+
*/
25+
GHMarketplaceAccountPlan wrapUp(GitHub root) {
26+
super.wrapUp(root);
27+
if (this.marketplacePendingChange != null)
28+
this.marketplacePendingChange.wrapUp(this.root);
29+
30+
if (this.marketplacePurchase != null)
31+
this.marketplacePurchase.wrapUp(this.root);
32+
33+
return this;
34+
}
35+
36+
/**
37+
* Gets marketplace pending change.
38+
*
39+
* @return the marketplace pending change
40+
*/
41+
public GHMarketplacePendingChange getMarketplacePendingChange() {
42+
return marketplacePendingChange;
43+
}
44+
45+
/**
46+
* Gets marketplace purchase.
47+
*
48+
* @return the marketplace purchase
49+
*/
50+
public GHMarketplacePurchase getMarketplacePurchase() {
51+
return marketplacePurchase;
52+
}
53+
}

src/main/java/org/kohsuke/github/GHMarketplaceListAccountBuilder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ public enum Sort {
5959
* <p>
6060
* OAuth Apps must use basic authentication with their client ID and client secret to access this endpoint.
6161
*
62-
* @return a paged iterable instance of GHMarketplaceAccount
62+
* @return a paged iterable instance of GHMarketplaceAccountPlan
6363
* @throws IOException
6464
* on error
6565
*/
66-
public PagedIterable<GHMarketplaceAccount> createRequest() throws IOException {
66+
public PagedIterable<GHMarketplaceAccountPlan> createRequest() throws IOException {
6767
return builder.asPagedIterable(String.format("/marketplace_listing/plans/%d/accounts", this.planId),
68-
GHMarketplaceAccount[].class,
68+
GHMarketplaceAccountPlan[].class,
6969
item -> item.wrapUp(root));
7070
}
7171

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package org.kohsuke.github;
2+
3+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
4+
5+
import java.util.Date;
6+
7+
/**
8+
* Github Marketplace User Purchase
9+
*
10+
* @author Paulo Miguel Almeida
11+
* @see GitHub#getMyMarketplacePurchases()
12+
*/
13+
public class GHMarketplaceUserPurchase {
14+
protected GitHub root;
15+
private String billingCycle;
16+
private String nextBillingDate;
17+
private boolean onFreeTrial;
18+
private String freeTrialEndsOn;
19+
private Long unitCount;
20+
private String updatedAt;
21+
@SuppressFBWarnings(value = "UWF_UNWRITTEN_FIELD", justification = "Field comes from JSON deserialization")
22+
private GHMarketplaceAccount account;
23+
@SuppressFBWarnings(value = "UWF_UNWRITTEN_FIELD", justification = "Field comes from JSON deserialization")
24+
private GHMarketplacePlan plan;
25+
26+
/**
27+
* Wrap up GHMarketplaceUserPurchase.
28+
*
29+
* @param root
30+
* the root
31+
* @return an instance of the GHMarketplaceUserPurchase class
32+
*/
33+
GHMarketplaceUserPurchase wrapUp(GitHub root) {
34+
this.root = root;
35+
if (this.account != null)
36+
this.account.wrapUp(this.root);
37+
if (this.plan != null)
38+
this.plan.wrapUp(this.root);
39+
return this;
40+
}
41+
42+
/**
43+
* Gets billing cycle.
44+
*
45+
* @return the billing cycle
46+
*/
47+
public String getBillingCycle() {
48+
return billingCycle;
49+
}
50+
51+
/**
52+
* Gets next billing date.
53+
*
54+
* @return the next billing date
55+
*/
56+
public Date getNextBillingDate() {
57+
return GitHub.parseDate(nextBillingDate);
58+
}
59+
60+
/**
61+
* Is on free trial boolean.
62+
*
63+
* @return the boolean
64+
*/
65+
public boolean isOnFreeTrial() {
66+
return onFreeTrial;
67+
}
68+
69+
/**
70+
* Gets free trial ends on.
71+
*
72+
* @return the free trial ends on
73+
*/
74+
public Date getFreeTrialEndsOn() {
75+
return GitHub.parseDate(freeTrialEndsOn);
76+
}
77+
78+
/**
79+
* Gets unit count.
80+
*
81+
* @return the unit count
82+
*/
83+
public Long getUnitCount() {
84+
return unitCount;
85+
}
86+
87+
/**
88+
* Gets updated at.
89+
*
90+
* @return the updated at
91+
*/
92+
public Date getUpdatedAt() {
93+
return GitHub.parseDate(updatedAt);
94+
}
95+
96+
/**
97+
* Gets account.
98+
*
99+
* @return the account
100+
*/
101+
public GHMarketplaceAccount getAccount() {
102+
return account;
103+
}
104+
105+
/**
106+
* Gets plan.
107+
*
108+
* @return the plan
109+
*/
110+
public GHMarketplacePlan getPlan() {
111+
return plan;
112+
}
113+
}

src/main/java/org/kohsuke/github/GitHub.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,26 @@ public Map<String, GHOrganization> getMyOrganizations() throws IOException {
758758
return r;
759759
}
760760

761+
/**
762+
* Returns only active subscriptions.
763+
* <p>
764+
* You must use a user-to-server OAuth access token, created for a user who has authorized your GitHub App, to
765+
* access this endpoint
766+
* <p>
767+
* OAuth Apps must authenticate using an OAuth token.
768+
*
769+
* @return the paged iterable of GHMarketplaceUserPurchase
770+
* @throws IOException
771+
* the io exception
772+
* @see <a href="https://developer.github.com/v3/apps/marketplace/#get-a-users-marketplace-purchases">Get a user's
773+
* Marketplace purchases</a>
774+
*/
775+
public PagedIterable<GHMarketplaceUserPurchase> getMyMarketplacePurchases() throws IOException {
776+
return createRequest().asPagedIterable("/user/marketplace_purchases",
777+
GHMarketplaceUserPurchase[].class,
778+
item -> item.wrapUp(this));
779+
}
780+
761781
/**
762782
* Alias for {@link #getUserPublicOrganizations(String)}.
763783
*

src/test/java/org/kohsuke/github/GHMarketplacePlanTest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void listMarketplacePlans() throws IOException {
3434
public void listAccounts() throws IOException {
3535
List<GHMarketplacePlan> plans = gitHub.listMarketplacePlans().asList();
3636
assertEquals(3, plans.size());
37-
List<GHMarketplaceAccount> marketplaceUsers = plans.get(0).listAccounts().createRequest().asList();
37+
List<GHMarketplaceAccountPlan> marketplaceUsers = plans.get(0).listAccounts().createRequest().asList();
3838
assertEquals(2, marketplaceUsers.size());
3939
marketplaceUsers.forEach(this::testMarketplaceAccount);
4040
}
@@ -45,7 +45,10 @@ public void listAccountsWithDirection() throws IOException {
4545
assertEquals(3, plans.size());
4646

4747
for (GHMarketplacePlan plan : plans) {
48-
List<GHMarketplaceAccount> marketplaceUsers = plan.listAccounts().direction(DESC).createRequest().asList();
48+
List<GHMarketplaceAccountPlan> marketplaceUsers = plan.listAccounts()
49+
.direction(DESC)
50+
.createRequest()
51+
.asList();
4952
assertEquals(2, marketplaceUsers.size());
5053
marketplaceUsers.forEach(this::testMarketplaceAccount);
5154
}
@@ -58,7 +61,7 @@ public void listAccountsWithSortAndDirection() throws IOException {
5861
assertEquals(3, plans.size());
5962

6063
for (GHMarketplacePlan plan : plans) {
61-
List<GHMarketplaceAccount> marketplaceUsers = plan.listAccounts()
64+
List<GHMarketplaceAccountPlan> marketplaceUsers = plan.listAccounts()
6265
.sort(UPDATED)
6366
.direction(DESC)
6467
.createRequest()
@@ -87,7 +90,7 @@ private void testMarketplacePlan(GHMarketplacePlan plan) {
8790
assertEquals(2, plan.getBullets().size());
8891
}
8992

90-
private void testMarketplaceAccount(GHMarketplaceAccount account) {
93+
private void testMarketplaceAccount(GHMarketplaceAccountPlan account) {
9194
// Non-nullable fields
9295
assertNotNull(account.getLogin());
9396
assertNotNull(account.getUrl());

src/test/java/org/kohsuke/github/GitHubTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.*;
99

1010
import static org.hamcrest.CoreMatchers.*;
11+
import static org.kohsuke.github.GHMarketplaceAccountType.ORGANIZATION;
1112

1213
/**
1314
* Unit test for {@link GitHub}.
@@ -108,4 +109,49 @@ public void getMeta() throws IOException {
108109
assertEquals(19, metaExample.getWeb().size());
109110
}
110111
}
112+
113+
@Test
114+
public void getMyMarketplacePurchases() throws IOException {
115+
List<GHMarketplaceUserPurchase> userPurchases = gitHub.getMyMarketplacePurchases().asList();
116+
assertEquals(2, userPurchases.size());
117+
118+
for (GHMarketplaceUserPurchase userPurchase : userPurchases) {
119+
assertFalse(userPurchase.isOnFreeTrial());
120+
assertNull(userPurchase.getFreeTrialEndsOn());
121+
assertEquals("monthly", userPurchase.getBillingCycle());
122+
123+
GHMarketplacePlan plan = userPurchase.getPlan();
124+
// GHMarketplacePlan - Non-nullable fields
125+
assertNotNull(plan.getUrl());
126+
assertNotNull(plan.getAccountsUrl());
127+
assertNotNull(plan.getName());
128+
assertNotNull(plan.getDescription());
129+
assertNotNull(plan.getPriceModel());
130+
assertNotNull(plan.getState());
131+
132+
// GHMarketplacePlan - primitive fields
133+
assertNotEquals(0L, plan.getId());
134+
assertNotEquals(0L, plan.getNumber());
135+
assertTrue(plan.getMonthlyPriceInCents() >= 0);
136+
137+
// GHMarketplacePlan - list
138+
assertEquals(2, plan.getBullets().size());
139+
140+
GHMarketplaceAccount account = userPurchase.getAccount();
141+
// GHMarketplaceAccount - Non-nullable fields
142+
assertNotNull(account.getLogin());
143+
assertNotNull(account.getUrl());
144+
assertNotNull(account.getType());
145+
146+
// GHMarketplaceAccount - primitive fields
147+
assertNotEquals(0L, account.getId());
148+
149+
/* logical combination tests */
150+
// Rationale: organization_billing_email is only set when account type is ORGANIZATION.
151+
if (account.getType() == ORGANIZATION)
152+
assertNotNull(account.getOrganizationBillingEmail());
153+
else
154+
assertNull(account.getOrganizationBillingEmail());
155+
}
156+
}
111157
}

0 commit comments

Comments
 (0)