Skip to content

Commit aa98244

Browse files
author
Amelia Wattenberger
authored
Merge pull request #25 from RazvanDH/main
Add 'branch' option
2 parents 125a355 + d249862 commit aa98244

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-4
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ The commit message to use when updating the diagram. Useful for skipping CI. For
3636

3737
Default: `Repo visualizer: updated diagram`
3838

39+
## `branch`
40+
41+
The branch name to push the diagram to (branch will be created if it does not yet exist).
42+
43+
For example: `diagram`
44+
3945
## Example usage
4046

4147
You'll need to run the `actions/checkout` Action beforehand, to check out the code.

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: "The branch name to push the diagram to (branch will be created if it does not yet exist). For example: diagram"
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: 21 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,18 @@ var main = async () => {
1792717928
}));
1792817929
const outputFile = core.getInput("output_file") || "./diagram.svg";
1792917930
await import_fs2.default.writeFileSync(outputFile, componentCodeString);
17931+
let doesBranchExist = 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+
doesBranchExist = false;
17939+
core.info(`Branch ${branch} does not yet exist, creating ${branch}.`);
17940+
await (0, import_exec.exec)("git", ["checkout", "-b", branch]);
17941+
}
17942+
}
1793017943
await (0, import_exec.exec)("git", ["add", outputFile]);
1793117944
const diff = await execWithOutput("git", ["status", "--porcelain", outputFile]);
1793217945
core.info(`diff: ${diff}`);
@@ -17935,7 +17948,14 @@ var main = async () => {
1793517948
return;
1793617949
}
1793717950
await (0, import_exec.exec)("git", ["commit", "-m", commitMessage]);
17938-
await (0, import_exec.exec)("git", ["push"]);
17951+
if (doesBranchExist) {
17952+
await (0, import_exec.exec)("git", ["push"]);
17953+
} else {
17954+
await (0, import_exec.exec)("git", ["push", "--set-upstream", "origin", branch]);
17955+
}
17956+
if (branch) {
17957+
await (0, import_exec.exec)("git", "checkout", "-");
17958+
}
1793917959
console.log("All set!");
1794017960
};
1794117961
main();

src/index.jsx

Lines changed: 26 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,21 @@ const main = async () => {
3738

3839
await fs.writeFileSync(outputFile, componentCodeString)
3940

41+
let doesBranchExist = 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+
doesBranchExist = false
51+
core.info(`Branch ${branch} does not yet exist, creating ${branch}.`)
52+
await exec('git', ['checkout', '-b', branch])
53+
}
54+
}
55+
4056
await exec('git', ['add', outputFile])
4157
const diff = await execWithOutput('git', ['status', '--porcelain', outputFile])
4258
core.info(`diff: ${diff}`)
@@ -46,7 +62,16 @@ const main = async () => {
4662
}
4763

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

5176
console.log("All set!")
5277
}
@@ -73,4 +98,3 @@ function execWithOutput(command, args) {
7398
}
7499
})
75100
}
76-

0 commit comments

Comments
 (0)