44import org .kohsuke .github .GHAppInstallation ;
55import org .kohsuke .github .GHAppInstallationToken ;
66import org .kohsuke .github .GitHub ;
7+ import org .kohsuke .github .PagedIterable ;
78
89import java .io .IOException ;
10+ import java .net .URL ;
911import java .time .Duration ;
1012import java .time .Instant ;
13+ import java .util .HashMap ;
14+ import java .util .List ;
15+ import java .util .Map ;
1116import java .util .Objects ;
17+ import java .util .regex .Matcher ;
18+ import java .util .regex .Pattern ;
1219
1320import javax .annotation .Nonnull ;
1421
1724 */
1825public class OrgAppInstallationAuthorizationProvider extends GitHub .DependentAuthorizationProvider {
1926
20- private final String organizationName ;
27+ private static final Pattern pattern = Pattern . compile ( "/repos/(.*)/.*" ) ;
2128
22- private String latestToken ;
29+ private Map < String , String > latestToken = new HashMap <>() ;
2330
2431 @ Nonnull
25- private Instant validUntil = Instant . MIN ;
32+ private Map < String , Instant > validUntil = new HashMap <>() ;
2633
2734 /**
2835 * Provides an AuthorizationProvider that performs automatic token refresh, based on an previously authenticated
@@ -36,28 +43,52 @@ public class OrgAppInstallationAuthorizationProvider extends GitHub.DependentAut
3643 */
3744 @ BetaApi
3845 @ Deprecated
39- public OrgAppInstallationAuthorizationProvider (String organizationName ,
40- AuthorizationProvider authorizationProvider ) {
46+ public OrgAppInstallationAuthorizationProvider (AuthorizationProvider authorizationProvider ) {
4147 super (authorizationProvider );
42- this .organizationName = organizationName ;
4348 }
4449
4550 @ Override
46- public String getEncodedAuthorization () throws IOException {
51+ public String getEncodedAuthorization (URL url ) throws IOException {
4752 synchronized (this ) {
48- if (latestToken == null || Instant .now ().isAfter (this .validUntil )) {
49- refreshToken ();
53+ String org = getOrgFromURL (url );
54+ if (latestToken .get (org ) == null || this .validUntil .get (org ) == null
55+ || Instant .now ().isAfter (this .validUntil .get (org ))) {
56+ refreshToken (url );
5057 }
51- return String .format ("token %s" , latestToken );
58+ return String .format ("token %s" , latestToken . get ( org ) );
5259 }
5360 }
5461
55- private void refreshToken () throws IOException {
56- GitHub gitHub = this .gitHub ();
57- GHAppInstallation installationByOrganization = gitHub .getApp ()
58- .getInstallationByOrganization (this .organizationName );
59- GHAppInstallationToken ghAppInstallationToken = installationByOrganization .createToken ().create ();
60- this .validUntil = ghAppInstallationToken .getExpiresAt ().toInstant ().minus (Duration .ofMinutes (5 ));
61- this .latestToken = Objects .requireNonNull (ghAppInstallationToken .getToken ());
62+ @ Override
63+ public String getEncodedAuthorization () throws IOException {
64+ return getEncodedAuthorization (null );
65+ }
66+
67+ private String getOrgFromURL (URL url ) {
68+ if (url != null ) {
69+ Matcher matcher = pattern .matcher (url .getPath ());
70+ if (matcher .matches ()) {
71+ return matcher .group (1 );
72+ }
73+ }
74+ return "" ;
75+ }
76+
77+ private void refreshToken (URL url ) throws IOException {
78+ List <GHAppInstallation > installations = this .gitHub ().getApp ().listInstallations ().asList ();
79+ // take the first one if no one matches
80+ GHAppInstallation installation = installations .get (0 );
81+ String org = getOrgFromURL (url );
82+ for (GHAppInstallation ghAppInstallation : installations ) {
83+ if (org .equals (installation .getAccount ().getLogin ())) {
84+ System .out .println (
85+ String .format ("Found installation for path %s: %s" , url .getPath (), installation .getHtmlUrl ()));
86+ installation = ghAppInstallation ;
87+ break ;
88+ }
89+ }
90+ GHAppInstallationToken ghAppInstallationToken = installation .createToken ().create ();
91+ this .validUntil .put (org , ghAppInstallationToken .getExpiresAt ().toInstant ().minus (Duration .ofMinutes (5 )));
92+ this .latestToken .put (org , Objects .requireNonNull (ghAppInstallationToken .getToken ()));
6293 }
6394}
0 commit comments