/*
 * Copyright (c) 2018 - 2020 TomTom N.V.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

def version = 'experiment'
@NonCPS
String transform_ssh_to_https(String url)
{
  // Rewrite SSH URLs to HTTP URLs, assuming that we don't need authentication
  def m = url =~ /^ssh:\/\/(?:\w+@)?(\w+(?:\.\w+)*\.?)(?::\d+)?\/(.+)$/
  if (!m && !(url =~ /^\w+:\/\/.*/)) {
    m = url =~ /^(?:\w+@)?(\w+(?:\.\w+)*\.?):(.+)$/
  }
  m.each { match ->
    url = "https://${match[1]}/scm/${match[2]}"
  }
  return url
}
def repo = transform_ssh_to_https(scm.userRemoteConfigs[0].url.split('/')[0..-2].join('/') + '/hopic.git')

library(
    identifier: "hopic@${version}",
    retriever: modernSCM([
        $class: 'GitSCMSource',
        remote: repo
  ]))
def hopic = getCiDriver("git+${repo}@${version}", "hopic-ci-config.yml")

pipeline {
  // Build agents/nodes get allocated when required, not before.
  agent none

  parameters {
    // Customizable by the administrator
    booleanParam(defaultValue: false,
                 description: 'Clean build',
                 name: 'CLEAN')
  }

  options {
    // Customizable by the administrator
    timestamps()
    disableConcurrentBuilds()
  }

  stages {
    stage("Commit Stage") {
      steps {
        script {
          hopic.build(params.CLEAN)
        }
      }
    }
  }
}
