name: Publish to Confluence
on:
  push:
    branches: [ master ]
    # paths:
    #   - '**'
    #   - '!.gitignore'
    #   - '!README.md'
  # workflow_dispatch: # 允许手动触发工作流
  # schedule:
  #   # 每天晚六点十分执行一次
  #   - cron:  '10 10 * * *'

env:
  SEPARATOR: "," # 定义多文件分割符
  START_PREFIX: "<!--" # 定义需要同步的文件

jobs:
  confluence:
    runs-on: ubuntu-latest

    steps:
      # 检出文件
      - uses: https://gitea.com/actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Print current path
        run: |
          pwd
          ls

      # 获取变更的文件
      - name: Get changed files
        id: changed-markdown-files
        uses: https://texous.cn/actions/changed-files@v44.5.5
        with:
          files: |
            **.md
            **.png
          files_ignore: |
            **.tpl.md
          quotepath: false
          separator: ${{env.SEPARATOR}}
          old_new_files_separator: ";"

      # 打印所有变更的文件
      - name: Print all changed files
        if: steps.changed-markdown-files.outputs.any_changed == 'true'
        env:
          ALL_CHANGED_FILES: ${{ steps.changed-markdown-files.outputs.all_changed_files }}
        run: |
          IFS=${SEPARATOR}
          for file in ${ALL_CHANGED_FILES}; do
            echo "$file was changed, realfile $(echo $file | tr -d '\\')"
          done

      # 构建添加 mark 指令
      - name: Setup mark
        if: steps.changed-markdown-files.outputs.any_changed == 'true'
        uses: https://texous.cn/actions/setup-mark-action@v2.0.1
        with:
          version: 11.3.0
          url_prefix: https://texous.cn/actions/mark

      # 编译以及发布到
      - name: Mark and to confluence
        if: steps.changed-markdown-files.outputs.any_changed == 'true'
        env:
          ALL_CHANGED_FILES: ${{ steps.changed-markdown-files.outputs.all_changed_files }}
          MARK_USER: ${{ secrets.ATLASSIAN_USERNAME }}
          MARK_PASS: ${{ secrets.ATLASSIAN_API_TOKEN }}
          MARK_URL: ${{ secrets.ATLASSIAN_BASE_URL }}
        run: |
          IFS=${SEPARATOR}
          for file in ${ALL_CHANGED_FILES}; do
            if [[ $(head -n 1 $(echo $file | tr -d '\\')) =~ ^$START_PREFIX ]]; then
              export temp_path=$(echo $file | tr -d '\\')
              export temp_filename=${temp_path##*/}
              export temp_filename=${temp_filename%.*}
              export temp_filename=${temp_filename%.*}
              echo "file: $temp_path, filename: $temp_filename"
              # 打印文件第一行信息
              head -n 1 $(echo $file | tr -d '\\')
              # 配置图片大小为 750
              # sed -i -E 's/(.*\!\[.*\]\(.*\))([ ]*<\!\-\-[ ]*width=[0-9]*[ ]*\-\->)*/\1<!-- width=750 -->/g' $temp_path
              # path 替换
              # sed -i -E 's/(.*\!\[.*\]\([ \t]*([a-zA-Z]\:)?(\.*(\/|\\))?([^:\r\n\t\!]*\.[a-zA-Z]+)[ \t]*\))([ ]*<\!\-\-[ \t]*width=[0-9]*[ \t]*\-\->)*/\1<!-- width-attach=750 -->/g' $temp_path
              # url 替换
              sed -i -E 's/(.*\!\[.*\]\([ \t]*(http(s)?:\/\/)([^:\r\n\t\!]*(\.[a-zA-Z]+)?)[ \t]*\))([ ]*<\!\-\-[ \t]*width=[0-9]*[ \t]*\-\->)*/\1<!-- width=750 -->/g' $temp_path
              # 不支持的代码块替换
              sed -i -E 's/(.*)(```(dockerfile|json|toml|properties))(.*)/\1```shell\4/g' $temp_path
              # 标题添加锚点
              sed -i -E 's/([#]+[ ]+)(.*)/\1\2 :anchor(\2):/g' $temp_path
              sed -i -E "s@(\[.*\])(\(#)(.*)(\))@\1\2id-$(echo $temp_filename | tr -d '-')-\3\4@g" $temp_path
              # 构建及发布
              mark --ci --debug --trace --include-path $(pwd) -p $MARK_PASS -b $MARK_URL -f $temp_path || exit 1;
            else
              echo "$temp_path not config metadata info, ignore file"
            fi
          done