Skip to content

Commit 9c635f2

Browse files
RazvanDHrepo-visualizer
authored andcommitted
Add branch option
This is mostly needed for protected branches. If you want to push the diagram to a separate branch, you can pass the branch option. The script will check if that branch exists and push the diagram there or create the branch and push the diagram!
1 parent 125a355 commit 9c635f2

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ inputs:
1414
commit_message:
1515
description: "The commit message to use when updating the diagram. Default: Repo visualizer: updated diagram"
1616
required: false
17+
branch:
18+
description: "If you want to push the diagram to a different branch (branch will be created if it does not exist)"
19+
required: false
1720
runs:
1821
using: "node12"
1922
main: "index.js"

diagram.svg

Lines changed: 1 addition & 1 deletion
Loading

index.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17919,6 +17919,7 @@ var main = async () => {
1791917919
const commitMessage = core.getInput("commit_message") || "Repo visualizer: updated diagram";
1792017920
const excludedPathsString = core.getInput("excluded_paths") || "node_modules,bower_components,dist,out,build,eject,.next,.netlify,.yarn,.git,.vscode,package-lock.json,yarn.lock";
1792117921
const excludedPaths = excludedPathsString.split(",").map((str) => str.trim());
17922+
const branch = core.getInput("branch");
1792217923
const data = await processDir(`./`, excludedPaths);
1792317924
const componentCodeString = import_server.default.renderToStaticMarkup(/* @__PURE__ */ import_react3.default.createElement(Tree, {
1792417925
data,
@@ -17927,6 +17928,17 @@ var main = async () => {
1792717928
}));
1792817929
const outputFile = core.getInput("output_file") || "./diagram.svg";
1792917930
await import_fs2.default.writeFileSync(outputFile, componentCodeString);
17931+
let branchExists = true;
17932+
if (branch) {
17933+
await (0, import_exec.exec)("git", ["fetch"]);
17934+
try {
17935+
await (0, import_exec.exec)("git", ["rev-parse", "--verify", branch]);
17936+
await (0, import_exec.exec)("git", ["checkout", branch]);
17937+
} catch {
17938+
branchExists = false;
17939+
await (0, import_exec.exec)("git", ["checkout", "-b", branch]);
17940+
}
17941+
}
1793017942
await (0, import_exec.exec)("git", ["add", outputFile]);
1793117943
const diff = await execWithOutput("git", ["status", "--porcelain", outputFile]);
1793217944
core.info(`diff: ${diff}`);
@@ -17935,7 +17947,14 @@ var main = async () => {
1793517947
return;
1793617948
}
1793717949
await (0, import_exec.exec)("git", ["commit", "-m", commitMessage]);
17938-
await (0, import_exec.exec)("git", ["push"]);
17950+
if (branchExists) {
17951+
await (0, import_exec.exec)("git", ["push"]);
17952+
} else {
17953+
await (0, import_exec.exec)("git", ["push", "--set-upstream", "origin", branch]);
17954+
}
17955+
if (branch) {
17956+
await (0, import_exec.exec)("git", "checkout", "-");
17957+
}
1793917958
console.log("All set!");
1794017959
};
1794117960
main();

src/index.jsx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const main = async () => {
2727
const commitMessage = core.getInput("commit_message") || "Repo visualizer: updated diagram"
2828
const excludedPathsString = core.getInput("excluded_paths") || "node_modules,bower_components,dist,out,build,eject,.next,.netlify,.yarn,.git,.vscode,package-lock.json,yarn.lock"
2929
const excludedPaths = excludedPathsString.split(",").map(str => str.trim())
30+
const branch = core.getInput("branch")
3031
const data = await processDir(`./`, excludedPaths);
3132

3233
const componentCodeString = ReactDOMServer.renderToStaticMarkup(
@@ -37,6 +38,20 @@ const main = async () => {
3738

3839
await fs.writeFileSync(outputFile, componentCodeString)
3940

41+
let branchExists = true
42+
43+
if (branch) {
44+
await exec('git', ['fetch'])
45+
46+
try {
47+
await exec('git', ['rev-parse', '--verify', branch])
48+
await exec('git', ['checkout', branch])
49+
} catch {
50+
branchExists = false
51+
await exec('git', ['checkout', '-b', branch])
52+
}
53+
}
54+
4055
await exec('git', ['add', outputFile])
4156
const diff = await execWithOutput('git', ['status', '--porcelain', outputFile])
4257
core.info(`diff: ${diff}`)
@@ -46,7 +61,16 @@ const main = async () => {
4661
}
4762

4863
await exec('git', ['commit', '-m', commitMessage])
49-
await exec('git', ['push'])
64+
65+
if (branchExists) {
66+
await exec('git', ['push'])
67+
} else {
68+
await exec('git', ['push', '--set-upstream', 'origin', branch])
69+
}
70+
71+
if (branch) {
72+
await exec('git', 'checkout', '-')
73+
}
5074

5175
console.log("All set!")
5276
}
@@ -73,4 +97,3 @@ function execWithOutput(command, args) {
7397
}
7498
})
7599
}
76-

0 commit comments

Comments
 (0)