Skip to content

Commit cbce977

Browse files
Implement Pull Request Functionality (#10)
* Add some logging * Fix typo * More attemps at randomly fixing things * try creating branch from latest ref * Finally get things working * Updated names to make more sense * Update some docs
1 parent 2e6166d commit cbce977

File tree

4 files changed

+82
-19
lines changed

4 files changed

+82
-19
lines changed

action.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ inputs:
1616
description: 'The branch name to use for a pull request for your changelog (If `use_pullrequest` is true)'
1717
default_branch:
1818
required: false
19-
description: 'The branch name to commit / make pull request against for changelog'
19+
description: 'The branch name to commit / make pull request against for changelog if different than the tagged branch'
2020

2121
runs:
2222
using: 'node12'

dist/index.js

+40-9
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,9 @@ async function run() {
537537
changelogExists = false;
538538
}
539539

540-
let { url, tag, name, body } = getReleaseData(eventPath);
540+
let { url, tag, name, body, releaseBranch } = getReleaseData(eventPath);
541+
542+
let branch = defaultBranch || releaseBranch;
541543

542544
let newContents =
543545
`### [${name}](${url}) ${NEWLINE} **${tag}** ${NEWLINE} ${body}` + currentContents;
@@ -548,7 +550,7 @@ async function run() {
548550
owner,
549551
repo,
550552
path,
551-
branch: defaultBranch,
553+
branch,
552554
message: `Updated ${path} via Next Release action.`,
553555
content
554556
};
@@ -557,16 +559,31 @@ async function run() {
557559
options.sha = sha;
558560
}
559561

562+
let prBranch = core.getInput('branch_name') || `changelog-${tag}`;
563+
560564
if (usePr) {
561-
let branch = core.getInput('branch_name') || `changelog-${tag}`;
565+
let refSha;
566+
try {
567+
let { data } = await octokit.git.getRef({
568+
owner,
569+
repo,
570+
ref: `heads/${branch}`
571+
});
572+
573+
refSha = data.object.sha;
574+
} catch (e) {
575+
core.error(`Error creating changelog PR ${e}`);
576+
return;
577+
}
578+
562579
try {
563580
await octokit.git.createRef({
564581
owner,
565582
repo,
566-
sha,
567-
ref: `refs/heads/${branch}`
583+
sha: refSha,
584+
ref: `refs/heads/${prBranch}`
568585
});
569-
options.branch = branch;
586+
options.branch = prBranch;
570587
} catch (e) {
571588
core.error(`Failed creating changelog PR: ${e}`);
572589
return;
@@ -580,16 +597,30 @@ async function run() {
580597
}
581598

582599
if (usePr) {
583-
await octokit.repo.get();
600+
await octokit.pulls.create({
601+
owner,
602+
repo,
603+
head: prBranch,
604+
base: branch,
605+
title: `Update ${path}`,
606+
maintainer_can_modify: true,
607+
body: 'Automatically generated by Next Release Changelog Action'
608+
});
584609
}
585610
}
586611

587612
function getReleaseData(eventPath) {
588613
const event = JSON.parse(fs.readFileSync(eventPath, 'utf8'));
589614

590-
let { html_url: url, tag_name: tag, name, body } = event.release;
615+
let {
616+
html_url: url,
617+
tag_name: tag,
618+
name,
619+
body,
620+
target_commitish: releaseBranch
621+
} = event.release;
591622

592-
return { url, tag, name, body };
623+
return { url, tag, name, body, releaseBranch };
593624
}
594625

595626
run();

index.js

+40-9
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ async function run() {
4343
changelogExists = false;
4444
}
4545

46-
let { url, tag, name, body } = getReleaseData(eventPath);
46+
let { url, tag, name, body, releaseBranch } = getReleaseData(eventPath);
47+
48+
let branch = defaultBranch || releaseBranch;
4749

4850
let newContents =
4951
`### [${name}](${url}) ${NEWLINE} **${tag}** ${NEWLINE} ${body}` + currentContents;
@@ -54,7 +56,7 @@ async function run() {
5456
owner,
5557
repo,
5658
path,
57-
branch: defaultBranch,
59+
branch,
5860
message: `Updated ${path} via Next Release action.`,
5961
content
6062
};
@@ -63,16 +65,31 @@ async function run() {
6365
options.sha = sha;
6466
}
6567

68+
let prBranch = core.getInput('branch_name') || `changelog-${tag}`;
69+
6670
if (usePr) {
67-
let branch = core.getInput('branch_name') || `changelog-${tag}`;
71+
let refSha;
72+
try {
73+
let { data } = await octokit.git.getRef({
74+
owner,
75+
repo,
76+
ref: `heads/${branch}`
77+
});
78+
79+
refSha = data.object.sha;
80+
} catch (e) {
81+
core.error(`Error creating changelog PR ${e}`);
82+
return;
83+
}
84+
6885
try {
6986
await octokit.git.createRef({
7087
owner,
7188
repo,
72-
sha,
73-
ref: `refs/heads/${branch}`
89+
sha: refSha,
90+
ref: `refs/heads/${prBranch}`
7491
});
75-
options.branch = branch;
92+
options.branch = prBranch;
7693
} catch (e) {
7794
core.error(`Failed creating changelog PR: ${e}`);
7895
return;
@@ -86,16 +103,30 @@ async function run() {
86103
}
87104

88105
if (usePr) {
89-
await octokit.repo.get();
106+
await octokit.pulls.create({
107+
owner,
108+
repo,
109+
head: prBranch,
110+
base: branch,
111+
title: `Update ${path}`,
112+
maintainer_can_modify: true,
113+
body: 'Automatically generated by Next Release Changelog Action'
114+
});
90115
}
91116
}
92117

93118
function getReleaseData(eventPath) {
94119
const event = JSON.parse(fs.readFileSync(eventPath, 'utf8'));
95120

96-
let { html_url: url, tag_name: tag, name, body } = event.release;
121+
let {
122+
html_url: url,
123+
tag_name: tag,
124+
name,
125+
body,
126+
target_commitish: releaseBranch
127+
} = event.release;
97128

98-
return { url, tag, name, body };
129+
return { url, tag, name, body, releaseBranch };
99130
}
100131

101132
run();

workflow.example.yml

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ jobs:
1212
with:
1313
token: ${{ secrets.GITHUB_TOKEN }} # Required
1414
changelog: 'CHANGELOG.md' # optional, default: CHANGELOG.md
15+
use_pullrequest: false # default false, use true to create changelog in a PR

0 commit comments

Comments
 (0)