Skip to content

Commit c84edde

Browse files
committedJun 5, 2020
Refactor the action and get it working
1 parent 37a6b58 commit c84edde

8 files changed

+1095
-103
lines changed
 

‎.eslintignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
node_modules/
1+
node_modules/
2+
dist/

‎.huskyrc.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
hooks: {
3+
"pre-commit": "npm run build; git add dist/index.js",
4+
},
5+
};

‎README.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
# commit-files
22
GitHub action to commit files
3+
4+
## Building the action
5+
6+
After you've made the neccesary changes you must build the action for them to useable.
7+
8+
Husky will automatically run this whenever you commit and commit the built changes as well.
9+
10+
However, if you ever want to build the action manually just run `npm run build` which will compile all the JS code into single `index.js` file, inside the `dist` folder.

‎action.yml

+5-17
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,12 @@ inputs:
77
commit_message:
88
description: Commit message against the commit
99
required: true
10-
branch:
11-
description: Branch to commit the file changes to
10+
github_username:
11+
description: GitHub username to commit via
12+
required: true
13+
github_token:
14+
description: GitHub token to authenticate the push
1215
required: true
13-
repository:
14-
description: Pepository to commit the file changes to
15-
required: false
16-
default: '.'
17-
commit_user_name:
18-
description: Name used for the commit user
19-
required: false
20-
default: GitHub Actions
21-
commit_user_email:
22-
description: Email address used for the commit user
23-
required: false
24-
default: actions@github.com
25-
debug:
26-
description: To return logs
27-
required: false
2816

2917
outputs:
3018
changes_commited:

‎dist/index.js

+53-38
Original file line numberDiff line numberDiff line change
@@ -437,56 +437,71 @@ exports.getState = getState;
437437

438438
const cmdExecutor = __webpack_require__(328);
439439
const githubAction = __webpack_require__(470);
440+
440441
const gitClient = cmdExecutor.git;
441442

443+
const getBranch = async () => {
444+
const githubRef = process.env.GITHUB_REF;
445+
446+
if (githubRef.search(/refs\/heads\//g) !== -1) {
447+
return githubRef.replace('refs/heads/', '');
448+
}
449+
throw new Error('Unable to retrieve branch name to commit to');
450+
};
451+
442452
const commitFiles = async () => {
443-
const activeChanges = await hasActiveChanges();
444-
445-
const commitMessage = githubAction.getInput("commit_message");
446-
const gitBranch = githubAction.getInput("branch");
447-
const debug = githubAction.getInput("debug");
448-
449-
if (debug) console.log(`Checking out to '${gitBranch}'`)
450-
if (debug) console.log(`Working directory: ${await cmdExecutor.pwd()}`);
451-
if (debug) console.log(`Current directory tree: ${await cmdExecutor.ls("-la")}`);
452-
if (debug) console.log(
453-
`Current branch: ${await gitClient["rev-parse"](
454-
"--abbrev-ref HEAD"
455-
)}`
456-
);
457-
458-
if (gitBranch) {
459-
await gitClient.fetch();
460-
await gitClient.checkout(`origin/${gitBranch}`);
453+
const commitMessage = githubAction.getInput('commit_message');
454+
const githubToken = githubAction.getInput('github_token');
455+
const githubUsername = githubAction.getInput('github_username');
456+
457+
const gitBranch = await getBranch().catch(githubAction.setFailed);
458+
const activeChanges = await gitClient.status('--porcelain');
459+
460+
const githubRepo = process.env.GITHUB_REPOSITORY;
461+
462+
// Skip step if no changes.
463+
if (!activeChanges) {
464+
githubAction.warning('No changes were found');
465+
process.exit(0); // Exit with success
461466
}
462467

463-
if (activeChanges) {
464-
if (!(await hasActiveChanges(1))) await gitClient.add("-A"); // Add all unstaged files if the changes aren't staged
465-
466-
await gitClient.commit(`-a -m ${commitMessage}`);
467-
await gitClient.push("-u");
468-
} else {
469-
githubAction.setOutput(
470-
"changes_commited",
471-
"No changes to be commited"
472-
);
473-
return false;
468+
await gitClient
469+
.remote(
470+
`set-url origin https://${githubUsername}:${githubToken}@github.com/${githubRepo}.git`,
471+
)
472+
.catch(githubAction.setFailed);
473+
474+
// Debug info to confirm it's all working correctly
475+
if (githubAction.isDebug()) {
476+
githubAction.debug(`Current branch: ${
477+
await gitClient['rev-parse']('--abbrev-ref HEAD')
478+
}`);
474479
}
475-
}
476480

477-
const hasActiveChanges = async (staged = 0) => {
478-
let commandArguments = '--quiet --ignore-submodules HEAD 2>/dev/null; echo $?';
481+
await gitClient.add('-A').catch(githubAction.setFailed);
479482

480-
if (staged) commandArguments = "--cached " + commandArguments;
483+
await gitClient
484+
.config(`--global user.name "${githubUsername}"`)
485+
.catch(githubAction.setFailed);
481486

482-
const changes = await gitClient["diff"](commandArguments);
487+
await gitClient
488+
.config('--global user.email "<>"')
489+
.catch(githubAction.setFailed);
483490

484-
return changes.trim() != 1 ? false : true
485-
}
491+
await gitClient
492+
.commit(`-m "${commitMessage}"`)
493+
.catch(githubAction.setFailed);
494+
495+
await gitClient
496+
.push(`--set-upstream origin ${gitBranch}`)
497+
.catch(githubAction.setFailed);
498+
499+
process.exit(0); // Exit with success
500+
};
486501

487502
module.exports = {
488-
commitFiles
489-
}
503+
commitFiles,
504+
};
490505

491506

492507
/***/ }),

