diff --git a/action.yml b/action.yml index e39e786..7a04c45 100644 --- a/action.yml +++ b/action.yml @@ -16,7 +16,7 @@ inputs: description: 'The branch name to use for a pull request for your changelog (If `use_pullrequest` is true)' default_branch: required: false - description: 'The branch name to commit / make pull request against for changelog' + description: 'The branch name to commit / make pull request against for changelog if different than the tagged branch' runs: using: 'node12' diff --git a/dist/index.js b/dist/index.js index 95129b7..2993487 100644 --- a/dist/index.js +++ b/dist/index.js @@ -537,7 +537,9 @@ async function run() { changelogExists = false; } - let { url, tag, name, body } = getReleaseData(eventPath); + let { url, tag, name, body, releaseBranch } = getReleaseData(eventPath); + + let branch = defaultBranch || releaseBranch; let newContents = `### [${name}](${url}) ${NEWLINE} **${tag}** ${NEWLINE} ${body}` + currentContents; @@ -548,7 +550,7 @@ async function run() { owner, repo, path, - branch: defaultBranch, + branch, message: `Updated ${path} via Next Release action.`, content }; @@ -557,16 +559,31 @@ async function run() { options.sha = sha; } + let prBranch = core.getInput('branch_name') || `changelog-${tag}`; + if (usePr) { - let branch = core.getInput('branch_name') || `changelog-${tag}`; + let refSha; + try { + let { data } = await octokit.git.getRef({ + owner, + repo, + ref: `heads/${branch}` + }); + + refSha = data.object.sha; + } catch (e) { + core.error(`Error creating changelog PR ${e}`); + return; + } + try { await octokit.git.createRef({ owner, repo, - sha, - ref: `refs/heads/${branch}` + sha: refSha, + ref: `refs/heads/${prBranch}` }); - options.branch = branch; + options.branch = prBranch; } catch (e) { core.error(`Failed creating changelog PR: ${e}`); return; @@ -580,16 +597,30 @@ async function run() { } if (usePr) { - await octokit.repo.get(); + await octokit.pulls.create({ + owner, + repo, + head: prBranch, + base: branch, + title: `Update ${path}`, + maintainer_can_modify: true, + body: 'Automatically generated by Next Release Changelog Action' + }); } } function getReleaseData(eventPath) { const event = JSON.parse(fs.readFileSync(eventPath, 'utf8')); - let { html_url: url, tag_name: tag, name, body } = event.release; + let { + html_url: url, + tag_name: tag, + name, + body, + target_commitish: releaseBranch + } = event.release; - return { url, tag, name, body }; + return { url, tag, name, body, releaseBranch }; } run(); diff --git a/index.js b/index.js index 2cae308..c3d1b21 100644 --- a/index.js +++ b/index.js @@ -43,7 +43,9 @@ async function run() { changelogExists = false; } - let { url, tag, name, body } = getReleaseData(eventPath); + let { url, tag, name, body, releaseBranch } = getReleaseData(eventPath); + + let branch = defaultBranch || releaseBranch; let newContents = `### [${name}](${url}) ${NEWLINE} **${tag}** ${NEWLINE} ${body}` + currentContents; @@ -54,7 +56,7 @@ async function run() { owner, repo, path, - branch: defaultBranch, + branch, message: `Updated ${path} via Next Release action.`, content }; @@ -63,16 +65,31 @@ async function run() { options.sha = sha; } + let prBranch = core.getInput('branch_name') || `changelog-${tag}`; + if (usePr) { - let branch = core.getInput('branch_name') || `changelog-${tag}`; + let refSha; + try { + let { data } = await octokit.git.getRef({ + owner, + repo, + ref: `heads/${branch}` + }); + + refSha = data.object.sha; + } catch (e) { + core.error(`Error creating changelog PR ${e}`); + return; + } + try { await octokit.git.createRef({ owner, repo, - sha, - ref: `refs/heads/${branch}` + sha: refSha, + ref: `refs/heads/${prBranch}` }); - options.branch = branch; + options.branch = prBranch; } catch (e) { core.error(`Failed creating changelog PR: ${e}`); return; @@ -86,16 +103,30 @@ async function run() { } if (usePr) { - await octokit.repo.get(); + await octokit.pulls.create({ + owner, + repo, + head: prBranch, + base: branch, + title: `Update ${path}`, + maintainer_can_modify: true, + body: 'Automatically generated by Next Release Changelog Action' + }); } } function getReleaseData(eventPath) { const event = JSON.parse(fs.readFileSync(eventPath, 'utf8')); - let { html_url: url, tag_name: tag, name, body } = event.release; + let { + html_url: url, + tag_name: tag, + name, + body, + target_commitish: releaseBranch + } = event.release; - return { url, tag, name, body }; + return { url, tag, name, body, releaseBranch }; } run(); diff --git a/workflow.example.yml b/workflow.example.yml index 95779b6..1f75c57 100644 --- a/workflow.example.yml +++ b/workflow.example.yml @@ -12,3 +12,4 @@ jobs: with: token: ${{ secrets.GITHUB_TOKEN }} # Required changelog: 'CHANGELOG.md' # optional, default: CHANGELOG.md + use_pullrequest: false # default false, use true to create changelog in a PR