Skip to content

Commit

Permalink
Add support for / in branch names
Browse files Browse the repository at this point in the history
Author:    Niels ten Boom <[email protected]>
Date:      Sun Feb 5 11:09:00 2023 +0100
  • Loading branch information
nielstenboom authored and metcalfc committed Mar 13, 2023
1 parent 1999345 commit f624ce0
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 18 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ The name of the head reference. Default `${{github.sha}}`.

The name of the second branch. Defaults to the `tag_name` of the latest GitHub release. *This must be a GitHub release. Git tags or branches will not work.*

### `reverse`

Whether the order of commits should be printed in reverse. Default: 'false'

### `fetch`

Whether this action should pull in all other branches and tags. Default: 'true'

## Outputs

### `changelog`
Expand Down Expand Up @@ -58,6 +66,25 @@ Or, if you have two specific references you want:
base-ref: 'v0.0.1'
```
If you want to point to a branch containing forward slashes (https://github.com/metcalfc/changelog-generator/issues/179) do the following:
```yaml

# let the checkout action do the fetching
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Generate changelog
id: changelog
uses: metcalfc/[email protected] #TODO: bump this after release
with:
myToken: ${{ secrets.GITHUB_TOKEN }}
head-ref: 'origin/my/branch/with/slashes' #add 'origin/` in front of your branch name
base-ref: 'v1.0.0'
fetch: false
```
### Second block
Then you can use the resulting changelog:
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ inputs:
description: 'Git log is chronological order by default. Set to true to reverse chronological order. '
default: 'false'
required: false
fetch:
description: 'Whether to have this action fetch all other branches and tags'
default: 'true'
required: false
outputs:
changelog:
description: 'Markdown formatted changelog'
Expand Down
12 changes: 8 additions & 4 deletions changelog.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ if [ "$4" == "true" ]; then
extra_flags='--reverse'
fi

fetch=$5

# By default a GitHub action checkout is shallow. Get all the tags, branches,
# and history. Redirect output to standard error which we can collect in the
# action.
git fetch --depth=1 origin +refs/tags/*:refs/tags/* 1>&2
git fetch --no-tags --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/* 1>&2
git fetch --prune --unshallow 1>&2
if [ "$fetch" == "true" ]; then
git fetch --depth=1 origin +refs/tags/*:refs/tags/* 1>&2
git fetch --no-tags --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/* 1>&2
git fetch --prune --unshallow 1>&2
fi

# if folks don't have a base ref to compare against just use the initial
# commit. This will show all the changes since the beginning but I can't
Expand All @@ -28,7 +32,7 @@ fi
# Bash quoting will get you. Do not quote the extra_flags. If its null
# we want it to disappear. If you quote it, it will go to git as an ""
# and thats not a valid arg.
log=$(git log "${base_ref}...${head_ref}" \
log=$(git log "${base_ref}"..."${head_ref}" \
--pretty=format:"- [%h](http://github.com/${repo_url}/commit/%H) - %s" \
${extra_flags})

Expand Down
12 changes: 8 additions & 4 deletions dist/changelog.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ if [ "$4" == "true" ]; then
extra_flags='--reverse'
fi

fetch=$5

# By default a GitHub action checkout is shallow. Get all the tags, branches,
# and history. Redirect output to standard error which we can collect in the
# action.
git fetch --depth=1 origin +refs/tags/*:refs/tags/* 1>&2
git fetch --no-tags --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/* 1>&2
git fetch --prune --unshallow 1>&2
if [ "$fetch" == "true" ]; then
git fetch --depth=1 origin +refs/tags/*:refs/tags/* 1>&2
git fetch --no-tags --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/* 1>&2
git fetch --prune --unshallow 1>&2
fi

# if folks don't have a base ref to compare against just use the initial
# commit. This will show all the changes since the beginning but I can't
Expand All @@ -28,7 +32,7 @@ fi
# Bash quoting will get you. Do not quote the extra_flags. If its null
# we want it to disappear. If you quote it, it will go to git as an ""
# and thats not a valid arg.
log=$(git log "${base_ref}...${head_ref}" \
log=$(git log "${base_ref}"..."${head_ref}" \
--pretty=format:"- [%h](http://github.com/${repo_url}/commit/%H) - %s" \
${extra_flags})

Expand Down
11 changes: 6 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11641,9 +11641,10 @@ async function run() {
var baseRef = (0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput)('base-ref')
const myToken = (0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput)('myToken')
const reverse = (0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput)('reverse')
const fetch = (0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput)('fetch')
const octokit = new _actions_github__WEBPACK_IMPORTED_MODULE_2__.getOctokit(myToken)
const { owner, repo } = _actions_github__WEBPACK_IMPORTED_MODULE_2__.context.repo
const regexp = /^[.A-Za-z0-9_-]*$/
const regexp = /^[.A-Za-z0-9_/-]*$/

if (!headRef) {
headRef = _actions_github__WEBPACK_IMPORTED_MODULE_2__.context.sha
Expand Down Expand Up @@ -11672,18 +11673,18 @@ async function run() {
regexp.test(headRef) &&
regexp.test(baseRef)
) {
getChangelog(headRef, baseRef, owner + '/' + repo, reverse)
getChangelog(headRef, baseRef, owner + '/' + repo, reverse, fetch)
} else {
(0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.setFailed)(
'Branch names must contain only numbers, strings, underscores, periods, and dashes.'
'Branch names must contain only numbers, strings, underscores, periods, forward slashes, and dashes.'
)
}
} catch (error) {
(0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.setFailed)(error.message)
}
}

async function getChangelog(headRef, baseRef, repoName, reverse) {
async function getChangelog(headRef, baseRef, repoName, reverse, fetch) {
try {
let output = ''
let err = ''
Expand All @@ -11702,7 +11703,7 @@ async function getChangelog(headRef, baseRef, repoName, reverse) {

await (0,_actions_exec__WEBPACK_IMPORTED_MODULE_1__.exec)(
__nccwpck_require__.ab + "changelog.sh",
[headRef, baseRef, repoName, reverse],
[headRef, baseRef, repoName, reverse, fetch],
options
)

Expand Down
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ async function run() {
var baseRef = getInput('base-ref')
const myToken = getInput('myToken')
const reverse = getInput('reverse')
const fetch = getInput('fetch')
const octokit = new getOctokit(myToken)
const { owner, repo } = context.repo
const regexp = /^[.A-Za-z0-9_-]*$/
const regexp = /^[.A-Za-z0-9_/-]*$/

if (!headRef) {
headRef = context.sha
Expand Down Expand Up @@ -41,18 +42,18 @@ async function run() {
regexp.test(headRef) &&
regexp.test(baseRef)
) {
getChangelog(headRef, baseRef, owner + '/' + repo, reverse)
getChangelog(headRef, baseRef, owner + '/' + repo, reverse, fetch)
} else {
setFailed(
'Branch names must contain only numbers, strings, underscores, periods, and dashes.'
'Branch names must contain only numbers, strings, underscores, periods, forward slashes, and dashes.'
)
}
} catch (error) {
setFailed(error.message)
}
}

async function getChangelog(headRef, baseRef, repoName, reverse) {
async function getChangelog(headRef, baseRef, repoName, reverse, fetch) {
try {
let output = ''
let err = ''
Expand All @@ -71,7 +72,7 @@ async function getChangelog(headRef, baseRef, repoName, reverse) {

await _exec(
`${src}/changelog.sh`,
[headRef, baseRef, repoName, reverse],
[headRef, baseRef, repoName, reverse, fetch],
options
)

Expand Down

0 comments on commit f624ce0

Please sign in to comment.