|
| 1 | +import "npm:array-unique-proposal"; |
| 2 | + |
| 3 | +import { components } from "npm:@octokit/openapi-types"; |
| 4 | +import { $, argv, YAML } from "npm:zx"; |
| 5 | + |
| 6 | +import { Reward } from "./type.ts"; |
| 7 | + |
| 8 | +$.verbose = true; |
| 9 | + |
| 10 | +const [ |
| 11 | + repositoryOwner, |
| 12 | + repositoryName, |
| 13 | + issueNumber, |
| 14 | + payer, // GitHub username of the payer (provided by workflow, defaults to issue creator) |
| 15 | + currency, |
| 16 | + reward, |
| 17 | + source, |
| 18 | +] = argv._; |
| 19 | + |
| 20 | +interface PRMeta { |
| 21 | + author: components["schemas"]["simple-user"]; |
| 22 | + assignees: components["schemas"]["simple-user"][]; |
| 23 | +} |
| 24 | + |
| 25 | +const graphqlQuery = ` |
| 26 | + query($owner: String!, $name: String!, $number: Int!) { |
| 27 | + repository(owner: $owner, name: $name) { |
| 28 | + issue(number: $number) { |
| 29 | + closedByPullRequestsReferences(first: 10) { |
| 30 | + nodes { |
| 31 | + url |
| 32 | + merged |
| 33 | + mergeCommit { |
| 34 | + oid |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | +`; |
| 42 | +const PR_DATA = await $`gh api graphql \ |
| 43 | + -f query=${graphqlQuery} \ |
| 44 | + -f owner=${repositoryOwner} \ |
| 45 | + -f name=${repositoryName} \ |
| 46 | + -F number=${issueNumber} \ |
| 47 | + --jq '.data.repository.issue.closedByPullRequestsReferences.nodes[] | select(.merged == true) | {url: .url, mergeCommitSha: .mergeCommit.oid}' | head -n 1`; |
| 48 | + |
| 49 | +const prData = PR_DATA.text().trim(); |
| 50 | + |
| 51 | +if (!prData) |
| 52 | + throw new ReferenceError("No merged PR is found for the given issue number."); |
| 53 | + |
| 54 | +const { url: PR_URL, mergeCommitSha } = JSON.parse(prData); |
| 55 | + |
| 56 | +if (!PR_URL || !mergeCommitSha) |
| 57 | + throw new Error("Missing required fields in PR data"); |
| 58 | + |
| 59 | +console.table({ PR_URL, mergeCommitSha }); |
| 60 | + |
| 61 | +const { author, assignees }: PRMeta = await ( |
| 62 | + await $`gh pr view ${PR_URL} --json author,assignees` |
| 63 | +).json(); |
| 64 | + |
| 65 | +function isBotUser(login: string) { |
| 66 | + const lowerLogin = login.toLowerCase(); |
| 67 | + return ( |
| 68 | + lowerLogin.includes("copilot") || |
| 69 | + lowerLogin.includes("[bot]") || |
| 70 | + lowerLogin === "github-actions[bot]" || |
| 71 | + lowerLogin.endsWith("[bot]") |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | +// Filter out Bot users from the list |
| 76 | +const allUsers = [ |
| 77 | + author.login, |
| 78 | + ...assignees.map(({ login }) => login), |
| 79 | +].uniqueBy(); |
| 80 | + |
| 81 | +const users = allUsers.filter((login) => !isBotUser(login)); |
| 82 | + |
| 83 | +console.log(`All users: ${allUsers.join(", ")}`); |
| 84 | +console.log(`Filtered users (excluding bots): ${users.join(", ")}`); |
| 85 | + |
| 86 | +if (!users[0]) |
| 87 | + throw new ReferenceError( |
| 88 | + "No real users found (all users are bots). Skipping reward distribution.", |
| 89 | + ); |
| 90 | + |
| 91 | +const rewardNumber = parseFloat(reward); |
| 92 | + |
| 93 | +if (isNaN(rewardNumber) || rewardNumber <= 0) |
| 94 | + throw new RangeError( |
| 95 | + `Reward amount is not a valid number, can not proceed with reward distribution. Received reward value: ${reward}`, |
| 96 | + ); |
| 97 | + |
| 98 | +const averageReward = (rewardNumber / users.length).toFixed(2); |
| 99 | + |
| 100 | +const list: Reward[] = users.map((login) => ({ |
| 101 | + issue: `#${issueNumber}`, |
| 102 | + payer: `@${payer}`, |
| 103 | + payee: `@${login}`, |
| 104 | + currency, |
| 105 | + reward: parseFloat(averageReward), |
| 106 | + source, |
| 107 | +})); |
| 108 | +const listText = YAML.stringify(list); |
| 109 | + |
| 110 | +console.log(listText); |
| 111 | + |
| 112 | +const tagName = `reward-${issueNumber}`; |
| 113 | + |
| 114 | +await $`git config user.name "github-actions[bot]"`; |
| 115 | +await $`git config user.email "github-actions[bot]@users.noreply.github.com"`; |
| 116 | + |
| 117 | +await $`git tag -a ${tagName} ${mergeCommitSha} -m ${listText}`; |
| 118 | +await $`git push origin ${tagName} --no-verify`; |
| 119 | + |
| 120 | +await $`git config unset user.name`; |
| 121 | +await $`git config unset user.email`; |
| 122 | + |
| 123 | +const commentBody = `## Reward data |
| 124 | +
|
| 125 | +\`\`\`yml |
| 126 | +${listText} |
| 127 | +\`\`\` |
| 128 | +`; |
| 129 | +await $`gh issue comment ${issueNumber} --body ${commentBody}`; |
0 commit comments