Skip to content

Add workflow to determine API changes and comment on PRs #1

Add workflow to determine API changes and comment on PRs

Add workflow to determine API changes and comment on PRs #1

name: Analyze PR Changes
on: [pull_request]
env:
OPENSEARCH_INITIAL_ADMIN_PASSWORD: BobgG7YrtsdKf9M
jobs:
determine-changes:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Checkout Repo
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Build Docker Image
run: docker build coverage --tag opensearch-with-api-plugin
- name: Run OpenSearch
shell: bash -eo pipefail {0}
run: |
docker run \
--name opensearch \
--rm -d \
-p 9200:9200 -p 9600:9600 \
-e "discovery.type=single-node" \
-e OPENSEARCH_INITIAL_ADMIN_PASSWORD="$OPENSEARCH_INITIAL_ADMIN_PASSWORD" \
opensearch-with-api-plugin
for attempt in {1..20}; do
sleep 5
if curl -ksS -u "admin:$OPENSEARCH_INITIAL_ADMIN_PASSWORD" https://localhost:9200; then
echo '=> ready'
exit 0
fi
echo '=> waiting...'
done
exit 1
- name: Dump Cluster's API
shell: bash -eo pipefail {0}
run: curl -ksS -u "admin:$OPENSEARCH_INITIAL_ADMIN_PASSWORD" https://localhost:9200/_plugins/api | jq > /tmp/opensearch-openapi-CLUSTER.json
- name: Determine Branch Point
shell: bash -eo pipefail {0}
run: |
{
echo "BRANCH_POINT_SHA=$(git merge-base "$BASE_SHA" "$HEAD_SHA")"
echo "HEAD_SHA=${HEAD_SHA}"
} | tee "$GITHUB_ENV"
env:
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
BASE_SHA: ${{ github.event.pull_request.base.sha }}
- name: Checkout Original Spec
shell: bash -eo pipefail {0}
run: git checkout $BRANCH_POINT_SHA
- name: Build Original Spec
shell: bash -eo pipefail {0}
run: |
npm install
npm run merge -- --source ./spec --output /tmp/opensearch-openapi-ORIGINAL.yaml
- name: Checkout Changed Spec
shell: bash -eo pipefail {0}
run: git checkout $HEAD_SHA
- name: Build Changed Spec
shell: bash -eo pipefail {0}
run: |
npm install
npm run merge -- --source ./spec --output /tmp/opensearch-openapi-CHANGED.yaml
- name: Calculate Coverage
shell: bash -eo pipefail {0}
run: |
for variant in ORIGINAL CHANGED ; do
echo "Calculating coverage of ${variant}"
npm run coverage:spec -- \
--cluster /tmp/opensearch-openapi-CLUSTER.json \
--specification /tmp/opensearch-openapi-${variant}.yaml \
--output /tmp/coverage-api-${variant}.json
done
- name: Upload Coverage Data
id: upload-coverage
uses: actions/upload-artifact@v4
with:
name: coverage-api
path: |
/tmp/coverage-api-ORIGINAL.json
/tmp/coverage-api-CHANGED.json
- name: Install openapi-changes
shell: bash -eo pipefail {0}
run: npm install --global @pb33f/openapi-changes
- name: Generate HTML Report
shell: bash -eo pipefail {0}
run: openapi-changes html-report --no-logo --no-color /tmp/opensearch-openapi-ORIGINAL.yaml /tmp/opensearch-openapi-CHANGED.yaml
- name: Upload HTML Report
id: upload-report
uses: actions/upload-artifact@v4
with:
name: changes-report
path: |
report.html
/tmp/opensearch-openapi-ORIGINAL.yaml
/tmp/opensearch-openapi-CHANGED.yaml
- name: Generate Summary
shell: bash -eo pipefail {0}
run: |
if ! openapi-changes summary --no-logo --no-color --markdown /tmp/opensearch-openapi-ORIGINAL.yaml /tmp/opensearch-openapi-CHANGED.yaml >output.md ; then
if ! grep -q 'breaking changes discovered' output.md ; then
cat output.md >/dev/stderr
exit 1
fi
fi
gawk -v HEAD_SHA="${HEAD_SHA}" -v REPORT_URL="${REPORT_URL}" '
BEGIN {
print "### API Changes Summary"
RS = "(\r|\n|\r\n)"
WAS_BLANK = 0
HAD_CHANGES = 0
}
/^starting work/ || /^Building original model/ || /^SPEC: extracted/ || /^ERROR: breaking/ || /^DONE: completed/ {
next
}
/^[[:space:]]*$/ {
WAS_BLANK = 1
next
}
WAS_BLANK {
WAS_BLANK = 0
print ""
}
{
HAD_CHANGES = 1
sub(/Commit: New:/, "Commit: " HEAD_SHA ", New:")
print
}
END {
if (!HAD_CHANGES) {
print "Commit: " HEAD_SHA ", **NO CHANGES**\n"
}
print "\nFull Report: " REPORT_URL
}
' output.md | tee changes-summary.md
env:
REPORT_URL: ${{ steps.upload-report.outputs.artifact-url }}
- name: Upload Summary
uses: actions/upload-artifact@v4
with:
name: changes-summary
path: changes-summary.md