11#!/usr/bin/env node
22/**
3- * Updator script triggered from minecraft-data repository
4- * This script can be customized to handle updates from minecraft-data
3+ * Updator script triggered from minecraft-data repository to auto generate PR
54 */
6- const github = require ( 'gh-helpers' ) ( )
75const fs = require ( 'fs' )
86const cp = require ( 'child_process' )
7+ const assert = require ( 'assert' )
8+ const github = require ( 'gh-helpers' ) ( )
99const { join } = require ( 'path' )
1010const exec = ( cmd ) => github . mock ? console . log ( '> ' , cmd ) : ( console . log ( '> ' , cmd ) , cp . execSync ( cmd , { stdio : 'inherit' } ) )
1111
1212console . log ( 'Starting update process...' )
13- const triggerBranch = process . env . TRIGGER_SOURCE
14- const newVersion = process . env . DATA_VERSION
15- const onBehalfOf = process . env . TRIGGER_REASON || 'workflow_dispatch'
16- console . log ( 'Trigger reason:' , onBehalfOf )
17- console . log ( 'New version:' , newVersion )
13+ // Sanitize and validate environment variables all non alpha numeric / underscore / dot
14+ const newVersion = process . env . NEW_MC_VERSION ?. replace ( / [ ^ a - z A - Z 0 - 9 _ . ] / g , '_' )
15+ const triggerBranch = process . env . MCDATA_BRANCH ?. replace ( / [ ^ a - z A - Z 0 - 9 _ . ] / g , '_' )
16+ const mcdataPrURL = process . env . MCDATA_PR_URL
17+ console . log ( { newVersion , triggerBranch , mcdataPrURL } )
1818
19- if ( ! newVersion ) {
20- console . error ( 'No new version provided. Exiting...' )
21- process . exit ( 1 )
22- }
19+ assert ( newVersion )
20+ assert ( triggerBranch )
2321
2422async function main ( ) {
2523 const currentSupportedPath = require . resolve ( '../../src/version.js' )
@@ -39,7 +37,7 @@ async function main () {
3937 // Update the README.md
4038 const currentContentsReadme = fs . readFileSync ( readmePath , 'utf8' )
4139 if ( ! currentContentsReadme . includes ( newVersion ) ) {
42- const newReadmeContents = currentContentsReadme . replace ( ' <!-- NEXT_VERSION -->' , `, ${ newVersion } <!-- NEXT_VERSION -->` )
40+ const newReadmeContents = currentContentsReadme . replace ( '\n <!--add_next_version_above -->' , `, ${ newVersion } \n <!--add_next_version_above -->` )
4341 fs . writeFileSync ( readmePath , newReadmeContents )
4442 console . log ( 'Updated README with new version:' , newVersion )
4543 }
@@ -49,29 +47,51 @@ async function main () {
4947 const currentContentsCI = fs . readFileSync ( ciPath , 'utf8' )
5048 if ( ! currentContentsCI . includes ( newVersion ) ) {
5149 const newCIContents = currentContentsCI . replace (
52- ' run: npm install' , `
53- run: npm install
50+ 'run: npm install' , `run: npm install
5451 - run: cd node_modules && cd minecraft-data && mv minecraft-data minecraft-data-old && git clone -b ${ triggerBranch } https://github.com/PrismarineJS/minecraft-data --depth 1 && node bin/generate_data.js
5552 - run: curl -o node_modules/protodef/src/serializer.js https://raw.githubusercontent.com/extremeheat/node-protodef/refs/heads/dlog/src/serializer.js && curl -o node_modules/protodef/src/compiler.js https://raw.githubusercontent.com/extremeheat/node-protodef/refs/heads/dlog/src/compiler.js
5653` )
5754 fs . writeFileSync ( ciPath , newCIContents )
5855 console . log ( 'Updated CI workflow with new version:' , newVersion )
5956 }
6057
61- const branchName = 'pc' + newVersion . replace ( / [ ^ a - z A - Z 0 - 9 _ ] / g, '. ' )
58+ const branchName = 'pc' + newVersion . replace ( / [ ^ a - z A - Z 0 - 9 _ ] / g, '_ ' )
6259 exec ( `git checkout -b ${ branchName } ` )
60+ exec ( 'git config user.name "github-actions[bot]"' )
61+ exec ( 'git config user.email "41898282+github-actions[bot]@users.noreply.github.com"' )
6362 exec ( 'git add --all' )
6463 exec ( `git commit -m "Update to version ${ newVersion } "` )
65- exec ( `git push origin ${ branchName } ` )
64+ exec ( `git push origin ${ branchName } --force ` )
6665 // createPullRequest(title: string, body: string, fromBranch: string, intoBranch?: string): Promise<{ number: number, url: string }>;
6766 const pr = await github . createPullRequest (
68- `${ newVersion } updates` ,
69- `Automatically generated PR for Minecraft version ${ newVersion } .\n\nRef: ${ onBehalfOf } ` ,
67+ `🎈 ${ newVersion } ` ,
68+ `This automated PR sets up the relevant boilerplate for Minecraft version ${ newVersion } .
69+
70+ Ref: ${ mcdataPrURL }
71+
72+ * You can help contribute to this PR by opening a PR against this <code branch>${ branchName } </code> branch instead of <code>master</code>.
73+ ` ,
7074 branchName ,
7175 'master'
7276 )
73- console . log ( `Pull request created: ${ pr . url } (PR #${ pr . number } )` )
74- console . log ( 'Update process completed successfully!' )
77+ console . log ( `Pull request created` , pr )
78+
79+ // Ask mineflayer to handle new update
80+ const nodeDispatchPayload = {
81+ owner : 'PrismarineJS' ,
82+ repo : 'mineflayer' ,
83+ workflow : 'handle-update.yml' ,
84+ branch : 'master' ,
85+ inputs : {
86+ new_mc_version : newVersion ,
87+ mcdata_branch : triggerBranch ,
88+ mcdata_pr_url : mcdataPrURL ,
89+ nmp_branch : branchName ,
90+ nmp_pr_url : pr . url
91+ }
92+ }
93+ console . log ( 'Sending workflow dispatch' , nodeDispatchPayload )
94+ await github . sendWorkflowDispatch ( nodeDispatchPayload )
7595}
7696
7797main ( ) . catch ( err => {
0 commit comments