Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 下書き記事が存在しない際にエラーになる現象を修正 #24

Merged
merged 1 commit into from Oct 16, 2023

Conversation

spider-man-tm
Copy link
Contributor

@spider-man-tm spider-man-tm commented Oct 14, 2023

Closes #23

.github/workflows/initialize.yamlについて、対象ファイルがない場合に、xargs コマンドが受け取る引数がなく、エラーが発生

(修正前)

      - name: delete draft files
        if: inputs.is_draft_included == false
        run: |
          target=$(git ls-files -mo --exclude-standard | xargs grep -xl 'Draft: true')
          IFS=" " read -r -a draft_files <<< "$(echo "$target" | xargs)"
          for file in "${draft_files[@]}"; do
            rm "$file"
          done

修正後は以下の通り、下書き記事があった場合にのみ、ファイル削除を行なっています。

(修正後)

          target=$(git ls-files -mo --exclude-standard | xargs grep -xl 'Draft: true' || echo "")
          if [[ ! -z "$target" ]]; then
            IFS=" " read -r -a draft_files <<< "$(echo "$target" | xargs)"
            for file in "${draft_files[@]}"; do
              rm "$file"
            done
          fi

.github/actions/move-draft-and-update-metadata/action.yamlについて、こちらも対象ファイルがない場合に、draft_filesが空になることでエラーが発生していました。

(修正前)

    - name: move draft and update metadata
      run: |
        draft_files=($(grep -xl 'Draft: true' $(git ls-files -mo --exclude-standard)))
        for file in ${draft_files[@]}; do
          entry_id=$(yq --front-matter=extract '.EditURL' "$file" | grep -oP '[^/]+\d$')
          yq --front-matter=process -i 'del(.Date,.URL)' "$file" 
          mv "$file" "draft_entries/$entry_id.${file##*.}"
        done
      shell: bash

修正後は以下のように、空だった場合に処理をスキップするようにしています。

(修正後)

        draft_files=($(grep -xl 'Draft: true' $(git ls-files -mo --exclude-standard) || echo ""))
        if [[ ${#draft_files[@]} -eq 0 ]]; then
          exit 0
        fi
        for file in ${draft_files[@]}; do
          entry_id=$(yq --front-matter=extract '.EditURL' "$file" | grep -oP '[^/]+\d$')
          yq --front-matter=process -i 'del(.Date,.URL)' "$file"
          mv "$file" "draft_entries/$entry_id.${file##*.}"
        done

Copy link
Member

@halkt halkt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご提案ありがとうございます!

おっしゃる通り下書き記事がない場合に動作しないようになっていたので変更をMergeさせていただきます.

@halkt halkt merged commit 3f5a698 into hatena:main Oct 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants