diff --git a/src/std/fwlib/blockTypes/_postDiffToGitHubSnippet.nix b/src/std/fwlib/blockTypes/_postDiffToGitHubSnippet.nix new file mode 100644 index 00000000..f8a20d0b --- /dev/null +++ b/src/std/fwlib/blockTypes/_postDiffToGitHubSnippet.nix @@ -0,0 +1,68 @@ +_: path: cmd: script: '' + + if [[ -v CI ]] && [[ -v BRANCH ]] && [[ -v OWNER_AND_REPO ]] && command -v gh > /dev/null ; then + + OWNER_REPO_NAME=$(gh repo view "$OWNER_AND_REPO" --json nameWithOwner --jq '.nameWithOwner') + + if ! gh pr view "$BRANCH" --repo "$OWNER_REPO_NAME" >/dev/null 2>&1; then + exit 0 + fi + + DIFF_OUTPUT=$(${script}) + if [[ -z "$DIFF_OUTPUT" ]]; then + exit 0 + fi + + CENTRAL_COMMENT_HEADER="" + ENTRY_START_MARKER="" + ENTRY_END_MARKER="" + + DIFF_ENTRY=$(cat < +//${path}:${cmd} + +\`\`\`diff +$DIFF_OUTPUT +\`\`\` + + +$ENTRY_END_MARKER +EOF + ) + + PR_NUMBER=$(gh pr view "$BRANCH" --repo "$OWNER_REPO_NAME" --json number --jq '.number') + + EXISTING_COMMENT_ID=$(gh api "repos/$OWNER_REPO_NAME/issues/$PR_NUMBER/comments?per_page=100" --jq ".[] | select(.body | contains(\"$CENTRAL_COMMENT_HEADER\")) | .id" | head -n 1) + + if [[ -n "$EXISTING_COMMENT_ID" ]]; then + EXISTING_BODY=$(gh api "repos/$OWNER_REPO_NAME/issues/comments/$EXISTING_COMMENT_ID" --jq '.body') + + if echo "$EXISTING_BODY" | grep -q "$ENTRY_START_MARKER"; then + UPDATED_BODY=$(echo "$EXISTING_BODY" | sed -e "\#$ENTRY_START_MARKER#,\#$ENTRY_END_MARKER#d") + else + UPDATED_BODY="$EXISTING_BODY" + fi + + UPDATED_BODY="$UPDATED_BODY +$DIFF_ENTRY" + + gh api --method PATCH "repos/$OWNER_REPO_NAME/issues/comments/$EXISTING_COMMENT_ID" -f body="$UPDATED_BODY" + + else + NEW_COMMENT=$(cat < /dev/null ; then + ${postDiffToGitHubSnippet fragmentRelPath "kubectl" "diff"} - set +e # diff exits 1 if diff existed - read -r -d "" DIFFSTREAM <Preview - - \`\`\`diff - $(diff) - \`\`\` - - - DIFF - set -e # we're past the invocation of diff - - if ! gh pr --repo "$OWNER_AND_REPO" comment "$BRANCH" --edit-last -b "$DIFFSTREAM"; then - echo "Make a first post ..." - gh pr --repo "$OWNER_AND_REPO" comment "$BRANCH" -b "$DIFFSTREAM" - fi - else - KUBECTL_EXTERNAL_DIFF="icdiff -N -r" - export KUBECTL_EXTERNAL_DIFF - diff - fi + KUBECTL_EXTERNAL_DIFF="icdiff -N -r" + export KUBECTL_EXTERNAL_DIFF + diff '' {}) (mkCommand currentSystem "apply" "Apply the manifests to K8s" [pkgs.kubectl pkgs.icdiff] '' ${build} diff --git a/src/std/fwlib/blockTypes/terra.nix b/src/std/fwlib/blockTypes/terra.nix index ee14b805..c7b7caa1 100644 --- a/src/std/fwlib/blockTypes/terra.nix +++ b/src/std/fwlib/blockTypes/terra.nix @@ -21,11 +21,11 @@ Available actions: */ let inherit (root) mkCommand; - inherit (super) addSelectorFunctor; + inherit (super) addSelectorFunctor postDiffToGitHubSnippet; in name: repo: { inherit name; - __functor = self: selectors: self // selectors; + __functor = addSelectorFunctor; type = "terra"; actions = { currentSystem, @@ -60,15 +60,16 @@ in .config); setup = '' + export TF_VAR_fragment=${pkgs.lib.strings.escapeShellArg fragment} + export TF_VAR_fragmentRelPath=${fragmentRelPath} export TF_IN_AUTOMATION=1 - # export TF_INPUT=0 export TF_DATA_DIR="$PRJ_DATA_HOME/${fragmentRelPath}" export TF_PLUGIN_CACHE_DIR="$PRJ_CACHE_HOME/tf-plugin-cache" mkdir -p "$TF_DATA_DIR" mkdir -p "$TF_PLUGIN_CACHE_DIR" - dir="$PRJ_ROOT/${repoFolder}/.tf" - mkdir -p "$PRJ_ROOT/${repoFolder}/.tf" - cat << MESSAGE > "$PRJ_ROOT/${repoFolder}/.tf/readme.md" + dir="$PRJ_ROOT/.cache/${fragmentRelPath}/.tf" + mkdir -p "$dir" + cat << MESSAGE > "$dir/readme.md" This is a tf staging area. It is motivated by the terraform CLI requiring to be executed in a staging area. MESSAGE @@ -76,16 +77,30 @@ in if [[ -e "$dir/config.tf.json" ]]; then rm -f "$dir/config.tf.json"; fi jq '.' ${terraformConfiguration} > "$dir/config.tf.json" ''; - - wrap = cmd: '' - ${setup} - terraform-backend-git git \ - --dir "$dir" \ - --repository ${git.repo} \ - --ref ${git.ref} \ - --state ${git.state} \ - terraform ${cmd} "$@"; - ''; + wrap = cmd: + setup + + ( + (pkgs.lib.optionalString (cmd == "plan")) ( + postDiffToGitHubSnippet fragmentRelPath cmd '' + terraform-backend-git git \ + --dir "$dir" \ + --repository ${git.repo} \ + --ref ${git.ref} \ + --state ${git.state} \ + terraform plan \ + -lock=false \ + -no-color + '' + ) + ) + + '' + terraform-backend-git git \ + --dir "$dir" \ + --repository ${git.repo} \ + --ref ${git.ref} \ + --state ${git.state} \ + terraform ${cmd} "$@"; + ''; in [ (mkCommand currentSystem "init" "tf init" [pkgs.jq pkgs.terraform pkgs.terraform-backend-git] (wrap "init") {}) (mkCommand currentSystem "plan" "tf plan" [pkgs.jq pkgs.terraform pkgs.terraform-backend-git] (wrap "plan") {})