3
3
import java .io .IOException ;
4
4
import java .util .Arrays ;
5
5
import java .util .Collections ;
6
+ import java .util .HashSet ;
7
+ import java .util .Iterator ;
6
8
import java .util .List ;
9
+ import java .util .Map ;
10
+ import java .util .Set ;
11
+ import java .util .TreeMap ;
7
12
8
13
/**
9
14
* Represents the account that's logging into GitHub.
@@ -35,7 +40,50 @@ public List<String> getEmails() throws IOException {
35
40
public List <GHKey > getPublicKeys () throws IOException {
36
41
return Collections .unmodifiableList (Arrays .asList (root .retrieve ().to ("/user/keys" , GHKey [].class )));
37
42
}
38
-
43
+
44
+ /**
45
+ * Gets the organization that this user belongs to.
46
+ */
47
+ public GHPersonSet <GHOrganization > getAllOrganizations () throws IOException {
48
+ GHPersonSet <GHOrganization > orgs = new GHPersonSet <GHOrganization >();
49
+ Set <String > names = new HashSet <String >();
50
+ for (GHOrganization o : root .retrieve ().to ("/user/orgs" , GHOrganization [].class )) {
51
+ if (names .add (o .getLogin ())) // in case of rumoured duplicates in the data
52
+ orgs .add (root .getOrganization (o .getLogin ()));
53
+ }
54
+ return orgs ;
55
+ }
56
+
57
+ /**
58
+ * Gets the all repositories this user owns (public and private).
59
+ */
60
+ public synchronized Map <String ,GHRepository > getAllRepositories () throws IOException {
61
+ Map <String ,GHRepository > repositories = new TreeMap <String , GHRepository >();
62
+ for (GHRepository r : listAllRepositories ()) {
63
+ repositories .put (r .getName (),r );
64
+ }
65
+ return Collections .unmodifiableMap (repositories );
66
+ }
67
+
68
+ /**
69
+ * Lists up all repositories this user owns (public and private).
70
+ *
71
+ * Unlike {@link #getAllRepositories()}, this does not wait until all the repositories are returned.
72
+ */
73
+ public PagedIterable <GHRepository > listAllRepositories () {
74
+ return new PagedIterable <GHRepository >() {
75
+ public PagedIterator <GHRepository > iterator () {
76
+ return new PagedIterator <GHRepository >(root .retrieve ().asIterator ("/user/repos" , GHRepository [].class )) {
77
+ @ Override
78
+ protected void wrapUp (GHRepository [] page ) {
79
+ for (GHRepository c : page )
80
+ c .wrap (root );
81
+ }
82
+ };
83
+ }
84
+ };
85
+ }
86
+
39
87
// public void addEmails(Collection<String> emails) throws IOException {
40
88
//// new Requester(root,ApiVersion.V3).withCredential().to("/user/emails");
41
89
// root.retrieveWithAuth3()
0 commit comments