2022-08-30 21:32:52 +02:00
|
|
|
import { release } from './release.js'
|
|
|
|
|
2022-08-24 15:17:31 +02:00
|
|
|
const config = {
|
|
|
|
owner: "ouchadam",
|
|
|
|
repo: "small-talk",
|
|
|
|
pathToVersionFile: "version.json",
|
|
|
|
rcBranchesFrom: "main",
|
|
|
|
rcMergesTo: "release",
|
2022-08-31 23:38:22 +02:00
|
|
|
packageName: "app.dapk.st",
|
|
|
|
matrixRoomId: "!jgNenzNPtSpJLjjsxe:matrix.org"
|
2022-08-24 15:17:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const rcBranchName = "release-candidate"
|
|
|
|
|
2022-08-24 16:15:26 +02:00
|
|
|
export const startReleaseProcess = async ({ github, context, core }) => {
|
2022-08-24 15:17:31 +02:00
|
|
|
console.log("script start")
|
|
|
|
if (await doesNotHaveInProgressRelease(github) && await isWorkingBranchAhead(github)) {
|
|
|
|
await startRelease(github)
|
|
|
|
} else {
|
|
|
|
console.log(`Release skipped due to being behind`)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-08-30 21:32:52 +02:00
|
|
|
export const publishRelease = async (github, artifacts) => {
|
2022-08-31 22:46:44 +02:00
|
|
|
const versionFile = await readVersionFile(github, config.rcMergesTo)
|
2022-08-30 21:32:52 +02:00
|
|
|
await release(
|
|
|
|
github,
|
|
|
|
versionFile.content,
|
|
|
|
config.packageName,
|
|
|
|
artifacts,
|
|
|
|
config,
|
2022-08-31 22:46:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const createdPr = await github.rest.pulls.create({
|
|
|
|
owner: config.owner,
|
|
|
|
repo: config.repo,
|
|
|
|
title: "[Auto] Sync Release",
|
|
|
|
head: config.rcMergesTo,
|
|
|
|
base: config.rcBranchesFrom,
|
|
|
|
body: "Syncing changes from release",
|
|
|
|
})
|
|
|
|
|
2022-09-01 23:26:57 +02:00
|
|
|
await enablePrAutoMerge(github, createdPr.data.node_id)
|
2022-08-24 16:15:26 +02:00
|
|
|
}
|
|
|
|
|
2022-08-24 15:17:31 +02:00
|
|
|
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",
|
|
|
|
})
|
|
|
|
|
2022-09-01 23:26:57 +02:00
|
|
|
await enablePrAutoMerge(github, createdPr.data.node_id)
|
2022-08-31 22:46:44 +02:00
|
|
|
}
|
|
|
|
|
2022-09-01 23:26:57 +02:00
|
|
|
const enablePrAutoMerge = async (github, prNodeId) => {
|
2022-08-31 22:46:44 +02:00
|
|
|
await github.graphql(
|
2022-08-24 15:17:31 +02:00
|
|
|
`
|
|
|
|
mutation ($pullRequestId: ID!, $mergeMethod: PullRequestMergeMethod!) {
|
|
|
|
enablePullRequestAutoMerge(input: {
|
|
|
|
pullRequestId: $pullRequestId,
|
|
|
|
mergeMethod: $mergeMethod
|
|
|
|
}) {
|
|
|
|
pullRequest {
|
|
|
|
autoMergeRequest {
|
|
|
|
enabledAt
|
|
|
|
enabledBy {
|
|
|
|
login
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
{
|
2022-08-31 22:46:44 +02:00
|
|
|
pullRequestId: prNodeId,
|
2022-08-24 15:17:31 +02:00
|
|
|
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) => {
|
2022-08-31 21:48:29 +02:00
|
|
|
const versionFile = await readVersionFile(github, branchName)
|
2022-09-03 10:40:35 +02:00
|
|
|
const [date, rc] = versionFile.content.name.split("-V")
|
|
|
|
const today = new Date().toLocaleDateString("en-GB")
|
|
|
|
|
|
|
|
let updatedVersionName = undefined
|
|
|
|
if (today == date) {
|
2022-09-04 15:20:30 +02:00
|
|
|
updatedVersionName = `${date}-V${parseInt(rc) + 1}`
|
2022-09-03 10:40:35 +02:00
|
|
|
} else {
|
|
|
|
updatedVersionName = `${today}-V1`
|
|
|
|
}
|
2022-08-24 15:17:31 +02:00
|
|
|
|
|
|
|
const updatedVersionFile = {
|
|
|
|
code: versionFile.content.code + 1,
|
2022-09-03 10:40:35 +02:00
|
|
|
name: updatedVersionName,
|
2022-08-24 15:17:31 +02:00
|
|
|
}
|
2022-09-03 10:40:35 +02:00
|
|
|
|
2022-08-24 15:17:31 +02:00
|
|
|
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",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-31 21:48:29 +02:00
|
|
|
const readVersionFile = async (github, branch) => {
|
2022-08-24 15:17:31 +02:00
|
|
|
const result = await github.rest.repos.getContent({
|
|
|
|
owner: config.owner,
|
|
|
|
repo: config.repo,
|
|
|
|
path: config.pathToVersionFile,
|
2022-08-31 21:48:29 +02:00
|
|
|
ref: branch,
|
2022-08-24 15:17:31 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
const content = Buffer.from(result.data.content, result.data.encoding).toString()
|
|
|
|
return {
|
|
|
|
content: JSON.parse(content),
|
|
|
|
sha: result.data.sha,
|
|
|
|
}
|
|
|
|
}
|