<commit_before>import groovy.json.JsonBuilder

/* Libraries -----------------------------------------------------------------*/

ci = load 'ci/jenkins.groovy'
nix = load 'ci/nix.groovy'
utils = load 'ci/utils.groovy'

/* Small Helpers -------------------------------------------------------------*/

def pkgUrl(build) {
  return utils.getEnv(build, 'PKG_URL')
}

def updateBucketJSON(urls, fileName) {
  /* latest.json has slightly different key names */
  def content = [
    DIAWI: urls.Diawi,
    APK: urls.Apk, IOS: urls.iOS,
    APP: urls.App, MAC: urls.Mac,
    WIN: urls.Win, SHA: urls.SHA
  ]
  def filePath = "${pwd()}/pkg/${fileName}"
  /* it might not exist */
  sh "mkdir -p ${pwd()}/pkg"
  def contentJson = new JsonBuilder(content).toPrettyString()
  println "${filePath}:\n${contentJson}"
  writeFile(file: filePath, text: contentJson)
  return uploadArtifact(filePath)
}

def prep(type = 'nightly') {
  /* build/downloads all nix deps in advance */
  nix.prepEnv()
  /* rebase unless this is a release build */
  utils.doGitRebase()
  /* ensure that we start from a known state */
  sh 'make clean'
  /* Disable git hooks in CI, it's not useful, takes time and creates weird errors at times
     (e.g. bin/sh: 2: /etc/ssl/certs/ca-certificates.crt: Permission denied) */
  sh 'make disable-githooks'

  /* pick right .env and update from params */
  utils.updateEnv(type)

  if (['android', 'ios'].contains(env.TARGET)) {
    /* Run at start to void mismatched numbers */
    utils.genBuildNumber()
  }

  nix.shell('watchman watch-del-all', attr: 'shells.watchman')

  if (env.TARGET == 'ios') {
    /* install ruby dependencies */
    nix.shell(
      'bundle install --gemfile=fastlane/Gemfile --quiet',
      attr: 'shells.fastlane')
  }

  if (['macos', 'linux', 'windows'].contains(env.TARGET)) {
    /* node deps, pods, and status-go download */
    nix.shell('scripts/prepare-for-desktop-platform.sh', pure: false)
  }
  /* run script in the nix shell so that node_modules gets instantiated before attempting the copies */
  nix.shell('scripts/copy-translations.sh chmod', attr: "shells.${env.TARGET}")
}

def uploadArtifact(path) {
  /* defaults for upload */
  def domain = 'ams3.digitaloceanspaces.com'
  def bucket = 'status-im'
  /* There's so many PR builds we need a separate bucket */
  if (utils.isPRBuild()) {
    bucket = 'status-im-prs'
  }
  /* WARNING: s3cmd can't guess APK MIME content-type */
  def customOpts = ''
  if (path.endsWith('apk')) {
    customOpts += "--mime-type='application/vnd.android.package-archive'"
  }
  /* We also need credentials for the upload */
  withCredentials([usernamePassword(
    credentialsId: 'digital-ocean-access-keys',
    usernameVariable: 'DO_ACCESS_KEY',
    passwordVariable: 'DO_SECRET_KEY'
  )]) {
    sh("""
      s3cmd ${customOpts} \\
        --acl-public \\
        --host="${domain}" \\
        --host-bucket="%(bucket)s.${domain}" \\
        --access_key=${DO_ACCESS_KEY} \\<commit_msg>fix bundle install by disabling Nix sandbox<commit_after>import groovy.json.JsonBuilder

/* Libraries -----------------------------------------------------------------*/

ci = load 'ci/jenkins.groovy'
nix = load 'ci/nix.groovy'
utils = load 'ci/utils.groovy'

/* Small Helpers -------------------------------------------------------------*/

def pkgUrl(build) {
  return utils.getEnv(build, 'PKG_URL')
}

def updateBucketJSON(urls, fileName) {
  /* latest.json has slightly different key names */
  def content = [
    DIAWI: urls.Diawi,
    APK: urls.Apk, IOS: urls.iOS,
    APP: urls.App, MAC: urls.Mac,
    WIN: urls.Win, SHA: urls.SHA
  ]
  def filePath = "${pwd()}/pkg/${fileName}"
  /* it might not exist */
  sh "mkdir -p ${pwd()}/pkg"
  def contentJson = new JsonBuilder(content).toPrettyString()
  println "${filePath}:\n${contentJson}"
  writeFile(file: filePath, text: contentJson)
  return uploadArtifact(filePath)
}

def prep(type = 'nightly') {
  /* build/downloads all nix deps in advance */
  nix.prepEnv()
  /* rebase unless this is a release build */
  utils.doGitRebase()
  /* ensure that we start from a known state */
  sh 'make clean'
  /* Disable git hooks in CI, it's not useful, takes time and creates weird errors at times
     (e.g. bin/sh: 2: /etc/ssl/certs/ca-certificates.crt: Permission denied) */
  sh 'make disable-githooks'

  /* pick right .env and update from params */
  utils.updateEnv(type)

  if (['android', 'ios'].contains(env.TARGET)) {
    /* Run at start to void mismatched numbers */
    utils.genBuildNumber()
  }

  nix.shell('watchman watch-del-all', attr: 'shells.watchman')

  if (env.TARGET == 'ios') {
    /* install ruby dependencies */
    nix.shell(
      'bundle install --gemfile=fastlane/Gemfile --quiet',
      attr: 'shells.fastlane',
      sandbox: false
    )
  }

  if (['macos', 'linux', 'windows'].contains(env.TARGET)) {
    /* node deps, pods, and status-go download */
    nix.shell('scripts/prepare-for-desktop-platform.sh', pure: false)
  }
  /* run script in the nix shell so that node_modules gets instantiated before attempting the copies */
  nix.shell(
    'scripts/copy-translations.sh chmod',
    attr: "shells.${env.TARGET}",
    sandbox: false
  )
}

def uploadArtifact(path) {
  /* defaults for upload */
  def domain = 'ams3.digitaloceanspaces.com'
  def bucket = 'status-im'
  /* There's so many PR builds we need a separate bucket */
  if (utils.isPRBuild()) {
    bucket = 'status-im-prs'
  }
  /* WARNING: s3cmd can't guess APK MIME content-type */
  def customOpts = ''
  if (path.endsWith('apk')) {
    customOpts += "--mime-type='application/vnd.android.package-archive'"
  }
  /* We also need credentials for the upload */
  withCredentials([usernamePassword(
    credentialsId: 'digital-ocean-access-keys',
    usernameVariable: 'DO_ACCESS_KEY',
    passwordVariable: 'DO_SECRET_KEY'
  )]) {
    sh("""
      s3cmd ${customOpts} \\
        --acl-public \\
        --host="${domain}" \\
        --host-bucket="%(bucket)s.${domain}" \\
        --access_key=${DO_ACCESS_KEY} \\
{'fraction_non_alphanumeric': 0.18771726535341832, 'fraction_numerical': 0.0013242840589306405, 'mean_word_length': 2.66626213592233, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '757121', 'n_tokens_mistral': 2023, 'n_tokens_neox': 1961, 'n_words': 507}