Skip to content

Commit 9c33536

Browse files
authored
Add update workflow (#3727)
* add handle-update workflow * direct expansion * typo * update node * fix * fix * fix
1 parent ec8220d commit 9c33536

4 files changed

Lines changed: 145 additions & 1 deletion

File tree

.github/helper/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"gh-helpers": "^1.0.0"
4+
}
5+
}

.github/helper/updator.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env node
2+
/**
3+
* Updator script triggered from minecraft-data repository to auto generate PR
4+
*/
5+
const fs = require('fs')
6+
const cp = require('child_process')
7+
const assert = require('assert')
8+
const github = require('gh-helpers')()
9+
const { join } = require('path')
10+
const exec = (cmd) => github.mock ? console.log('> ', cmd) : (console.log('> ', cmd), cp.execSync(cmd, { stdio: 'inherit' }))
11+
12+
console.log('Starting update process...')
13+
// Sanitize and validate environment variables all non alpha numeric / underscore / dot
14+
const newVersion = process.env.NEW_MC_VERSION?.replace(/[^a-zA-Z0-9_.]/g, '_')
15+
const triggerBranch = process.env.MCDATA_BRANCH?.replace(/[^a-zA-Z0-9_.]/g, '_')
16+
const mcdataPrURL = process.env.MCDATA_PR_URL
17+
console.log({ newVersion, triggerBranch, mcdataPrURL })
18+
19+
assert(newVersion)
20+
assert(triggerBranch)
21+
22+
async function main () {
23+
const currentSupportedPath = require.resolve('../../lib/version.js')
24+
const readmePath = join(__dirname, '../../docs/README.md')
25+
const ciPath = join(__dirname, '../../.github/workflows/ci.yml')
26+
27+
// Update the version.js
28+
const currentSupportedVersion = require('../../lib/version.js')
29+
const currentContents = fs.readFileSync(currentSupportedPath, 'utf8')
30+
console.log('Current supported version:', currentContents)
31+
const latestV = currentSupportedVersion.testedVersions.at(-1)
32+
const newContents = currentContents.includes(newVersion)
33+
? currentContents
34+
: currentContents
35+
.replace(`, '${latestV}'`, `, '${latestV}', '${newVersion}'`)
36+
37+
// Update the README.md
38+
const currentContentsReadme = fs.readFileSync(readmePath, 'utf8')
39+
if (!currentContentsReadme.includes(newVersion)) {
40+
const newReadmeContents = currentContentsReadme
41+
.replace(/Minecraft 1\.8 to [0-9A-Za-z._-]+ \(/, `Minecraft 1.8 to ${newVersion} (`)
42+
.replace(') <!--version-->', `, ${newVersion}) <!--version-->`)
43+
fs.writeFileSync(readmePath, newReadmeContents)
44+
console.log('Updated README with new version:', newVersion)
45+
}
46+
fs.writeFileSync(currentSupportedPath, newContents)
47+
48+
// Update the CI workflow
49+
const currentContentsCI = fs.readFileSync(ciPath, 'utf8')
50+
if (!currentContentsCI.includes(newVersion)) {
51+
const newCIContents = currentContentsCI.replace(
52+
'run: npm install', `run: npm install
53+
- 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
54+
- 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
55+
`)
56+
fs.writeFileSync(ciPath, newCIContents)
57+
console.log('Updated CI workflow with new version:', newVersion)
58+
}
59+
60+
const branchName = 'pc' + newVersion.replace(/[^a-zA-Z0-9_]/g, '_')
61+
exec(`git checkout -b ${branchName}`)
62+
exec('git config user.name "github-actions[bot]"')
63+
exec('git config user.email "41898282+github-actions[bot]@users.noreply.github.com"')
64+
exec('git add --all')
65+
exec(`git commit -m "Update to version ${newVersion}"`)
66+
exec(`git push origin ${branchName} --force`)
67+
// createPullRequest(title: string, body: string, fromBranch: string, intoBranch?: string): Promise<{ number: number, url: string }>;
68+
const pr = await github.createPullRequest(
69+
`🎈 ${newVersion}`,
70+
`This automated PR sets up the relevant boilerplate for Minecraft version ${newVersion}.
71+
72+
Ref: ${mcdataPrURL}
73+
74+
* You can help contribute to this PR by opening a PR against this <code branch>${branchName}</code> branch instead of <code>master</code>.
75+
`,
76+
branchName,
77+
'master'
78+
)
79+
console.log(`Pull request created`, pr)
80+
}
81+
82+
main().catch(err => {
83+
console.error('Error during update process:', err)
84+
process.exit(1)
85+
})
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: Update from minecraft-data
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
new_mc_version:
7+
description: New minecraft version number
8+
required: true
9+
type: string
10+
mcdata_branch:
11+
description: minecraft-data branch for this version
12+
required: true
13+
type: string
14+
mcdata_pr_url:
15+
description: minecraft-data PR number to open a PR here against
16+
required: false
17+
default: ''
18+
type: string
19+
nmp_branch:
20+
description: minecraft-protocol branch for this version
21+
required: true
22+
type: string
23+
nmp_pr_url:
24+
description: minecraft-protocol PR number to open a PR here against
25+
required: false
26+
default: ''
27+
type: string
28+
29+
jobs:
30+
update:
31+
runs-on: ubuntu-latest
32+
33+
steps:
34+
- name: Checkout repository
35+
uses: actions/checkout@v4
36+
with:
37+
token: ${{ secrets.PAT_PASSWORD }}
38+
39+
- name: Use Node.js 22.x
40+
uses: actions/setup-node@v1.4.4
41+
with:
42+
node-version: 22.x
43+
44+
- run: npm install PrismarineJS/node-minecraft-protocol#${{ github.event.inputs.nmp_branch }}
45+
46+
- name: Run updator script
47+
run: cd .github/helper && npm install && node updator.js
48+
env:
49+
GITHUB_TOKEN: ${{ secrets.PAT_PASSWORD }}
50+
NEW_MC_VERSION: ${{ github.event.inputs.new_mc_version }}
51+
MCDATA_BRANCH: ${{ github.event.inputs.mcdata_branch }}
52+
MCDATA_PR_URL: ${{ github.event.inputs.mcdata_pr_url }}
53+
NMP_BRANCH: ${{ github.event.inputs.nmp_branch }}
54+
NMP_PR_URL: ${{ github.event.inputs.nmp_pr_url }}

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ First time using Node.js? You may want to start with the [tutorial](tutorial.md)
1717

1818
## Features
1919

20-
* Supports Minecraft 1.8 to 1.21 (1.8, 1.9, 1.10, 1.11, 1.12, 1.13, 1.14, 1.15, 1.16, 1.17, 1.18, 1.19, 1.20, and 1.21)
20+
* Supports Minecraft 1.8 to 1.21 (1.8, 1.9, 1.10, 1.11, 1.12, 1.13, 1.14, 1.15, 1.16, 1.17, 1.18, 1.19, 1.20, 1.21) <!--version-->
2121
* Entity knowledge and tracking.
2222
* Block knowledge. You can query the world around you. Milliseconds to find any block.
2323
* Physics and movement - handle all bounding boxes

0 commit comments

Comments
 (0)