11package com .cloudbees .jenkins ;
22
33import hudson .util .AdaptedIterator ;
4+ import hudson .util .Iterators .FilterIterator ;
45import org .kohsuke .github .GHOrganization ;
56import org .kohsuke .github .GHPerson ;
67import org .kohsuke .github .GHRepository ;
@@ -55,10 +56,23 @@ public GitHubRepositoryName(String host, String userName, String repositoryName)
5556 this .repositoryName = repositoryName ;
5657 }
5758
59+ /**
60+ * Resolves this name to the actual reference by {@link GHRepository}.
61+ *
62+ * <p>
63+ * Since the system can store multiple credentials, and only some of them might be able to see this name in question,
64+ * this method uses {@link GitHubWebHook#login(String, String)} and attempt to find the right credential that can
65+ * access this repository.
66+ *
67+ * <p>
68+ * This method walks multiple repositories for each credential that can access the repository. Depending on
69+ * what you are trying to do with the repository, you might have to keep trying until a {@link GHRepository}
70+ * with suitable permission is returned.
71+ */
5872 public Iterable <GHRepository > resolve () {
5973 return new Iterable <GHRepository >() {
6074 public Iterator <GHRepository > iterator () {
61- return new AdaptedIterator <GitHub ,GHRepository >(GitHubWebHook .get ().login (host ,userName )) {
75+ return filterNull ( new AdaptedIterator <GitHub ,GHRepository >(GitHubWebHook .get ().login (host ,userName )) {
6276 protected GHRepository adapt (GitHub item ) {
6377 try {
6478 GHRepository repo = item .getUser (userName ).getRepository (repositoryName );
@@ -71,7 +85,16 @@ protected GHRepository adapt(GitHub item) {
7185 return null ;
7286 }
7387 }
74- };
88+ });
89+ }
90+ };
91+ }
92+
93+ private <V > Iterator <V > filterNull (Iterator <V > itr ) {
94+ return new FilterIterator <V >(itr ) {
95+ @ Override
96+ protected boolean filter (V v ) {
97+ return v !=null ;
7598 }
7699 };
77100 }
0 commit comments