|
| 1 | +name: Altis Module Auto Tagging |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - v*-branch* |
| 7 | + |
| 8 | +jobs: |
| 9 | + release: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + steps: |
| 12 | + - uses: actions/checkout@v2 |
| 13 | + |
| 14 | + - run: npm install semver |
| 15 | + - uses: actions/github-script@v5 |
| 16 | + with: |
| 17 | + script: | |
| 18 | + const semver = require( 'semver' ); |
| 19 | + const [ owner, repo ] = '${{ github.repository }}'.split( '/' ); |
| 20 | + const [ |
| 21 | + ref, |
| 22 | + branch, |
| 23 | + branchVersion, |
| 24 | + isRC |
| 25 | + ] = '${{ github.ref }}'.match( /^refs\/heads\/(v(\d+)-branch(-rc)?)/ ); |
| 26 | + const tags = await github.paginate( github.rest.git.listMatchingRefs, { |
| 27 | + owner, |
| 28 | + repo, |
| 29 | + ref: `tags/${ branchVersion }${ isRC ? '' : '.0' }.`, |
| 30 | + } ); |
| 31 | +
|
| 32 | + // Start from lowest possible tag. |
| 33 | + const initialTag = isRC ? `${ branchVersion }.1.0-rc.1` : `${ branchVersion }.0.0-beta.1`; |
| 34 | +
|
| 35 | + // Find the latest tag from results. |
| 36 | + const newTag = tags.reduce( ( carry, tag ) => { |
| 37 | + const cleanTag = semver.clean( tag.ref.replace( 'refs/tags/', '' ) ); |
| 38 | + let nextTag = ''; |
| 39 | + if ( semver.prerelease( cleanTag ) ) { |
| 40 | + nextTag = semver.inc( cleanTag, 'prerelease' ); |
| 41 | + } else { |
| 42 | + nextTag = semver.inc( cleanTag, 'patch' ); |
| 43 | + } |
| 44 | + return semver.gt( nextTag, carry ) ? nextTag : carry; |
| 45 | + }, initialTag ); |
| 46 | +
|
| 47 | + const isPreRelease = semver.prerelease( newTag ); |
| 48 | +
|
| 49 | + // Don't make new Pre-Release tags once we already have one. |
| 50 | + if ( isPreRelease && ! isRC && newTag !== initialTag ) { |
| 51 | + return; |
| 52 | + } |
| 53 | +
|
| 54 | + const baseTag = `${ semver.major( newTag ) }.${ semver.minor( newTag ) }.${ semver.patch( newTag ) }`; |
| 55 | + let nameSuffix = ''; |
| 56 | + if ( isPreRelease ) { |
| 57 | + nameSuffix = isRC ? 'Preview Release' : isPreRelease.join( ' ' ); |
| 58 | + nameSuffix = ' ' + nameSuffix; |
| 59 | + } |
| 60 | +
|
| 61 | + // Create new tag. |
| 62 | + await github.request( `POST /repos/${{ github.repository }}/releases`, { |
| 63 | + name: `${ baseTag }${ nameSuffix }`, |
| 64 | + tag_name: newTag, |
| 65 | + target_commitish: branch, |
| 66 | + generate_release_notes: true, |
| 67 | + prerelease: !! isPreRelease, |
| 68 | + } ); |
0 commit comments