aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Wolf2018-05-08 15:30:37 -0400
committerThomas Wolf2018-05-08 15:30:37 -0400
commita039ba440106f7fcc4e6fa718771ce3a8d822377 (patch)
treec0dd453cd7f348e9ae31a397c1409dee9bf66b7c
parent5fad06251c0b68aa4743fbfde25ad474d73f1a46 (diff)
downloadegit-github-a039ba440106f7fcc4e6fa718771ce3a8d822377.zip
egit-github-a039ba440106f7fcc4e6fa718771ce3a8d822377.tar.gz
egit-github-a039ba440106f7fcc4e6fa718771ce3a8d822377.tar.xz
Fix Repository.getWatchers(); add Repository.getStars()
Per [1] it appears that the JSON returned by Github changed, apparently already in 2012. What Github shows today as the number of "watchers" is stored in field "subscribers_count". Field "watchers" actually is a copy of "stargazers_count" (as is "watchers_count"). So add fields "stargazersCount" and "subscribersCount", but keep the legacy "watchers" field to be able to deal with old serialized (via Gson or via Java serialization) objects. Add tests, including a new test for Gson de-serialization. The test data in test_repo.json is anonymized actual output for an existing repository obtained from the Github API as of 2018-05-08. [1] https://developer.github.com/changes/2012-09-05-watcher-api/ Bug: 534440 Change-Id: I1faba649986ab6ed9e367bfefdb0bbe2fbaf7071 Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
-rw-r--r--org.eclipse.egit.github.core.tests/.classpath1
-rw-r--r--org.eclipse.egit.github.core.tests/build.properties3
-rw-r--r--org.eclipse.egit.github.core.tests/resources/org/eclipse/egit/github/core/tests/test_repo.json98
-rw-r--r--org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/RepositoryTest.java19
-rw-r--r--org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/Repository.java29
5 files changed, 147 insertions, 3 deletions
diff --git a/org.eclipse.egit.github.core.tests/.classpath b/org.eclipse.egit.github.core.tests/.classpath
index b862a29..1747f41 100644
--- a/org.eclipse.egit.github.core.tests/.classpath
+++ b/org.eclipse.egit.github.core.tests/.classpath
@@ -3,5 +3,6 @@
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
+ <classpathentry kind="src" path="resources"/>
<classpathentry kind="output" path="bin"/>
</classpath>
diff --git a/org.eclipse.egit.github.core.tests/build.properties b/org.eclipse.egit.github.core.tests/build.properties
index aa1a008..84f1c95 100644
--- a/org.eclipse.egit.github.core.tests/build.properties
+++ b/org.eclipse.egit.github.core.tests/build.properties
@@ -1,4 +1,5 @@
-source.. = src/
+source.. = src/,\
+ resources/
output.. = bin/
bin.includes = META-INF/,\
.,\
diff --git a/org.eclipse.egit.github.core.tests/resources/org/eclipse/egit/github/core/tests/test_repo.json b/org.eclipse.egit.github.core.tests/resources/org/eclipse/egit/github/core/tests/test_repo.json
new file mode 100644
index 0000000..545ddfe
--- /dev/null
+++ b/org.eclipse.egit.github.core.tests/resources/org/eclipse/egit/github/core/tests/test_repo.json
@@ -0,0 +1,98 @@
+{
+ "id": 1234567,
+ "name": "some-repo",
+ "full_name": "some-user/some-repo",
+ "owner": {
+ "login": "some-user",
+ "id": 123456,
+ "avatar_url": "",
+ "gravatar_id": "",
+ "url": "https://api.github.com/users/some-user",
+ "html_url": "https://github.com/some-user",
+ "followers_url": "https://api.github.com/users/some-user/followers",
+ "following_url": "https://api.github.com/users/some-user/following{/other_user}",
+ "gists_url": "https://api.github.com/users/some-user/gists{/gist_id}",
+ "starred_url": "https://api.github.com/users/some-user/starred{/owner}{/repo}",
+ "subscriptions_url": "https://api.github.com/users/some-user/subscriptions",
+ "organizations_url": "https://api.github.com/users/some-user/orgs",
+ "repos_url": "https://api.github.com/users/some-user/repos",
+ "events_url": "https://api.github.com/users/some-user/events{/privacy}",
+ "received_events_url": "https://api.github.com/users/some-user/received_events",
+ "type": "User",
+ "site_admin": false
+ },
+ "private": false,
+ "html_url": "https://github.com/some-user/some-repo",
+ "description": "Some fictitious test repo",
+ "fork": false,
+ "url": "https://api.github.com/repos/some-user/some-repo",
+ "forks_url": "https://api.github.com/repos/some-user/some-repo/forks",
+ "keys_url": "https://api.github.com/repos/some-user/some-repo/keys{/key_id}",
+ "collaborators_url": "https://api.github.com/repos/some-user/some-repo/collaborators{/collaborator}",
+ "teams_url": "https://api.github.com/repos/some-user/some-repo/teams",
+ "hooks_url": "https://api.github.com/repos/some-user/some-repo/hooks",
+ "issue_events_url": "https://api.github.com/repos/some-user/some-repo/issues/events{/number}",
+ "events_url": "https://api.github.com/repos/some-user/some-repo/events",
+ "assignees_url": "https://api.github.com/repos/some-user/some-repo/assignees{/user}",
+ "branches_url": "https://api.github.com/repos/some-user/some-repo/branches{/branch}",
+ "tags_url": "https://api.github.com/repos/some-user/some-repo/tags",
+ "blobs_url": "https://api.github.com/repos/some-user/some-repo/git/blobs{/sha}",
+ "git_tags_url": "https://api.github.com/repos/some-user/some-repo/git/tags{/sha}",
+ "git_refs_url": "https://api.github.com/repos/some-user/some-repo/git/refs{/sha}",
+ "trees_url": "https://api.github.com/repos/some-user/some-repo/git/trees{/sha}",
+ "statuses_url": "https://api.github.com/repos/some-user/some-repo/statuses/{sha}",
+ "languages_url": "https://api.github.com/repos/some-user/some-repo/languages",
+ "stargazers_url": "https://api.github.com/repos/some-user/some-repo/stargazers",
+ "contributors_url": "https://api.github.com/repos/some-user/some-repo/contributors",
+ "subscribers_url": "https://api.github.com/repos/some-user/some-repo/subscribers",
+ "subscription_url": "https://api.github.com/repos/some-user/some-repo/subscription",
+ "commits_url": "https://api.github.com/repos/some-user/some-repo/commits{/sha}",
+ "git_commits_url": "https://api.github.com/repos/some-user/some-repo/git/commits{/sha}",
+ "comments_url": "https://api.github.com/repos/some-user/some-repo/comments{/number}",
+ "issue_comment_url": "https://api.github.com/repos/some-user/some-repo/issues/comments{/number}",
+ "contents_url": "https://api.github.com/repos/some-user/some-repo/contents/{+path}",
+ "compare_url": "https://api.github.com/repos/some-user/some-repo/compare/{base}...{head}",
+ "merges_url": "https://api.github.com/repos/some-user/some-repo/merges",
+ "archive_url": "https://api.github.com/repos/some-user/some-repo/{archive_format}{/ref}",
+ "downloads_url": "https://api.github.com/repos/some-user/some-repo/downloads",
+ "issues_url": "https://api.github.com/repos/some-user/some-repo/issues{/number}",
+ "pulls_url": "https://api.github.com/repos/some-user/some-repo/pulls{/number}",
+ "milestones_url": "https://api.github.com/repos/some-user/some-repo/milestones{/number}",
+ "notifications_url": "https://api.github.com/repos/some-user/some-repo/notifications{?since,all,participating}",
+ "labels_url": "https://api.github.com/repos/some-user/some-repo/labels{/name}",
+ "releases_url": "https://api.github.com/repos/some-user/some-repo/releases{/id}",
+ "deployments_url": "https://api.github.com/repos/some-user/some-repo/deployments",
+ "created_at": "2018-05-08T08:00:00Z",
+ "updated_at": "2018-05-08T10:01:01Z",
+ "pushed_at": "2018-05-08T10:00:58Z",
+ "git_url": "git://github.com/some-user/some-repo.git",
+ "ssh_url": "git@github.com:some-user/some-repo.git",
+ "clone_url": "https://github.com/some-user/some-repo.git",
+ "svn_url": "https://github.com/some-user/some-repo",
+ "homepage": "",
+ "size": 677,
+ "stargazers_count": 21,
+ "watchers_count": 21,
+ "language": "Java",
+ "has_issues": true,
+ "has_projects": true,
+ "has_downloads": true,
+ "has_wiki": false,
+ "has_pages": false,
+ "forks_count": 2,
+ "mirror_url": null,
+ "archived": false,
+ "open_issues_count": 3,
+ "license": {
+ "key": "apache-2.0",
+ "name": "Apache License 2.0",
+ "spdx_id": "Apache-2.0",
+ "url": "https://api.github.com/licenses/apache-2.0"
+ },
+ "forks": 2,
+ "open_issues": 3,
+ "watchers": 21,
+ "default_branch": "master",
+ "network_count": 2,
+ "subscribers_count": 9
+}
diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/RepositoryTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/RepositoryTest.java
index 5506d23..f0eb3ec 100644
--- a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/RepositoryTest.java
+++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/RepositoryTest.java
@@ -15,10 +15,16 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.nio.charset.StandardCharsets;
import java.util.Date;
import org.eclipse.egit.github.core.Repository;
import org.eclipse.egit.github.core.User;
+import org.eclipse.egit.github.core.client.GsonUtils;
import org.junit.Test;
/**
@@ -53,6 +59,7 @@ public class RepositoryTest {
assertNull(repo.getSvnUrl());
assertNull(repo.getUrl());
assertEquals(0, repo.getWatchers());
+ assertEquals(0, repo.getStars());
assertFalse(repo.isFork());
assertFalse(repo.isHasDownloads());
assertFalse(repo.isHasIssues());
@@ -93,6 +100,7 @@ public class RepositoryTest {
assertEquals("svn://", repo.setSvnUrl("svn://").getSvnUrl());
assertEquals("url://", repo.setUrl("url://").getUrl());
assertEquals(200, repo.setWatchers(200).getWatchers());
+ assertEquals(42, repo.setStars(42).getStars());
assertTrue(repo.setFork(true).isFork());
assertTrue(repo.setHasDownloads(true).isHasDownloads());
assertTrue(repo.setHasIssues(true).isHasIssues());
@@ -168,4 +176,15 @@ public class RepositoryTest {
repo.setOwner(new User().setLogin("tuser"));
assertEquals("tuser/myproject", repo.generateId());
}
+
+ @Test
+ public void fromJson() throws IOException {
+ try (Reader r = new BufferedReader(new InputStreamReader (
+ this.getClass().getResourceAsStream("test_repo.json"),
+ StandardCharsets.UTF_8))) {
+ Repository repo = GsonUtils.fromJson(r, Repository.class);
+ assertEquals(9,repo.getWatchers());
+ assertEquals(21, repo.getStars());
+ }
+ }
}
diff --git a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/Repository.java b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/Repository.java
index 132838c..0f07568 100644
--- a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/Repository.java
+++ b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/Repository.java
@@ -50,8 +50,14 @@ public class Repository implements IRepositoryIdProvider, Serializable {
private int size;
+ /** Legacy field, kept for backwards compatibility. It's actually the stargazersCount. */
private int watchers;
+ private int stargazersCount;
+
+ /** This is what Github shows as "watchers". */
+ private int subscribersCount = -1;
+
private Repository parent;
private Repository source;
@@ -243,10 +249,29 @@ public class Repository implements IRepositoryIdProvider, Serializable {
}
/**
+ * @return stars
+ * @since 5.0
+ */
+ public int getStars() {
+ return stargazersCount;
+ }
+
+ /**
+ * @param stars
+ * @return this repository
+ * @since 5.0
+ */
+ public Repository setStars(int stars) {
+ this.stargazersCount = stars;
+ return this;
+ }
+
+ /**
* @return watchers
*/
public int getWatchers() {
- return watchers;
+ // Account for legacy serializations that had only the watchers field
+ return subscribersCount < 0 ? watchers : subscribersCount;
}
/**
@@ -254,7 +279,7 @@ public class Repository implements IRepositoryIdProvider, Serializable {
* @return this repository
*/
public Repository setWatchers(int watchers) {
- this.watchers = watchers;
+ this.subscribersCount = watchers;
return this;
}