nightly task to automatically PR and merge changes from main into release

This commit is contained in:
Adam Brown 2022-08-24 14:17:31 +01:00
parent 702ef5d5cd
commit 5ce79bd2b8
5 changed files with 192 additions and 1 deletions

31
.github/workflows/nightly.yml vendored Normal file
View File

@ -0,0 +1,31 @@
name: Nightly
on:
schedule:
- cron: '0 19 * * *'
jobs:
check-develop-beta-changes:
name: Check if develop is ahead of beta release
runs-on: ubuntu-latest
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v3
with:
node-version:
16
- run: npm ci
working-directory: ./tools/beta-release/
- uses: actions/github-script@v6
with:
script: |
const { script } = await import('${{ github.workspace }}/tools/beta-release/app.js')
await script({github, context, core})

1
.gitignore vendored
View File

@ -11,3 +11,4 @@
.cxx
local.properties
/benchmark-out
**/node_modules

138
tools/beta-release/app.js Normal file
View File

@ -0,0 +1,138 @@
const config = {
owner: "ouchadam",
repo: "small-talk",
pathToVersionFile: "version.json",
rcBranchesFrom: "main",
rcMergesTo: "release",
}
const rcBranchName = "release-candidate"
export const script = async ({ github, context, core }) => {
console.log("script start")
if (await doesNotHaveInProgressRelease(github) && await isWorkingBranchAhead(github)) {
await startRelease(github)
} else {
console.log(`Release skipped due to being behind`)
}
return ""
}
const isWorkingBranchAhead = async (github) => {
const result = await github.rest.repos.compareCommitsWithBasehead({
owner: config.owner,
repo: config.repo,
basehead: `${config.rcMergesTo}...${config.rcBranchesFrom}`,
per_page: 1,
page: 1,
})
return result.data.status === "ahead"
}
const doesNotHaveInProgressRelease = async (github) => {
const releasePrs = await github.rest.pulls.list({
owner: config.owner,
repo: config.repo,
state: "open",
base: config.rcMergesTo
})
const syncPrs = await github.rest.pulls.list({
owner: config.owner,
repo: config.repo,
state: "open",
base: config.rcBranchesFrom,
head: `${config.owner}:${config.rcMergesTo}`
})
return releasePrs.data.length === 0 && syncPrs.data.length === 0
}
const startRelease = async (github) => {
console.log(`creating release candidate from head of ${config.rcBranchesFrom}`)
await createBranch(github, "release-candidate", config.rcBranchesFrom)
await incrementVersionFile(github, rcBranchName)
const createdPr = await github.rest.pulls.create({
owner: config.owner,
repo: config.repo,
title: "[Auto] Release Candidate",
head: rcBranchName,
base: config.rcMergesTo,
body: "todo",
})
github.graphql(
`
mutation ($pullRequestId: ID!, $mergeMethod: PullRequestMergeMethod!) {
enablePullRequestAutoMerge(input: {
pullRequestId: $pullRequestId,
mergeMethod: $mergeMethod
}) {
pullRequest {
autoMergeRequest {
enabledAt
enabledBy {
login
}
}
}
}
}
`,
{
pullRequestId: createdPr.data.node_id,
mergeMethod: "MERGE"
}
)
}
const createBranch = async (github, branchName, fromBranch) => {
const mainRef = await github.rest.git.getRef({
owner: config.owner,
repo: config.repo,
ref: `heads/${fromBranch}`,
})
await github.rest.git.createRef({
owner: config.owner,
repo: config.repo,
ref: `refs/heads/${branchName}`,
sha: mainRef.data.object.sha,
})
}
const incrementVersionFile = async (github, branchName) => {
const versionFile = await readVersionFile(github)
const updatedVersionFile = {
...versionFile.content,
code: versionFile.content.code + 1,
}
const encodedContentUpdate = Buffer.from(JSON.stringify(updatedVersionFile, null, 2)).toString('base64')
await github.rest.repos.createOrUpdateFileContents({
owner: config.owner,
repo: config.repo,
content: encodedContentUpdate,
path: config.pathToVersionFile,
sha: versionFile.sha,
branch: branchName,
message: "updating version for release",
})
}
const readVersionFile = async (github) => {
const result = await github.rest.repos.getContent({
owner: config.owner,
repo: config.repo,
path: config.pathToVersionFile,
ref: config.rcBranchesFrom,
})
const content = Buffer.from(result.data.content, result.data.encoding).toString()
return {
content: JSON.parse(content),
sha: result.data.sha,
}
}

13
tools/beta-release/package-lock.json generated Normal file
View File

@ -0,0 +1,13 @@
{
"name": "beta-release",
"version": "1.0.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "beta-release",
"version": "1.0.0",
"license": "MIT"
}
}
}

View File

@ -0,0 +1,8 @@
{
"name": "beta-release",
"version": "1.0.0",
"main": "app.js",
"license": "MIT",
"type": "module",
"private": true
}