Skip to content

Commit

Permalink
Fix: "Is on branch" detection on closed events (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarampampam authored Apr 19, 2023
1 parent 6c659c4 commit f43f24d
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 2 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to this package will be documented in this file.

The format is based on [Keep a Changelog][keepachangelog] and this project adheres to [Semantic Versioning][semver].

## v1.2.5

### Fixed

- Is on branch detection on `closed` events [#58]

[#58]:https://github.com/gacts/github-slug/issues/58

## v1.2.4

### Changed
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/env/names.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ module.exports = Object.freeze({
// tag is available for the event type, the variable will not exist
GITHUB_REF: 'GITHUB_REF',

// The type of ref that triggered the workflow run. Valid values are `branch` or `tag`
GITHUB_REF_TYPE: 'GITHUB_REF_TYPE',

// Only set for pull request events. The name of the head branch
GITHUB_HEAD_REF: 'GITHUB_HEAD_REF',

Expand Down
32 changes: 32 additions & 0 deletions src/export.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ describe('isOnBranch', () => {
giveEnv: [{name: envGithub.GITHUB_REF, value: 'refs/heads/foo/branch'}],
want: true,
},
{
name: 'on the branch (detected by GITHUB_REF_TYPE)',
giveEnv: [
{name: envGithub.GITHUB_REF, value: 'master'},
{name: envGithub.GITHUB_REF_TYPE, value: 'branch'},
],
want: true,
},
{
name: 'not on the branch',
giveEnv: [
{name: envGithub.GITHUB_REF, value: 'bar'},
{name: envGithub.GITHUB_REF_TYPE, value: 'foo'},
],
want: false,
},
{
name: 'on the tag',
giveEnv: [{name: envGithub.GITHUB_REF, value: 'refs/tags/foo-tag'}],
Expand Down Expand Up @@ -71,6 +87,22 @@ describe('isOnTag', () => {
giveEnv: [{name: envGithub.GITHUB_REF, value: 'refs/heads/foo/branch'}],
want: false,
},
{
name: 'on the tag (detected by GITHUB_REF_TYPE)',
giveEnv: [
{name: envGithub.GITHUB_REF, value: 'foo'},
{name: envGithub.GITHUB_REF_TYPE, value: 'tag'},
],
want: true,
},
{
name: 'not on the tag',
giveEnv: [
{name: envGithub.GITHUB_REF, value: 'bar'},
{name: envGithub.GITHUB_REF_TYPE, value: 'foo'},
],
want: false,
},
{
name: 'on the tag',
giveEnv: [{name: envGithub.GITHUB_REF, value: 'refs/tags/foo-tag'}],
Expand Down
16 changes: 16 additions & 0 deletions src/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ const separator = '/'
* @return {boolean}
*/
function isOnBranch() {
const githubRefType = getEnv(envGithub.GITHUB_REF_TYPE)

if (githubRefType !== undefined) {
if (githubRefType.trim().toLowerCase() === 'branch') {
return true
}
}

const githubRef = getEnv(envGithub.GITHUB_REF)

if (githubRef !== undefined) {
Expand All @@ -30,6 +38,14 @@ function isOnBranch() {
* @return {boolean}
*/
function isOnTag() {
const githubRefType = getEnv(envGithub.GITHUB_REF_TYPE)

if (githubRefType !== undefined) {
if (githubRefType.trim().toLowerCase() === 'tag') {
return true
}
}

const githubRef = getEnv(envGithub.GITHUB_REF)

if (githubRef !== undefined) {
Expand Down

0 comments on commit f43f24d

Please sign in to comment.