jenkins:
  systemMessage: "Jenkins configured automatically by Jenkins Configuration as Code plugin\n\n"

jobs:
  - script: >
      organizationFolder('Bitbucket Organization Folder') {
        description("Bitbucket orga folder configured with JCasC")
        displayName('My Project')

        // "Projects"
        organizations {
          bitbucket {
            // DEPRECATED : bitbucketServerUrl("https://$BITBUCKET_URL")
            serverUrl("https://$BITBUCKET_URL")
            repoOwner("OWN")
            credentialsId("bitbucket-http")

            // "Traits" ("Behaviours" in the GUI) that are "declarative-compatible"
            traits {
              webhookRegistrationTrait {
                mode('ITEM')
              }
              submoduleOptionTrait {
                extension {
                  disableSubmodules(false)
                  recursiveSubmodules(true)
                  trackingSubmodules(false)
                  reference(null)
                  timeout(null)
                  parentCredentials(true)
                }
              }
            }
          }

        }

        // "Traits" ("Behaviours" in the GUI) that are NOT "declarative-compatible"
        // For some 'traits, we need to configure this stuff by hand until JobDSL handles it
        // https://issues.jenkins.io/browse/JENKINS-45504
        configure { node ->
            def traits = node / navigators / 'com.cloudbees.jenkins.plugins.bitbucket.BitbucketSCMNavigator' / traits

            // Discover branches
            traits << 'com.cloudbees.jenkins.plugins.bitbucket.BranchDiscoveryTrait' {
                strategyId('1')
                // Values
                //  1 : Exclude branches that are also filed as PRs
                //  2 : Only branches that are also filed as PRs
                //  3 : All branches
            }

            traits << 'com.cloudbees.jenkins.plugins.bitbucket.SSHCheckoutTrait' {
                credentialsId('bitbucket-ssh')
            }            

            // Filter by name (with regular expression)
            traits << 'jenkins.scm.impl.trait.RegexSCMSourceFilterTrait' {
                regex('some-regex-you-need')
            }

            // Discover pull requests from origin
            traits << 'com.cloudbees.jenkins.plugins.bitbucket.OriginPullRequestDiscoveryTrait' {
                strategyId('1')
                // Values
                // 1 : Merging the pull request with the current target branch revision
                // 2 : The current pull request revision
                // 3 : Both the current pull request revision and the pull request merged with the current target branch revision
            }

            // Discover pull requests from forks
            traits << 'com.cloudbees.jenkins.plugins.bitbucket.ForkPullRequestDiscoveryTrait' {
                strategyId('1')
                // Values
                // 1 : Merging the pull request with the current target branch revision
                // 2 : The current pull request revision
                // 3 : Both the current pull request revision and the pull request merged with the current target branch revision

                trustID('1')
                // Values
                // 0 : Everyone
                // 1 : Forks in the same account
                // 2 : Nobody
            }
        }

        // "Project Recognizers"
        projectFactories {
            workflowMultiBranchProjectFactory {
                scriptPath 'ValidationJenkinsfile'
            }
        }

        // "Orphaned Item Strategy"
        orphanedItemStrategy {
          discardOldItems {
            daysToKeep(-1)
            numToKeep(-1)
          }
        }

        // "Scan Organization Folder Triggers" : 1 day
        // We need to configure this stuff by hand because JobDSL only allow 'periodic(int min)' for now
        configure { node ->
          node / triggers / 'com.cloudbees.hudson.plugins.folder.computed.PeriodicFolderTrigger' {
            spec('H H * * *')
            interval(86400000)
          }
        }
      }
