|
| 1 | +const { exec } = require("child_process"); |
| 2 | +const util = require("util"); |
| 3 | + |
| 4 | +const execAsync = util.promisify(exec); |
| 5 | + |
| 6 | +module.exports = async ({ github, context, versionType }) => { |
| 7 | + async function configureGit() { |
| 8 | + await execAsync('git config user.name "github-actions"'); |
| 9 | + await execAsync('git config user.email "github-actions@github.com"'); |
| 10 | + console.log("Configured git user"); |
| 11 | + } |
| 12 | + |
| 13 | + async function incrementVersion() { |
| 14 | + console.log(`Incrementing version with ${versionType}`); |
| 15 | + await execAsync(`npm version ${versionType}`); |
| 16 | + const packageJson = require("../../package.json"); |
| 17 | + const version = packageJson.version; |
| 18 | + console.log(`Incremented version to v${version}(${versionType})`); |
| 19 | + const featureBranch = `chore-increment-verion-${version}-${versionType}-${context.runNumber}`; |
| 20 | + return { featureBranch, version }; |
| 21 | + } |
| 22 | + |
| 23 | + async function pushChangesAndTags(featureBranch) { |
| 24 | + await execAsync(`git push origin HEAD:refs/heads/${featureBranch}`); |
| 25 | + await execAsync("git push origin --tags"); |
| 26 | + } |
| 27 | + |
| 28 | + async function getPull(source, target) { |
| 29 | + const existingPr = await github.pulls.list({ |
| 30 | + owner: context.repo.owner, |
| 31 | + repo: context.repo.repo, |
| 32 | + head: source, |
| 33 | + base: target, |
| 34 | + }); |
| 35 | + return existingPr.data.length > 0; |
| 36 | + } |
| 37 | + |
| 38 | + async function createPull(title, source, target) { |
| 39 | + await github.pulls.create({ |
| 40 | + title, |
| 41 | + body: `${title} PR.`, |
| 42 | + owner: context.repo.owner, |
| 43 | + repo: context.repo.repo, |
| 44 | + head: source, |
| 45 | + base: target, |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + await configureGit(); |
| 50 | + const { featureBranch, version } = await incrementVersion(); |
| 51 | + const hasOpenPr = await getPull(featureBranch, "master"); |
| 52 | + |
| 53 | + if (!hasOpenPr) { |
| 54 | + await pushChangesAndTags(featureBranch); |
| 55 | + const title = `Version Increment to v${version}(${versionType})`; |
| 56 | + await createPull(title, featureBranch, "master"); |
| 57 | + console.log( |
| 58 | + `Created pull request for version increment to v${version}(${versionType}).` |
| 59 | + ); |
| 60 | + } else { |
| 61 | + throw new Error( |
| 62 | + `Pull request already exists for version increment to v${version}(${versionType}).` |
| 63 | + ); |
| 64 | + } |
| 65 | +}; |
0 commit comments