Skip to content

Commit ba5d7c1

Browse files
authored
Merge pull request #2 from pirafrank/dev
v0.1.2
2 parents 94bba48 + 6052523 commit ba5d7c1

File tree

16 files changed

+360
-36
lines changed

16 files changed

+360
-36
lines changed

.dockerignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
node_modules
2+
.gitignore
3+
*.test.js
4+
jest.config.js
5+
.docker
6+
.github
7+
.vscode
8+
dummy
9+
.env
10+
.env*

.env.sh.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export GITHUB_TOKEN="gh123abc"

.github/workflows/pr.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,25 @@ jobs:
3636
run: npm run test
3737
env:
3838
GITHUB_TOKEN: ${{ secrets.GH_TKN }}
39+
40+
# docs: https://github.com/marketplace/actions/build-and-push-docker-images
41+
- name: Set up QEMU
42+
uses: docker/setup-qemu-action@v3
43+
44+
- name: Set up Docker Buildx
45+
uses: docker/setup-buildx-action@v3
46+
47+
# docs: https://docs.docker.com/build/ci/github-actions/test-before-push/
48+
- name: Build Docker image
49+
uses: docker/build-push-action@v5
50+
with:
51+
push: false
52+
context: .
53+
load: true
54+
tags: github-graphql-client:latest
55+
56+
- name: Run Docker tests
57+
env:
58+
GITHUB_TOKEN: ${{ secrets.GH_TKN }}
59+
run: npm run docker:test
60+
continue-on-error: true

.vscode/launch.json

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,54 @@
2828
"--help"
2929
]
3030
},
31+
{
32+
"type": "node",
33+
"request": "launch",
34+
"name": "commit good branch",
35+
"skipFiles": [
36+
"<node_internals>/**"
37+
],
38+
"cwd": "${workspaceFolder}",
39+
"program": "${workspaceFolder}/github.js",
40+
"args": [
41+
"commit",
42+
"--owner",
43+
"pirafrank",
44+
"--repo",
45+
"test-repo",
46+
"--branch",
47+
"main",
48+
"-c",
49+
"dummy/file1.txt",
50+
"-m",
51+
"this is a commit msg"
52+
],
53+
"envFile": "${workspaceFolder}/.env"
54+
},
55+
{
56+
"type": "node",
57+
"request": "launch",
58+
"name": "commit BAD branch",
59+
"skipFiles": [
60+
"<node_internals>/**"
61+
],
62+
"cwd": "${workspaceFolder}",
63+
"program": "${workspaceFolder}/github.js",
64+
"args": [
65+
"commit",
66+
"--owner",
67+
"pirafrank",
68+
"--repo",
69+
"test-repo",
70+
"--branch",
71+
"not-main",
72+
"-c",
73+
"dummy/file1.txt",
74+
"-m",
75+
"this is a commit msg"
76+
],
77+
"envFile": "${workspaceFolder}/.env"
78+
},
3179
{
3280
"type": "node",
3381
"request": "launch",
@@ -38,9 +86,31 @@
3886
"program": "${workspaceFolder}/github.js",
3987
"args": [
4088
"branch",
41-
"--owner", "pirafrank",
42-
"--repo", "test-repo",
43-
"--branch", "main"
89+
"--owner",
90+
"pirafrank",
91+
"--repo",
92+
"test-repo",
93+
"--branch",
94+
"main"
95+
],
96+
"envFile": "${workspaceFolder}/.env"
97+
},
98+
{
99+
"type": "node",
100+
"request": "launch",
101+
"name": "branch does NOT exist",
102+
"skipFiles": [
103+
"<node_internals>/**"
104+
],
105+
"program": "${workspaceFolder}/github.js",
106+
"args": [
107+
"branch",
108+
"--owner",
109+
"pirafrank",
110+
"--repo",
111+
"test-repo",
112+
"--branch",
113+
"not-main"
44114
],
45115
"envFile": "${workspaceFolder}/.env"
46116
}

Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ ENV DEBIAN_FRONTEND=noninteractive
66
COPY . /app/
77
COPY ./entrypoint.sh /entrypoint.sh
88

9+
# IMPORTANT:
10+
#
11+
# GitHub sets the working directory path in the GITHUB_WORKSPACE
12+
# environment variable. It's recommended to not use the WORKDIR
13+
# instruction in your Dockerfile
14+
#
15+
# docs: https://docs.github.com/en/actions/creating-actions/dockerfile-support-for-github-actions#workdir
16+
917
RUN cd /app && npm install --omit=dev
1018

1119
ENTRYPOINT ["/entrypoint.sh"]

action.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ inputs:
88
description: 'Arguments to pass to the Dockerfile entrypoint.'
99
required: true
1010
default: '--help'
11+
debug:
12+
description: 'Whether to enable debug mode.'
13+
required: false
14+
default: 'false'
1115
outputs:
1216
command:
1317
description: 'The command that was executed.'
@@ -18,7 +22,8 @@ outputs:
1822
runs:
1923
using: 'docker'
2024
image: 'Dockerfile'
21-
#env:
25+
env:
26+
DEBUG: ${{ inputs.debug }}
2227
# GITHUB_TOKEN is already available in the action container context
2328
# but be sure that it has 'writer' permissions on the target repository.
2429
args:

entrypoint.sh

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
#!/usr/bin/env bash
22

