diff --git a/.github/workflows/label-on-approval.yml b/.github/workflows/label-on-approval.yml index 72cbd30a..c7e57faa 100644 --- a/.github/workflows/label-on-approval.yml +++ b/.github/workflows/label-on-approval.yml @@ -1,39 +1,46 @@ -name: Label on Approval +name: Add Label on Approval on: pull_request_review: - types: [ submitted ] + types: [ submitted,edited ] jobs: - add-label-on-approval: + add-label: runs-on: ubuntu-latest steps: - - name: Checkout repository - uses: actions/checkout@v2 + - name: Checkout code + uses: actions/checkout@v4 - - name: Check for approval and add label - uses: actions/github-script@v6 - env: - GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }} + - name: Check if approval is from Collaborators + id: check_approval + uses: actions/github-script@v7 with: + github-token: ${{ secrets.PAT_TOKEN }} script: | - const approved = context.payload.review.state === 'approved'; - const owner = context.repo.owner; - const repo = context.repo.repo; - const pull_number = context.payload.pull_request.number; - - if (approved) { - const { data: collaborators } = await github.repos.listCollaborators({ owner, repo }); - const reviewer = context.payload.review.user.login; - const isOwner = collaborators.some(collab => collab.login === reviewer && collab.permissions.admin); - - if (isOwner) { - await github.issues.addLabels({ - owner, - repo, - issue_number: pull_number, - labels: ['approved','auto merge'] - }); - } + const { owner, repo } = context.repo; + const { pull_request, review } = context.payload; + + // Define your list of collaborators + const collaborators = ['mxsm', 'TeslaRustor','SpaceXCN']; // Replace with actual GitHub usernames + console.log('review:', review.state); + console.log('review:', review.state === 'approved'); + console.log('Collaborators:', collaborators); + console.log('Collaborators:', collaborators.includes(review.user.login)); + // Check if the review is approved and from a collaborator + const isApprovedByCollaborator = review.state === 'approved' && collaborators.includes(review.user.login); + console.log('isApprovedByCollaborator:', isApprovedByCollaborator); + console.log('pull_request:', pull_request.number); + // Return the result to the workflow + if (isApprovedByCollaborator) { + await github.rest.issues.addLabels({ + owner, + repo, + issue_number: pull_request.number, + labels: ['approved','auto merge'] + }); } + + + +