Skip to content

Commit

Permalink
Merge pull request #2 from pirafrank/dev
Browse files Browse the repository at this point in the history
v0.1.2
  • Loading branch information
pirafrank committed May 3, 2024
2 parents 94bba48 + 6052523 commit ba5d7c1
Show file tree
Hide file tree
Showing 16 changed files with 360 additions and 36 deletions.
10 changes: 10 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
node_modules
.gitignore
*.test.js
jest.config.js
.docker
.github
.vscode
dummy
.env
.env*
1 change: 1 addition & 0 deletions .env.sh.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export GITHUB_TOKEN="gh123abc"
22 changes: 22 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,25 @@ jobs:
run: npm run test
env:
GITHUB_TOKEN: ${{ secrets.GH_TKN }}

# docs: https://github.com/marketplace/actions/build-and-push-docker-images
- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

# docs: https://docs.docker.com/build/ci/github-actions/test-before-push/
- name: Build Docker image
uses: docker/build-push-action@v5
with:
push: false
context: .
load: true
tags: github-graphql-client:latest

- name: Run Docker tests
env:
GITHUB_TOKEN: ${{ secrets.GH_TKN }}
run: npm run docker:test
continue-on-error: true
76 changes: 73 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,54 @@
"--help"
]
},
{
"type": "node",
"request": "launch",
"name": "commit good branch",
"skipFiles": [
"<node_internals>/**"
],
"cwd": "${workspaceFolder}",
"program": "${workspaceFolder}/github.js",
"args": [
"commit",
"--owner",
"pirafrank",
"--repo",
"test-repo",
"--branch",
"main",
"-c",
"dummy/file1.txt",
"-m",
"this is a commit msg"
],
"envFile": "${workspaceFolder}/.env"
},
{
"type": "node",
"request": "launch",
"name": "commit BAD branch",
"skipFiles": [
"<node_internals>/**"
],
"cwd": "${workspaceFolder}",
"program": "${workspaceFolder}/github.js",
"args": [
"commit",
"--owner",
"pirafrank",
"--repo",
"test-repo",
"--branch",
"not-main",
"-c",
"dummy/file1.txt",
"-m",
"this is a commit msg"
],
"envFile": "${workspaceFolder}/.env"
},
{
"type": "node",
"request": "launch",
Expand All @@ -38,9 +86,31 @@
"program": "${workspaceFolder}/github.js",
"args": [
"branch",
"--owner", "pirafrank",
"--repo", "test-repo",
"--branch", "main"
"--owner",
"pirafrank",
"--repo",
"test-repo",
"--branch",
"main"
],
"envFile": "${workspaceFolder}/.env"
},
{
"type": "node",
"request": "launch",
"name": "branch does NOT exist",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/github.js",
"args": [
"branch",
"--owner",
"pirafrank",
"--repo",
"test-repo",
"--branch",
"not-main"
],
"envFile": "${workspaceFolder}/.env"
}
Expand Down
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ ENV DEBIAN_FRONTEND=noninteractive
COPY . /app/
COPY ./entrypoint.sh /entrypoint.sh

# IMPORTANT:
#
# GitHub sets the working directory path in the GITHUB_WORKSPACE
# environment variable. It's recommended to not use the WORKDIR
# instruction in your Dockerfile
#
# docs: https://docs.github.com/en/actions/creating-actions/dockerfile-support-for-github-actions#workdir

RUN cd /app && npm install --omit=dev

ENTRYPOINT ["/entrypoint.sh"]
7 changes: 6 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ inputs:
description: 'Arguments to pass to the Dockerfile entrypoint.'
required: true
default: '--help'
debug:
description: 'Whether to enable debug mode.'
required: false
default: 'false'
outputs:
command:
description: 'The command that was executed.'
Expand All @@ -18,7 +22,8 @@ outputs:
runs:
using: 'docker'
image: 'Dockerfile'
#env:
env:
DEBUG: ${{ inputs.debug }}
# GITHUB_TOKEN is already available in the action container context
# but be sure that it has 'writer' permissions on the target repository.
args:
Expand Down
17 changes: 14 additions & 3 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
#!/usr/bin/env bash

set -e
argv=(node /app/github.js "$@")
cmd=$(printf '%q ' "${argv[@]}")
eval $cmd

if [ -z "$GITHUB_TOKEN" ]; then
echo "GITHUB_TOKEN is not set. Exiting."
exit 1
fi

