diff --git a/.github/workflows/sync-cli-help-to-user-docs.yml b/.github/workflows/sync-cli-help-to-user-docs.yml index 8720b04080..f86da99170 100644 --- a/.github/workflows/sync-cli-help-to-user-docs.yml +++ b/.github/workflows/sync-cli-help-to-user-docs.yml @@ -1,43 +1,108 @@ -name: Synchronize Help - +name: Synchronize CLI Help to User Docs on: workflow_dispatch: schedule: - - cron: '0 9 * * 1' # Mon at 9 - push: - branches: [chore/docs-action] + - cron: '0 9 * * 1' # Monday at 9 + +env: + DESTINATION_BRANCH: docs/automatic-gitbook-update-cli-help-${{ github.ref_name }} + COMMIT_MESSAGE: 'docs: synchronizing help from snyk/user-docs' jobs: build: name: synchronize-help runs-on: ubuntu-latest steps: - - run: | - gh auth setup-git + - name: Checkout user-docs repository + uses: actions/checkout@v4 + with: + repository: snyk/user-docs + path: user-docs + ref: main + + - name: Checkout cli repository + uses: actions/checkout@v4 + with: + repository: snyk/cli + ref: ${{ github.ref_name }} + path: cli + token: ${{ secrets.GITHUB_TOKEN }} + fetch-depth: 0 + + - name: Configure git user + run: | git config --global user.email "noreply@snyk.io" + + # GITHUB_ACTOR: The name of the person or app that initiated the workflow. git config --global user.name "$GITHUB_ACTOR" - gh repo clone snyk/snyk cli -- --depth=1 --quiet - gh repo clone snyk/user-docs docs -- --depth=1 --quiet - git -C ./cli checkout -b docs/automatic-gitbook-update - - cp ./docs/docs/snyk-cli/commands/*.md ./cli/help/cli-commands/ - - if [[ $(git -C ./cli status --porcelain) ]]; then - echo "Documentation changes detected" - cd ./cli - npm clean-install - npx prettier --write ./help/cli-commands/*.md - git --no-pager diff --name-only - git add . - git commit -m "docs: synchronizing help from snyk/user-docs" - git push --force --set-upstream origin docs/automatic-gitbook-update - if [[ ! $(gh pr view docs/automatic-gitbook-update 2>&1 | grep -q "no open pull requests";) ]]; then - echo "Creating PR" - gh pr create --title="docs: Synchronizing CLI help from user-docs" --body="Automatic PR controlled by GitHub Action" --head docs/automatic-gitbook-update - fi - echo "PR exists, pushed changes to it." - else + + - name: Create or checkout destination branch + run: | + cd cli + if git show-ref --verify --quiet refs/heads/${{ env.DESTINATION_BRANCH }}; then + echo "Branch ${{ env.DESTINATION_BRANCH }} already exists, checking out." + git checkout ${{ env.DESTINATION_BRANCH }} + exit 0 + fi + + echo "Branch ${{ env.DESTINATION_BRANCH }} does not exist, creating and checking out." + git checkout -b ${{ env.DESTINATION_BRANCH }} + + - name: Copy help documentation from user-docs to CLI + run: | + cp ./user-docs/docs/snyk-cli/commands/*.md ./cli/help/cli-commands/ + + - name: Format help files with Prettier + run: | + cd ./cli + npm clean-install + npx prettier --write ./help/cli-commands/*.md + + - name: Check for changes + id: check_changes + run: | + cd ./cli + if [[ -z "$(git status --porcelain)" ]]; then echo "No documentation changes detected, exiting." + echo "continue=false" >> "$GITHUB_OUTPUT" + exit 0 fi + + echo "--- Documentation changes detected from GitBooks ---" + git --no-pager diff --color=always + echo "------------------------------------------------" + + echo "continue=true" >> "$GITHUB_OUTPUT" + + - name: Commit and push changes (if any) + if: steps.check_changes.outputs.continue == 'true' + run: | + cd ./cli + git add help + git commit -m "${{ env.COMMIT_MESSAGE }}" + git push --force --set-upstream origin ${{ env.DESTINATION_BRANCH }} + + - name: Create or update a pull request + if: steps.check_changes.outputs.continue == 'true' + run: | + cd ./cli + PR_NUMBER=$(gh pr list \ + --head "${{ env.DESTINATION_BRANCH }}" \ + --json number \ + --jq '.[0].number' \ + --limit 1) + + if [ -n "$PR_NUMBER" ]; then + echo "PR #$PR_NUMBER already exists. Updating it." + echo "Pushed changes to existing PR #$PR_NUMBER." + exit 0 + fi + + echo "No existing PR found. Creating a new one." + gh pr create \ + --title="${{ env.COMMIT_MESSAGE }}" \ + --body="Automatic PR controlled by GitHub Action" \ + --head "${{ env.DESTINATION_BRANCH }}" \ + --base ${{ github.ref_name || 'main' }} env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/sync-cli-readme.yml b/.github/workflows/sync-cli-readme.yml new file mode 100644 index 0000000000..64b3c495e5 --- /dev/null +++ b/.github/workflows/sync-cli-readme.yml @@ -0,0 +1,119 @@ +name: Synchronize README from GitBooks +on: + workflow_dispatch: + schedule: + - cron: '0 9 * * 1' # Monday at 9 + +env: + DESTINATION_BRANCH: docs/automatic-gitbook-update-readme + COMMIT_MESSAGE: 'docs: synchronizing README from snyk/user-docs' + +jobs: + build: + name: synchronize-readme + runs-on: ubuntu-latest + steps: + - name: Checkout user-docs repository + uses: actions/checkout@v4 + with: + repository: snyk/user-docs + path: user-docs + ref: main + + - name: Checkout cli repository + uses: actions/checkout@v4 + with: + repository: snyk/cli + path: cli + token: ${{ secrets.GITHUB_TOKEN }} + fetch-depth: 0 + + - name: Configure git user + run: | + git config --global user.email "noreply@snyk.io" + + # GITHUB_ACTOR: The name of the person or app that initiated the workflow. + git config --global user.name "$GITHUB_ACTOR" + + - name: Create or checkout destination branch + run: | + cd cli + if git show-ref --verify --quiet refs/heads/${{ env.DESTINATION_BRANCH }}; then + echo "Branch ${{ env.DESTINATION_BRANCH }} already exists, checking out." + git checkout ${{ env.DESTINATION_BRANCH }} + exit 0 + fi + + echo "Branch ${{ env.DESTINATION_BRANCH }} does not exist, creating and checking out." + git checkout -b ${{ env.DESTINATION_BRANCH }} + + - name: Retrieve GitBook content and update README + run: | + cp ./docs/snyk-cli/getting-started-with-the-snyk-cli.md ./cli/README.md + + # GitBook Markdown files often use relative paths for assets (e.g., images) + # like `../.gitbook/assets/image.png`. When this README.md is viewed directly + # on GitHub, these relative paths won't resolve correctly. This `sed` command + # replaces those relative paths with the full, absolute URL to the assets + # hosted in the `snyk/user-docs` GitHub repository's raw content. + # + # - `-i`: Edits the file in-place, modifying the original README.md directly. + # - `-e`: Specifies the script to be executed. In this case, it's the 's' + # (substitute) command which replaces the old path with the new URL. + sed -i \ + -e "s|../.gitbook/assets/|https://github.com/snyk/user-docs/raw/HEAD/docs/.gitbook/assets/|g" \ + ./cli/README.md + + - name: Format README with Prettier + run: | + cd ./cli + npx prettier --write README.md + + - name: Check for changes + id: check_changes + run: | + cd ./cli + if [[ -z "$(git status --porcelain)" ]]; then + echo "No documentation changes detected, exiting." + echo "continue=false" >> "$GITHUB_OUTPUT" + exit 0 + fi + + echo "--- Documentation changes detected from GitBooks (before Prettier) ---" + git --no-pager diff --color=always + echo "---------------------------------------------------------------------" + + echo "continue=true" >> "$GITHUB_OUTPUT" + + - name: Commit and push changes (if any) + if: steps.check_changes.outputs.continue == 'true' + run: | + cd ./cli + git add help + git commit -m "${{ env.COMMIT_MESSAGE }}" + git push --force --set-upstream origin ${{ env.DESTINATION_BRANCH }} + + - name: Create or update a pull request + if: steps.check_changes.outputs.continue == 'true' + run: | + cd ./cli + PR_NUMBER=$(gh pr list \ + --head "${{ env.DESTINATION_BRANCH }}" \ + --json number \ + --jq '.[0].number' \ + --limit 1) + + if [ -n "$PR_NUMBER" ]; then + echo "PR #$PR_NUMBER already exists. Updating it." + echo "Pushed changes to existing PR #$PR_NUMBER." + exit 0 + fi + + echo "No existing PR found. Creating a new one." + gh pr create \ + --title="${{ env.COMMIT_MESSAGE }}" \ + --body="Automatic PR controlled by GitHub Action" \ + --head "${{ env.DESTINATION_BRANCH }}" \ + --base main + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/synchronize-readme.yml b/.github/workflows/synchronize-readme.yml deleted file mode 100644 index 1f1acdd352..0000000000 --- a/.github/workflows/synchronize-readme.yml +++ /dev/null @@ -1,63 +0,0 @@ -name: Synchronize Readme - -on: - workflow_dispatch: - #schedule: - # - cron: '0 12 * * 1-5' # Mon-Fri at 12 - -jobs: - build: - name: synchronize-readme - runs-on: ubuntu-latest - steps: - - run: | - # Setup - gh auth setup-git - git config --global user.email "noreply@snyk.io" - git config --global user.name "$GITHUB_ACTOR" - - # Clone the CLI repository - gh repo clone snyk/cli cli -- --depth=1 --quiet - git -C ./cli checkout -B $DESTINATION_BRANCH - - # Retrieve the GitBook content - wget https://raw.githubusercontent.com/snyk/user-docs/main/docs/snyk-cli/getting-started-with-the-snyk-cli.md -O current_gitbook.md - - # Find relative paths to GitBooks assets (such as images) and replace with absolute paths - sed -i \ - -e "s|../.gitbook/assets/|https://github.com/snyk/user-docs/raw/HEAD/docs/.gitbook/assets/|g" \ - current_gitbook.md - - # Replace the README.md content with the GitBook content - cp current_gitbook.md ./cli/README.md - - # If changes, commit and create PR - if [[ $(git -C ./cli status --porcelain) ]]; then - echo "Documentation changes detected" - cd ./cli - npm clean-install - npx prettier --write README.md - git push -f -u origin $DESTINATION_BRANCH - - export SHA=$( git rev-parse $DESTINATION_BRANCH:README.md ) - export CONTENT=$( base64 -i README.md ) - gh api --method PUT /repos/:owner/:repo/contents/README.md \ - --field message="$MESSAGE" \ - --field content="$CONTENT" \ - --field encoding="base64" \ - --field branch="$DESTINATION_BRANCH" \ - --field sha="$SHA" - - if [[ ! $(gh pr list --search "$MESSAGE" 2>&1 | grep -e "$MESSAGE";) ]]; then - echo "Creating PR" - gh pr create --title="$MESSAGE" --body="Automatic PR controlled by GitHub Action." --head $DESTINATION_BRANCH - else - echo "PR exists, pushed changes to it." - fi - else - echo "No documentation changes detected, exiting." - fi - env: - DESTINATION_BRANCH: docs/automatic-gitbook-update - MESSAGE: 'docs: synchronizing README from GitBook' - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/help/README.md b/help/README.md index 1a912f4160..75d5cb1b4b 100644 --- a/help/README.md +++ b/help/README.md @@ -1,9 +1,18 @@ # CLI Help files -Snyk CLI Help files are managed by GitBook connected to the [snyk/user-docs](https://github.com/snyk/user-docs) repository. +Snyk CLI Help files are managed by GitBook connected to the [snyk/user-docs](https://github.com/snyk/user-docs) +repository. -It's recommended to make all CLI Help changes there. Changes from GitBook are automatically [synced to the Snyk CLI repository with a GitHub Action](https://github.com/snyk/snyk/actions/workflows/sync-cli-help-to-user-docs.yml). The Action creates a PR that needs to be approved and merged. This Action could also be manually triggered. +It's recommended to make all CLI Help changes there, as changes here will be overwritten by the next sync. -If you need to make changes tied to a specific PR, you can make them in this repository first, merge the changes and then move them over to the GitBook. +Changes from GitBook are automatically synced to the Snyk CLI repository with GitHub Actions: + +- https://github.com/snyk/snyk/actions/workflows/sync-cli-help-to-user-docs.yml and +- https://github.com/snyk/snyk/actions/workflows/sync-cli-readme.yml + +The actions create PRs that need to be approved and merged. This action could also be manually triggered. + +If you need to make changes tied to a specific PR, you can make them in this repository first, merge the changes and +then move them over to the GitBook. CLI help files are a standard Markdown.