1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

GitHub Actionsで修正のあったファイルを取得しformatterを実行する

Last updated at Posted at 2020-12-06

歴史あるリポジトリとかで数千ファイルが既にあったりするケースのリポジトリに対して全体的にformatterをかけたりはしたくないけど直近で修正してるファイルはformatterをかけたいみたいなケースで使える小技。

GitHub scriptを自分たちで書いても良さそうだが既に良さげなActionが公開されているので使ってみる。

jitterbit/get-changed-files

使ってみる

以下の例では修正されたファイルをjsonに入れている。ドキュメントを読んでるとスペース区切りとcsvでも取得できる模様。

      - name: get-changed-files
        id: files
        uses: jitterbit/get-changed-files@v1
        with:
          format: 'json'

あとはjqなりなんだりでシェルスクリプトを書いて修正ファイル分ループさせつつformatterを実行すれば用件は達成できる。

      - name: Run Formatter
        run: |
          pip install --upgrade black
          readarray -t files <<<"$(jq -r '.[]' <<<'${{ steps.files.outputs.added_modified }}')"
          for file in ${files[@]}; do
            if [[ ${file##*.} == "py" ]]; then
              black ${file}
            fi
          done

まとめ

今回はformatterを使った例を書いてみましたが修正ファイルが取れるのでそこから任意のラベルをPRなりに貼ったり他CI用のエンドポイントに渡したりと結構応用的に使えそうな小技の紹介でした。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?