8000 Adds methods to GHRepository to get the top ten referral paths and re… · hub4j/github-api@3e478c2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3e478c2

Browse files
ihrigbbitwiseman
andauthored
Adds methods to GHRepository to get the top ten referral paths and referrers via the API (#1770)
* Add methods to get the referral and referrer metrics * Add links * Apply code style * Add methods to get the referral and referrer metrics * Add links * Apply code style * Fix link tags * Fix javadocs * Make this work * Fix format * Enhance test coverage * Use hamcrest matchers * Clear formatting * Add javadoc to test classes * Avoid pagination when not required * Add testcases for methods * Add javadoc to test methods * Use java.util.Arrays --------- Co-authored-by: Liam Newman <bitwiseman@gmail.com>
1 parent 7faf9f0 commit 3e478c2

18 files changed

+889
-0
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.net.URL;
4242
import java.util.AbstractSet;
4343
import java.util.ArrayList;
44+
import java.util.Arrays;
4445
import java.util.Collection;
4546
import java.util.Collections;
4647
import java.util.Date;
@@ -3618,6 +3619,36 @@ public void unstar() throws IOException {
36183619
root().createRequest().method("DELETE").withUrlPath(String.format("/user/starred/%s", full_name)).send();
36193620
}
36203621

3622+
/**
3623+
* Get the top 10 popular contents over the last 14 days as described on
3624+
* https://docs.github.com/en/rest/metrics/traffic?apiVersion=2022-11-28#get-top-referral-paths
3625+
*
3626+
* @return list of top referral paths
3627+
* @throws IOException
3628+
* the io exception
3629+
*/
3630+
public List<GHRepositoryTrafficTopReferralPath> getTopReferralPaths() throws IOException {
3631+
return Arrays.asList(root().createRequest()
3632+
.method("GET")
3633+
.withUrlPath(getApiTailUrl("/traffic/popular/paths"))
3634+
.fetch(GHRepositoryTrafficTopReferralPath[].class));
3635+
}
3636+
3637+
/**
3638+
* Get the top 10 referrers over the last 14 days as described on
3639+
* https://docs.github.com/en/rest/metrics/traffic?apiVersion=2022-11-28#get-top-referral-sources
3640+
*
3641+
* @return list of top referrers
3642+
* @throws IOException
3643+
* the io exception
3644+
*/
3645+
public List<GHRepositoryTrafficTopReferralSources> getTopReferralSources() throws IOException {
3646+
return Arrays.asList(root().createRequest()
3647+
.method("GET")
3648+
.withUrlPath(getApiTailUrl("/traffic/popular/referrers"))
3649+
.fetch(GHRepositoryTrafficTopReferralSources[].class));
3650+
}
3651+
36213652
/**
36223653
* A {@link GHRepositoryBuilder} that allows multiple properties to be updated per request.
36233654
*
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.kohsuke.github;
2+
3+
/**
4+
* Base class for traffic referral objects.
5+
*/
6+
public class GHRepositoryTrafficReferralBase {
7+
private int count;
8+
private int uniques;
9+
10+
/**
11+
* Instantiates a new Gh repository traffic referral base.
12+
*/
13+
GHRepositoryTrafficReferralBase() {
14+
}
15+
16+
/**
17+
* Instantiates a new Gh repository traffic referral base.
18+
*
19+
* @param count
20+
* the count
21+
* @param uniques
22+
* the uniques
23+
*/
24+
GHRepositoryTrafficReferralBase(int count, int uniques) {
25+
this.count = count;
26+
this.uniques = uniques;
27+
}
28+
29+
/**
30+
* Gets count.
31+
*
32+
* @return the count
33+
*/
34+
public int getCount() {
35+
return this.count;
36+
}
37+
38+
/**
39+
* Gets uniques.
40+
*
41+
* @return the uniques
42+
*/
43+
public int getUniques() {
44+
return this.uniques;
45+
}
46+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.kohsuke.github;
2+
3+
/**
4+
* Top referral path object.
5+
*/
6+
public class GHRepositoryTrafficTopReferralPath extends GHRepositoryTrafficReferralBase {
7+
private String path;
8+
private String title;
9+
10+
/**
11+
* Instantiates a new Gh repository traffic top referral path.
12+
*/
13+
GHRepositoryTrafficTopReferralPath() {
14+
}
15+
16+
/**
17+
* Instantiates a new Gh repository traffic top referral path.
18+
*
19+
* @param count
20+
* the count
21+
* @param uniques
22+
* the uniques
23+
* @param path
24+
* the path
25+
* @param title
26+
* the title
27+
*/
28+
GHRepositoryTrafficTopReferralPath(int count, int uniques, String path, String title) {
29+
super(count, uniques);
30+
this.path = path;
31+
this.title = title;
32+
}
33+
34+
/**
35+
* Gets path.
36+
*
37+
* @return the path
38+
*/
39+
public String getPath() {
40+
return this.path;
41+
}
42+
43+
/**
44+
* Gets title.
45+
*
46+
* @return the title
47+
*/
48+
public String getTitle() {
49+
return this.title;
50+
}
51+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.kohsuke.github;
2+
3+
/**
4+
* Top referral source object.
5+
*/
6+
public class GHRepositoryTrafficTopReferralSources extends GHRepositoryTrafficReferralBase {
7+
private String referrer;
8+
9+
/**
10+
* Instantiates a new Gh repository traffic top referral sources.
11+
*/
12+
GHRepositoryTrafficTopReferralSources() {
13+
}
14+
15+
/**
16+
* Instantiates a new Gh repository traffic top referral sources.
17+
*
18+
* @param count
19+
* the count
20+
* @param uniques
21+
* the uniques
22+
* @param referrer
23+
* the referrer
24+
*/
25+
GHRepositoryTrafficTopReferralSources(int count, int uniques, String referrer) {
26+
super(count, uniques);
27+
this.referrer = referrer;
28+
}
29+
30+
/**
31+
* Gets referrer.
32+
*
33+
* @return the referrer
34+
*/
35+
public String getReferrer() {
36+
return this.referrer;
37+
}
38+
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,6 +1823,32 @@ public void testSearchPullRequests() throws Exception {
18231823
this.verifySingleResult(searchResult, mergedPR);
18241824
}
18251825

1826+
/**
1827+
* Test getTopReferralPaths.
1828+
*
1829+
* @throws Exception
1830+
* the exception
1831+
*/
1832+
@Test
1833+
public void testGetTopReferralPaths() throws Exception {
1834+
GHRepository repository = gitHub.getRepository("ihrigb/node-doorbird");
1835+
List<GHRepositoryTrafficTopReferralPath> referralPaths = repository.getTopReferralPaths();
1836+
assertThat(referralPaths.size(), greaterThan(0));
1837+
}
1838+
1839+
/**
1840+
* Test getTopReferralSources.
1841+
*
1842+
* @throws Exception
1843+
* the exception
1844+
*/
1845+
@Test
1846+
public void testGetTopReferralSources() throws Exception {
1847+
GHRepository repository = gitHub.getRepository("ihrigb/node-doorbird");
1848+
List<GHRepositoryTrafficTopReferralSources> referralSources = repository.getTopReferralSources();
1849+
assertThat(referralSources.size(), greaterThan(0));
1850+
}
1851+
18261852
private void verifyEmptyResult(PagedSearchIterable<GHPullRequest> searchResult) {
18271853
assertThat(searchResult.getTotalCount(), is(0));
18281854
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.kohsuke.github;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.MatcherAssert.assertThat;
6+
import static org.hamcrest.Matchers.equalTo;
7+
import static org.hamcrest.Matchers.is;
8+
9+
/**
10+
* Unit test for {@link GHRepositoryTrafficReferralBase}.
11+
*/
12+
public class GHRepositoryTrafficReferralBaseTest {
13+
14+
/**
15+
* Test the constructor.
16+
*/
17+
@Test
18+
public void test() {
19+
GHRepositoryTrafficReferralBase testee = new GHRepositoryTrafficReferralBase(1, 2);
20+
assertThat(testee.getCount(), is(equalTo(1)));
21+
assertThat(testee.getUniques(), is(equalTo(2)));
22+
}
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.kohsuke.github;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.MatcherAssert.assertThat;
6+
import static org.hamcrest.Matchers.equalTo;
7+
import static org.hamcrest.Matchers.is;
8+
9+
/**
10+
* Unit tests for {@link GHRepositoryTrafficTopReferralPath}.
11+
*/
12+
public class GHRepositoryTrafficTopReferralPathTest {
13+
14+
/**
15+
* Test the constructor.
16+
*/
17+
@Test
18+
public void test() {
19+
GHRepositoryTrafficTopReferralPath testee = new GHRepositoryTrafficTopReferralPath(1, 2, "path", "title");
20+
assertThat(testee.getCount(), is(equalTo(1)));
21+
assertThat(testee.getUniques(), is(equalTo(2)));
22+
assertThat(testee.getPath(), is(equalTo("path")));
23+
assertThat(testee.getTitle(), is(equalTo("title")));
24+
}
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.kohsuke.github;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.MatcherAssert.assertThat;
6+
import static org.hamcrest.Matchers.equalTo;
7+
import static org.hamcrest.Matchers.is;
8+
9+
/**
10+
* Unit test for {@link GHRepositoryTrafficTopReferralSources}.
11+
*/
12+
public class GHRepositoryTrafficTopReferralSourcesTest {
13+
14+
/**
15+
* Test the constructor.
16+
*/
17+
@Test
18+
public void test() {
19+
GHRepositoryTrafficTopReferralSources testee = new GHRepositoryTrafficTopReferralSources(1, 2, "referrer");
20+
assertThat(testee.getCount(), is(equalTo(1)));
21+
assertThat(testee.getUniques(), is(equalTo(2)));
22+
assertThat(testee.getReferrer(), is(equalTo("referrer")));
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"login": "ihrigb",
3+
"id": 3423161,
4+
"node_id": "MDQ6VXNlcjM0MjMxNjE=",
5+
"avatar_url": "https://avatars.githubusercontent.com/u/3423161?v=4",
6+
"gravatar_id": "",
7+
"url": "https://api.github.com/users/ihrigb",
8+
"html_url": "https://github.com/ihrigb",
9+
"followers_url": "https://api.github.com/users/ihrigb/followers",
10+
"following_url": "https://api.github.com/users/ihrigb/following{/other_user}",
11+
"gists_url": "https://api.github.com/users/ihrigb/gists{/gist_id}",
12+
"starred_url": "https://api.github.com/users/ihrigb/starred{/owner}{/repo}",
13+
"subscriptions_url": "https://api.github.com/users/ihrigb/subscriptions",
14+
"organizations_url": "https://api.github.com/users/ihrigb/orgs",
15+
"repos_url": "https://api.github.com/users/ihrigb/repos",
16+
"events_url": "https://api.github.com/users/ihrigb/events{/privacy}",
17+
"received_events_url": "https://api.github.com/users/ihrigb/received_events",
18+
"type": "User",
19+
"site_admin": false,
20+
"name": "Benjamin Ihrig",
21+
"company": "SAP SE",
22+
"blog": "",
23+
"location": "Germany",
24+
"email": null,
25+
"hireable": null,
26+
"bio": "Working at @SAP.\r\nDoing software projects for a volunteer fire brigade in free time. Smart home enthusiast. Scuba diving instructor.",
27+
"twitter_username": null,
28+
"public_repos": 26,
29+
"public_gists": 1,
30+
"followers": 5,
31+
"following": 20,
32+
"created_at": "2013-01-30T02:20:16Z",
33+
"updated_at": "2024-02-23T21:51:51Z"
34+
}

0 commit comments

Comments
 (0)
0