diff --git a/.github/actions/check-editorconfig/action.yaml b/.github/actions/check-editorconfig/action.yaml new file mode 100644 index 00000000..907b9de1 --- /dev/null +++ b/.github/actions/check-editorconfig/action.yaml @@ -0,0 +1,80 @@ +name: "(Reusable) Check .editorconfig" + +inputs: + ref: + description: "Specify a branch or commit ID" + required: true + type: string + max-retries: + description: "Maximum number of retries if API call fails" + required: false + type: string + default: "3" + retry-delay: + description: "Delay in seconds between retries" + required: false + type: string + default: "5" + fail-on-missing: + description: "Fail the workflow if .editorconfig does not exist" + required: false + type: boolean + default: true +outputs: + exists: + description: "Indicates whether the .editorconfig file exists" + value: ${{ steps.check-editorconfig.outputs.exists }} + +runs: + using: "composite" + steps: + - name: Check for .editorconfig + id: check-editorconfig + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + file_path=".editorconfig" + ref="${{ inputs.ref }}" + max_retries="${{ inputs.max-retries }}" + retry_delay_sec="${{ inputs.retry-delay }}" + fail_on_missing="${{ inputs.fail-on-missing }}" + + echo "Checking .editorconfig in ref: $ref" + + success=false + + for ((i=1; i<=max_retries; i++)); do + response=$(gh api "repos/${{ github.repository }}/contents/$file_path?ref=$ref" \ + --header "Authorization: Bearer $GH_TOKEN" \ + --timeout 10s \ + --jq ". | select(.type==\"file\") | select(.name==\"$file_path\")") + + api_exit_code=$? + + if [ $api_exit_code -eq 124 ]; then + echo "::warning::Attempt $i: API request timed out. Retrying in $retry_delay_sec seconds..." + sleep $retry_delay_sec + continue + elif [ -z "$response" ]; then + if [ $i -eq $max_retries ]; then + if [ "$fail_on_missing" = "true" ]; then + echo "::error::Failed to find .editorconfig after $max_retries retries in ref '$ref'." + else + echo "::warning::Failed to find .editorconfig after $max_retries retries in ref '$ref'. Not failing workflow." + fi + else + echo "::warning::Attempt $i: .editorconfig file not found. Retrying in $retry_delay_sec seconds..." + sleep $retry_delay_sec + fi + else + echo "::notice::Success: .editorconfig file exists in ref '$ref'." + success=true + break + fi + done + + echo "exists=$success" | tee -a "$GITHUB_OUTPUT" + + if [ "$success" = "false" ] && [ "$fail_on_missing" = "true" ]; then + exit 1 + fi \ No newline at end of file diff --git a/.github/workflows/reusable-unity-cs-linter-pull-request.yaml b/.github/workflows/reusable-unity-cs-linter-pull-request.yaml index 9cb3f55c..adbd9864 100644 --- a/.github/workflows/reusable-unity-cs-linter-pull-request.yaml +++ b/.github/workflows/reusable-unity-cs-linter-pull-request.yaml @@ -80,31 +80,12 @@ jobs: echo "Head Branch: $head_branch" echo "Editorconfig Branch: $editorconfig_branch" - - name: Check for .editorconfig via API - env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - branch="${{ steps.branch-conf.outputs.editorconfig-branch }}" - file_path=".editorconfig" - max_retries=3 - retry_delay_sec=5 - - for ((i=1; i<=max_retries; i++)); do - response=$(timeout 10s gh api "repos/${{ github.repository }}/contents/$file_path?ref=$branch" --jq ". | select(.type==\"file\") | select(.name==\"$file_path\")") - - if [ $? -eq 124 ]; then - echo "Attempt $i: Timeout occurred while requesting GitHub API. Retrying in $retry_delay_sec seconds..." - sleep $retry_delay_sec - elif [ -z "$response" ]; then - if [ $i -eq $max_retries ]; then - echo "::error title=.editorconfig Missing::The repository is missing an .editorconfig file in the base branch (${{ steps.branch-conf.outputs.base-branch }}); see ${{ env.EDITORCONFIG_SAMPLE_URI }} for reference." - exit 1 - fi - else - echo ".editorconfig file exists at the root of the repository." - break - fi - done + - uses: IShix-g/Unity-GitHubActions/.github/actions/check-editorconfig@main + with: + ref: ${{ steps.branch-conf.outputs.editorconfig-branch }} + max-retries: '3' + retry-delay: '5' + fail-on-missing: true - name: Checkout repository uses: actions/checkout@v4