Skip to content

Commit b05936e

Browse files
committed
fix: adding automatic error catalog synchronization script
1 parent 1cf754a commit b05936e

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
name: Synchronize Error Catalog Docs
2+
on:
3+
# This gets triggered on merges in the error-catalog repository.
4+
# See: https://github.com/snyk/error-catalog
5+
workflow_dispatch:
6+
# And for good measure, also runs at 3 AM UTC on the 1st of every month
7+
schedule:
8+
- cron: '0 3 1 * *'
9+
10+
env:
11+
DESTINATION_BRANCH: auto/synchronize-error-catalog-docs-${{ github.ref_name }}
12+
COMMIT_MESSAGE: 'fix: synchronize Error Catalog documentation'
13+
ERROR_CATALOG_DOCS_FILE_LOCAL: 'error-catalog/packages/error-catalog-docs/docs/README.md'
14+
ERROR_CATALOG_DOCS_FILE_REMOTE: 'docs/scan-with-snyk/error-catalog.md'
15+
16+
jobs:
17+
synchronize-variants:
18+
name: synchronize-snyk-variants
19+
runs-on: ubuntu-latest
20+
steps:
21+
- name: Checkout repository
22+
uses: actions/checkout@v4
23+
with:
24+
fetch-depth: 0
25+
26+
- name: Configure Git user
27+
run: |
28+
git config --global user.name "Team CLI Bot"
29+
git config --global user.email "[email protected]"
30+
31+
- name: Import and configure GPG
32+
env:
33+
GPG_KEY: ${{ secrets.TEAM_CLI_BOT_GPG_KEY }}
34+
GPG_PASSPHRASE: ${{ secrets.TEAM_CLI_BOT_GPG_PASSPHRASE }}
35+
run: |
36+
echo "$GPG_KEY" | gpg --batch --import
37+
gpg --pinentry-mode loopback --passphrase "$GPG_PASSPHRASE" --batch --sign >/dev/null 2>&1
38+
39+
- name: Create or checkout destination branch
40+
run: |
41+
if git show-ref --verify --quiet refs/heads/${{ env.DESTINATION_BRANCH }}; then
42+
echo "Branch ${{ env.DESTINATION_BRANCH }} already exists, checking out."
43+
git checkout ${{ env.DESTINATION_BRANCH }}
44+
echo "Cleaning it for a fresh start."
45+
git reset --hard origin/main
46+
exit 0
47+
fi
48+
49+
echo "Branch ${{ env.DESTINATION_BRANCH }} does not exist, creating and checking out."
50+
git checkout -b ${{ env.DESTINATION_BRANCH }}
51+
52+
- name: Checkout error-catalog repository
53+
uses: actions/checkout@v4
54+
with:
55+
repository: snyk/error-catalog
56+
path: error-catalog
57+
58+
- uses: actions/setup-node@v4
59+
with:
60+
node-version: '20'
61+
cache: 'npm'
62+
63+
- name: Generate documentation
64+
id: generate_documentation
65+
working-directory: error-catalog
66+
run: |
67+
npm i
68+
node_modules/.bin/nx run error-catalog-docs:generate-docs
69+
70+
- name: Check for changes
71+
id: check_changes
72+
run: |
73+
# Check if the error catalog documentation file has changed from what we have in the repository
74+
COMMAND="diff ${{ env.ERROR_CATALOG_DOCS_FILE_REMOTE }} ${{ env.ERROR_CATALOG_DOCS_FILE_LOCAL }}"
75+
if [[ -z "$($COMMAND)" ]]; then
76+
echo "No changes detected, exiting."
77+
echo "continue=false" >> "$GITHUB_OUTPUT"
78+
exit 0
79+
fi
80+
81+
echo "Changes detected:"
82+
$COMMAND
83+
echo "continue=true" >> "$GITHUB_OUTPUT"
84+
85+
- name: Commit and push changes (if any)
86+
if: steps.check_changes.outputs.continue == 'true'
87+
run: |
88+
cp ${{ env.ERROR_CATALOG_DOCS_FILE_LOCAL }} ${{ env.ERROR_CATALOG_DOCS_FILE_REMOTE }}
89+
git add .
90+
git commit -S -m "${{ env.COMMIT_MESSAGE }}"
91+
git push --force --set-upstream origin ${{ env.DESTINATION_BRANCH }}
92+
93+
- name: Create or update a pull request
94+
if: steps.check_changes.outputs.continue == 'true'
95+
env:
96+
GITHUB_TOKEN: ${{ secrets.TEAM_CLI_BOT_GITHUB_PAT }}
97+
run: |
98+
PR_NUMBER=$(gh pr list \
99+
--head "${{ env.DESTINATION_BRANCH }}" \
100+
--json number \
101+
--jq '.[0].number' \
102+
--limit 1)
103+
104+
if [ -n "$PR_NUMBER" ]; then
105+
echo "PR #$PR_NUMBER already exists. Updating it."
106+
echo "Pushed changes to existing PR #$PR_NUMBER."
107+
exit 0
108+
fi
109+
110+
echo "No existing PR found. Creating a new one."
111+
gh pr create \
112+
--title="${{ env.COMMIT_MESSAGE }}" \
113+
--body="This PR automatically updates the [error-catalog](https://github.com/snyk/error-catalog) documentation as changes were detected." \
114+
--head "${{ env.DESTINATION_BRANCH }}" \
115+
--base main

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.DS_Store
22
**/.DS_Store
33
.idea/
4+
error-catalog/

0 commit comments

Comments
 (0)