Skip to content

Add 'branch' option #25

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

Merged
merged 4 commits into from
Aug 10, 2021
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ The commit message to use when updating the diagram. Useful for skipping CI. For

Default: `Repo visualizer: updated diagram`

## `branch`

The branch name to push the diagram to (branch will be created if it does not yet exist).

For example: `diagram`

## Example usage

You'll need to run the `actions/checkout` Action beforehand, to check out the code.
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ inputs:
commit_message:
description: "The commit message to use when updating the diagram. Default: Repo visualizer: updated diagram"
required: false
branch:
description: "The branch name to push the diagram to (branch will be created if it does not yet exist). For example: diagram"
required: false
runs:
using: "node12"
main: "index.js"
Expand Down
2 changes: 1 addition & 1 deletion diagram.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 21 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17919,6 +17919,7 @@ var main = async () => {
const commitMessage = core.getInput("commit_message") || "Repo visualizer: updated diagram";
const excludedPathsString = core.getInput("excluded_paths") || "node_modules,bower_components,dist,out,build,eject,.next,.netlify,.yarn,.git,.vscode,package-lock.json,yarn.lock";
const excludedPaths = excludedPathsString.split(",").map((str) => str.trim());
const branch = core.getInput("branch");
const data = await processDir(`./`, excludedPaths);
const componentCodeString = import_server.default.renderToStaticMarkup(/* @__PURE__ */ import_react3.default.createElement(Tree, {
data,
Expand All @@ -17927,6 +17928,18 @@ var main = async () => {
}));
const outputFile = core.getInput("output_file") || "./diagram.svg";
await import_fs2.default.writeFileSync(outputFile, componentCodeString);
let doesBranchExist = true;
if (branch) {
await (0, import_exec.exec)("git", ["fetch"]);
try {
await (0, import_exec.exec)("git", ["rev-parse", "--verify", branch]);
await (0, import_exec.exec)("git", ["checkout", branch]);
} catch {
doesBranchExist = false;
core.info(`Branch ${branch} does not yet exist, creating ${branch}.`);
await (0, import_exec.exec)("git", ["checkout", "-b", branch]);
}
}
await (0, import_exec.exec)("git", ["add", outputFile]);
const diff = await execWithOutput("git", ["status", "--porcelain", outputFile]);
core.info(`diff: ${diff}`);
Expand All @@ -17935,7 +17948,14 @@ var main = async () => {
return;
}
await (0, import_exec.exec)("git", ["commit", "-m", commitMessage]);
await (0, import_exec.exec)("git", ["push"]);
if (doesBranchExist) {
await (0, import_exec.exec)("git", ["push"]);
} else {
await (0, import_exec.exec)("git", ["push", "--set-upstream", "origin", branch]);
}
if (branch) {
await (0, import_exec.exec)("git", "checkout", "-");
}
console.log("All set!");
};
main();
Expand Down
28 changes: 26 additions & 2 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const main = async () => {
const commitMessage = core.getInput("commit_message") || "Repo visualizer: updated diagram"
const excludedPathsString = core.getInput("excluded_paths") || "node_modules,bower_components,dist,out,build,eject,.next,.netlify,.yarn,.git,.vscode,package-lock.json,yarn.lock"
const excludedPaths = excludedPathsString.split(",").map(str => str.trim())
const branch = core.getInput("branch")
const data = await processDir(`./`, excludedPaths);

const componentCodeString = ReactDOMServer.renderToStaticMarkup(
Expand All @@ -37,6 +38,21 @@ const main = async () => {

await fs.writeFileSync(outputFile, componentCodeString)

let doesBranchExist = true

if (branch) {
await exec('git', ['fetch'])

try {
await exec('git', ['rev-parse', '--verify', branch])
await exec('git', ['checkout', branch])
} catch {
doesBranchExist = false
core.info(`Branch ${branch} does not yet exist, creating ${branch}.`)
await exec('git', ['checkout', '-b', branch])
}
}

await exec('git', ['add', outputFile])
const diff = await execWithOutput('git', ['status', '--porcelain', outputFile])
core.info(`diff: ${diff}`)
Expand All @@ -46,7 +62,16 @@ const main = async () => {
}

await exec('git', ['commit', '-m', commitMessage])
await exec('git', ['push'])

if (doesBranchExist) {
await exec('git', ['push'])
} else {
await exec('git', ['push', '--set-upstream', 'origin', branch])
}

if (branch) {
await exec('git', 'checkout', '-')
}

console.log("All set!")
}
Expand All @@ -73,4 +98,3 @@ function execWithOutput(command, args) {
}
})
}