Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
oneingan committed Oct 18, 2024
1 parent f8f6f70 commit 406bfe2
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 44 deletions.
68 changes: 68 additions & 0 deletions src/std/fwlib/blockTypes/_postDiffToGitHubSnippet.nix
Original file line number Diff line number Diff line change
@@ -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="<!-- Unified Diff Comment -->"
ENTRY_START_MARKER="<!-- Start Diff for ${path}:${cmd} -->"
ENTRY_END_MARKER="<!-- End Diff for ${path}:${cmd} -->"
DIFF_ENTRY=$(cat <<EOF
$ENTRY_START_MARKER
<details>
<summary>//${path}:${cmd}</summary>
\`\`\`diff
$DIFF_OUTPUT
\`\`\`
</details>
$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 <<EOF
$CENTRAL_COMMENT_HEADER
This PR includes the following diffs:
$DIFF_ENTRY
EOF
)
gh pr comment "$PR_NUMBER" --repo "$OWNER_REPO_NAME" --body "$NEW_COMMENT"
fi
exit 0
fi
''
33 changes: 5 additions & 28 deletions src/std/fwlib/blockTypes/kubectl.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Available actions:
*/
let
inherit (root) mkCommand;
inherit (super) addSelectorFunctor askUserToProceedSnippet;
inherit (super) addSelectorFunctor askUserToProceedSnippet postDiffToGitHubSnippet;
in
name: {
__functor = addSelectorFunctor;
Expand Down Expand Up @@ -137,34 +137,11 @@ in
} "$manifest_path/";
}
# GitHub case
if [[ -v CI ]] && [[ -v BRANCH ]] && [[ -v OWNER_AND_REPO ]] && command gh > /dev/null ; then
${postDiffToGitHubSnippet fragmentRelPath "kubectl" "diff"}
set +e # diff exits 1 if diff existed
read -r -d "" DIFFSTREAM <<DIFF
## Standard DiffPost
This PR would generate the following \`kubectl\` diff:
<details><summary>Preview</summary>
\`\`\`diff
$(diff)
\`\`\`
</details>
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}
Expand Down
47 changes: 31 additions & 16 deletions src/std/fwlib/blockTypes/terra.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -60,32 +60,47 @@ 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
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") {})
Expand Down

0 comments on commit 406bfe2

Please sign in to comment.