<commit_before>#!/bin/bash

# A script that uses configurator to create a YAML file, and then pushes that
# YAML file to k8s/test-infra's config/testgrids directory
#
# Used by teams that host different instances of Prow, but want to show their
# results on testgrid.k8s.io
#

set -o errexit
set -o nounset
set -o pipefail

main() {
  branch="transfigure-branch"

  if [[ ! $# -eq 5 ]]; then
    echo "Usage: $(basename "$0") [github_token] [prow_config] [prow_job_config] [testgrid_yaml] [repo_subdir]" >&2
    echo "All [arguments] are required paths" >&2
    exit 1
  fi

  parse-args "$@"
  user-from-token
  ensure-git-config

  echo "Ensuring kubernetes/test-infra repo"
  if [[ -d test-infra ]]; then
    echo "Directory 'test-infra' found; using as kubernetes/test-infra repository"
  else
    #TODO(chases2): Clone only the test-infra/config/testgrids subdirectory used
    git clone https://github.com/kubernetes/test-infra.git
    trap "cleanup-repository" EXIT
    echo "Created temporary repository"
  fi

  k8s_repo=$(readlink -f test-infra)
  testgrid_dir=$(readlink -f "test-infra/config/testgrids")

  echo "Checking out ${branch}"
  cd "${k8s_repo}"
  git checkout -B "${branch}"

  echo "Generating testgrid yaml"
  /configurator \
    --prow-config "${prow_config}" \
    --prow-job-config "${job_config}" \
    --output-yaml \
    --yaml "${testgrid_config}" \
    --oneshot \
    --output "${testgrid_dir}/${testgrid_subdir}/gen-config.yaml"

  git add --all

  if ! git diff --quiet ; then
    echo "Transfigure did not change anything. Aborting no-op bump"
    exit 0
  fi

  title="Update TestGrid for ${testgrid_subdir}"
  git commit -m "${title}"
  echo "Pushing commit to ${user}/${remote_fork_repo}:${branch}..."
  git push -f "https://${user}:$(cat "${token}")@github.com/${user}/${remote_fork_repo}" "HEAD:${branch}"

  echo "Creating PR to merge ${user}:${branch} into k8s/test-infra:master..."
  /pr-creator \
    --github-token-path="${token}" \
    --org="kubernetes" --repo="test-infra" --branch=master \
    --title="${title}" --match-title="${title}" \
    --body="Generated by transfigure.sh" \
    --source="${user}:${branch}" \
    --confirm

  echo "PR created successfully!"
  return 0
}

parse-args() {
  token=$(readlink -f "$1")
  prow_config="${CONFIG_PATH:-$2}"
  job_config="${JOB_CONFIG_PATH:-$3}"
  testgrid_config="${TESTGRID_CONFIG:-$4}"
  testgrid_subdir="${TESTGRID_LOCAL_SUBDIR:-$5}"
  remote_fork_repo="${FORK_GH_REPO:-"test-infra"}"

  if [[ -f ${token} ]]; then
    echo "Error: [github_token] ${token} must be a file path." >&2
    exit 1
  elif [[ -f "${prow_config}" ]]; then
    echo "ERROR: [prow_config] ${prow_config} must be a file path." >&2
    exit 1
  elif [[ -e "${job_config}" ]]; then
    echo "ERROR: [prow_job_config] ${job_config} must exist." >&2
    exit 1
  elif [[ -e "${testgrid_config}" ]]; then
    echo "ERROR: [testgrid_yaml] ${testgrid_config} must exist." >&2
    exit 1
  elif [[ -z "${testgrid_subdir}" ]]; then
    echo "ERROR: [repo_subdir] must be specified." >&2
    exit 1
  fi
}

user-from-token() {
  user=$(curl -H "Authorization: token $(cat "${token}")" "https://api.github.com/user" 2>/dev/null | sed -n "s/\s\+\"login\": \"\(.*\)\",/\1/p")
}

cleanup-repository() {
  echo "Cleaning temporary repository at ${k8s_repo}"
  cd ..
  rm -rf "${k8s_repo}"
}

ensure-git-config() {
  git config user.name &>/dev/null && git config user.email &>/dev/null && return 0
  echo "ERROR: git config user.name, user.email unset. No defaults provided" >&2
  return 1
}

main "$@"<commit_msg>Logical improvements to transfigure.sh<commit_after>#!/bin/bash

# A script that uses configurator to create a YAML file, and then pushes that
# YAML file to k8s/test-infra's config/testgrids directory
#
# Used by teams that host different instances of Prow, but want to show their
# results on testgrid.k8s.io
#

set -o errexit
set -o nounset
set -o pipefail

main() {
  branch="transfigure-branch"

  if [[ $# -ne 5 ]]; then
    echo "Usage: $(basename "$0") [github_token] [prow_config] [prow_job_config] [testgrid_yaml] [repo_subdir]" >&2
    echo "All [arguments] are required paths" >&2
    exit 1
  fi

  parse-args "$@"
  user-from-token
  ensure-git-config

  echo "Ensuring kubernetes/test-infra repo"
  if [[ -d test-infra ]]; then
    echo "Directory 'test-infra' found; using as kubernetes/test-infra repository"
  else
    #TODO(chases2): Clone only the test-infra/config/testgrids subdirectory used
    git clone https://github.com/kubernetes/test-infra.git
    trap "cleanup-repository" EXIT
    echo "Created temporary repository"
  fi

  k8s_repo=$(readlink -f test-infra)
  testgrid_dir=$(readlink -f "test-infra/config/testgrids")

  echo "Checking out ${branch}"
  cd "${k8s_repo}"
  git checkout -B "${branch}"

  echo "Generating testgrid yaml"
  /configurator \
    --prow-config "${prow_config}" \
    --prow-job-config "${job_config}" \
    --output-yaml \
    --yaml "${testgrid_config}" \
    --oneshot \
    --output "${testgrid_dir}/${testgrid_subdir}/gen-config.yaml"

  git add --all

  if ! git diff --quiet ; then
    echo "Transfigure did not change anything. Aborting no-op bump"
    exit 0
  fi

  title="Update TestGrid for ${testgrid_subdir}"
  git commit -m "${title}"
  echo "Pushing commit to ${user}/${remote_fork_repo}:${branch}..."
  git push -f "https://${user}:$(cat "${token}")@github.com/${user}/${remote_fork_repo}" "HEAD:${branch}"

  echo "Creating PR to merge ${user}:${branch} into k8s/test-infra:master..."
  /pr-creator \
    --github-token-path="${token}" \
    --org="kubernetes" --repo="test-infra" --branch=master \
    --title="${title}" --match-title="${title}" \
    --body="Generated by transfigure.sh" \
    --source="${user}:${branch}" \
    --confirm

  echo "PR created successfully!"
  return 0
}

parse-args() {
  token=$(readlink -m "$1")
  prow_config="${CONFIG_PATH:-$2}"
  job_config="${JOB_CONFIG_PATH:-$3}"
  testgrid_config="${TESTGRID_CONFIG:-$4}"
  testgrid_subdir="${TESTGRID_LOCAL_SUBDIR:-$5}"
  remote_fork_repo="${FORK_GH_REPO:-"test-infra"}"

  if [[ ! -f ${token} ]]; then
    echo "ERROR: [github_token] ${token} must be a file path." >&2
    exit 1
  elif [[ ! -f "${prow_config}" ]]; then
    echo "ERROR: [prow_config] ${prow_config} must be a file path." >&2
    exit 1
  elif [[ ! -e "${job_config}" ]]; then
    echo "ERROR: [prow_job_config] ${job_config} must exist." >&2
    exit 1
  elif [[ ! -e "${testgrid_config}" ]]; then
    echo "ERROR: [testgrid_yaml] ${testgrid_config} must exist." >&2
    exit 1
  elif [[ -z "${testgrid_subdir}" ]]; then
    echo "ERROR: [repo_subdir] must be specified." >&2
    exit 1
  fi
}

user-from-token() {
  user=$(curl -H "Authorization: token $(cat "${token}")" "https://api.github.com/user" 2>/dev/null | sed -n "s/\s\+\"login\": \"\(.*\)\",/\1/p")
}

cleanup-repository() {
  echo "Cleaning temporary repository at ${k8s_repo}"
  cd ..
  rm -rf "${k8s_repo}"
}

ensure-git-config() {
  git config user.name &>/dev/null && git config user.email &>/dev/null && return 0
  echo "ERROR: git config user.name, user.email unset. No defaults provided" >&2
  return 1
}

main "$@"
{'fraction_non_alphanumeric': 0.1772645053568944, 'fraction_numerical': 0.009739807986642549, 'mean_word_length': 3.4206642066420665, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6425450', 'n_tokens_mistral': 2615, 'n_tokens_neox': 2514, 'n_words': 677}