Skip to content

Commit

Permalink
communicate breaking changes to the user (GoogleCloudPlatform#6642)
Browse files Browse the repository at this point in the history
Co-authored-by: Riley Karson <[email protected]>
  • Loading branch information
ScottSuarez and rileykarson authored Oct 19, 2022
1 parent b82613a commit 038434c
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 9 deletions.
10 changes: 5 additions & 5 deletions .ci/containers/github-differ/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FROM alpine/git
from golang:1.18-stretch as resource
SHELL ["/bin/bash", "-c"]

RUN apk add --no-cache curl
RUN apk add --no-cache curl-dev
RUN apk add --no-cache bash
RUN apk add --no-cache jq
RUN apt-get update
RUN apt-get install -y curl jq
ADD generate_comment.sh /generate_comment.sh
ADD compare_breaking_changes.sh /compare_breaking_changes.sh
ENTRYPOINT ["/generate_comment.sh"]
59 changes: 59 additions & 0 deletions .ci/containers/github-differ/compare_breaking_changes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash
# input: two environment variables
# TPG_BREAKING - results of runing breaking change detector
# against tpg
# TPGB_BREAKING - results of runing breaking change detector
# against tpgb
# output: echo to console
# message section cotaining: a header,
# tpg's unique messages, and all of tpgb's messages

tpgUnique=""
newline=$'\n'

# This while loop itterates over each individual
# line of TPG_BREAKING. The input to the while loop
# is through the <<< at the conclusion of the loop.
while read -r tpgi; do
simpleTPG=$(sed 's/-.*//' <<< "$tpgi")
found="false"
while read -r tpgbi; do
simpleTPGB=$(sed 's/-.*//' <<< "$tpgbi")
if [ "$simpleTPG" == "$simpleTPGB" ]; then
found="true"
fi
done <<< "$TPGB_BREAKING"
if [ "$found" != "true" ]; then
if [ "$tpgUnique" == "" ]; then
tpgUnique="${tpgi}"
else
tpgUnique="${tpgUnique}${newline}${tpgi}"
fi
fi
done <<< "$TPG_BREAKING"


breakingchanges=""
if [ "$tpgUnique" != "" ]; then
tpgUnique=$(sed 's/^/\* /' <<< "$tpgUnique")
breakingchanges="${breakingchanges}${tpgUnique}${newline}"
fi

if [ "$TPGB_BREAKING" != "" ]; then
tpgbBreaking=$(sed 's/^/\* /' <<< "$TPGB_BREAKING")
breakingchanges="${breakingchanges}${tpgbBreaking}${newline}"
fi

if [ "$breakingchanges" != "" ]; then
message="## Breaking Change(s) Detected
The following breaking change(s) were detected within your pull request.
${breakingchanges}
If you believe this detection to be incorrect please raise the concern with your reviewer. If you intend to make this change you will need to wait for a [major release](https://www.terraform.io/plugin/sdkv2/best-practices/versioning#example-major-number-increments) window. An \`override-breaking-change\` label can be added to allow merging.
"
fi

echo "$message"


91 changes: 87 additions & 4 deletions .ci/containers/github-differ/generate_comment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fi
PR_NUMBER=$1
NEW_BRANCH=auto-pr-$PR_NUMBER
OLD_BRANCH=auto-pr-$PR_NUMBER-old
MM_LOCAL_PATH=$PWD
TPG_SCRATCH_PATH=https://modular-magician:$GITHUB_TOKEN@github.com/modular-magician/terraform-provider-google
TPG_LOCAL_PATH=$PWD/../tpg
TPGB_SCRATCH_PATH=https://modular-magician:$GITHUB_TOKEN@github.com/modular-magician/terraform-provider-google-beta
Expand All @@ -30,7 +31,7 @@ TFC_LOCAL_PATH=$PWD/../tfc
DIFFS=""
NEWLINE=$'\n'

# TPG
# TPG difference
mkdir -p $TPG_LOCAL_PATH
git clone -b $NEW_BRANCH $TPG_SCRATCH_PATH $TPG_LOCAL_PATH
pushd $TPG_LOCAL_PATH
Expand All @@ -39,6 +40,28 @@ if ! git diff --exit-code origin/$OLD_BRANCH origin/$NEW_BRANCH; then
SUMMARY=`git diff origin/$OLD_BRANCH origin/$NEW_BRANCH --shortstat`
DIFFS="${DIFFS}${NEWLINE}Terraform GA: [Diff](https://github.com/modular-magician/terraform-provider-google/compare/$OLD_BRANCH..$NEW_BRANCH) ($SUMMARY)"
fi
git checkout origin/$NEW_BRANCH
popd

## Breaking change setup and execution
TPG_LOCAL_PATH_OLD="${TPG_LOCAL_PATH}old"
mkdir -p $TPG_LOCAL_PATH_OLD
cp -r $TPG_LOCAL_PATH/. $TPG_LOCAL_PATH_OLD
pushd $TPG_LOCAL_PATH_OLD
git checkout origin/$OLD_BRANCH
popd
set +e
pushd $MM_LOCAL_PATH/tools/breaking-change-detector
sed -i.bak -E "s~google/provider/(.*)/([0-9A-Za-z-]*)~google/provider/\1/google~" comparison.go
go mod edit -replace google/provider/new=$(realpath $TPG_LOCAL_PATH)
go mod edit -replace google/provider/old=$(realpath $TPG_LOCAL_PATH_OLD)
go mod tidy
export TPG_BREAKING="$(go run .)"
retVal=$?
if [ $retVal -ne 0 ]; then
export TPG_BREAKING=""
fi
set -e
popd

# TPGB
Expand All @@ -50,6 +73,30 @@ if ! git diff --exit-code origin/$OLD_BRANCH origin/$NEW_BRANCH; then
SUMMARY=`git diff origin/$OLD_BRANCH origin/$NEW_BRANCH --shortstat`
DIFFS="${DIFFS}${NEWLINE}Terraform Beta: [Diff](https://github.com/modular-magician/terraform-provider-google-beta/compare/$OLD_BRANCH..$NEW_BRANCH) ($SUMMARY)"
fi
git checkout origin/$NEW_BRANCH
popd


## Breaking change setup and execution
TPGB_LOCAL_PATH_OLD="${TPGB_LOCAL_PATH}old"
mkdir -p $TPGB_LOCAL_PATH_OLD
cp -r $TPGB_LOCAL_PATH/. $TPGB_LOCAL_PATH_OLD
pushd $TPGB_LOCAL_PATH_OLD
git checkout origin/$OLD_BRANCH
popd
set +e
pushd $MM_LOCAL_PATH/tools/breaking-change-detector
sed -i.bak -E "s~google/provider/(.*)/([0-9A-Za-z-]*)~google/provider/\1/google-beta~" comparison.go
go mod edit -replace google/provider/new=$(realpath $TPGB_LOCAL_PATH)
go mod edit -replace google/provider/old=$(realpath $TPGB_LOCAL_PATH_OLD)
go mod tidy
export TPGB_BREAKING="$(go run .)"
retVal=$?
if [ $retVal -ne 0 ]; then
export TPGB_BREAKING=""
fi
BREAKINGCHANGES="$(/compare_breaking_changes.sh)"
set -e
popd

# TF Conversion - for compatibility until at least Nov 15 2021
Expand Down Expand Up @@ -89,12 +136,48 @@ if ! git diff --exit-code --quiet origin/$OLD_BRANCH origin/$NEW_BRANCH; then
fi
popd

MESSAGE="Hi there, I'm the Modular magician. I've detected the following information about your changes:${NEWLINE}${NEWLINE}"

BREAKINGSTATE="success"
if [ -n "$BREAKINGCHANGES" ]; then
MESSAGE="${MESSAGE}${BREAKINGCHANGES}${NEWLINE}${NEWLINE}"

BREAKINGCHANGE_OVERRIDE=$(curl \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
"https://api.github.com/repos/GoogleCloudPlatform/magic-modules/pulls/$PR_NUMBER"\
| jq ".labels|any(.id==4598495472)")

if [ "${BREAKINGCHANGE_OVERRIDE}" == "true" ]; then
BREAKINGSTATE="success"
else
BREAKINGSTATE="failure"
fi
fi


if [ -z "$DIFFS" ]; then
DIFFS="Hi! I'm the modular magician. Your PR hasn't generated any diffs, but I'll let you know if a future commit does."
MESSAGE="${MESSAGE}## Diff report ${NEWLINE}Your PR hasn't generated any diffs, but I'll let you know if a future commit does."
else
DIFFS="Hi! I'm the modular magician. Your PR generated some diffs in downstreams - here they are.$NEWLINE# Diff report:$NEWLINE$NEWLINE$DIFFS"
MESSAGE="${MESSAGE}## Diff report ${NEWLINE}Your PR generated some diffs in downstreams - here they are.${NEWLINE}${DIFFS}"
fi



#;region=global/${BUILD_ID};step=19?project=${PROJECT_ID}"
BREAKINGSTATE_BODY=$( jq -n \
--arg context "terraform-provider-breaking-change-test" \
--arg target_url "https://console.cloud.google.com/cloud-build/builds;region=global/${BUILD_ID};step=${BUILD_STEP}?project=${PROJECT_ID}"" \
--arg breakingstate "${BREAKINGSTATE}" \
'{context: $context, target_url: $target_url, state: $breakingstate}')
curl \
-X POST \
-u "$github_username:$GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/GoogleCloudPlatform/magic-modules/statuses/$COMMIT_SHA" \
-d "$BREAKINGSTATE_BODY"
curl -H "Authorization: token ${GITHUB_TOKEN}" \
-d "$(jq -r --arg diffs "$DIFFS" -n "{body: \$diffs}")" \
-d "$(jq -r --arg diffs "$MESSAGE" -n "{body: \$diffs}")" \
"https://api.github.com/repos/GoogleCloudPlatform/magic-modules/issues/${PR_NUMBER}/comments"
6 changes: 6 additions & 0 deletions .ci/gcb-generate-diffs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ steps:
- name: 'gcr.io/graphite-docker-images/github-differ'
id: diff
secretEnv: ["GITHUB_TOKEN"]
env:
- BUILD_ID=$BUILD_ID
- PROJECT_ID=$PROJECT_ID
- BUILD_STEP="19"
- COMMIT_SHA=$COMMIT_SHA
- PR_NUMBER=$_PR_NUMBER
args:
- $_PR_NUMBER

Expand Down

0 comments on commit 038434c

Please sign in to comment.