-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.js
More file actions
194 lines (188 loc) · 4.81 KB
/
github.js
File metadata and controls
194 lines (188 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env node
const fs = require("fs");
const yargs = require("yargs");
const CURRENT_VERSION = require("./package.json").version;
const { info, error, debug } = require("./src/log");
const {
init,
checkChangedOrDeletedFiles,
createCommitOnBranch,
checkIfBranchExists,
} = require("./index");
const commitCommand = "commit";
const branchCommand = "branch";
const knownCommands = [commitCommand, branchCommand];
const appendLineToFile = (filename, line) => {
try {
fs.appendFileSync(filename, `${line}\n`);
} catch (e) {
error(`Error appending line to file ${filename}: ${e.message}`);
throw e;
}
};
const writeResultToGithubOutputFile = (results) => {
if (!!process.env.GITHUB_OUTPUT) {
let line = "";
results.forEach((result) => {
line += `${result.label}=${result.value}\n`;
});
appendLineToFile(process.env.GITHUB_OUTPUT, line);
}
};
yargs
.command(
commitCommand,
"Create a commit on a branch",
(yargs) => {
yargs
.option("owner", {
describe: "Owner of the repository",
demandOption: true,
alias: "o",
type: "string",
})
.option("repo", {
describe: "Name of the repository",
demandOption: true,
alias: "r",
type: "string",
})
.option("branch", {
describe: "Name of the branch to commit to",
demandOption: true,
alias: "b",
type: "string",
})
.option("changed", {
describe: "Paths of new or modified files",
alias: "c",
type: "array",
})
.option("deleted", {
describe: "Paths of tracked deleted files",
alias: "d",
type: "array",
})
.option("commitMessage", {
describe: "Mandatory commit message",
demandOption: true,
alias: "m",
type: "string",
})
.option("commitDescription", {
describe: "Optional commit description",
type: "string",
})
.check((argv) => {
if (!argv.changed && !argv.deleted) {
throw new Error(
"Missing required argument: either specify changed or deleted files."
);
}
return checkChangedOrDeletedFiles(argv.changed, argv.deleted);
});
},
(argv) => {
const {
owner,
repo,
branch,
changed,
deleted,
commitMessage,
commitDescription,
} = argv;
debug("Passed args:", JSON.stringify(argv, null, 2));
createCommitOnBranch(
owner,
repo,
branch,
changed,
deleted,
commitMessage,
commitDescription
)
.then((response) => {
info(`Commit created: ${response.commitUrl}`);
writeResultToGithubOutputFile([
{
label: "command",
value: commitCommand,
},
{
label: "commitUrl",
value: response.commitUrl,
},
]);
})
.catch((err) => {
error("Failed to create commit:", err.message);
process.exit(1);
});
}
)
.command(
branchCommand,
"Check if a branch exists",
(yargs) => {
yargs
.option("owner", {
describe: "Owner of the repository",
demandOption: true,
alias: "o",
type: "string",
})
.option("repo", {
describe: "Name of the repository",
demandOption: true,
alias: "r",
type: "string",
})
.option("branch", {
describe: "Name of the branch to check for existence",
demandOption: true,
alias: "b",
type: "string",
});
},
(argv) => {
const { owner, repo, branch } = argv;
debug("Passed args:", JSON.stringify(argv, null, 2));
checkIfBranchExists(owner, repo, branch)
.then((response) => {
const n = response ? "a" : "no";
info(`Repository ${owner}/${repo} has ${n} branch named '${branch}'`);
writeResultToGithubOutputFile([
{
label: "command",
value: branchCommand,
},
{
label: "hasBranch",
value: n.toString(),
},
]);
})
.catch((err) => {
error("Failed to check if branch exists:", err.message);
process.exit(1);
});
}
)
.demandCommand()
.version(CURRENT_VERSION)
.alias({
h: "help",
v: "version",
})
.check((argv) => {
const cmd = argv._[0];
if (!knownCommands.includes(cmd)) {
throw new Error(`Unknown command: ${cmd}`);
}
return true;
})
.check(() => {
return init();
})
.help().argv;