|
| 1 | +name: Assign a reviewer for documentation |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [opened] |
| 6 | + |
| 7 | +jobs: |
| 8 | + assign-docs-reviewer: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + if: contains(github.event.pull_request.body, '* Documentation') && !contains(github.event.pull_request.body, '* New feature') |
| 11 | + steps: |
| 12 | + - name: Assign a random documentation reviewer, excluding the PR author and busy members |
| 13 | + uses: actions/github-script@v7 |
| 14 | + with: |
| 15 | + github-token: ${{ secrets.YDBOT_TOKEN }} |
| 16 | + script: | |
| 17 | + const teamSlug = "primary-docs-reviewers"; |
| 18 | + const org = "ydb-platform"; |
| 19 | + |
| 20 | + // Get team members |
| 21 | + const teamMembers = await github.paginate("GET /orgs/{org}/teams/{team_slug}/members", { |
| 22 | + org, |
| 23 | + team_slug: teamSlug |
| 24 | + }); |
| 25 | + if (teamMembers.length === 0) { |
| 26 | + core.setFailed("No team members found in the team."); |
| 27 | + } |
| 28 | + |
| 29 | + // Get the PR author |
| 30 | + const prAuthor = context.payload.pull_request.user.login; |
| 31 | + |
| 32 | + // Function to check if a user is busy: |
| 33 | + // It checks for "busy" in their status message OR if they have limitedAvailability set (checkbox). |
| 34 | + async function isBusy(login) { |
| 35 | + const query = ` |
| 36 | + query($login: String!) { |
| 37 | + user(login: $login) { |
| 38 | + status { |
| 39 | + message |
| 40 | + indicatesLimitedAvailability |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + `; |
| 45 | + try { |
| 46 | + const result = await github.graphql(query, { login }); |
| 47 | + const status = result.user.status; |
| 48 | + if (!status) return false; |
| 49 | + // Consider user busy if limitedAvailability is true or message contains "busy" |
| 50 | + return status.indicatesLimitedAvailability === true || |
| 51 | + (status.message && status.message.toLowerCase().includes("busy")); |
| 52 | + } catch (error) { |
| 53 | + console.log(`Error checking status for ${login}: ${error}`); |
| 54 | + return false; // On error, assume not busy |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Filter out the PR author and busy members |
| 59 | + const availableMembers = []; |
| 60 | + for (const member of teamMembers) { |
| 61 | + if (member.login === prAuthor) { |
| 62 | + console.log(`Skipping the author ${member.login}.`); |
| 63 | + continue; |
| 64 | + } |
| 65 | + if (await isBusy(member.login)) { |
| 66 | + console.log(`Skipping ${member.login} as they appear busy.`); |
| 67 | + continue; |
| 68 | + } |
| 69 | + availableMembers.push(member); |
| 70 | + } |
| 71 | + |
| 72 | + if (availableMembers.length === 0) { |
| 73 | + core.setFailed("No available team members after excluding PR author and busy members."); |
| 74 | + } |
| 75 | + |
| 76 | + // Pick a random available member |
| 77 | + const randomIndex = Math.floor(Math.random() * availableMembers.length); |
| 78 | + const randomMember = availableMembers[randomIndex].login; |
| 79 | + |
| 80 | + // Get the PR number from the event context |
| 81 | + const prNumber = context.payload.pull_request.number; |
| 82 | + // Assign the PR to the selected team member |
| 83 | + await github.rest.issues.addAssignees({ |
| 84 | + owner: context.repo.owner, |
| 85 | + repo: context.repo.repo, |
| 86 | + issue_number: prNumber, |
| 87 | + assignees: [randomMember] |
| 88 | + }); |
| 89 | + console.log(`Assigned PR #${prNumber} to ${randomMember}`); |
0 commit comments