<commit_before>#!/usr/bin/env bash
#
# Advance changes into testing, or bomb if no changes
# 
source `dirname $0`/shared.inc

h1 "Find and validate new pantry changes, or die"
# FIXME: This dodges the need for rake *:version:bump in homebases. 
#   Add that, then come back and make this work with homebases as 
#   well.
changes_found=0
for name in $PANTRIES; do
  switch_to $name
  # if echo $name | grep homebase;
  # then cookbooks='org_cookbooks';
  # else cookbooks='cookbooks'; fi
  cookbooks='cookbooks'

  changes=`git diff --name-only staging -- $cookbooks/*/ | cut -d/ -f2 | sort | uniq`
  if [ "no$changes" == "no" ]; then
    item "No cookbook changes between master and staging"
    continue
  fi

  item "Changes found: ${changes//\\n/ }"       # Don't include newlines
  changes_found=1

  item "Ensure changes have version bump"
  for cookbook in $changes; do
    if git diff --quiet staging -- cookbooks/$cookbook/VERSION; then
      warning "Changes found but no version change in $cookbook"
      rake $cookbook:version:bump
    fi
  done
done
if [ $changes_found -ne 1 ]; then success "No changes found for testing"; fi

h1 "Ensuring proper matryoshka"
for name in $ALL_PATHS; do
  switch_to $name
  git merge -s ours testing  # Edge case: master updated with revert, as testing is queuing
  if ! git merge staging | grep 'Already up-to-date' > /dev/null; then
    echo "FATAL: master is not a descendant of staging" >&2
    exit 1
  fi
done

h1 "Get all cookbooks into place in homebases"
staging="staging_cookbooks"
testing="cookbooks"
change_all_branches "staging" h2
install_berkshelves $staging h2
change_all_branches "master" h2
install_berkshelves $testing h2

h1 "Record the testing cookbook versions"
for name in $HOMEBASES; do
  switch_to $name
  VERSIONS=environments/inc/cookbook_versions.rb
  cat $ROOT_DIR/cookbook_versions.rb.h > $VERSIONS
  for cookbook in `ls -1d $testing/*/ | cut -d/ -f2`; do
    version=`cat $testing/$cookbook/VERSION`
    echo -e "\$cookbooks['$cookbook']\t= '$version'" >> $VERSIONS
  done
  if git diff $VERSIONS > /dev/null; then
    git commit $VERSIONS -m"$VERSIONS: updating testing cookbook versions"
  fi
done

h1 "Push updates to development"
for name in $ALL_PATHS; do
  switch_to $name
  git pull
  git push origin master
done

h1 "Advance all changes to testing"
change_all_branches "testing" h2
h2 "Merge master into testing"
for name in $ALL_PATHS; do
  switch_to $name
  git merge master
  git push origin testing
done

h1 "Find, validate, upload, and freeze changed cookbooks"
for name in $HOMEBASES; do
  switch_to $name
  changed=''
  for cookbook in `ls -1d $testing/*/ | cut -d/ -f2`; do
    test_cb=$testing/$cookbook
    stage_cb=$staging/$cookbook
    # If $testing/$cookbook and $staging/$cookbook aren't identical
    no_change="diff -qr"
    if ! $no_change $test_cb $stage_cb > /dev/null 2>&1; then
      # make sure VERSIONs aren't identical either
      if $no_change $test_cb/VERSION $stage_cb/VERSION > /dev/null 2>&1; then
        failure 1 "testing version of $cookbook has changes, but same VERSION as staging"
      fi
      changed="$changed$cookbook "
    fi
  done
  if [ "nothing$changed" == "nothing" ]; then
    warning "Nothing changed - very suspicious"
    continue
  fi
  knife cookbook upload -V --freeze --force $changed
done

h1 "Upload testing environment"
for name in $HOMEBASES; do
  switch_to $name
  knife environment from file testing.rb
done

h1 "Uploading changed roles (test universe only)"
switch_to $TEST_PATH
changed=''
for file in `git diff --name-only staging roles/`; do
  if [ -f $file ]; then         # Don't treat a removed role as changed
    knife role from file $file
    changed="$changed$file "
  fi
done
if [ "nothing$changed" == "nothing" ]; then item "Nothing changed"; fi

success $TEST_KOAN
<commit_msg>jenkins_integration/enqueue_tests: fixing diff code the right way, adding commentary to prevent future confusion about results<commit_after>#!/usr/bin/env bash
#
# Advance changes into testing, or bomb if no changes
# 
source `dirname $0`/shared.inc

