From 09181967f9a12c2f12d3a8fafc5c193d370464b3 Mon Sep 17 00:00:00 2001 From: Francesco Pira Date: Tue, 30 Apr 2024 10:36:34 +0200 Subject: [PATCH 01/12] improved initial checks improved initial checks by implicit init() calling --- github.js | 5 +++-- index.js | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/github.js b/github.js index 1d01dab..5ac120f 100644 --- a/github.js +++ b/github.js @@ -96,7 +96,6 @@ yargs commitDescription, } = argv; - init(); createCommitOnBranch( owner, repo, @@ -150,7 +149,6 @@ yargs }, (argv) => { const { owner, repo, branch } = argv; - init(); checkIfBranchExists(owner, repo, branch) .then((response) => { const n = response ? "a" : "no"; @@ -179,4 +177,7 @@ yargs h: "help", v: "version" }) + .check(() => { + return init(); + }) .help().argv; diff --git a/index.js b/index.js index 289557b..2a5e1e7 100644 --- a/index.js +++ b/index.js @@ -18,7 +18,7 @@ let client = null; function init(token, apiUrl) { githubToken = token || process.env.GITHUB_TOKEN; if (!githubToken) { - throw new Error("ERROR: GITHUB_TOKEN environment variable not set."); + throw new Error("Error: token argument missing or GITHUB_TOKEN env var not set."); } GITHUB_GRAPHQL_URL = @@ -35,6 +35,8 @@ function init(token, apiUrl) { }, }, }); + + return true; } function arrayIsArray(array) { From 3d5143c6d4d3e1a730494fab605b84bb48bd888d Mon Sep 17 00:00:00 2001 From: Francesco Pira Date: Tue, 30 Apr 2024 11:19:56 +0200 Subject: [PATCH 02/12] improved handling of error responses from API --- github.js | 12 +++++++++++- index.js | 14 +++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/github.js b/github.js index 5ac120f..a703b77 100644 --- a/github.js +++ b/github.js @@ -12,6 +12,7 @@ const { const commitCommand = "commit"; const branchCommand = "branch" +const knownCommands = [commitCommand, branchCommand]; const appendLineToFile = (filename, line) => { try { @@ -120,6 +121,7 @@ yargs }) .catch((error) => { console.error("Failed to create commit:", error.message); + process.exit(1); }); } ) @@ -168,6 +170,7 @@ yargs }) .catch((error) => { console.error("Failed to check if branch exists:", error.message); + process.exit(1); }); } ) @@ -175,7 +178,14 @@ yargs .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(); diff --git a/index.js b/index.js index 2a5e1e7..b12ee08 100644 --- a/index.js +++ b/index.js @@ -85,6 +85,16 @@ function extractChangedOrDeletedFiles(changedFiles, deletedFiles) { }; } +function checkErrorResponse(response) { + if (!!response.errors || !!response.error || !response.data) { + console.error( + "Error response from API:", + JSON.stringify(response, null, 2) + ); + throw new Error("Received error response from API"); + } +} + async function fetchBranchData(repoOwner, repoName, branchName) { const query = ` query($owner: String!, $repo: String!, $branch: String!) { @@ -107,10 +117,11 @@ async function fetchBranchData(repoOwner, repoName, branchName) { try { const response = await client.query(query, variables); + checkErrorResponse(response); return response; } catch (error) { console.error( - `Error while trying to fetching data from API: ${error.message}` + `Error while trying to fetch data from API: ${error.message}` ); throw error; } @@ -200,6 +211,7 @@ async function createCommitOnBranch( const response = await client .mutation(graphqlRequest.query, graphqlRequest.variables) .toPromise(); + checkErrorResponse(response); return { data: response, commitUrl: response?.data?.createCommitOnBranch?.commit?.url || null From f9bb8f8e14ea52a8c2b5907f86e795b7226e6ce1 Mon Sep 17 00:00:00 2001 From: Francesco Pira Date: Tue, 30 Apr 2024 15:31:59 +0200 Subject: [PATCH 03/12] added tests for github.js file --- github.test.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ index.test.js | 1 - jest.config.js | 2 ++ package.json | 4 +++- 4 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 github.test.js create mode 100644 jest.config.js diff --git a/github.test.js b/github.test.js new file mode 100644 index 0000000..ddd3d05 --- /dev/null +++ b/github.test.js @@ -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); + }); +}); diff --git a/index.test.js b/index.test.js index 7866988..c97fe9d 100644 --- a/index.test.js +++ b/index.test.js @@ -1,4 +1,3 @@ -require("dotenv").config(); const { repoOwner, repoName, diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..5fc288b --- /dev/null +++ b/jest.config.js @@ -0,0 +1,2 @@ +// add dot env to all jest tests +require("dotenv").config(); diff --git a/package.json b/package.json index dc2be95..147ea61 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,9 @@ }, "scripts": { "gh": "node github.js", - "test": "jest" + "test": "jest", + "test:index": "jest --testPathPattern=index.test.js", + "test:github": "jest --testPathPattern=github.test.js" }, "keywords": [ "git", From 1d16284404ccd3fe0d7d9737ae10898fb1f3e248 Mon Sep 17 00:00:00 2001 From: Francesco Pira Date: Tue, 30 Apr 2024 15:50:26 +0200 Subject: [PATCH 04/12] added warning in Docker env for missing gh token --- entrypoint.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/entrypoint.sh b/entrypoint.sh index e38d02d..59fa423 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,6 +1,12 @@ #!/usr/bin/env bash set -e + +if [ -z "$GITHUB_TOKEN" ]; then + echo "GITHUB_TOKEN is not set. Exiting." + exit 1 +fi + argv=(node /app/github.js "$@") cmd=$(printf '%q ' "${argv[@]}") eval $cmd From e4152f66179a257d55c4a50362bf3cdfb0bcb79b Mon Sep 17 00:00:00 2001 From: Francesco Pira Date: Tue, 30 Apr 2024 16:14:39 +0200 Subject: [PATCH 05/12] docker additions --- .dockerignore | 8 ++++++++ Dockerfile | 8 ++++++++ package.json | 3 ++- 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..b174b0d --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +node_modules +.gitignore +*.test.js +jest.config.js +.docker +.github +.vscode +dummy diff --git a/Dockerfile b/Dockerfile index 0b3b67f..7ab0f93 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] diff --git a/package.json b/package.json index 147ea61..c6a25ef 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "gh": "node github.js", "test": "jest", "test:index": "jest --testPathPattern=index.test.js", - "test:github": "jest --testPathPattern=github.test.js" + "test:github": "jest --testPathPattern=github.test.js", + "docker:build": "docker build -t github-graphql-client -f Dockerfile ." }, "keywords": [ "git", From 278f3165bfa0c10004869b475bca73b0e895f913 Mon Sep 17 00:00:00 2001 From: Francesco Pira Date: Tue, 30 Apr 2024 16:46:15 +0200 Subject: [PATCH 06/12] chores --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9993200..d4a3296 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@pirafrank/github-graphql-client", - "version": "0.1.0", + "version": "0.1.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@pirafrank/github-graphql-client", - "version": "0.1.0", + "version": "0.1.2", "license": "MIT", "dependencies": { "graphql": "^16.8.1", diff --git a/package.json b/package.json index c6a25ef..ec8aa58 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@pirafrank/github-graphql-client", - "version": "0.1.0", + "version": "0.1.2", "description": "Simple GitHub client to commit via their graphql APIs", "main": "index.js", "private": false, From 7d5ae5fa9afcb82c6e57902e300f8038a33f0cdb Mon Sep 17 00:00:00 2001 From: Francesco Pira Date: Thu, 2 May 2024 17:17:21 +0200 Subject: [PATCH 07/12] better bits of logs --- .dockerignore | 2 ++ github.js | 18 +++++++++------- index.js | 28 ++++++++++++------------- index.test.js | 2 +- src/log.js | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 24 deletions(-) create mode 100644 src/log.js diff --git a/.dockerignore b/.dockerignore index b174b0d..4c5fc70 100644 --- a/.dockerignore +++ b/.dockerignore @@ -6,3 +6,5 @@ jest.config.js .github .vscode dummy +.env +.env* diff --git a/github.js b/github.js index a703b77..a53c718 100644 --- a/github.js +++ b/github.js @@ -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, @@ -18,7 +19,7 @@ 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; } }; @@ -96,7 +97,7 @@ yargs commitMessage, commitDescription, } = argv; - + debug("Passed args:", JSON.stringify(argv, null, 2)); createCommitOnBranch( owner, repo, @@ -107,7 +108,7 @@ yargs commitDescription ) .then((response) => { - console.log(`Commit created: ${response.commitUrl}`); + info(`Commit created: ${response.commitUrl}`); writeResultToGithubOutputFile([ { label: "command", @@ -119,8 +120,8 @@ yargs }, ]); }) - .catch((error) => { - console.error("Failed to create commit:", error.message); + .catch((err) => { + error("Failed to create commit:", err.message); process.exit(1); }); } @@ -151,10 +152,11 @@ yargs }, (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"; - console.log( + info( `Repository ${owner}/${repo} has ${n} branch named '${branch}'` ); writeResultToGithubOutputFile([ @@ -168,8 +170,8 @@ 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); }); } diff --git a/index.js b/index.js index b12ee08..1647c82 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ const { cacheExchange, fetchExchange } = require("@urql/core"); +const { info, error, debug } = require("./src/log"); let GITHUB_GRAPHQL_URL = null; let githubToken = null; @@ -16,7 +17,7 @@ let client = null; * @param {string} apiUrl GitHub GraphQL API URL */ function init(token, apiUrl) { - githubToken = token || process.env.GITHUB_TOKEN; + const githubToken = token || process.env.GITHUB_TOKEN; if (!githubToken) { throw new Error("Error: token argument missing or GITHUB_TOKEN env var not set."); } @@ -87,10 +88,7 @@ function extractChangedOrDeletedFiles(changedFiles, deletedFiles) { function checkErrorResponse(response) { if (!!response.errors || !!response.error || !response.data) { - console.error( - "Error response from API:", - JSON.stringify(response, null, 2) - ); + error("Error response from API:", JSON.stringify(response, null, 2)); throw new Error("Received error response from API"); } } @@ -119,11 +117,11 @@ async function fetchBranchData(repoOwner, repoName, branchName) { const response = await client.query(query, variables); checkErrorResponse(response); return response; - } catch (error) { - console.error( - `Error while trying to fetch data from API: ${error.message}` + } catch (err) { + error( + `Error while trying to fetch data from API: ${err.message}` ); - throw error; + throw err; } } @@ -179,8 +177,8 @@ async function createCommitOnBranch( changedFiles, deletedFiles )); - console.log("Changed files:", JSON.stringify(changedFiles, null, 2)); - console.log("Deleted files:", JSON.stringify(deletedFiles, null, 2)); + info("Changed files:", JSON.stringify(changedFiles, null, 2)); + info("Deleted files:", JSON.stringify(deletedFiles, null, 2)); if (!commitMessage) { throw new Error("No commit message provided. Aborting."); @@ -216,11 +214,11 @@ async function createCommitOnBranch( data: response, commitUrl: response?.data?.createCommitOnBranch?.commit?.url || null }; - } catch (error) { - console.error( - `Error while performing commit action via GraphQL API: ${error.message}` + } catch (err) { + error( + `Error while performing commit action via GraphQL API: ${err.message}` ); - throw error; + throw err; } } diff --git a/index.test.js b/index.test.js index c97fe9d..8a70034 100644 --- a/index.test.js +++ b/index.test.js @@ -25,7 +25,7 @@ describe("createCommitOnBranch", () => { "this is a commit msg", "the description of the commit" ); - console.log(JSON.stringify(result, null, 2)); + //console.log(JSON.stringify(result, null, 2)); const commitUrl = result?.commitUrl || ""; expect(commitUrl).toMatch( new RegExp( diff --git a/src/log.js b/src/log.js new file mode 100644 index 0000000..301c9da --- /dev/null +++ b/src/log.js @@ -0,0 +1,58 @@ +// +// Description: This file contains the log utility functions. +// The log utility functions are used to log messages to the console. +// IMPORTANT: to avoid circular dependencies, the log utility functions +// should not import any other files. +// + +const isRunningInCI = () => { + return ( + !!process.env.GITHUB_ACTIONS || + !!process.env.TRAVIS || + !!process.env.CIRCLECI || + !!process.env.GITLAB_CI || + !!process.env.APPVEYOR + ); +}; + +// flag to determine if we are running in CI +const _isCI = isRunningInCI(); + +const pad = (str, length, separator) => { + if (str.length === 0 || str.length >= length) return str; + const d = length - str.length; + const m = d % 2; + const p = (d - m) / 2; + return str.padStart(str.length + p, separator).padEnd(length, separator); +}; + +const getPrefix = (level) => { + // only print timestamp if not running in CI, CI have their own timestamps + const timestamp = _isCI ? "" : `[${new Date().toISOString()}] `; + // second argument is max possible length of log level string + level = pad(level, 5, " "); + return `${timestamp}[${level}] :` +}; + +const info = (...args) => { + const prefix = getPrefix("INFO"); + console.log(prefix, ...args); +}; + +const error = (...args) => { + const prefix = getPrefix("ERROR"); + console.error(prefix, ...args); +}; + +const debug = (...args) => { + if (!!process.env.DEBUG) { + const prefix = getPrefix("DEBUG"); + console.log(prefix, ...args); + } +}; + +module.exports = { + info, + error, + debug, +}; From 5b2d7d70a407d1f802d33314a9146a35c395c334 Mon Sep 17 00:00:00 2001 From: Francesco Pira Date: Fri, 3 May 2024 23:34:56 +0200 Subject: [PATCH 08/12] vscode commit debug config --- .vscode/launch.json | 76 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 3 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 10b0968..7ef0e27 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -28,6 +28,54 @@ "--help" ] }, + { + "type": "node", + "request": "launch", + "name": "commit good branch", + "skipFiles": [ + "/**" + ], + "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": [ + "/**" + ], + "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", @@ -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": [ + "/**" + ], + "program": "${workspaceFolder}/github.js", + "args": [ + "branch", + "--owner", + "pirafrank", + "--repo", + "test-repo", + "--branch", + "not-main" ], "envFile": "${workspaceFolder}/.env" } From b4c1e39261d97c11647f8b9b269d276f24176c60 Mon Sep 17 00:00:00 2001 From: Francesco Pira Date: Sat, 4 May 2024 00:10:13 +0200 Subject: [PATCH 09/12] debug mode for GH action --- action.yml | 7 ++++++- entrypoint.sh | 7 +++++++ src/log.js | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/action.yml b/action.yml index e989518..f0d395e 100644 --- a/action.yml +++ b/action.yml @@ -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.' @@ -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: diff --git a/entrypoint.sh b/entrypoint.sh index 59fa423..007be56 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -7,6 +7,13 @@ if [ -z "$GITHUB_TOKEN" ]; then exit 1 fi +if [[ "$DEBUG" == "true" ]]; then + echo "first arg" + echo $1 + echo "all args:" + echo $@ +fi + argv=(node /app/github.js "$@") cmd=$(printf '%q ' "${argv[@]}") eval $cmd diff --git a/src/log.js b/src/log.js index 301c9da..2e01cb0 100644 --- a/src/log.js +++ b/src/log.js @@ -45,7 +45,7 @@ const error = (...args) => { }; const debug = (...args) => { - if (!!process.env.DEBUG) { + if (!!process.env.DEBUG && process.env.DEBUG === "true") { const prefix = getPrefix("DEBUG"); console.log(prefix, ...args); } From b1810ae69227351f3c8cc5b0b9fd3b1f31d6f2bf Mon Sep 17 00:00:00 2001 From: Francesco Pira Date: Sat, 4 May 2024 00:11:51 +0200 Subject: [PATCH 10/12] almost reverted fix for commit msg with spaces # test multiple parameters surrounded by double quotes # this is to test if the entrypoint.sh script can handle double quotes, # because GitHub Actions will pass arguments as a single string. docker run --rm \ -v ./dummy:/app/dummy \ -w /app \ -e GITHUB_TOKEN \ -e DEBUG \ github-graphql-client:latest "commit -o pirafrank -r "test-repo" -b main -c dummy/file1.txt -m 'this is a commit msg'" --- entrypoint.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 007be56..591e177 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -14,6 +14,4 @@ if [[ "$DEBUG" == "true" ]]; then echo $@ fi -argv=(node /app/github.js "$@") -cmd=$(printf '%q ' "${argv[@]}") -eval $cmd +eval "node /app/github.js $@" From acc69696db6d32e62255a6d86c5b08d12335363f Mon Sep 17 00:00:00 2001 From: Francesco Pira Date: Sat, 4 May 2024 00:13:36 +0200 Subject: [PATCH 11/12] docker tests --- .env.sh.sample | 1 + .github/workflows/pr.yml | 22 ++++++++++++++++++++ package.json | 4 +++- test.docker.sh | 45 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 .env.sh.sample create mode 100755 test.docker.sh diff --git a/.env.sh.sample b/.env.sh.sample new file mode 100644 index 0000000..cbfb25a --- /dev/null +++ b/.env.sh.sample @@ -0,0 +1 @@ +export GITHUB_TOKEN="gh123abc" diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 122fe99..7c46c6e 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -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 diff --git a/package.json b/package.json index ec8aa58..b438d4a 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,9 @@ "test": "jest", "test:index": "jest --testPathPattern=index.test.js", "test:github": "jest --testPathPattern=github.test.js", - "docker:build": "docker build -t github-graphql-client -f Dockerfile ." + "docker:build": "docker build -t github-graphql-client -f Dockerfile .", + "docker:test": "./test.docker.sh", + "docker:all": "npm run docker:build && npm run docker:test" }, "keywords": [ "git", diff --git a/test.docker.sh b/test.docker.sh new file mode 100755 index 0000000..d47ef8b --- /dev/null +++ b/test.docker.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash + +source .env.sh + +echo "************** help *****************" + +# test handling of single parameter +docker run --rm \ +-v ./dummy:/app/dummy \ +-w /app \ +-e GITHUB_TOKEN \ +-e DEBUG \ +github-graphql-client:latest "--help" + +echo "************ branch arg missing *******************" + +# test handling of multiple parameters +docker run --rm \ +-v ./dummy:/app/dummy \ +-w /app \ +-e GITHUB_TOKEN \ +-e DEBUG \ +github-graphql-client:latest commit -o pirafrank -r 'test-repo' -c dummy/file1.txt -m 'this is a commit msg' + +echo "************** all separated *****************" + +# test handling of multiple parameters +docker run --rm \ +-v ./dummy:/app/dummy \ +-w /app \ +-e GITHUB_TOKEN \ +-e DEBUG \ +github-graphql-client:latest commit -o pirafrank -r 'test-repo' -b main -c dummy/file1.txt -m onewordcommitmsg + +echo "************** all as one arg *****************" + +# test multiple parameters surrounded by double quotes +# this is to test if the entrypoint.sh script can handle double quotes, +# because GitHub Actions will pass arguments as a single string. +docker run --rm \ +-v ./dummy:/app/dummy \ +-w /app \ +-e GITHUB_TOKEN \ +-e DEBUG \ +github-graphql-client:latest "commit -o pirafrank -r "test-repo" -b main -c dummy/file1.txt -m 'this is a commit msg'" From 60525235bcdb52592de73826c655a371d991639c Mon Sep 17 00:00:00 2001 From: Francesco Pira Date: Sat, 4 May 2024 00:39:10 +0200 Subject: [PATCH 12/12] force run tests sequentially --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index b438d4a..67044fe 100644 --- a/package.json +++ b/package.json @@ -9,9 +9,9 @@ }, "scripts": { "gh": "node github.js", - "test": "jest", - "test:index": "jest --testPathPattern=index.test.js", - "test:github": "jest --testPathPattern=github.test.js", + "test": "jest --runInBand", + "test:index": "jest --runInBand --testPathPattern=index.test.js", + "test:github": "jest --runInBand --testPathPattern=github.test.js", "docker:build": "docker build -t github-graphql-client -f Dockerfile .", "docker:test": "./test.docker.sh", "docker:all": "npm run docker:build && npm run docker:test"