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))
2831const REPO_ROOT = process . env . REPO_ROOT ?? resolve ( __dirname , ".." )
2932const 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+
3169function 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
154183try {
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