forked from gha-utilities/workspace-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
243 lines (194 loc) · 6.03 KB
/
index.js
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/env node
'use strict';
const child_process = require('child_process');
const fs = require('fs');
const process = require('process');
/**
* @function get_gha_input
* @param {string} name
* @return {string?}
*/
const get_gha_input = (name) => {
return process.env[`INPUT_${name.toUpperCase()}`];
};
/**
* @function get_git_config
* @param {string} key
* @return {string?}
*/
const get_git_config = (key) => {
try {
return child_process.execSync(`git config ${key}`, { encoding: 'utf8' });
} catch (error) {
const error_message = [
`Error attempting to get: git config ${key}`,
`Status code -> ${error.status}`,
`Error message -> ${error.message}`,
`Standard error -> ${error.stderr}`,
`Standard out -> ${error.stdout}`,
];
console.error(error_message.join('\n'));
throw error;
}
};
/**
* @function set_git_config
* @param {string} key
* @param {string} value
*/
const set_git_config = (key, value) => {
try {
return child_process.execSync(`git config ${key} ${value}`, { encoding: 'utf8' });
} catch (error) {
const error_message = [
`Error attempting to set: git config ${key} ${value}`,
`Status code -> ${e.status}`,
`Error message -> ${e.message}`,
`Standard error -> ${e.stderr}`,
`Standard out -> ${e.stdout}`,
];
console.error(error_message.join('\n'));
throw error;
}
};
/**
* @function git_commit
* @param {string} obj.commit_message
* @param {string[]} obj.files_list
*/
const git_commit = ({commit_message, files_list}) => {
try {
if (files_list) {
return child_process.execSync(
`git commit -m "${commit_message}" "${files_list.join(' ')}"`,
{ encoding: 'utf8' },
);
} else {
return child_process.execSync(
`git commit commit -m "${commit_message}"`,
{ encoding: 'utf8' },
);
}
} catch (e) {
const error_message = [
'Error attempting to commit...',
`Status code -> ${e.status}`,
`Error message -> ${e.message}`,
`Standard error -> ${e.stderr}`,
`Standard out -> ${e.stdout}`,
''
];
console.error(error_message.join('\n'));
throw e;
}
};
const actor = process.env.GITHUB_ACTOR;
const repository = process.env.GITHUB_REPOSITORY.split('/')[1];
if (actor === undefined || repository === undefined) {
const error_message = ['Environment variable `GITHUB_ACTOR` or `GITHUB_REPOSITORY` is undefined',
'Please ensure that you are testing with something like...',
' GITHUB_ACTOR="your-name" GITHUB_REPOSITORY="repo-name" node index.js',
'',
'... hint if errors about undefined Inputs then pop, try...',
' GITHUB_ACTOR=your-name\\',
' node index.js',
'',
];
throw new ReferenceError(error_message.join('\n'));
}
const error_message__base = ['Please check that your Workflow file looks similar to...'];
const gha_example = [' - name: Workspace Commit',
' uses: gha-utilities/workspace-commit@master',
' with:',
];
const commit_message = get_gha_input('message');
if (!commit_message) {
const error_message = ['No commit message provided!',
...error_message__base,
...gha_example,
' commit: Adds or alters files',
''
];
throw new ReferenceError(error_message.join('\n'));
}
const staged_files = child_process.execSync('git diff --name-only --cached', {encoding: 'utf8'});
if (!staged_files) {
const error_message = ['No files staged to commit!',
'Please stage changes before attempting to commit, eg...',
' git add README.md some-script.ext etc',
'',
];
throw new ReferenceError(error_message.join('\n'));
}
const input_paths = get_gha_input('paths');
const files_list = [];
staged_files.split(/\r?\n/).forEach((path) => {
if (path) {
if (get_gha_input('all')) {
files_list.push(path);
} else if (!input_paths) {
const error_message = [
'No files or directories provided!',
...error_message__base,
...gha_example,
' paths: |',
' README.md',
' index.js',
' etc',
''
];
throw new ReferenceError(error_message.join('\n'));
} else if (input_paths.includes(path)) {
files_list.push(path);
} else {
console.warn(`Skipping un-staged file -> ${path}`);
}
}
});
/**
* Set git config user.name
*/
let commit_author = 'gha-utilities';
const git_config__user_name = get_git_config('user.name');
let git_config__user_name__was_set = false;
if (![undefined, '', '\n'].includes(git_config__user_name)) {
git_config__user_name__was_set = true;
commit_author = git_config__user_name;
}
const action_config__author = get_gha_input('author');
if (action_config__author !== undefined) {
commit_author = action_config__author;
}
set_git_config('user.name', commit_author);
/**
* Set git config user.email
*/
let commit_email = '[email protected]';
const git_config__user_email = get_git_config('user.email');
let git_config__user_email__was_set = false;
if (![undefined, '', '\n'].includes(git_config__user_email)) {
git_config__user_email__was_set = true;
commit_email = git_config__user_email;
}
const action_config__email = get_gha_input('email');
if (action_config__email !== undefined) {
commit_email = action_config__email;
}
set_git_config('user.email', commit_email);
/**
* Do the git commit
*/
const commit_results = git_commit({commit_message, files_list});
console.log(`commit_results -> ${commit_results}`);
/**
* Revert git config user.name
*/
if (git_config__user_name__was_set) {
set_git_config('user.name', git_config__user_name);
}
/**
* Revert git config user.name
*/
if (git_config__user_email__was_set) {
set_git_config('user.email', git_config__user_email);
}