From 5ce79bd2b870fa280f42ace4ea2056791b7b9125 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 24 Aug 2022 14:17:31 +0100 Subject: [PATCH] nightly task to automatically PR and merge changes from main into release --- .github/workflows/nightly.yml | 31 ++++++ .gitignore | 3 +- tools/beta-release/app.js | 138 +++++++++++++++++++++++++++ tools/beta-release/package-lock.json | 13 +++ tools/beta-release/package.json | 8 ++ 5 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/nightly.yml create mode 100644 tools/beta-release/app.js create mode 100644 tools/beta-release/package-lock.json create mode 100644 tools/beta-release/package.json diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml new file mode 100644 index 0000000..945a0de --- /dev/null +++ b/.github/workflows/nightly.yml @@ -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}) + diff --git a/.gitignore b/.gitignore index 3b76762..9257dec 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ .externalNativeBuild .cxx local.properties -/benchmark-out \ No newline at end of file +/benchmark-out +**/node_modules \ No newline at end of file diff --git a/tools/beta-release/app.js b/tools/beta-release/app.js new file mode 100644 index 0000000..b0ebf35 --- /dev/null +++ b/tools/beta-release/app.js @@ -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, + } +} \ No newline at end of file diff --git a/tools/beta-release/package-lock.json b/tools/beta-release/package-lock.json new file mode 100644 index 0000000..358934c --- /dev/null +++ b/tools/beta-release/package-lock.json @@ -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" + } + } +} diff --git a/tools/beta-release/package.json b/tools/beta-release/package.json new file mode 100644 index 0000000..975ce6c --- /dev/null +++ b/tools/beta-release/package.json @@ -0,0 +1,8 @@ +{ + "name": "beta-release", + "version": "1.0.0", + "main": "app.js", + "license": "MIT", + "type": "module", + "private": true +}