|
| 1 | +import { |
| 2 | + getInput, |
| 3 | + setFailed |
| 4 | +} from '@actions/core' |
| 5 | +import { |
| 6 | + context, |
| 7 | + getOctokit |
| 8 | +} from '@actions/github' |
| 9 | +import { exec } from '@actions/exec' |
| 10 | +import { Releaser } from '@simple-release/core' |
| 11 | +import { PnpmWorkspacesProject } from '@simple-release/pnpm' |
| 12 | + |
| 13 | +const { |
| 14 | + owner, |
| 15 | + repo |
| 16 | +} = context.repo |
| 17 | +const branch = getInput('branch') || 'simple-release' |
| 18 | +const token = (getInput('token') || process.env.GITHUB_TOKEN)! |
| 19 | + |
| 20 | +await exec('git', [ |
| 21 | + 'config', |
| 22 | + '--global', |
| 23 | + 'user.email', |
| 24 | + 'github-actions[bot]@users.noreply.github.com' |
| 25 | +]) |
| 26 | +await exec('git', [ |
| 27 | + 'config', |
| 28 | + '--global', |
| 29 | + 'user.name', |
| 30 | + 'github-actions[bot]' |
| 31 | +]) |
| 32 | + |
| 33 | +await exec('git', [ |
| 34 | + 'branch', |
| 35 | + '-D', |
| 36 | + branch |
| 37 | +]) |
| 38 | +await exec('git', [ |
| 39 | + 'checkout', |
| 40 | + '-b', |
| 41 | + branch |
| 42 | +]) |
| 43 | + |
| 44 | +const project = new PnpmWorkspacesProject({ |
| 45 | + mode: 'fixed' |
| 46 | +}) |
| 47 | +const releaser = new Releaser(project) |
| 48 | + .bump() |
| 49 | + .commit() |
| 50 | +const { logger } = releaser |
| 51 | + |
| 52 | +try { |
| 53 | + await releaser.run() |
| 54 | +} catch (error) { |
| 55 | + setFailed((error as Error).message) |
| 56 | + throw error |
| 57 | +} |
| 58 | + |
| 59 | +if (project.versionUpdates.length) { |
| 60 | + logger.info('push', `Pushing changes to ${branch}...`) |
| 61 | + |
| 62 | + await exec('git', [ |
| 63 | + 'push', |
| 64 | + 'origin', |
| 65 | + branch, |
| 66 | + '--force' |
| 67 | + ]) |
| 68 | + |
| 69 | + const octokit = getOctokit(token) |
| 70 | + const { data: [pr] } = await octokit.rest.pulls.list({ |
| 71 | + owner, |
| 72 | + repo, |
| 73 | + head: `${owner}:${branch}`, |
| 74 | + state: 'open' |
| 75 | + }) |
| 76 | + const [title] = project.getCommitMessage().split('\n') |
| 77 | + const body = ` |
| 78 | +${project.versionUpdates.map(({ name, notes }) => `# ${name}\n\n${notes.trim()}`).join('\n\n')} |
| 79 | +
|
| 80 | +--- |
| 81 | +This PR was generated with [simple-release](https://github.com/TrigenSoftware/simple-release). |
| 82 | +` |
| 83 | + |
| 84 | + if (pr) { |
| 85 | + logger.info('pr', 'Updating existing pull request...') |
| 86 | + await octokit.rest.pulls.update({ |
| 87 | + owner, |
| 88 | + repo, |
| 89 | + pull_number: pr.number, |
| 90 | + title, |
| 91 | + body |
| 92 | + }) |
| 93 | + } else { |
| 94 | + logger.info('pr', 'Creating existing pull request...') |
| 95 | + |
| 96 | + const { data: { default_branch } } = await octokit.rest.repos.get({ |
| 97 | + owner, |
| 98 | + repo |
| 99 | + }) |
| 100 | + |
| 101 | + await octokit.rest.pulls.create({ |
| 102 | + owner, |
| 103 | + repo, |
| 104 | + title, |
| 105 | + base: default_branch, |
| 106 | + head: branch, |
| 107 | + body |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
0 commit comments