Skip to content

Commit ad1f4ca

Browse files
extremeheatrom1504
andauthored
Update update-from-minecraft-data.yml workflow (#1426)
* update workflow * update workflow * fix git config * update workflow * fix token * wrong wf * update workflow * update workflow * dispatch to mineflayer * fix --------- Co-authored-by: Romain Beaumont <[email protected]>
1 parent 8a99613 commit ad1f4ca

5 files changed

Lines changed: 74 additions & 66 deletions

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: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
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')()
75
const fs = require('fs')
86
const cp = require('child_process')
7+
const assert = require('assert')
8+
const github = require('gh-helpers')()
99
const { join } = require('path')
1010
const exec = (cmd) => github.mock ? console.log('> ', cmd) : (console.log('> ', cmd), cp.execSync(cmd, { stdio: 'inherit' }))
1111

1212
console.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-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 })
1818

19-
if (!newVersion) {
20-
console.error('No new version provided. Exiting...')
21-
process.exit(1)
22-
}
19+
assert(newVersion)
20+
assert(triggerBranch)
2321

2422
async 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-zA-Z0-9_]/g, '.')
58+
const branchName = 'pc' + newVersion.replace(/[^a-zA-Z0-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

7797
main().catch(err => {

.github/workflows/commands.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ on:
55
types: [created]
66
pull_request: # Handle renamed PRs
77
types: [edited]
8+
permissions:
9+
contents: write
810

911
jobs:
1012
comment-trigger:
Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
11
name: Update from minecraft-data
22

3-
# This workflow can be triggered from external repositories (like minecraft-data)
4-
# via workflow_dispatch API call or repository_dispatch webhook
53
on:
6-
# Allow manual triggering
74
workflow_dispatch:
85
inputs:
9-
trigger_source:
10-
description: 'Repository branch that triggered this update'
11-
required: false
12-
default: 'minecraft-data'
6+
new_mc_version:
7+
description: New minecraft version number
8+
required: true
139
type: string
14-
trigger_reason:
15-
description: 'What PR or issue triggered this update'
16-
required: false
17-
default: ''
10+
mcdata_branch:
11+
description: minecraft-data branch for this version
12+
required: true
1813
type: string
19-
data_version:
20-
description: 'MC Version that triggered this update'
14+
mcdata_pr_url:
15+
description: minecraft-data PR number to open a PR here against
2116
required: false
17+
default: ''
2218
type: string
2319

24-
# Listen for repository dispatch events (webhook-based triggers)
25-
repository_dispatch:
26-
types: [minecraft-data-update]
27-
2820
jobs:
2921
update:
3022
runs-on: ubuntu-latest
@@ -33,29 +25,12 @@ jobs:
3325
- name: Checkout repository
3426
uses: actions/checkout@v4
3527
with:
36-
# Use a token that has write permissions if you need to push changes
37-
token: ${{ secrets.GITHUB_TOKEN }}
38-
39-
- name: Setup Node.js
40-
uses: actions/setup-node@v4
41-
with:
42-
node-version: '18'
43-
cache: 'npm'
44-
45-
- name: Install dependencies
46-
run: npm install gh-helpers
47-
48-
- name: Set environment variables
49-
run: |
50-
echo "TRIGGER_SOURCE=${{ github.event.inputs.trigger_source || github.event.client_payload.repository || 'minecraft-data' }}" >> $GITHUB_ENV
51-
echo "TRIGGER_REASON=${{ github.event.inputs.trigger_reason || github.event.client_payload.reason || 'repository_dispatch' }}" >> $GITHUB_ENV
52-
echo "DATA_VERSION=${{ github.event.inputs.data_version || github.event.client_payload.version || 'unknown' }}" >> $GITHUB_ENV
53-
28+
token: ${{ secrets.PAT_PASSWORD }}
29+
5430
- name: Run updator script
55-
run: node .github/helper/updator.js
31+
run: cd .github/helper && npm install && node updator.js
5632
env:
57-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
58-
TRIGGER_SOURCE: ${{ env.TRIGGER_SOURCE }}
59-
TRIGGER_REASON: ${{ env.TRIGGER_REASON }}
60-
DATA_VERSION: ${{ env.DATA_VERSION }}
61-
33+
GITHUB_TOKEN: ${{ secrets.PAT_PASSWORD }}
34+
MCDATA_BRANCH: ${{ github.event.inputs.mcdata_branch }}
35+
MCDATA_PR_URL: ${{ github.event.inputs.mcdata_pr_url }}
36+
NEW_MC_VERSION: ${{ github.event.inputs.new_mc_version }}

docs/README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ Parse and serialize minecraft packets, plus authentication and encryption.
1111

1212
## Features
1313

14-
* Supports Minecraft PC version 1.7.10, 1.8.8, 1.9 (15w40b, 1.9, 1.9.1-pre2, 1.9.2, 1.9.4),
15-
1.10 (16w20a, 1.10-pre1, 1.10, 1.10.1, 1.10.2), 1.11 (16w35a, 1.11, 1.11.2), 1.12 (17w15a, 17w18b, 1.12-pre4, 1.12, 1.12.1, 1.12.2), and 1.13 (17w50a, 1.13, 1.13.1, 1.13.2-pre1, 1.13.2-pre2, 1.13.2), 1.14 (1.14, 1.14.1, 1.14.3, 1.14.4)
16-
, 1.15 (1.15, 1.15.1, 1.15.2) and 1.16 (20w13b, 20w14a, 1.16-rc1, 1.16, 1.16.1, 1.16.2, 1.16.3, 1.16.4, 1.16.5), 1.17 (21w07a, 1.17, 1.17.1), 1.18 (1.18, 1.18.1 and 1.18.2), 1.19 (1.19, 1.19.1, 1.19.2, 1.19.3, 1.19.4), 1.20 (1.20, 1.20.1, 1.20.2, 1.20.3, 1.20.4, 1.20.5, 1.20.6), 1.21 (1.21, 1.21.1, 1.21.3, 1.21.4, 1.21.5)
17-
, 1.21.6, 1.21.8 <!-- NEXT_VERSION -->
14+
* Supports Minecraft PC version
15+
1.7.10, 1.8.8, 1.9 (15w40b, 1.9, 1.9.1-pre2, 1.9.2, 1.9.4), 1.10 (16w20a, 1.10-pre1, 1.10, 1.10.1, 1.10.2),
16+
1.11 (16w35a, 1.11, 1.11.2), 1.12 (17w15a, 17w18b, 1.12-pre4, 1.12, 1.12.1, 1.12.2),
17+
1.13 (17w50a, 1.13, 1.13.1, 1.13.2-pre1, 1.13.2-pre2, 1.13.2),1.14 (1.14, 1.14.1, 1.14.3, 1.14.4),
18+
1.15 (1.15, 1.15.1, 1.15.2), 1.16 (20w13b, 20w14a, 1.16-rc1, 1.16, 1.16.1, 1.16.2, 1.16.3, 1.16.4, 1.16.5),
19+
1.17 (21w07a, 1.17, 1.17.1), 1.18 (1.18, 1.18.1 and 1.18.2),
20+
1.19 (1.19, 1.19.1, 1.19.2, 1.19.3, 1.19.4), 1.20 (1.20, 1.20.1, 1.20.2, 1.20.3, 1.20.4, 1.20.5, 1.20.6),
21+
1.21, 1.21.1, 1.21.3, 1.21.4, 1.21.5, 1.21.6, 1.21.8
22+
<!--add_next_version_above-->
23+
1824
* Parses all packets and emits events with packet fields as JavaScript
1925
objects.
2026
* Send a packet by supplying fields as a JavaScript object.

0 commit comments

Comments
 (0)