Skip to content

Commit 48924f3

Browse files
author
Kaspar Lyngsie
committed
chore: did the same to the help sync script
1 parent c25ef3b commit 48924f3

1 file changed

Lines changed: 89 additions & 25 deletions

File tree

Lines changed: 89 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,107 @@
1-
name: Synchronize Help
2-
1+
name: Synchronize CLI Help to User Docs
32
on:
43
workflow_dispatch:
54
schedule:
6-
- cron: '0 9 * * 1' # Mon at 9
7-
push:
8-
branches: [chore/docs-action]
5+
- cron: '0 9 * * 1' # Monday at 9
6+
7+
env:
8+
DESTINATION_BRANCH: docs/automatic-gitbook-update-cli-help
9+
COMMIT_MESSAGE: "docs: synchronizing help from snyk/user-docs"
910

1011
jobs:
1112
build:
1213
name: synchronize-help
1314
runs-on: ubuntu-latest
1415
steps:
15-
- run: |
16-
gh auth setup-git
16+
- name: Checkout user-docs repository
17+
uses: actions/checkout@v4
18+
with:
19+
repository: snyk/user-docs
20+
path: user-docs
21+
ref: main
22+
23+
- name: Checkout cli repository
24+
uses: actions/checkout@v4
25+
with:
26+
repository: snyk/cli
27+
path: cli
28+
token: ${{ secrets.GITHUB_TOKEN }}
29+
fetch-depth: 0
30+
31+
- name: Configure git user
32+
run: |
1733
git config --global user.email "noreply@snyk.io"
34+
35+
# GITHUB_ACTOR: The name of the person or app that initiated the workflow.
1836
git config --global user.name "$GITHUB_ACTOR"
19-
gh repo clone snyk/snyk cli -- --depth=1 --quiet
20-
gh repo clone snyk/user-docs docs -- --depth=1 --quiet
21-
git -C ./cli checkout -b docs/automatic-gitbook-update
2237
38+
- name: Create or checkout destination branch
39+
run: |
40+
cd cli
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+
exit 0
45+
fi
46+
47+
echo "Branch ${{ env.DESTINATION_BRANCH }} does not exist, creating and checking out."
48+
git checkout -b ${{ env.DESTINATION_BRANCH }}
49+
50+
- name: Copy help documentation from user-docs to CLI
51+
run: |
2352
cp ./docs/docs/snyk-cli/commands/*.md ./cli/help/cli-commands/
2453
25-
if [[ $(git -C ./cli status --porcelain) ]]; then
26-
echo "Documentation changes detected"
27-
cd ./cli
28-
npm clean-install
29-
npx prettier --write ./help/cli-commands/*.md
30-
git --no-pager diff --name-only
31-
git add .
32-
git commit -m "docs: synchronizing help from snyk/user-docs"
33-
git push --force --set-upstream origin docs/automatic-gitbook-update
34-
if [[ ! $(gh pr view docs/automatic-gitbook-update 2>&1 | grep -q "no open pull requests";) ]]; then
35-
echo "Creating PR"
36-
gh pr create --title="docs: Synchronizing CLI help from user-docs" --body="Automatic PR controlled by GitHub Action" --head docs/automatic-gitbook-update
37-
fi
38-
echo "PR exists, pushed changes to it."
39-
else
54+
- name: Format help files with Prettier
55+
run: |
56+
cd ./cli
57+
npm clean-install
58+
npx prettier --write ./help/cli-commands/*.md
59+
60+
- name: Check for changes
61+
id: check_changes
62+
run: |
63+
cd ./cli
64+
if [[ -z "$(git status --porcelain)" ]]; then
4065
echo "No documentation changes detected, exiting."
66+
echo "continue=false" >> "$GITHUB_OUTPUT"
67+
exit 0
68+
fi
69+
70+
echo "--- Documentation changes detected from GitBooks ---"
71+
git --no-pager diff --name-only --color=always
72+
echo "------------------------------------------------"
73+
74+
echo "continue=true" >> "$GITHUB_OUTPUT"
75+
76+
- name: Commit and push changes (if any)
77+
if: steps.check_changes.outputs.continue == 'true'
78+
run: |
79+
cd ./cli
80+
git add .
81+
git commit -m "${{ env.COMMIT_MESSAGE }}"
82+
git push --force --set-upstream origin ${{ env.DESTINATION_BRANCH }}
83+
84+
- name: Create or update a pull request
85+
if: steps.check_changes.outputs.continue == 'true'
86+
run: |
87+
cd ./cli
88+
PR_NUMBER=$(gh pr list \
89+
--head "${{ env.DESTINATION_BRANCH }}" \
90+
--json number \
91+
--jq '.[0].number' \
92+
--limit 1)
93+
94+
if [ -n "$PR_NUMBER" ]; then
95+
echo "PR #$PR_NUMBER already exists. Updating it."
96+
echo "Pushed changes to existing PR #$PR_NUMBER."
97+
exit 0
4198
fi
99+
100+
echo "No existing PR found. Creating a new one."
101+
gh pr create \
102+
--title="${{ env.COMMIT_MESSAGE }}" \
103+
--body="Automatic PR controlled by GitHub Action" \
104+
--head "${{ env.DESTINATION_BRANCH }}" \
105+
--base main
42106
env:
43107
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)