h1 "Find and validate new pantry changes, or die"
# FIXME: This dodges the need for rake *:version:bump in homebases. 
#   Add that, then come back and make this work with homebases as 
#   well.
changes_found=0
for name in $PANTRIES; do
  switch_to $name
  # if echo $name | grep homebase;
  # then cookbooks='org_cookbooks';
  # else cookbooks='cookbooks'; fi
  cookbooks='cookbooks'

  changes=`git diff --name-only staging -- $cookbooks/*/ | cut -d/ -f2 | sort | uniq`
  if [ "no$changes" == "no" ]; then
    item "No cookbook changes between master and staging"
    continue
  fi

  item "Changes found: ${changes//\\n/ }"       # Don't include newlines
  changes_found=1

  item "Ensure changes have version bump"
  for cookbook in $changes; do
    if git diff --quiet staging -- cookbooks/$cookbook/VERSION; then
      warning "Changes found but no version change in $cookbook"
      rake $cookbook:version:bump
    fi
  done
done
if [ $changes_found -ne 1 ]; then success "No changes found for testing"; fi

h1 "Ensuring proper matryoshka"
for name in $ALL_PATHS; do
  switch_to $name
  git merge -s ours testing  # Edge case: master updated with revert, as testing is queuing
  if ! git merge staging | grep 'Already up-to-date' > /dev/null; then
    echo "FATAL: master is not a descendant of staging" >&2
    exit 1
  fi
done

h1 "Get all cookbooks into place in homebases"
staging="staging_cookbooks"
testing="cookbooks"
change_all_branches "staging" h2
install_berkshelves $staging h2
change_all_branches "master" h2
install_berkshelves $testing h2

h1 "Record the testing cookbook versions"
for name in $HOMEBASES; do
  switch_to $name
  VERSIONS=environments/inc/cookbook_versions.rb
  cat $ROOT_DIR/cookbook_versions.rb.h > $VERSIONS
  for cookbook in `ls -1d $testing/*/ | cut -d/ -f2`; do
    version=`cat $testing/$cookbook/VERSION`
    echo -e "\$cookbooks['$cookbook']\t= '$version'" >> $VERSIONS
  done
  if ! git diff --quiet $VERSIONS > /dev/null; then
    item "Committing new cookbook versions to homebase"
    git commit $VERSIONS -m"$VERSIONS: updating testing cookbook versions"
  else
    item "No new cookbook versions to commit"
  fi
done

h1 "Push updates to development"
for name in $ALL_PATHS; do
  switch_to $name
  git pull
  git push origin master
done

h1 "Advance all changes to testing"
change_all_branches "testing" h2
h2 "Merge master into testing"
for name in $ALL_PATHS; do
  switch_to $name
  git merge master
  git push origin testing
done

h1 "Find, validate, upload, and freeze changed cookbooks"
for name in $HOMEBASES; do
  switch_to $name
  changed=''
  for cookbook in `ls -1d $testing/*/ | cut -d/ -f2`; do
    test_cb=$testing/$cookbook
    stage_cb=$staging/$cookbook
    # If $testing/$cookbook and $staging/$cookbook aren't identical
    no_change="diff -qr"
    if ! $no_change $test_cb $stage_cb > /dev/null 2>&1; then
      # make sure VERSIONs aren't identical either
      if $no_change $test_cb/VERSION $stage_cb/VERSION > /dev/null 2>&1; then
        failure 1 "testing version of $cookbook has changes, but same VERSION as staging"
      fi
      changed="$changed$cookbook "
    fi
  done
  if [ "nothing$changed" == "nothing" ]; then
    warning "Nothing changed - very suspicious"
    continue
  fi
  knife cookbook upload -V --freeze --force $changed
done

h1 "Upload testing environment"
for name in $HOMEBASES; do
  switch_to $name
  knife environment from file testing.rb
done

h1 "Uploading changed roles (test universe only)"
switch_to $TEST_PATH
changed=''
for file in `git diff --name-only staging roles/`; do
  if [ -f $file ]; then         # Don't treat a removed role as changed
    knife role from file $file
    changed="$changed$file "
  fi
done
if [ "nothing$changed" == "nothing" ]; then item "Nothing changed"; fi

success $TEST_KOAN

{'fraction_non_alphanumeric': 0.09683944374209862, 'fraction_numerical': 0.007838179519595449, 'mean_word_length': 3.607454863133372, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, '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': '258788', 'n_tokens_mistral': 2624, 'n_tokens_neox': 2560, 'n_words': 966}