|
| 1 | +name: Generate release draft and label PRs |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | + # pull_request event is required only for autolabeler |
| 11 | + # pull_request: |
| 12 | + # Only following types are handled by the action, but one can default to all as well |
| 13 | + # types: [opened, reopened, synchronize] |
| 14 | + # pull_request_target event is required for autolabeler to support PRs from forks |
| 15 | + pull_request_target: |
| 16 | + types: [opened, reopened, synchronize] |
| 17 | + |
| 18 | +permissions: |
| 19 | + contents: read |
| 20 | + |
| 21 | +jobs: |
| 22 | + update_release_draft: |
| 23 | + permissions: |
| 24 | + # write permission is required to create a github release |
| 25 | + contents: write |
| 26 | + # write permission is required for autolabeler |
| 27 | + # otherwise, read permission is required at least |
| 28 | + pull-requests: write |
| 29 | + runs-on: ubuntu-latest |
| 30 | + steps: |
| 31 | + - name: Checkout code |
| 32 | + uses: actions/checkout@v4 |
| 33 | + |
| 34 | + # Setup Node.js environment |
| 35 | + - uses: actions/setup-node@v4 |
| 36 | + with: |
| 37 | + node-version: "20.x" |
| 38 | + - run: npm install js-yaml |
| 39 | + |
| 40 | + # ラベルが存在しなければ作成 |
| 41 | + - name: Ensure autolabeler labels exist |
| 42 | + uses: actions/github-script@v7 |
| 43 | + with: |
| 44 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 45 | + script: | |
| 46 | + const fs = require('fs'); |
| 47 | + const yaml = require('js-yaml'); |
| 48 | +
|
| 49 | + const configPath = '.github/release-drafter.yml'; |
| 50 | + const content = fs.readFileSync(configPath, 'utf8'); |
| 51 | + const config = yaml.load(content); |
| 52 | +
|
| 53 | + const DEFAULT_COLOR = 'ededed'; // 色は最後で指定 |
| 54 | +
|
| 55 | + // 既存ラベル一覧を取得 |
| 56 | + const existingLabelsResponse = await github.rest.issues.listLabelsForRepo({ |
| 57 | + owner: context.repo.owner, |
| 58 | + repo: context.repo.repo, |
| 59 | + per_page: 100, |
| 60 | + }); |
| 61 | + const existingLabels = new Set(existingLabelsResponse.data.map(label => label.name)); |
| 62 | +
|
| 63 | + // autolabeler + exclude-labels のラベルをセットで保持(重複防止) |
| 64 | + const labelsToEnsure = new Set(); |
| 65 | +
|
| 66 | + // autolabelerのラベル追加 |
| 67 | + if (Array.isArray(config.autolabeler)) { |
| 68 | + for (const rule of config.autolabeler) { |
| 69 | + if (rule.label) { |
| 70 | + labelsToEnsure.add(rule.label); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +
|
| 75 | + // exclude-labelsのラベル追加 |
| 76 | + if (Array.isArray(config['exclude-labels'])) { |
| 77 | + for (const label of config['exclude-labels']) { |
| 78 | + labelsToEnsure.add(label); |
| 79 | + } |
| 80 | + } |
| 81 | +
|
| 82 | + // ラベル作成処理 |
| 83 | + for (const label of labelsToEnsure) { |
| 84 | + if (!existingLabels.has(label)) { |
| 85 | + console.log(`Creating label: ${label} with color ${DEFAULT_COLOR}`); |
| 86 | + await github.rest.issues.createLabel({ |
| 87 | + owner: context.repo.owner, |
| 88 | + repo: context.repo.repo, |
| 89 | + name: label, |
| 90 | + color: DEFAULT_COLOR, |
| 91 | + }); |
| 92 | + } else { |
| 93 | + console.log(`Label already exists: ${label}`); |
| 94 | + } |
| 95 | + } |
| 96 | +
|
| 97 | + # Drafts your next Release notes as Pull Requests are merged into "master" |
| 98 | + - uses: release-drafter/release-drafter@v6 |
| 99 | + # (Optional) specify config name to use, relative to .github/. Default: release-drafter.yml |
| 100 | + # with: |
| 101 | + # config-name: my-config.yml |
| 102 | + # disable-autolabeler: true |
| 103 | + env: |
| 104 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments