├── .eslintrc ├── .github └── workflows │ └── ci.yml ├── .gitignore ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── action.yml ├── dist └── index.js ├── package-lock.json ├── package.json ├── src ├── create-release.js └── main.js └── tests └── create-release.test.js /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "airbnb", 4 | "prettier" 5 | ], 6 | "env": { 7 | "es6": true, 8 | "node": true, 9 | "jest": true 10 | }, 11 | "rules": { 12 | "global-require": 0, 13 | "prettier/prettier": [ 14 | "error", 15 | { 16 | "trailingComma": "none", 17 | "singleQuote": true, 18 | "printWidth": 120 19 | } 20 | ] 21 | }, 22 | "plugins": [ 23 | "prettier" 24 | ] 25 | } 26 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: "Tests" 2 | 3 | on: 4 | pull_request: 5 | push: 6 | branches: # array of glob patterns matching against refs/heads. Optional; defaults to all 7 | - master # triggers on pushes that contain changes in master 8 | 9 | jobs: 10 | test: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: zendesk/checkout@v3 14 | - run: npm ci 15 | - run: npm test 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Optional REPL history 57 | .node_repl_history 58 | 59 | # Output of 'npm pack' 60 | *.tgz 61 | 62 | # Yarn Integrity file 63 | .yarn-integrity 64 | 65 | # dotenv environment variables file 66 | .env 67 | .env.test 68 | 69 | # parcel-bundler cache (https://parceljs.org/) 70 | .cache 71 | 72 | # next.js build output 73 | .next 74 | 75 | # nuxt.js build output 76 | .nuxt 77 | 78 | # vuepress build output 79 | .vuepress/dist 80 | 81 | # Serverless directories 82 | .serverless/ 83 | 84 | # FuseBox cache 85 | .fusebox/ 86 | 87 | # DynamoDB Local files 88 | .dynamodb/ 89 | 90 | # Misc 91 | .idea/ 92 | .DS_Store 93 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to make participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies within all project spaces, and it also applies when 49 | an individual is representing the project or its community in public spaces. 50 | Examples of representing a project or community include using an official 51 | project e-mail address, posting via an official social media account, or acting 52 | as an appointed representative at an online or offline event. Representation of 53 | a project may be further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at opensource+actions/create-release@github.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## Contributing 2 | 3 | [fork]: https://github.com/actions/create-release/fork 4 | [pr]: https://github.com/actions/create-release/compare 5 | [style]: https://github.com/styleguide/js 6 | [code-of-conduct]: CODE_OF_CONDUCT.md 7 | 8 | Hi there! We're thrilled that you'd like to contribute to this project. Your help is essential for keeping it great. 9 | 10 | Contributions to this project are [released](https://help.github.com/articles/github-terms-of-service/#6-contributions-under-repository-license) to the public under the [project's open source license](LICENSE). 11 | 12 | Please note that this project is released with a [Contributor Code of Conduct][code-of-conduct]. By participating in this project you agree to abide by its terms. 13 | 14 | ## Submitting a pull request 15 | 16 | 1. [Fork][fork] and clone the repository 17 | 2. Configure and install the dependencies: `npm install` 18 | 3. Make sure the tests pass on your machine: `npm run test` 19 | 4. Create a new branch: `git checkout -b my-branch-name` 20 | 5. Make your change, add tests, and make sure the tests still pass 21 | 6. Push to your fork and [submit a pull request][pr] 22 | 7. Pat your self on the back and wait for your pull request to be reviewed and merged. 23 | 24 | Here are a few things you can do that will increase the likelihood of your pull request being accepted: 25 | 26 | - Follow the [style guide][style]. 27 | - Write tests. 28 | - Keep your change as focused as possible. If there are multiple changes you would like to make that are not dependent upon each other, consider submitting them as separate pull requests. 29 | - Write a [good commit message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html). 30 | 31 | ## Resources 32 | 33 | - [How to Contribute to Open Source](https://opensource.guide/how-to-contribute/) 34 | - [Using Pull Requests](https://help.github.com/articles/about-pull-requests/) 35 | - [GitHub Help](https://help.github.com) 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2018 GitHub, Inc. and contributors 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # GitHub Action - Releases API 3 | This GitHub Action (written in JavaScript) wraps the [Create a Release](https://developer.github.com/v3/repos/releases/#create-a-release) endpoint and [Auto Increment Tag Version ](#auto-increment---path-type---push-to-master) feature. Which allows to create tag automatically when pushed to the release branch. This is also an alternative to Samson Release functionality. 4 | 5 | GitHub Actions status 6 | 7 | ## Usage 8 | ### Pre-requisites 9 | Create a workflow `.yml` file in your `.github/workflows` directory. An [example workflow](#example-workflow---create-a-release) is available below. For more information, reference the GitHub Help Documentation for [Creating a workflow file](https://help.github.com/en/articles/configuring-a-workflow#creating-a-workflow-file). 10 | 11 | ### Inputs 12 | For more information on these inputs, see the [API Documentation](https://developer.github.com/v3/repos/releases/#input) 13 | 14 | | Parameter | Description | Required | Default | 15 | | ------------------- | ------------------------------------------------------------------------------------------ | -------- | ------------ | 16 | | `tag_name` | The name of the tag for this release. | N | Auto Incremented | 17 | | `release_name` | The name of the release. | N | TAG_NAME | 18 | | `body` | Text describing the contents of the release | N | N/A | 19 | | `draft` | `true` to create a draft (unpublished) release, `false` to create a published one. | N | `false` | 20 | | `prerelease` | `true` to identify the release as a prerelease. `false` to identify the release as a full release. | N | `false` | 21 | | `prerelease_suffix` | The suffix added to a prerelease tag, if none already exists. | N | `beta` | 22 | | `auto_increment_type` | Used for auto-incrementing the tag version. One of (`major`, `minor`, `patch`, `prerelease`, `premajor`). | N | `patch` | 23 | | `tag_schema` | Used for computing the tag schema. One of (`continuous`, `semantic`). | N | `continuous` | 24 | 25 | 26 | 27 | ### Outputs 28 | For more information on these outputs, see the [API Documentation](https://developer.github.com/v3/repos/releases/#response-4) for an example of what these outputs look like 29 | 30 | | Parameter | Description | 31 | | ------------------- | ---------------------------------------------------------------------------- | 32 | | `id` | The release ID | 33 | | `html_url` | The URL users can navigate to in order to view the release. i.e. `https://github.com/octocat/Hello-World/releases/v1.0.0` | 34 | | `upload_url` | The URL for uploading assets to the release, which could be used by GitHub Actions for additional uses, for example the [`@actions/upload-release-asset`](https://www.github.com/actions/upload-release-asset) GitHub Action| 35 | | `previous_tag` | The most recent tag found during auto incrementing the tag version.| 36 | | `current_tag` | The tag used to create a Release.| 37 | 38 | 39 | ## Example workflow - Create a release 40 | 41 | ### Auto Increment - Patch type - Push to master 42 | On every `push` to a `master` branch will create tag incrementally. 43 | 44 | ```yaml 45 | on: 46 | push: 47 | branches: 48 | - master 49 | 50 | name: Create Release 51 | 52 | jobs: 53 | build: 54 | name: Create Release 55 | runs-on: ['self-hosted'] 56 | steps: 57 | - name: Create Release 58 | id: create_release 59 | uses: zendesk/action-create-release@v1 60 | env: 61 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 62 | ``` 63 | 64 | ***Sample Output:*** 65 | ``` 66 | previous_tag: v5.0.3 67 | current_tag: v5.0.4 68 | id: 123 69 | upload_url: https://uploads.github.com/repos/zendesk/Hello-World/releases/1/assets 70 | html_url: https://github.com/octocat/Hello-World/releases/v1.0.0 71 | ``` 72 | 73 | ### Auto Increment types and outputs 74 | 75 | | Type | Previous Tag | Current Tag | 76 | | ----------------- | ------------------- | ------------------- | 77 | | major | `v5.0.1` | `v6.0.0` | 78 | | minor | `v5.0.1` | `v5.1.1` | 79 | | patch | `v5.0.1` | `v5.0.2` | 80 | | prerelease | `v5.0.1` | `v5.0.2-beta.0` | 81 | | premajor | `v5.0.1` | `v6.0.1-beta.0` | 82 | 83 | 84 | ### Tag as parameter 85 | On every `push` to a tag matching the pattern `v*`, [create a release](https://developer.github.com/v3/repos/releases/#create-a-release): 86 | 87 | ```yaml 88 | on: 89 | push: 90 | # Sequence of patterns matched against refs/tags 91 | tags: 92 | - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10 93 | 94 | name: Create Release 95 | 96 | jobs: 97 | build: 98 | name: Create Release 99 | runs-on: ['self-hosted'] 100 | steps: 101 | - name: Create Release 102 | id: create_release 103 | uses: zendesk/action-create-release@v1 104 | env: 105 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token 106 | with: 107 | tag_name: ${{ github.ref }} 108 | release_name: Release ${{ github.ref }} 109 | body: | 110 | Changes in this Release 111 | - First Change 112 | - Second Change 113 | draft: false 114 | prerelease: false 115 | ``` 116 | 117 | This will create a [Release](https://help.github.com/en/articles/creating-releases), as well as a [`release` event](https://developer.github.com/v3/activity/events/types/#releaseevent), which could be handled by a third party service, or by GitHub Actions for additional uses, for example the [`@actions/upload-release-asset`](https://www.github.com/actions/upload-release-asset) GitHub Action. This uses the `GITHUB_TOKEN` provided by the [virtual environment](https://help.github.com/en/github/automating-your-workflow-with-github-actions/virtual-environments-for-github-actions#github_token-secret), so no new token is needed. 118 | 119 | ## Contributing 120 | We would love you to contribute to `@zendesk/action-create-release`, pull requests are welcome! Please see the [CONTRIBUTING.md](CONTRIBUTING.md) for more information. 121 | 122 | ## License 123 | The scripts and documentation in this project are released under the [MIT License](LICENSE) 124 | -------------------------------------------------------------------------------- /action.yml: -------------------------------------------------------------------------------- 1 | name: 'Create a Release' 2 | description: 'Create a release for a tag in your repository' 3 | author: 'GitHub' 4 | inputs: 5 | tag_name: 6 | description: 'The name of the tag. This should come from the webhook payload, `github.GITHUB_REF` when a user pushes a new tag' 7 | required: false 8 | release_name: 9 | description: 'The name of the release. For example, `Release v1.0.1`' 10 | required: false 11 | body: 12 | description: 'Text describing the contents of the tag.' 13 | required: false 14 | draft: 15 | description: '`true` to create a draft (unpublished) release, `false` to create a published one. Default: `false`' 16 | required: false 17 | default: false 18 | prerelease: 19 | description: '`true` to identify the release as a prerelease. `false` to identify the release as a full release. Default: `false`' 20 | required: false 21 | default: false 22 | prerelease_suffix: 23 | description: 'Annotate a prerelease version tag with suffix' 24 | required: false 25 | default: 'beta' 26 | auto_increment_type: 27 | description: 'bump tag version type, Is this a major, minor, patch, or prerelease?' 28 | required: false 29 | default: 'patch' 30 | tag_schema: 31 | description: 'schema used to compute next tag. continuous or semantic' 32 | required: false 33 | default: 'continuous' 34 | outputs: 35 | id: 36 | description: 'The ID of the created Release' 37 | html_url: 38 | description: 'The URL users can navigate to in order to view the release' 39 | upload_url: 40 | description: 'The URL for uploading assets to the release' 41 | current_tag: 42 | description: 'The tag used to create a Release' 43 | previous_tag: 44 | description: 'The most recent tag found during auto incrementing the tag version' 45 | runs: 46 | using: 'node16' 47 | main: 'dist/index.js' 48 | branding: 49 | icon: 'tag' 50 | color: 'gray-dark' 51 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "create-release", 3 | "version": "1.0.0", 4 | "description": "Create a release", 5 | "main": "dist/index.js", 6 | "scripts": { 7 | "lint": "eslint 'src/**.js' 'tests/**.js' --fix", 8 | "test": "eslint 'src/**.js' 'tests/**.js' && jest --coverage", 9 | "build": "ncc build src/main.js", 10 | "precommit": "npm run build && git add dist/" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/actions/create-release" 15 | }, 16 | "keywords": [ 17 | "actions", 18 | "node" 19 | ], 20 | "author": "GitHub", 21 | "license": "MIT", 22 | "dependencies": { 23 | "@actions/core": "^1.10.0", 24 | "@actions/exec": "^1.1.0", 25 | "@actions/github": "^2.2.0", 26 | "semver": "^7.3.2", 27 | "minimist": ">=0.2.1", 28 | "kind-of": ">=6.0.3" 29 | }, 30 | "devDependencies": { 31 | "@zeit/ncc": "^0.20.4", 32 | "eslint": "^5.13.0", 33 | "eslint-config-airbnb": "^17.1.0", 34 | "eslint-config-prettier": "^3.6.0", 35 | "eslint-plugin-import": "^2.16.0", 36 | "eslint-plugin-jsx-a11y": "^6.2.1", 37 | "eslint-plugin-prettier": "^2.7.0", 38 | "eslint-plugin-react": "^7.12.4", 39 | "jest": "^24.8.0", 40 | "prettier": "^1.16.4", 41 | "husky": "^3.0.5" 42 | }, 43 | "jest": { 44 | "testEnvironment": "node", 45 | "collectCoverageFrom": [ 46 | "src/create-release.js" 47 | ], 48 | "coverageThreshold": { 49 | "global": { 50 | "branches": 80, 51 | "functions": 80, 52 | "lines": 80, 53 | "statements": 80 54 | } 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/create-release.js: -------------------------------------------------------------------------------- 1 | const core = require('@actions/core'); 2 | const { GitHub, context } = require('@actions/github'); 3 | 4 | const { owner, repo } = context.repo; 5 | const semver = require('semver'); 6 | const process = require('process'); 7 | 8 | const octokit = new GitHub(process.env.GITHUB_TOKEN); 9 | const Scheme = { 10 | Continuous: 'continuous', 11 | Semantic: 'semantic' 12 | }; 13 | const Semantic = { 14 | Major: 'major', 15 | Minor: 'minor', 16 | Patch: 'patch', 17 | Premajor: 'premajor', 18 | Prerelease: 'prerelease' 19 | }; 20 | const prerelease = core.getInput('prerelease', { required: false }) === 'true'; 21 | 22 | // Check string is null 23 | function isNullString(string) { 24 | return !string || string.length === 0 || string === 'null' || string === 'undefined'; 25 | } 26 | 27 | // If there is no previous tag, Then the initial tag will be used 28 | function initialTag(tag) { 29 | const suffix = core.getInput('prerelease_suffix'); 30 | const newTag = prerelease ? `${tag}-${suffix}.0` : tag; 31 | 32 | return newTag; 33 | } 34 | 35 | async function existingTags() { 36 | const { data: refs } = await octokit.git.listMatchingRefs({ 37 | owner, 38 | repo, 39 | ref: 'tags' 40 | }); 41 | return refs.reverse(); 42 | } 43 | 44 | function semanticVersion(tag) { 45 | try { 46 | const [version, pre] = tag.split('-', 2); 47 | const sem = semver.parse(semver.coerce(version)); 48 | 49 | if (!isNullString(pre)) { 50 | sem.prerelease = semver.prerelease(`0.0.0-${pre}`); 51 | } 52 | 53 | return sem; 54 | } catch (_) { 55 | // semver will return null if it fails to parse, maintain this behavior in our API 56 | return null; 57 | } 58 | } 59 | 60 | function determineContinuousBumpType(semTag) { 61 | const type = core.getInput('auto_increment_type') || Semantic.Major; 62 | const hasExistingPrerelease = semTag.prerelease.length > 0; 63 | 64 | switch (type) { 65 | case Semantic.Prerelease: 66 | return hasExistingPrerelease ? Semantic.Prerelease : Semantic.Premajor; 67 | case Semantic.Premajor: 68 | return Semantic.Premajor; 69 | default: 70 | return Semantic.Major; 71 | } 72 | } 73 | 74 | function determinePrereleaseName(semTag) { 75 | if (semTag.prerelease.length > 0) { 76 | return semTag.prerelease[0]; 77 | } 78 | return core.getInput('prerelease_suffix') || 'beta'; 79 | } 80 | 81 | function computeNextContinuous(semTag) { 82 | const bumpType = determineContinuousBumpType(semTag); 83 | const preName = determinePrereleaseName(semTag); 84 | const nextSemTag = semver.parse(semver.inc(semTag, bumpType, preName)); 85 | const tagSuffix = nextSemTag.prerelease.length > 0 ? `-${nextSemTag.prerelease.join('.')}` : ''; 86 | return [semTag.options.tagPrefix, nextSemTag.major, tagSuffix].join(''); 87 | } 88 | 89 | function computeNextSemantic(semTag) { 90 | try { 91 | const type = core.getInput('auto_increment_type') || Semantic.Patch; 92 | const preName = determinePrereleaseName(semTag); 93 | 94 | switch (type) { 95 | case Semantic.Major: 96 | case Semantic.Minor: 97 | case Semantic.Patch: 98 | case Semantic.Premajor: 99 | case Semantic.Prerelease: 100 | return `${semTag.options.tagPrefix}${semver.inc(semTag, type, preName)}`; 101 | default: 102 | core.setFailed( 103 | `Unsupported semantic version type ${type}. Must be one of (${Object.values(Semantic).join(', ')})` 104 | ); 105 | } 106 | } catch (error) { 107 | core.setFailed(`Failed to compute next semantic tag: ${error}`); 108 | } 109 | return null; 110 | } 111 | 112 | async function computeLastTag() { 113 | const recentTags = await existingTags(); 114 | if (recentTags.length < 1) { 115 | return null; 116 | } 117 | return recentTags.shift().ref.replace('refs/tags/', ''); 118 | } 119 | 120 | async function computeNextTag(scheme) { 121 | const lastTag = await computeLastTag(); 122 | // Handle zero-state where no tags exist for the repo 123 | if (!lastTag) { 124 | if (scheme === Scheme.Continuous) { 125 | return initialTag('v1'); 126 | } 127 | return initialTag('v1.0.0'); 128 | } 129 | core.info(`Computing the next tag based on: ${lastTag}`); 130 | core.setOutput('previous_tag', lastTag); 131 | 132 | const semTag = semanticVersion(lastTag); 133 | 134 | if (semTag == null) { 135 | core.setFailed(`Failed to parse tag: ${lastTag}`); 136 | return null; 137 | } 138 | semTag.options.tagPrefix = lastTag.startsWith('v') ? 'v' : ''; 139 | 140 | if (scheme === Scheme.Continuous) { 141 | return computeNextContinuous(semTag); 142 | } 143 | return computeNextSemantic(semTag); 144 | } 145 | 146 | async function run() { 147 | try { 148 | // Get the inputs from the workflow file: https://github.com/actions/toolkit/tree/master/packages/core#inputsoutputs 149 | const tagName = core.getInput('tag_name', { required: false }); 150 | const scheme = core.getInput('tag_schema', { required: false }); 151 | if (scheme !== Scheme.Continuous && scheme !== Scheme.Semantic) { 152 | core.setFailed(`Unsupported version scheme: ${scheme}`); 153 | return; 154 | } 155 | // Use predefined tag or calculate automatic next tag 156 | const tag = isNullString(tagName) ? await computeNextTag(scheme) : tagName.replace('refs/tags/', ''); 157 | 158 | const releaseName = core.getInput('release_name', { required: false }); 159 | const release = isNullString(releaseName) ? tag : releaseName.replace('refs/tags/', ''); 160 | 161 | const body = core.getInput('body', { required: false }); 162 | const draft = core.getInput('draft', { required: false }) === 'true'; 163 | 164 | // Create a release 165 | // API Documentation: https://developer.github.com/v3/repos/releases/#create-a-release 166 | // Octokit Documentation: https://octokit.github.io/rest.js/#octokit-routes-repos-create-release 167 | const createReleaseResponse = await octokit.repos.createRelease({ 168 | owner, 169 | repo, 170 | tag_name: tag, 171 | name: release, 172 | body, 173 | draft, 174 | prerelease 175 | }); 176 | 177 | // Get the ID, html_url, and upload URL for the created Release from the response 178 | const { 179 | data: { id: releaseId, html_url: htmlUrl, upload_url: uploadUrl } 180 | } = createReleaseResponse; 181 | 182 | // Set the output variables for use by other actions: https://github.com/actions/toolkit/tree/master/packages/core#inputsoutputs 183 | core.setOutput('current_tag', tag); 184 | core.setOutput('id', releaseId); 185 | core.setOutput('html_url', htmlUrl); 186 | core.setOutput('upload_url', uploadUrl); 187 | } catch (error) { 188 | core.setFailed(error.message); 189 | } 190 | } 191 | 192 | module.exports = run; 193 | -------------------------------------------------------------------------------- /src/main.js: -------------------------------------------------------------------------------- 1 | const run = require('./create-release'); 2 | 3 | if (require.main === module) { 4 | run(); 5 | } 6 | -------------------------------------------------------------------------------- /tests/create-release.test.js: -------------------------------------------------------------------------------- 1 | jest.mock('@actions/core'); 2 | jest.mock('@actions/github'); 3 | 4 | /* eslint-disable no-undef */ 5 | describe('Create Release', () => { 6 | let context; 7 | let core; 8 | let createRelease; 9 | let GitHub; 10 | let listMatchingRefs; 11 | let run; 12 | 13 | function mockValues(previousTags = []) { 14 | core = require('@actions/core'); 15 | const gitHub = require('@actions/github'); 16 | [GitHub, context] = [gitHub.GitHub, gitHub.context]; 17 | createRelease = jest.fn().mockReturnValueOnce({ 18 | data: { 19 | id: 'releaseId', 20 | html_url: 'htmlUrl', 21 | upload_url: 'uploadUrl' 22 | } 23 | }); 24 | 25 | listMatchingRefs = jest.fn().mockReturnValueOnce({ 26 | data: previousTags 27 | }); 28 | 29 | context.repo = { 30 | owner: 'owner', 31 | repo: 'repo' 32 | }; 33 | 34 | const octokit = { 35 | repos: { 36 | createRelease 37 | }, 38 | git: { 39 | listMatchingRefs 40 | } 41 | }; 42 | 43 | GitHub.mockImplementation(() => octokit); 44 | run = require('../src/create-release.js'); 45 | } 46 | 47 | beforeEach(() => { 48 | jest.resetModules(); 49 | mockValues(); 50 | }); 51 | 52 | test('Create release endpoint is called', async () => { 53 | core.getInput = jest 54 | .fn() 55 | .mockReturnValueOnce('refs/tags/v1.0.0') 56 | .mockReturnValueOnce('continuous') 57 | .mockReturnValueOnce('myRelease') 58 | .mockReturnValueOnce('myBody') 59 | .mockReturnValueOnce('false') 60 | .mockReturnValueOnce('false'); 61 | 62 | await run(); 63 | 64 | expect(createRelease).toHaveBeenCalledWith({ 65 | owner: 'owner', 66 | repo: 'repo', 67 | tag_name: 'v1.0.0', 68 | name: 'myRelease', 69 | body: 'myBody', 70 | draft: false, 71 | prerelease: false 72 | }); 73 | }); 74 | 75 | test('Draft release is created', async () => { 76 | core.getInput = jest 77 | .fn() 78 | .mockReturnValueOnce('refs/tags/v1.0.0') 79 | .mockReturnValueOnce('continuous') 80 | .mockReturnValueOnce('myRelease') 81 | .mockReturnValueOnce('myBody') 82 | .mockReturnValueOnce('true') 83 | .mockReturnValueOnce('false'); 84 | 85 | await run(); 86 | 87 | expect(createRelease).toHaveBeenCalledWith({ 88 | owner: 'owner', 89 | repo: 'repo', 90 | tag_name: 'v1.0.0', 91 | name: 'myRelease', 92 | body: 'myBody', 93 | draft: true, 94 | prerelease: false 95 | }); 96 | }); 97 | 98 | test('auto tagged with semantic version', async () => { 99 | core.getInput = jest 100 | .fn() 101 | .mockReturnValueOnce('') 102 | .mockReturnValueOnce('semantic') 103 | .mockReturnValueOnce('major') 104 | .mockReturnValueOnce('myRelease') 105 | .mockReturnValueOnce('') // <-- The default value for body in action.yml 106 | .mockReturnValueOnce('false') 107 | .mockReturnValueOnce('false'); 108 | 109 | await run(); 110 | 111 | expect(createRelease).toHaveBeenCalledWith({ 112 | owner: 'owner', 113 | repo: 'repo', 114 | tag_name: 'v1.0.0', 115 | name: 'myRelease', 116 | body: '', 117 | draft: false, 118 | prerelease: false 119 | }); 120 | }); 121 | 122 | test('Unsupported tag schema', async () => { 123 | core.getInput = jest 124 | .fn() 125 | .mockReturnValueOnce('') 126 | .mockReturnValueOnce('test'); 127 | 128 | await run(); 129 | 130 | expect(createRelease).not.toHaveBeenCalled(); 131 | }); 132 | 133 | test('Release with empty body is created', async () => { 134 | core.getInput = jest 135 | .fn() 136 | .mockReturnValueOnce('refs/tags/v1.0.0') 137 | .mockReturnValueOnce('continuous') 138 | .mockReturnValueOnce('myRelease') 139 | .mockReturnValueOnce('') // <-- The default value for body in action.yml 140 | .mockReturnValueOnce('false') 141 | .mockReturnValueOnce('false'); 142 | 143 | await run(); 144 | 145 | expect(createRelease).toHaveBeenCalledWith({ 146 | owner: 'owner', 147 | repo: 'repo', 148 | tag_name: 'v1.0.0', 149 | name: 'myRelease', 150 | body: '', 151 | draft: false, 152 | prerelease: false 153 | }); 154 | }); 155 | 156 | test('Outputs are set', async () => { 157 | core.getInput = jest 158 | .fn() 159 | .mockReturnValueOnce('refs/tags/v1.0.0') 160 | .mockReturnValueOnce('continuous') 161 | .mockReturnValueOnce('myRelease') 162 | .mockReturnValueOnce('myBody') 163 | .mockReturnValueOnce('false') 164 | .mockReturnValueOnce('false'); 165 | 166 | core.setOutput = jest.fn(); 167 | 168 | await run(); 169 | 170 | expect(core.setOutput).toHaveBeenNthCalledWith(1, 'current_tag', 'v1.0.0'); 171 | expect(core.setOutput).toHaveBeenNthCalledWith(2, 'id', 'releaseId'); 172 | expect(core.setOutput).toHaveBeenNthCalledWith(3, 'html_url', 'htmlUrl'); 173 | expect(core.setOutput).toHaveBeenNthCalledWith(4, 'upload_url', 'uploadUrl'); 174 | }); 175 | 176 | test('Action fails elegantly', async () => { 177 | core.getInput = jest 178 | .fn() 179 | .mockReturnValueOnce('refs/tags/v1.0.0') 180 | .mockReturnValueOnce('continuous') 181 | .mockReturnValueOnce('myRelease') 182 | .mockReturnValueOnce('myBody') 183 | .mockReturnValueOnce('false') 184 | .mockReturnValueOnce('false'); 185 | 186 | createRelease.mockRestore(); 187 | createRelease.mockImplementation(() => { 188 | throw new Error('Error creating release'); 189 | }); 190 | 191 | core.setOutput = jest.fn(); 192 | 193 | core.setFailed = jest.fn(); 194 | 195 | await run(); 196 | 197 | expect(createRelease).toHaveBeenCalled(); 198 | expect(core.setFailed).toHaveBeenCalledWith('Error creating release'); 199 | expect(core.setOutput).toHaveBeenCalledTimes(0); 200 | }); 201 | 202 | test('Auto increment set initial tag', async () => { 203 | core.getInput = jest 204 | .fn() 205 | .mockReturnValueOnce('') 206 | .mockReturnValueOnce('continuous') 207 | .mockReturnValueOnce('') 208 | .mockReturnValueOnce('myRelease') 209 | .mockReturnValueOnce('') // <-- The default value for body in action.yml 210 | .mockReturnValueOnce('false'); 211 | 212 | await run(); 213 | 214 | expect(createRelease).toHaveBeenCalledWith({ 215 | owner: 'owner', 216 | repo: 'repo', 217 | tag_name: 'v1', 218 | name: 'myRelease', 219 | body: '', 220 | draft: false, 221 | prerelease: false 222 | }); 223 | }); 224 | 225 | test('Auto increment with defaults', async () => { 226 | jest.resetModules(); 227 | mockValues([{ ref: 'v1.1.0' }]); 228 | core.getInput = jest 229 | .fn() 230 | .mockReturnValueOnce('') 231 | .mockReturnValueOnce('continuous') 232 | .mockReturnValueOnce('') 233 | .mockReturnValueOnce('') 234 | .mockReturnValueOnce('myRelease') 235 | .mockReturnValueOnce('') // <-- The default value for body in action.yml 236 | .mockReturnValueOnce('false'); 237 | 238 | await run(); 239 | 240 | expect(createRelease).toHaveBeenCalledWith({ 241 | owner: 'owner', 242 | repo: 'repo', 243 | tag_name: 'v2', 244 | name: 'myRelease', 245 | body: '', 246 | draft: false, 247 | prerelease: false 248 | }); 249 | }); 250 | 251 | test('Auto increment with prerelease version', async () => { 252 | jest.resetModules(); 253 | mockValues([{ ref: 'v1.1.0-pre.0' }]); 254 | core.getInput = jest 255 | .fn() 256 | .mockReturnValueOnce('') 257 | .mockReturnValueOnce('continuous') 258 | .mockReturnValueOnce('prerelease') 259 | .mockReturnValueOnce('myRelease') 260 | .mockReturnValueOnce('') // <-- The default value for body in action.yml 261 | .mockReturnValueOnce('false'); 262 | 263 | await run(); 264 | 265 | expect(createRelease).toHaveBeenCalledWith({ 266 | owner: 'owner', 267 | repo: 'repo', 268 | tag_name: 'v1-pre.1', 269 | name: 'myRelease', 270 | body: '', 271 | draft: false, 272 | prerelease: false 273 | }); 274 | }); 275 | 276 | test('Auto increment with prerelease version - semantic', async () => { 277 | jest.resetModules(); 278 | mockValues([{ ref: 'v1.0.0' }]); 279 | core.getInput = jest 280 | .fn() 281 | .mockReturnValueOnce('') 282 | .mockReturnValueOnce('semantic') 283 | .mockReturnValueOnce('prerelease') 284 | .mockReturnValueOnce('') 285 | .mockReturnValueOnce('myRelease') 286 | .mockReturnValueOnce('') // <-- The default value for body in action.yml 287 | .mockReturnValueOnce('false'); 288 | 289 | await run(); 290 | 291 | expect(createRelease).toHaveBeenCalledWith({ 292 | owner: 'owner', 293 | repo: 'repo', 294 | tag_name: 'v1.0.1-beta.0', 295 | name: 'myRelease', 296 | body: '', 297 | draft: false, 298 | prerelease: false 299 | }); 300 | }); 301 | 302 | test('Auto increment with premajor version - semantic', async () => { 303 | jest.resetModules(); 304 | mockValues([{ ref: 'v1.0.0' }]); 305 | core.getInput = jest 306 | .fn() 307 | .mockReturnValueOnce('') 308 | .mockReturnValueOnce('semantic') 309 | .mockReturnValueOnce('premajor') 310 | .mockReturnValueOnce('') 311 | .mockReturnValueOnce('myRelease') 312 | .mockReturnValueOnce('') // <-- The default value for body in action.yml 313 | .mockReturnValueOnce('false'); 314 | 315 | await run(); 316 | 317 | expect(createRelease).toHaveBeenCalledWith({ 318 | owner: 'owner', 319 | repo: 'repo', 320 | tag_name: 'v2.0.0-beta.0', 321 | name: 'myRelease', 322 | body: '', 323 | draft: false, 324 | prerelease: false 325 | }); 326 | }); 327 | 328 | test('Invalid last tag', async () => { 329 | jest.resetModules(); 330 | mockValues([{ ref: '$@#' }]); 331 | core.getInput = jest 332 | .fn() 333 | .mockReturnValueOnce('') 334 | .mockReturnValueOnce('semantic') 335 | .mockReturnValueOnce('xyz') 336 | .mockReturnValueOnce('') 337 | .mockReturnValueOnce('myRelease') 338 | .mockReturnValueOnce('') // <-- The default value for body in action.yml 339 | .mockReturnValueOnce('false'); 340 | 341 | await run(); 342 | 343 | expect(core.setFailed).toHaveBeenNthCalledWith(1, 'Failed to parse tag: $@#'); 344 | }); 345 | 346 | test('Unsupported auto increment type', async () => { 347 | jest.resetModules(); 348 | mockValues([{ ref: 'v1.1.0' }]); 349 | core.getInput = jest 350 | .fn() 351 | .mockReturnValueOnce('') 352 | .mockReturnValueOnce('semantic') 353 | .mockReturnValueOnce('xyz') 354 | .mockReturnValueOnce('') 355 | .mockReturnValueOnce('myRelease') 356 | .mockReturnValueOnce('') // <-- The default value for body in action.yml 357 | .mockReturnValueOnce('false'); 358 | 359 | await run(); 360 | 361 | expect(core.setFailed).toHaveBeenNthCalledWith( 362 | 1, 363 | 'Unsupported semantic version type xyz. Must be one of (major, minor, patch, premajor, prerelease)' 364 | ); 365 | }); 366 | }); 367 | --------------------------------------------------------------------------------