|
| 1 | +name: Dependabot PR Check |
| 2 | + |
| 3 | +# ワークフローの処理の流れ: |
| 4 | +# 1. トリガー条件: |
| 5 | +# - mainブランチへのプルリクエスト時 |
| 6 | +# - Dependabotによる実行であること |
| 7 | +# - コミットメッセージが"Bump version"で始まっていないこと |
| 8 | +# 2. ジョブの条件判定: Dependabot によるPRであることをチェック |
| 9 | +# 3. OS毎の環境設定 (macos-latest, ubuntu-latest, windows-latest) |
| 10 | +# 4. Python環境のセットアップ (3.11, 3.12) |
| 11 | +# 5. タイムゾーンの設定 (Asia/Tokyo) |
| 12 | +# 6. リポジトリのチェックアウト |
| 13 | +# 7. Poetry のインストール |
| 14 | +# 8. 依存関係のキャッシュ |
| 15 | +# 9. プロジェクト依存関係のインストール |
| 16 | +# 10. テストの実行とカバレッジの計算 |
| 17 | +# 11. カバレッジが90%以上であることのチェック |
| 18 | +# 12. テスト結果とカバレッジレポートの生成 |
| 19 | +# 13. テスト結果の確認と警告の表示 |
| 20 | +# 14. ジョブサマリーの作成 |
| 21 | +# 15. 全テストの結果確認 |
| 22 | +# 16. Discord Webhookの送信 |
| 23 | + |
| 24 | +on: |
| 25 | + pull_request: |
| 26 | + branches: |
| 27 | + - main |
| 28 | + |
| 29 | +jobs: |
| 30 | + set_variables: |
| 31 | + if: github.actor == 'dependabot[bot]' && !startsWith(github.event.pull_request.title, 'Bump version') |
| 32 | + runs-on: ubuntu-latest |
| 33 | + outputs: |
| 34 | + os: ${{ steps.json2vars.outputs.os }} |
| 35 | + versions_python: ${{ steps.json2vars.outputs.versions_python }} |
| 36 | + ghpages_branch: ${{ steps.json2vars.outputs.ghpages_branch }} |
| 37 | + steps: |
| 38 | + - name: Checkout repository |
| 39 | + uses: actions/checkout@v4.2.2 |
| 40 | + with: |
| 41 | + fetch-depth: 0 |
| 42 | + |
| 43 | + - name: Set variables from JSON |
| 44 | + id: json2vars |
| 45 | + uses: 7rikazhexde/json2vars-setter@main |
| 46 | + with: |
| 47 | + json-file: .github/workflows/matrix.json |
| 48 | + |
| 49 | + - name: Debug output values |
| 50 | + run: | |
| 51 | + echo "os: ${{ steps.json2vars.outputs.os }}" |
| 52 | + echo "versions_python: ${{ steps.json2vars.outputs.versions_python }}" |
| 53 | + echo "ghpages_branch: ${{ steps.json2vars.outputs.ghpages_branch }}" |
| 54 | +
|
| 55 | + test: |
| 56 | + needs: set_variables |
| 57 | + strategy: |
| 58 | + matrix: |
| 59 | + os: ${{ fromJson(needs.set_variables.outputs.os) }} |
| 60 | + python-version: ${{ fromJson(needs.set_variables.outputs.versions_python) }} |
| 61 | + runs-on: ${{ matrix.os }} |
| 62 | + env: |
| 63 | + TZ: 'Asia/Tokyo' |
| 64 | + permissions: |
| 65 | + contents: write |
| 66 | + pull-requests: write |
| 67 | + steps: |
| 68 | + - uses: actions/checkout@v4.2.2 |
| 69 | + - name: Set up Python |
| 70 | + uses: actions/setup-python@v5.4.0 |
| 71 | + with: |
| 72 | + python-version: ${{matrix.python-version}} |
| 73 | + - name: Set timezone |
| 74 | + uses: szenius/set-timezone@v2.0 |
| 75 | + with: |
| 76 | + timezoneLinux: "Asia/Tokyo" |
| 77 | + timezoneMacos: "Asia/Tokyo" |
| 78 | + timezoneWindows: "Tokyo Standard Time" |
| 79 | + - name: Check timezone |
| 80 | + shell: bash |
| 81 | + run: | |
| 82 | + echo "System date: $(date)" |
| 83 | + echo "TZ environment variable: ${TZ}" |
| 84 | + python -c "import datetime, platform; print(f'Python timezone: {datetime.datetime.now().astimezone().tzinfo}'); print(f'OS: {platform.system()}')" |
| 85 | + - name: Install poetry |
| 86 | + run: pip install poetry |
| 87 | + - name: Cache dependencies |
| 88 | + uses: actions/cache@v4.2.0 |
| 89 | + with: |
| 90 | + path: ~/.cache/pypoetry |
| 91 | + key: ${{ runner.os }}-poetry-${{ hashFiles('**/poetry.lock') }} |
| 92 | + - name: Install dependencies |
| 93 | + shell: bash |
| 94 | + run: | |
| 95 | + rm -f poetry.lock |
| 96 | + poetry lock |
| 97 | + poetry install |
| 98 | + - name: Run test |
| 99 | + shell: bash |
| 100 | + id: pytest |
| 101 | + run: | |
| 102 | + poetry run task test_ci_xml |
| 103 | + coverage_percentage=$(poetry run coverage report | grep TOTAL | awk '{print $NF}' | sed 's/%//') |
| 104 | + echo "Current coverage: ${coverage_percentage}%" |
| 105 | + echo "COVERAGE=${coverage_percentage}" >> "$GITHUB_ENV" |
| 106 | + - name: Check coverage |
| 107 | + shell: bash |
| 108 | + run: | |
| 109 | + if [ "${COVERAGE}" -lt 90 ]; then |
| 110 | + echo "Test coverage is below 90%. Current coverage: ${COVERAGE}%" |
| 111 | + exit 1 |
| 112 | + else |
| 113 | + echo "Test coverage is above or equal to 90%. Current coverage: ${COVERAGE}%" |
| 114 | + fi |
| 115 | + - name: Pytest coverage comment |
| 116 | + id: coverageComment |
| 117 | + uses: MishaKav/pytest-coverage-comment@v1.1.53 |
| 118 | + with: |
| 119 | + pytest-coverage-path: ./pytest-coverage.txt |
| 120 | + pytest-xml-coverage-path: ./coverage.xml |
| 121 | + title: Coverage Report (${{ matrix.os }} / Python ${{ matrix.python-version }}) |
| 122 | + badge-title: coverage |
| 123 | + hide-badge: false |
| 124 | + hide-report: false |
| 125 | + create-new-comment: true |
| 126 | + hide-comment: false |
| 127 | + report-only-changed-files: false |
| 128 | + remove-link-from-badge: false |
| 129 | + junitxml-path: ./pytest.xml |
| 130 | + junitxml-title: "Pytest Result Summary (os: ${{ matrix.os }} / python-version: ${{ matrix.python-version }})" |
| 131 | + github-token: ${{ secrets.PAT_FOR_PUSHES }} |
| 132 | + - name: Check test results |
| 133 | + if: steps.pytest.outcome == 'failure' |
| 134 | + run: | |
| 135 | + echo "Tests failed. This will be reported in the workflow summary." |
| 136 | + echo "::warning::Tests failed on ${{ matrix.os }} with Python ${{ matrix.python-version }}" |
| 137 | + - name: Write job summary |
| 138 | + id: check_status |
| 139 | + shell: bash |
| 140 | + run: | |
| 141 | + echo -e ${{ steps.coverageComment.outputs.summaryReport }} >> "$GITHUB_STEP_SUMMARY" |
| 142 | +
|
| 143 | + check_all_tests: |
| 144 | + needs: test |
| 145 | + runs-on: ubuntu-latest |
| 146 | + if: github.actor == 'dependabot[bot]' && !startsWith(github.event.pull_request.title, 'Bump version') |
| 147 | + steps: |
| 148 | + - name: Check test results |
| 149 | + if: contains(needs.test.result, 'failure') |
| 150 | + run: | |
| 151 | + echo "Some tests failed. Please check the test results and fix any issues before merging." |
| 152 | + exit 1 |
| 153 | +
|
| 154 | + send_notification: |
| 155 | + needs: [test, check_all_tests] |
| 156 | + runs-on: ubuntu-latest |
| 157 | + if: github.actor == 'dependabot[bot]' && !startsWith(github.event.pull_request.title, 'Bump version') |
| 158 | + steps: |
| 159 | + - name: Send Discord Notification |
| 160 | + env: |
| 161 | + DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }} |
| 162 | + PR_TITLE: ${{ github.event.pull_request.title }} |
| 163 | + WORKFLOW_ACTOR: ${{ github.actor }} |
| 164 | + run: | |
| 165 | + workflow_url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" |
| 166 | + status="${{ contains(needs.test.result, 'failure') && 'FAILED ❌' || 'SUCCESS ✅' }}" |
| 167 | +
|
| 168 | + message="## Dependabot PR Check Completed |
| 169 | +
|
| 170 | + ### Workflow Information |
| 171 | + • **Name:** ${{ github.workflow }} |
| 172 | + • **Status:** ${status} |
| 173 | + • **Run:** [View Details]($workflow_url) |
| 174 | + • **PR Title:** ${PR_TITLE} |
| 175 | + • **Actor:** ${WORKFLOW_ACTOR}" |
| 176 | +
|
| 177 | + timestamp="$(date -u +"%Y-%m-%dT%H:%M:%SZ")" |
| 178 | + json_payload=$(jq -n \ |
| 179 | + --arg title "${{ github.workflow }} - Dependabot Check Status" \ |
| 180 | + --arg description "$message" \ |
| 181 | + --argjson color "${{ contains(needs.test.result, 'failure') && '16711680' || '65280' }}" \ |
| 182 | + --arg timestamp "$timestamp" \ |
| 183 | + '{ |
| 184 | + "embeds": [ |
| 185 | + { |
| 186 | + "title": $title, |
| 187 | + "description": $description, |
| 188 | + "color": $color, |
| 189 | + "timestamp": $timestamp |
| 190 | + } |
| 191 | + ] |
| 192 | + }') |
| 193 | +
|
| 194 | + curl -X POST -H "Content-Type: application/json" \ |
| 195 | + -d "$json_payload" \ |
| 196 | + "$DISCORD_WEBHOOK_URL" |
0 commit comments