π μ΄λλ―Ό μΈμ¦ ν ν° νλ¦ λΆλ¦¬ #364
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Auto Label PR | |
| on: | |
| pull_request: | |
| types: [opened, synchronize, reopened] | |
| jobs: | |
| label: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| issues: write | |
| pull-requests: write | |
| steps: | |
| - name: Sync app labels | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const appLabels = [ | |
| { | |
| name: 'web', | |
| color: '1d76db', | |
| description: 'Changes in apps/web', | |
| pattern: /^apps\/web\//, | |
| }, | |
| { | |
| name: 'admin', | |
| color: 'd73a4a', | |
| description: 'Changes in apps/admin', | |
| pattern: /^apps\/admin\//, | |
| }, | |
| { | |
| name: 'university', | |
| color: '0e8a16', | |
| description: 'Changes in apps/university-web', | |
| pattern: /^apps\/university-web\//, | |
| }, | |
| ]; | |
| const { owner, repo } = context.repo; | |
| const issue_number = context.issue.number; | |
| const pull_number = context.payload.pull_request.number; | |
| const changedFiles = await github.paginate(github.rest.pulls.listFiles, { | |
| owner, | |
| repo, | |
| pull_number, | |
| per_page: 100, | |
| }); | |
| const filenames = changedFiles.map((file) => file.filename); | |
| console.log('Changed files:'); | |
| console.log(filenames.join('\n')); | |
| const labelsToAdd = appLabels | |
| .filter((label) => filenames.some((filename) => label.pattern.test(filename))) | |
| .map((label) => label.name); | |
| const managedLabelNames = new Set(appLabels.map((label) => label.name)); | |
| const currentLabels = await github.paginate(github.rest.issues.listLabelsOnIssue, { | |
| owner, | |
| repo, | |
| issue_number, | |
| per_page: 100, | |
| }); | |
| for (const label of currentLabels) { | |
| if (!managedLabelNames.has(label.name) || labelsToAdd.includes(label.name)) { | |
| continue; | |
| } | |
| console.log(`Removing stale label: ${label.name}`); | |
| await github.rest.issues.removeLabel({ | |
| owner, | |
| repo, | |
| issue_number, | |
| name: label.name, | |
| }); | |
| } | |
| for (const labelName of labelsToAdd) { | |
| const label = appLabels.find((item) => item.name === labelName); | |
| try { | |
| await github.rest.issues.getLabel({ owner, repo, name: label.name }); | |
| } catch (error) { | |
| if (error.status !== 404) { | |
| throw error; | |
| } | |
| console.log(`Creating missing label: ${label.name}`); | |
| await github.rest.issues.createLabel({ | |
| owner, | |
| repo, | |
| name: label.name, | |
| color: label.color, | |
| description: label.description, | |
| }); | |
| } | |
| } | |
| if (labelsToAdd.length === 0) { | |
| console.log('No app-specific changes detected'); | |
| return; | |
| } | |
| console.log(`Adding labels: ${labelsToAdd.join(', ')}`); | |
| await github.rest.issues.addLabels({ | |
| owner, | |
| repo, | |
| issue_number, | |
| labels: labelsToAdd, | |
| }); |