Uploaded image for project: 'SR for Jira - Development'
  1. SR for Jira - Development
  2. SRJIRA-3860

Script in docs for reindexing scripted fields needs updating for Jira 8 API

    Details

    • Type: Bug
    • Status: Done (View Workflow)
    • Priority: Medium
    • Resolution: Done
    • Affects Version/s: 5.6.2.1-jira8
    • Fix Version/s: 6.1.0
    • Component/s: Scripted Fields
    • Labels:
      None
    • Critical Points:
      1.4

      Description

      The script here: https://scriptrunner.adaptavist.com/5.6.2/jira/scripted-fields.html#_jql_searches_in_script_fields needs some adjusting to work correctly without run-time errors on Jira 8.

      import com.atlassian.jira.component.ComponentAccessor
      import com.atlassian.jira.issue.index.IssueIndexingService
      import com.atlassian.jira.issue.search.SearchProvider
      import com.atlassian.jira.issue.search.SearchProviderFactory
      import com.atlassian.jira.issue.util.IssueIdsIssueIterable
      import com.atlassian.jira.jql.builder.JqlQueryBuilder
      import com.atlassian.jira.jql.query.IssueIdCollector
      import com.atlassian.jira.task.context.LoggingContextSink
      import com.atlassian.jira.task.context.PercentageContext
      import com.onresolve.scriptrunner.runner.ScriptFieldManager
      import com.atlassian.jira.issue.search.SearchQuery
      import org.apache.log4j.Level
      
      def customFieldManager = ComponentAccessor.getCustomFieldManager()
      def searchProvider = ComponentAccessor.getComponent(SearchProvider)
      def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
      def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
      def searchProviderFactory = ComponentAccessor.getComponent(SearchProviderFactory)
      def issueManager = ComponentAccessor.getIssueManager()
      
      /*
       Loop through all script fields and get their contexts
       Create a query and execute it
       Reindex all results
      */
      
      def builder = JqlQueryBuilder.newBuilder().where().defaultOr()
      
      def scriptFields = customFieldManager.getCustomFieldObjects().findAll {
          it.customFieldType.descriptor.completeKey == ScriptFieldManager.SCRIPT_FIELD_TYPE
      }
      
      if (scriptFields.find {
          it.allProjects && it.allIssueTypes
      }) {
          log.warn("A full background reindex is required at least one script field has global scope")
      } else {
          scriptFields.each {
              def clauseBuilder = JqlQueryBuilder.newClauseBuilder().defaultAnd()
      
              if (!it.allProjects) {
                  clauseBuilder.project(*it.associatedProjectObjects*.id).buildClause()
              }
              if (!it.allIssueTypes) {
                  clauseBuilder.issueType(*it.associatedIssueTypes*.id).buildClause()
              }
      
              builder.addClause(clauseBuilder.buildClause())
          }
      }
      
      def query = builder.buildQuery()
      
      def issueIdCollector = new IssueIdCollector()
      
      searchProvider.search(SearchQuery.create(query, user), issueIdCollector)
      
      def total = issueIdCollector.issueIds.size()
      def issuesIdsIterable = new IssueIdsIssueIterable(issueIdCollector.issueIds*.toLong(), issueManager)
      def loggingContextSink = new LoggingContextSink(log, "Indexing script field issues {0}% ($total total)", Level.DEBUG)
      issueIndexingService.reIndexIssues(issuesIdsIterable, new PercentageContext(total, loggingContextSink))

       

        Attachments

          Structure

            Activity

              People

              Assignee:
              ctooley Chloe Tooley
              Reporter:
              jchoules Joanna Choules
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:

                  Structure Helper Panel