33
set -e
4-
argv=(node /app/github.js "$@")
5-
cmd=$(printf '%q ' "${argv[@]}")
6-
eval $cmd
4+
5+
if [ -z "$GITHUB_TOKEN" ]; then
6+
echo "GITHUB_TOKEN is not set. Exiting."
7+
exit 1
8+
fi
9+
10+
if [[ "$DEBUG" == "true" ]]; then
11+
echo "first arg"
12+
echo $1
13+
echo "all args:"
14+
echo $@
15+
fi
16+
17+
eval "node /app/github.js $@"

github.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const fs = require("fs");
22
const yargs = require("yargs");
33
const CURRENT_VERSION = require("./package.json").version;
4+
const { info, error, debug } = require("./src/log");
45

56
const {
67
init,
@@ -12,12 +13,13 @@ const {
1213

1314
const commitCommand = "commit";
1415
const branchCommand = "branch"
16+
const knownCommands = [commitCommand, branchCommand];
1517

1618
const appendLineToFile = (filename, line) => {
1719
try {
1820
fs.appendFileSync(filename, `${line}\n`);
1921
} catch (e) {
20-
console.error(`Error appending line to file ${filename}: ${e.message}`);
22+
error(`Error appending line to file ${filename}: ${e.message}`);
2123
throw e;
2224
}
2325
};
@@ -95,8 +97,7 @@ yargs
9597
commitMessage,
9698
commitDescription,
9799
} = argv;
98-
99-
init();
100+
debug("Passed args:", JSON.stringify(argv, null, 2));
100101
createCommitOnBranch(
101102
owner,
102103
repo,
@@ -107,7 +108,7 @@ yargs
107108
commitDescription
108109
)
109110
.then((response) => {
110-
console.log(`Commit created: ${response.commitUrl}`);
111+
info(`Commit created: ${response.commitUrl}`);
111112
writeResultToGithubOutputFile([
112113
{
113114
label: "command",
@@ -119,8 +120,9 @@ yargs
119120
},
120121
]);
121122
})
122-
.catch((error) => {
123-
console.error("Failed to create commit:", error.message);
123+
.catch((err) => {
124+
error("Failed to create commit:", err.message);
125+
process.exit(1);
124126
});
125127
}
126128
)
@@ -150,11 +152,11 @@ yargs
150152
},
151153
(argv) => {
152154
const { owner, repo, branch } = argv;
153-
init();
155+
debug("Passed args:", JSON.stringify(argv, null, 2));
154156
checkIfBranchExists(owner, repo, branch)
155157
.then((response) => {
156158
const n = response ? "a" : "no";
157-
console.log(
159+
info(
158160
`Repository ${owner}/${repo} has ${n} branch named '${branch}'`
159161
);
160162
writeResultToGithubOutputFile([
@@ -168,15 +170,26 @@ yargs
168170
},
169171
]);
170172
})
171-
.catch((error) => {
172-
console.error("Failed to check if branch exists:", error.message);
173+
.catch((err) => {
174+
error("Failed to check if branch exists:", err.message);
175+
process.exit(1);
173176
});
174177
}
175178
)
176179
.demandCommand()
177180
.version(CURRENT_VERSION)
178181
.alias({
179182
h: "help",
180-
v: "version"
183+
v: "version",
184+
})
185+
.check((argv) => {
186+
const cmd = argv._[0];
187+
if (!knownCommands.includes(cmd)) {
188+
throw new Error(`Unknown command: ${cmd}`);
189+
}
190+
return true;
191+
})
192+
.check(() => {
193+
return init();
181194
})
182195
.help().argv;

github.test.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const { exec } = require("child_process");
2+
const {
3+
repoOwner,
4+
repoName,
5+
correctBranch,
6+
wrongBranch,
7+
} = require("./test.common.js");
8+
9+
describe("github.js", () => {
10+
describe("commit command", () => {
11+
test("commit command, good branch", (done) => {
12+
exec(
13+
`node github.js commit -o ${repoOwner} -r ${repoName} -b ${correctBranch} -c dummy/file1.txt -m "this is a commit msg"`,
14+
(error, stdout, stderr) => {
15+
expect(error).toBeNull();
16+
expect(stdout).toContain(
17+
`Commit created: https://github.com/${repoOwner}/${repoName}/commit`
18+
);
19+
done();
20+
}
21+
);
22+
}, 10000);
23+
24+
test("commit command, BAD branch", (done) => {
25+
exec(
26+
`node github.js commit -o ${repoOwner} -r ${repoName} -b ${wrongBranch} -c dummy/file1.txt -m "this is a commit msg"`,
27+
(error, stdout, stderr) => {
28+
expect(error).not.toBeNull();
29+
expect(stderr).toMatch(/Failed to create commit:/);
30+
done();
31+
}
32+
);
33+
}, 10000);
34+
});
35+
36+
describe("branch command", () => {
37+
test("branch command, good branch", (done) => {
38+
exec(
39+
`node github.js branch -o ${repoOwner} -r ${repoName} -b ${correctBranch}`,
40+
(error, stdout, stderr) => {
41+
expect(error).toBeNull();
42+
expect(stdout).toContain(
43+
`Repository ${repoOwner}/${repoName} has a branch named '${correctBranch}'`
44+
);
45+
done();
46+
}
47+
);
48+
}, 10000);
49+
50+
test("branch command, BAD branch", (done) => {
51+
exec(
52+
`node github.js branch -o ${repoOwner} -r ${repoName} -b ${wrongBranch}`,
53+
(error, stdout, stderr) => {
54+
expect(error).toBeNull();
55+
expect(stdout).toContain(
56+
`Repository ${repoOwner}/${repoName} has no branch named '${wrongBranch}'`
57+
);
58+
done();
59+
}
60+
);
61+
}, 10000);
62+
});
63+
});

0 commit comments

Comments
 (0)