Skip to content

Commit 7252c5d

Browse files
committed
Fetch latest PR body during marketplace submission
Made-with: Cursor
1 parent 2c1a597 commit 7252c5d

2 files changed

Lines changed: 48 additions & 20 deletions

File tree

.github/workflows/submit-on-merge.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ on:
77
branches:
88
- main
99

10+
permissions:
11+
contents: write
12+
pull-requests: read
13+
1014
jobs:
1115
submit-on-merge:
1216
name: Submit Changed Plugins
@@ -40,18 +44,13 @@ jobs:
4044
working-directory: packages/plugin-tools
4145
run: yarn build
4246

43-
- name: Write PR body to file
44-
run: cat <<< "$PR_BODY" > /tmp/pr-body.txt
45-
env:
46-
PR_BODY: ${{ github.event.pull_request.body }}
47-
4847
- name: Submit changed plugins
4948
run: |
5049
export CHANGED_FILES=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }})
5150
yarn tsx scripts/submit-on-merge.ts
5251
env:
5352
DEBUG: "1"
54-
PR_BODY_FILE: /tmp/pr-body.txt
53+
PR_NUMBER: ${{ github.event.pull_request.number }}
5554
SESSION_TOKEN: ${{ secrets.SESSION_TOKEN }}
5655
FRAMER_ADMIN_SECRET: ${{ secrets.FRAMER_ADMIN_SECRET }}
5756
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}

scripts/submit-on-merge.ts

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
* Usage: yarn tsx scripts/submit-on-merge.ts
1010
*
1111
* Environment Variables:
12-
* PR_BODY_FILE - Path to file containing PR body text
12+
* PR_NUMBER - Pull request number to fetch latest PR body
13+
* GITHUB_REPOSITORY - Repository name in "owner/repo" format
14+
* GITHUB_TOKEN - GitHub token with pull request read access
15+
* PR_BODY_FILE - Optional fallback path to file containing PR body text
1316
* CHANGED_FILES - Space-separated list of changed files from the workflow
1417
* REPO_ROOT - Root of the git repository (optional, defaults to parent of scripts/)
1518
*
@@ -28,6 +31,41 @@ const __dirname = dirname(fileURLToPath(import.meta.url))
2831
const REPO_ROOT = process.env.REPO_ROOT ?? resolve(__dirname, "..")
2932
const PLUGINS_DIR = join(REPO_ROOT, "plugins")
3033

34+
async function getPrBody(): Promise<string> {
35+
const prNumber = process.env.PR_NUMBER
36+
const repository = process.env.GITHUB_REPOSITORY
37+
const token = process.env.GITHUB_TOKEN
38+
39+
if (prNumber && repository && token) {
40+
const apiBaseUrl = process.env.GITHUB_API_URL ?? "https://api.github.com"
41+
const response = await fetch(`${apiBaseUrl}/repos/${repository}/pulls/${prNumber}`, {
42+
headers: {
43+
Accept: "application/vnd.github+json",
44+
Authorization: `Bearer ${token}`,
45+
"X-GitHub-Api-Version": "2022-11-28",
46+
},
47+
})
48+
49+
if (!response.ok) {
50+
throw new Error(`Failed to fetch latest PR body: ${response.status} ${response.statusText}`)
51+
}
52+
53+
const pullRequest = (await response.json()) as { body?: string | null }
54+
return pullRequest.body ?? ""
55+
}
56+
57+
const prBodyFile = process.env.PR_BODY_FILE
58+
if (prBodyFile) {
59+
if (!existsSync(prBodyFile)) {
60+
throw new Error(`PR_BODY_FILE does not exist: ${prBodyFile}`)
61+
}
62+
63+
return readFileSync(prBodyFile, "utf-8")
64+
}
65+
66+
throw new Error("Missing PR body source. Set PR_NUMBER, GITHUB_REPOSITORY, and GITHUB_TOKEN, or set PR_BODY_FILE.")
67+
}
68+
3169
function getChangedPlugins(changedFiles: string): string[] {
3270
// Parse plugin names from changed files (pure function from lib)
3371
const pluginNames = parseChangedPlugins(changedFiles)
@@ -72,32 +110,23 @@ function submitPlugin(pluginName: string, changelog: string): void {
72110
}
73111
}
74112

75-
function run(): void {
113+
async function run(): Promise<void> {
76114
console.log("=".repeat(60))
77115
console.log("Auto submit to Marketplace on merge")
78116
console.log("=".repeat(60))
79117

80118
// 1. Validate required environment variables
81119
log.step("Configuration")
82-
const prBodyFile = process.env.PR_BODY_FILE
83120
const changedFiles = process.env.CHANGED_FILES
84121

85-
if (!prBodyFile) {
86-
throw new Error("Missing required environment variable: PR_BODY_FILE")
87-
}
88-
89122
if (!changedFiles) {
90123
throw new Error("Missing required environment variable: CHANGED_FILES")
91124
}
92125

93-
if (!existsSync(prBodyFile)) {
94-
throw new Error(`PR_BODY_FILE does not exist: ${prBodyFile}`)
95-
}
96-
97-
const prBody = readFileSync(prBodyFile, "utf-8")
126+
const prBody = await getPrBody()
98127

99128
log.info(`Dry run: ${process.env.DRY_RUN === "true" ? "yes" : "no"}`)
100-
log.debug(`PR body file: ${prBodyFile}`)
129+
log.debug(`PR number: ${process.env.PR_NUMBER ?? "not set"}`)
101130
log.debug(`PR body length: ${prBody.length} chars`)
102131
log.debug(`PR body (first 500 chars):\n${prBody.slice(0, 500)}`)
103132

@@ -152,7 +181,7 @@ function run(): void {
152181
}
153182

154183
try {
155-
run()
184+
await run()
156185
} catch (error) {
157186
log.error(error instanceof Error ? error.message : String(error))
158187
process.exit(1)

0 commit comments

Comments
 (0)