|
| 1 | +import { Octokit } from "@octokit/action" |
| 2 | +import { humanId } from "human-id" |
| 3 | +import fs from "fs" |
| 4 | +import { IDENTIFIER, PACKAGE_NAME } from "./params.mjs" |
| 5 | +import github from "@actions/github" |
| 6 | + |
| 7 | +const octokit = new Octokit({}) |
| 8 | +const prNumber = github.context.payload.pull_request.number |
| 9 | + |
| 10 | +async function createOrUpdateComment(prevComment, prNumber, body) { |
| 11 | + if (prevComment) { |
| 12 | + console.log("Updating comment") |
| 13 | + await octokit.issues.updateComment({ |
| 14 | + ...github.context.repo, |
| 15 | + comment_id: prevComment.id, |
| 16 | + body, |
| 17 | + }) |
| 18 | + } else { |
| 19 | + console.log("Creating comment", prNumber) |
| 20 | + await octokit.issues.createComment({ |
| 21 | + ...github.context.repo, |
| 22 | + issue_number: prNumber, |
| 23 | + body, |
| 24 | + }) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +console.log("Listing comments", prNumber) |
| 29 | +const { data: comments } = await octokit.issues.listComments({ |
| 30 | + ...github.context.repo, |
| 31 | + issue_number: prNumber, |
| 32 | +}) |
| 33 | +const prevComment = comments.find((comment) => |
| 34 | + comment.body.startsWith(IDENTIFIER), |
| 35 | +) |
| 36 | +console.log("prevComment", !!prevComment) |
| 37 | + |
| 38 | +console.log("Finding changeset") |
| 39 | +let changedFiles = await octokit.pulls.listFiles({ |
| 40 | + ...github.context.repo, |
| 41 | + pull_number: prNumber, |
| 42 | +}) |
| 43 | +const changeset = changedFiles.data.find( |
| 44 | + (file) => |
| 45 | + file.status === "added" && |
| 46 | + /^\.changeset\/.+\.md$/.test(file.filename) && |
| 47 | + file.filename !== ".changeset/README.md", |
| 48 | +) |
| 49 | +const hasChangesets = !!changeset |
| 50 | +console.log({ hasChangesets }) |
| 51 | + |
| 52 | +if (!hasChangesets) { |
| 53 | + console.log("Getting PR") |
| 54 | + const pr = await octokit.pulls.get({ |
| 55 | + ...github.context.repo, |
| 56 | + pull_number: prNumber, |
| 57 | + }) |
| 58 | + const filename = humanId({ |
| 59 | + separator: "-", |
| 60 | + capitalize: false, |
| 61 | + }) |
| 62 | + const value = encodeURIComponent(`--- |
| 63 | +"${PACKAGE_NAME}": patch |
| 64 | +--- |
| 65 | +
|
| 66 | +${pr.data.title} |
| 67 | +`) |
| 68 | + const repoURL = pr.data.head.repo.html_url |
| 69 | + const addChangesetURL = `${repoURL}/new/${pr.data.head.ref}?filename=.changeset/${filename}.md&value=${value}` |
| 70 | + const body = `${IDENTIFIER} |
| 71 | +No changeset detected. If you are changing ${ |
| 72 | + "`" + PACKAGE_NAME + "`" |
| 73 | + } [click here to add a changeset](${addChangesetURL}). |
| 74 | +` |
| 75 | + await createOrUpdateComment(prevComment, prNumber, body) |
| 76 | + process.exit(0) |
| 77 | +} |
| 78 | + |
| 79 | +// if has changesets |
| 80 | + |
| 81 | +console.log("Adding label") |
| 82 | +await octokit.issues.addLabels({ |
| 83 | + ...github.context.repo, |
| 84 | + issue_number: prNumber, |
| 85 | + labels: ["changeset"], |
| 86 | +}) |
| 87 | + |
| 88 | +console.log("Reading canary.json") |
| 89 | +const canary = await fs.promises.readFile("canary.json", "utf8") |
| 90 | +console.log({ canary }) |
| 91 | +const { packages } = JSON.parse(canary) |
| 92 | +const { name, url } = packages[0] |
| 93 | +const body = `${IDENTIFIER} |
| 94 | +Try ${"`" + name + "`"} from this pull request in your project with: |
| 95 | +
|
| 96 | +${"```"} |
| 97 | +npm i https://pkg.pr.new/${name}@${prNumber} |
| 98 | +${"```"} |
| 99 | +` |
| 100 | +await createOrUpdateComment(prevComment, prNumber, body) |
0 commit comments