|
| 1 | +name: Changes requested y Aprobado label |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_review: |
| 5 | + types: [submitted, edited, dismissed] |
| 6 | + |
| 7 | +jobs: |
| 8 | + changes-requested: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + steps: |
| 11 | + - uses: actions/github-script@v7 |
| 12 | + with: |
| 13 | + script: | |
| 14 | + const { owner, repo } = context.repo; |
| 15 | + const pr = context.payload.pull_request; |
| 16 | + const issue_number = pr.number; |
| 17 | +
|
| 18 | + const reviews = await github.rest.pulls.listReviews({ |
| 19 | + owner, |
| 20 | + repo, |
| 21 | + pull_number: issue_number |
| 22 | + }); |
| 23 | +
|
| 24 | + // última review por usuario |
| 25 | + const latestByUser = {}; |
| 26 | + for (const r of reviews.data) { |
| 27 | + latestByUser[r.user.login] = r.state; |
| 28 | + } |
| 29 | +
|
| 30 | + const hasChangesRequested = Object.values(latestByUser) |
| 31 | + .includes('CHANGES_REQUESTED'); |
| 32 | + |
| 33 | + const approvals = new Set( |
| 34 | + reviews.data |
| 35 | + .filter(r => r.state === 'APPROVED') |
| 36 | + .map(r => r.user.login) |
| 37 | + ); |
| 38 | +
|
| 39 | + const labels = pr.labels.map(l => l.name); |
| 40 | +
|
| 41 | + if (hasChangesRequested) { |
| 42 | + await github.rest.issues.addLabels({ |
| 43 | + owner, |
| 44 | + repo, |
| 45 | + issue_number, |
| 46 | + labels: ['Cambio Requerido'] |
| 47 | + }); |
| 48 | + try { |
| 49 | + await github.rest.issues.removeLabel({ |
| 50 | + owner, |
| 51 | + repo, |
| 52 | + issue_number, |
| 53 | + name: 'Aprobado' |
| 54 | + }); |
| 55 | + } catch (e) { |
| 56 | + // label no existía |
| 57 | + } |
| 58 | + } else { |
| 59 | + try { |
| 60 | + await github.rest.issues.removeLabel({ |
| 61 | + owner, |
| 62 | + repo, |
| 63 | + issue_number, |
| 64 | + name: 'Cambio Requerido' |
| 65 | + }); |
| 66 | + } catch (e) { |
| 67 | + // label no existía |
| 68 | + } |
| 69 | + if (approvals.size >= 2 && !labels.includes('En desarrollo')) { |
| 70 | + await github.rest.issues.addLabels({ |
| 71 | + owner, |
| 72 | + repo, |
| 73 | + issue_number, |
| 74 | + labels: ['Aprobado'] |
| 75 | + }); |
| 76 | + } else { |
| 77 | + try { |
| 78 | + await github.rest.issues.removeLabel({ |
| 79 | + owner, |
| 80 | + repo, |
| 81 | + issue_number, |
| 82 | + name: 'Aprobado' |
| 83 | + }); |
| 84 | + } catch (e) { |
| 85 | + // label no existía |
| 86 | + } |
| 87 | + } |
| 88 | + } |
0 commit comments