Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Pull Request Functionality #10

Merged
merged 7 commits into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
49 changes: 40 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -548,7 +550,7 @@ async function run() {
owner,
repo,
path,
branch: defaultBranch,
branch,
message: `Updated ${path} via Next Release action.`,
content
};
Expand All @@ -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;
Expand All @@ -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();
Expand Down
49 changes: 40 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -54,7 +56,7 @@ async function run() {
owner,
repo,
path,
branch: defaultBranch,
branch,
message: `Updated ${path} via Next Release action.`,
content
};
Expand All @@ -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;
Expand All @@ -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();
1 change: 1 addition & 0 deletions workflow.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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