Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-56693

Support wiping GitHub API response cache via static method

XMLWordPrintable

    • Icon: New Feature New Feature
    • Resolution: Unresolved
    • Icon: Minor Minor
    • github-plugin
    • None

      I generate jobs via Job DSL plugin scripts. One of the actions I perform is to search GitHub authorizations for whether or not a service account user (configured in Jenkins settings) has admin access via GitHub team for registering webhooks (via github branch source in multibranch pipelines). I do that in a freestyle job the following groovy postbuild step (after executing job DSL scripts):

      if(!(new GitHubRepositoryName(host, namespace, repo)).resolveOne()?.hasAdminAccess()) {
          throw new Exception('''
              |ERROR: please make "example" GitHub team an admin of your repository.  Otherwise, webhooks can't be automatically registered.
              '''.stripMargin().trim())
      }
      

      However, when users generating jobs add their "example" team and try to generate jobs again they get the same error. This is because the GitHub plugin caches API responses from GitHub including authorizations. I understand why this optimization is in place (to avoid hitting GitHub API limits).

      It would be nice to easily and simply clear the cached responses of the GitHub client. Here's how I'm wiping the cached responses today.

      Job DSL script code

      /**
        * Clear the authorization cache so that we can immediately discover new
        * permissions for generating a job.  This will wipe and create a fresh cache
        * when a user generates a job.  This is nessary to detect adding the "RE
        * Admin" GitHub team.
        *
        * Source code paths
        *     GitHubClientCacheOps https://github.com/jenkinsci/github-plugin/blob/master/src/main/java/org/jenkinsci/plugins/github/internal/GitHubClientCacheOps.java
        *     GitHubLoginFunction  https://github.com/jenkinsci/github-plugin/blob/master/src/main/java/org/jenkinsci/plugins/github/internal/GitHubLoginFunction.java
        *     GitHubPluginConfig   https://github.com/jenkinsci/github-plugin/blob/master/src/main/java/org/jenkinsci/plugins/github/config/GitHubPluginConfig.java
        *     GitHubServerConfig   https://github.com/jenkinsci/github-plugin/blob/master/src/main/java/org/jenkinsci/plugins/github/config/GitHubServerConfig.java
        *
        * Additional Information:
        *     Usage of uberClassLoader and getExtensionList are because of
        *     limitations in the Job DSL plugin.  These are helper functions provided
        *     by Jenkins core which helps work around classloader limitations (i.e.
        *     classes not available to import)
        */
      void wipeAuthorizationCache() {
          Class github_cache_ops = Jenkins.instance.pluginManager.uberClassLoader.loadClass('org.jenkinsci.plugins.github.internal.GitHubClientCacheOps')
          List github_server_configs = Jenkins.instance.getExtensionList('org.jenkinsci.plugins.github.config.GitHubPluginConfig')[0].configs
          github_server_configs.each { github_server_config ->
              github_cache_ops.toCacheDir().apply(github_server_config).delete()
              github_server_config.setCachedClient(null)
              def github_login_function = Jenkins.instance.pluginManager.uberClassLoader.loadClass('org.jenkinsci.plugins.github.internal.GitHubLoginFunction').newInstance()
              github_server_config.setCachedClient(github_login_function.apply(github_server_config))
          }
      }
      wipeAuthorizationCache()
      

      It feels dirty but it works. It would be nice if there was some static method provided by the GitHub plugin which easily does this for admins who need to get the latest API response and not use the cache.

      If there's a better way to do this then I'm open to learning!

            lanwen Kirill Merkushev
            sag47 Sam Gleske
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: