Skip to content

Commit 1017a23

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

File tree

2 files changed

+125
-0
lines changed

2 files changed

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