if [[ "$DEBUG" == "true" ]]; then
echo "first arg"
echo $1
echo "all args:"
echo $@
fi

eval "node /app/github.js $@"
35 changes: 24 additions & 11 deletions github.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require("fs");
const yargs = require("yargs");
const CURRENT_VERSION = require("./package.json").version;
const { info, error, debug } = require("./src/log");

const {
init,
Expand All @@ -12,12 +13,13 @@ const {

const commitCommand = "commit";
const branchCommand = "branch"
const knownCommands = [commitCommand, branchCommand];

const appendLineToFile = (filename, line) => {
try {
fs.appendFileSync(filename, `${line}\n`);
} catch (e) {
console.error(`Error appending line to file ${filename}: ${e.message}`);
error(`Error appending line to file ${filename}: ${e.message}`);
throw e;
}
};
Expand Down Expand Up @@ -95,8 +97,7 @@ yargs
commitMessage,
commitDescription,
} = argv;

init();
debug("Passed args:", JSON.stringify(argv, null, 2));
createCommitOnBranch(
owner,
repo,
Expand All @@ -107,7 +108,7 @@ yargs
commitDescription
)
.then((response) => {
console.log(`Commit created: ${response.commitUrl}`);
info(`Commit created: ${response.commitUrl}`);
writeResultToGithubOutputFile([
{
label: "command",
Expand All @@ -119,8 +120,9 @@ yargs
},
]);
})
.catch((error) => {
console.error("Failed to create commit:", error.message);
.catch((err) => {
error("Failed to create commit:", err.message);
process.exit(1);
});
}
)
Expand Down Expand Up @@ -150,11 +152,11 @@ yargs
},
(argv) => {
const { owner, repo, branch } = argv;
init();
debug("Passed args:", JSON.stringify(argv, null, 2));
checkIfBranchExists(owner, repo, branch)
.then((response) => {
const n = response ? "a" : "no";
console.log(
info(
`Repository ${owner}/${repo} has ${n} branch named '${branch}'`
);
writeResultToGithubOutputFile([
Expand All @@ -168,15 +170,26 @@ yargs
},
]);
})
.catch((error) => {
console.error("Failed to check if branch exists:", error.message);
.catch((err) => {
error("Failed to check if branch exists:", err.message);
process.exit(1);
});
}
)
.demandCommand()
.version(CURRENT_VERSION)
.alias({
h: "help",
v: "version"
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;
63 changes: 63 additions & 0 deletions github.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const { exec } = require("child_process");
const {
repoOwner,
repoName,
correctBranch,
wrongBranch,
} = require("./test.common.js");

describe("github.js", () => {
describe("commit command", () => {
test("commit command, good branch", (done) => {
exec(
`node github.js commit -o ${repoOwner} -r ${repoName} -b ${correctBranch} -c dummy/file1.txt -m "this is a commit msg"`,
(error, stdout, stderr) => {
expect(error).toBeNull();
expect(stdout).toContain(
`Commit created: https://github.com/${repoOwner}/${repoName}/commit`
);
done();
}
);
}, 10000);

test("commit command, BAD branch", (done) => {
exec(
`node github.js commit -o ${repoOwner} -r ${repoName} -b ${wrongBranch} -c dummy/file1.txt -m "this is a commit msg"`,
(error, stdout, stderr) => {
expect(error).not.toBeNull();
expect(stderr).toMatch(/Failed to create commit:/);
done();
}
);
}, 10000);
});

describe("branch command", () => {
test("branch command, good branch", (done) => {
exec(
`node github.js branch -o ${repoOwner} -r ${repoName} -b ${correctBranch}`,
(error, stdout, stderr) => {
expect(error).toBeNull();
expect(stdout).toContain(
`Repository ${repoOwner}/${repoName} has a branch named '${correctBranch}'`
);
done();
}
);
}, 10000);

test("branch command, BAD branch", (done) => {
exec(
`node github.js branch -o ${repoOwner} -r ${repoName} -b ${wrongBranch}`,
(error, stdout, stderr) => {
expect(error).toBeNull();
expect(stdout).toContain(
`Repository ${repoOwner}/${repoName} has no branch named '${wrongBranch}'`
);
done();
}
);
}, 10000);
});
});
Loading

0 comments on commit ba5d7c1

Please sign in to comment.