|
| 1 | +import { components } from "npm:@octokit/openapi-types"; |
| 2 | +import { $, YAML, argv } from "npm:zx"; |
| 3 | + |
| 4 | +import { Reward } from "./type.ts"; |
| 5 | + |
| 6 | +$.verbose = true; |
| 7 | + |
| 8 | +const [ |
| 9 | + repositoryOwner, |
| 10 | + repositoryName, |
| 11 | + issueNumber, |
| 12 | + payer, // GitHub username of the payer (provided by workflow, defaults to issue creator) |
| 13 | + currency, |
| 14 | + reward, |
| 15 | +] = argv._; |
| 16 | + |
| 17 | +interface PRMeta { |
| 18 | + author: components["schemas"]["simple-user"]; |
| 19 | + assignees: components["schemas"]["simple-user"][]; |
| 20 | +} |
| 21 | + |
| 22 | +const PR_URL = await $`gh api graphql -f query='{ |
| 23 | + repository(owner: "${repositoryOwner}", name: "${repositoryName}") { |
| 24 | + issue(number: ${issueNumber}) { |
| 25 | + closedByPullRequestsReferences(first: 10) { |
| 26 | + nodes { |
| 27 | + url |
| 28 | + merged |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | +}' --jq '.data.repository.issue.closedByPullRequestsReferences.nodes[] | select(.merged == true) | .url' | head -n 1`; |
| 34 | + |
| 35 | +if (!PR_URL.text().trim()) |
| 36 | + throw new ReferenceError("No merged PR is found for the given issue number."); |
| 37 | + |
| 38 | +const { author, assignees }: PRMeta = await ( |
| 39 | + await $`gh pr view ${PR_URL} --json author,assignees` |
| 40 | +).json(); |
| 41 | + |
| 42 | +// Function to check if a user is a Copilot/bot user |
| 43 | +function isCopilotUser(login: string): boolean { |
| 44 | + const lowerLogin = login.toLowerCase(); |
| 45 | + return lowerLogin.includes('copilot') || |
| 46 | + lowerLogin.includes('[bot]') || |
| 47 | + lowerLogin === 'github-actions[bot]' || |
| 48 | + lowerLogin.endsWith('[bot]'); |
| 49 | +} |
| 50 | + |
| 51 | +// Filter out Copilot and bot users from the list |
| 52 | +const allUsers = [author.login, ...assignees.map(({ login }) => login)]; |
| 53 | +const users = allUsers.filter(login => !isCopilotUser(login)); |
| 54 | + |
| 55 | +console.log(`All users: ${allUsers.join(', ')}`); |
| 56 | +console.log(`Filtered users (excluding bots/copilot): ${users.join(', ')}`); |
| 57 | + |
| 58 | +// Handle case where all users are bots/copilot |
| 59 | +if (users.length === 0) { |
| 60 | + console.log("No real users found (all users are bots/copilot). Skipping reward distribution."); |
| 61 | + console.log(`Filtered users: ${allUsers.join(', ')}`); |
| 62 | + process.exit(0); |
| 63 | +} |
| 64 | + |
| 65 | +const rewardNumber = parseFloat(reward); |
| 66 | + |
| 67 | +if (isNaN(rewardNumber) || rewardNumber <= 0) |
| 68 | + throw new RangeError(`Reward amount is not a valid number, can not proceed with reward distribution. Received reward value: ${reward}`); |
| 69 | + |
| 70 | +const averageReward = (rewardNumber / users.length).toFixed(2); |
| 71 | + |
| 72 | +const list: Reward[] = users.map((login) => ({ |
| 73 | + issue: `#${issueNumber}`, |
| 74 | + payer: `@${payer}`, |
| 75 | + payee: `@${login}`, |
| 76 | + currency, |
| 77 | + reward: parseFloat(averageReward), |
| 78 | +})); |
| 79 | +const listText = YAML.stringify(list); |
| 80 | + |
| 81 | +console.log(listText); |
| 82 | + |
| 83 | +await $`git config --global user.name "github-actions[bot]"`; |
| 84 | +await $`git config --global user.email "github-actions[bot]@users.noreply.github.com"`; |
| 85 | +await $`git tag -a "reward-${issueNumber}" -m ${listText}`; |
| 86 | +await $`git push origin --tags`; |
| 87 | + |
| 88 | +const commentBody = `## Reward data |
| 89 | +
|
| 90 | +\`\`\`yml |
| 91 | +${listText} |
| 92 | +\`\`\` |
| 93 | +`; |
| 94 | +await $`gh issue comment ${issueNumber} --body ${commentBody}`; |
0 commit comments