‎lib/index.js

+54-39
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,67 @@
1-
const cmdExecutor = require("cmd-executor");
1+
const cmdExecutor = require('cmd-executor');
22
const githubAction = require('@actions/core');
3+
34
const gitClient = cmdExecutor.git;
45

6+
const getBranch = async () => {
7+
const githubRef = process.env.GITHUB_REF;
8+
9+
if (githubRef.search(/refs\/heads\//g) !== -1) {
10+
return githubRef.replace('refs/heads/', '');
11+
}
12+
throw new Error('Unable to retrieve branch name to commit to');
13+
};
14+
515
const commitFiles = async () => {
6-
const activeChanges = await hasActiveChanges();
7-
8-
const commitMessage = githubAction.getInput("commit_message");
9-
const gitBranch = githubAction.getInput("branch");
10-
const debug = githubAction.getInput("debug");
11-
12-
if (debug) console.log(`Checking out to '${gitBranch}'`)
13-
if (debug) console.log(`Working directory: ${await cmdExecutor.pwd()}`);
14-
if (debug) console.log(`Current directory tree: ${await cmdExecutor.ls("-la")}`);
15-
if (debug) console.log(
16-
`Current branch: ${await gitClient["rev-parse"](
17-
"--abbrev-ref HEAD"
18-
)}`
19-
);
20-
21-
if (gitBranch) {
22-
await gitClient.fetch();
23-
await gitClient.checkout(`origin/${gitBranch}`);
16+
const commitMessage = githubAction.getInput('commit_message');
17+
const githubToken = githubAction.getInput('github_token');
18+
const githubUsername = githubAction.getInput('github_username');
19+
20+
const gitBranch = await getBranch().catch(githubAction.setFailed);
21+
const activeChanges = await gitClient.status('--porcelain');
22+
23+
const githubRepo = process.env.GITHUB_REPOSITORY;
24+
25+
// Skip step if no changes.
26+
if (!activeChanges) {
27+
githubAction.warning('No changes were found');
28+
process.exit(0); // Exit with success
2429
}
2530

26-
if (activeChanges) {
27-
if (!(await hasActiveChanges(1))) await gitClient.add("-A"); // Add all unstaged files if the changes aren't staged
28-
29-
await gitClient.commit(`-a -m ${commitMessage}`);
30-
await gitClient.push("-u");
31-
} else {
32-
githubAction.setOutput(
33-
"changes_commited",
34-
"No changes to be commited"
35-
);
36-
return false;
31+
await gitClient
32+
.remote(
33+
`set-url origin https://${githubUsername}:${githubToken}@github.com/${githubRepo}.git`,
34+
)
35+
.catch(githubAction.setFailed);
36+
37+
// Debug info to confirm it's all working correctly
38+
if (githubAction.isDebug()) {
39+
githubAction.debug(`Current branch: ${
40+
await gitClient['rev-parse']('--abbrev-ref HEAD')
41+
}`);
3742
}
38-
}
3943

40-
const hasActiveChanges = async (staged = 0) => {
41-
let commandArguments = '--quiet --ignore-submodules HEAD 2>/dev/null; echo $?';
44+
await gitClient.add('-A').catch(githubAction.setFailed);
45+
46+
await gitClient
47+
.config(`--global user.name "${githubUsername}"`)
48+
.catch(githubAction.setFailed);
49+
50+
await gitClient
51+
.config('--global user.email "<>"')
52+
.catch(githubAction.setFailed);
4253

43-
if (staged) commandArguments = "--cached " + commandArguments;
54+
await gitClient
55+
.commit(`-m "${commitMessage}"`)
56+
.catch(githubAction.setFailed);
4457

45-
const changes = await gitClient["diff"](commandArguments);
58+
await gitClient
59+
.push(`--set-upstream origin ${gitBranch}`)
60+
.catch(githubAction.setFailed);
4661

47-
return changes.trim() != 1 ? false : true
48-
}
62+
process.exit(0); // Exit with success
63+
};
4964

5065
module.exports = {
51-
commitFiles
52-
}
66+
commitFiles,
67+
};

‎package-lock.json

+964-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@
2424
"homepage": "https://github.com/SpareRoom/commit-files#readme",
2525
"dependencies": {
2626
"@actions/core": "^1.2.4",
27+
"@actions/github": "^2.2.0",
2728
"cmd-executor": "^1.1.1"
2829
},
2930
"devDependencies": {
3031
"@zeit/ncc": "^0.22.3",
3132
"eslint": "^7.1.0",
32-
"eslint-config-airbnb-base": "^14.1.0"
33+
"eslint-config-airbnb-base": "^14.1.0",
34+
"eslint-plugin-import": "^2.20.2",
35+
"husky": "^4.2.5"
3336
}
3437
}

0 commit comments

Comments
 (0)
Please sign in to comment.