Skip to content

Commit d41c9fa

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

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
push: # FIXME: remove me
10+
11+
env:
12+
DESTINATION_BRANCH: auto/synchronize-error-catalog-docs-${{ github.ref_name }}
13+
COMMIT_MESSAGE: 'fix: synchronize Error Catalog documentation'
14+
ERROR_CATALOG_LOCATION: 'error-catalog'
15+
ERROR_CATALOG_DOCS_FILE_LOCAL: 'error-catalog/packages/error-catalog-docs/docs/README.md'
16+
ERROR_CATALOG_DOCS_FILE_REMOTE: 'docs/scan-with-snyk/error-catalog.md'
17+
18+
jobs:
19+
synchronize-variants:
20+
name: synchronize-error-catalog-docs
21+
runs-on: ubuntu-latest
22+
steps:
23+
- name: Checkout repository
24+
uses: actions/checkout@v4
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+
token: ${{ secrets.TEAM_CLI_BOT_GITHUB_PAT }}
56+
repository: snyk/error-catalog
57+
path: ${{ env.ERROR_CATALOG_LOCATION }}
58+
59+
- uses: actions/setup-node@v4
60+
with:
61+
cache: 'npm'
62+
node-version: '20'
63+
cache-dependency-path: ${{ env.ERROR_CATALOG_LOCATION }}/package-lock.json
64+
65+
- name: Authenticate with NPM
66+
working-directory: ${{ env.ERROR_CATALOG_LOCATION }}
67+
run: echo "//registry.npmjs.org/:_authToken=${{ secrets.TEAM_CLI_NPM_TOKEN }}" > ~/.npmrc
68+
69+
- name: Generate documentation
70+
id: generate_documentation
71+
working-directory: ${{ env.ERROR_CATALOG_LOCATION }}
72+
env:
73+
NPM_TOKEN: ${{ secrets.TEAM_CLI_NPM_TOKEN }}
74+
run: |
75+
npm i
76+
node_modules/.bin/nx run error-catalog-docs:generate-docs
77+
78+
- name: Check for changes
79+
id: check_changes
80+
run: |
81+
# Check if the error catalog documentation file has changed from what we have in the repository
82+
COMMAND="diff ${{ env.ERROR_CATALOG_DOCS_FILE_REMOTE }} ${{ env.ERROR_CATALOG_DOCS_FILE_LOCAL }} --color=always"
83+
if [[ -z "$($COMMAND)" ]]; then
84+
echo "No changes detected, exiting."
85+
echo "continue=false" >> "$GITHUB_OUTPUT"
86+
exit 0
87+
fi
88+
89+
echo "Changes detected:"
90+
$COMMAND || true
91+
echo "continue=true" >> "$GITHUB_OUTPUT"
92+
93+
- name: Commit and push changes (if any)
94+
if: steps.check_changes.outputs.continue == 'true'
95+
run: |
96+
cp ${{ env.ERROR_CATALOG_DOCS_FILE_LOCAL }} ${{ env.ERROR_CATALOG_DOCS_FILE_REMOTE }}
97+
git add .
98+
git commit -S -m "${{ env.COMMIT_MESSAGE }}"
99+
git push --force --set-upstream origin ${{ env.DESTINATION_BRANCH }}
100+
101+
- name: Create or update a pull request
102+
if: steps.check_changes.outputs.continue == 'true'
103+
env:
104+
GITHUB_TOKEN: ${{ secrets.TEAM_CLI_BOT_GITHUB_PAT }}
105+
run: |
106+
PR_NUMBER=$(gh pr list \
107+
--head "${{ env.DESTINATION_BRANCH }}" \
108+
--json number \
109+
--jq '.[0].number' \
110+
--limit 1)
111+
112+
if [ -n "$PR_NUMBER" ]; then
113+
echo "PR #$PR_NUMBER already exists. Updating it."
114+
echo "Pushed changes to existing PR #$PR_NUMBER."
115+
exit 0
116+
fi
117+
118+
echo "No existing PR found. Creating a new one."
119+
gh pr create \
120+
--title="${{ env.COMMIT_MESSAGE }}" \
121+
--body="This PR automatically updates the [error-catalog](https://github.com/snyk/error-catalog) documentation as changes were detected." \
122+
--head "${{ env.DESTINATION_BRANCH }}" \
123+
--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)