Skip to content

Commit

Permalink
add circleci build and expand readme
Browse files Browse the repository at this point in the history
  • Loading branch information
marcocarvalho committed Jan 18, 2022
1 parent fbb2332 commit ceed62a
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 319 deletions.
54 changes: 54 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
version: 2
jobs:
prepare:
working_directory: ~/repo
docker:
- image: cimg/node:17.3.1
auth:
username: caremessagehub
password: $DOCKERHUB_PASSWORD
steps:
- checkout

- restore_cache:
key: node-dependencies-{{ checksum "yarn.lock" }}

- run: yarn install

- save_cache:
key: node-dependencies-{{ checksum "yarn.lock" }}
paths:
- node_modules
- ~/.cache

setup_and_test:
working_directory: ~/repo
docker:
- image: cimg/node:17.3.1
auth:
username: caremessagehub
password: $DOCKERHUB_PASSWORD
environment:
COVERAGE: true
steps:
- checkout

- restore_cache:
key: node-dependencies-{{ checksum "yarn.lock" }}

- run:
name: Run Unit Tests
command: yarn test

workflows:
version: 2
prepare_setup_and_test:
jobs:
- prepare:
context:
- docker-credential
- setup_and_test:
context:
- docker-credential
requires:
- prepare
16 changes: 16 additions & 0 deletions .github/workflows/check_if_merged.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: 'automate test for GH Action'

on:
push:
branches-ignore:
- 'master'
- 'staging'
jobs:
build:

runs-on: ubuntu-latest

steps:
- name: Dummy test to avoid "waiting build error"
run: |
exit 0
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v16.13.0
v17.3.1
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ jobs:
steps:
- uses: CareMessagePlatform/check-if-merged@v2
```

## make changes

To make the changes valid, you need to build again using `yarn build` to make the distribution version. Run: `yarn build` command, commit the dist files and tag them properly.

## ncc

See why we need to compile all the dependencies in one single JS script: https://docs.github.com/en/actions/creating-actions/creating-a-javascript-action#commit-tag-and-push-your-action-to-github
12 changes: 5 additions & 7 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ branding:
icon: git-merge
color: purple
inputs:
destination_branch: # id of input
destination_branch:
description: 'destination branch'
required: true
default: 'staging'
source_branch:
description: 'source branch to check or GITHUB_REF will be used'
required: false
outputs:
is_merged: # id of output
description: '0 - merged | 100 not merged'
github_token:
description: default token to use
required: true
default: ${{ github.token }}
runs:
using: 'node16'
main: 'dist/index.js'
17 changes: 7 additions & 10 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8280,32 +8280,29 @@ const github = __nccwpck_require__(5438);
const core = __nccwpck_require__(2186);

async function run() {
// This should be a token with access to your repository scoped in as a secret.
// The YML workflow will need to set myToken with the GitHub Secret Token
// github_token: ${{ secrets.GITHUB_TOKEN }}
// https://help.github.com/en/actions/automating-your-workflow-with-github-actions/authenticating-with-the-github_token#about-the-github_token-secret

var title = "Not Merged";

try {
const myToken = core.getInput('github_token');
const token = core.getInput('github_token');
const params = github.context.repo; // owner and repo
params.head = github.context.sha;
params.base = "staging"; // TODO: GET FROM INPUT
params.base = core.getInput('destination_branch'); // default is staging, see action.yml

const octokit = github.getOctokit(token);

const octokit = github.getOctokit(myToken)
// https://docs.github.com/en/rest/reference/commits#compare-two-commits
const response = await octokit.rest.repos.compareCommits(params);

if(response.status != 200) {
title = "Error"
title = "Error";
throw response.err;
}

if(response.data.merge_base_commit.sha != params.head) {
throw `${params.head} is not merged to ${params.base}`
}

core.notice(`${params.head} is merged to ${params.base}`, { title: "Merged" })
core.notice(`${params.head} is merged to ${params.base}`, { title: "Merged" });
}
catch (err) {
core.setFailed(`${err}`);
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

187 changes: 1 addition & 186 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,191 +4,6 @@
*/

module.exports = {
// All imported modules in your tests should be mocked automatically
// automock: false,

// Stop running tests after `n` failures
// bail: 0,

// The directory where Jest should store its cached dependency information
// cacheDirectory: "/private/var/folders/t6/sk2h3g1j4q99zqqzpzfkzh600000gn/T/jest_dx",

// Automatically clear mock calls, instances and results before every test
clearMocks: true,

// Indicates whether the coverage information should be collected while executing the test
// collectCoverage: false,

// An array of glob patterns indicating a set of files for which coverage information should be collected
// collectCoverageFrom: undefined,

// The directory where Jest should output its coverage files
// coverageDirectory: undefined,

// An array of regexp pattern strings used to skip coverage collection
// coveragePathIgnorePatterns: [
// "/node_modules/"
// ],

// Indicates which provider should be used to instrument code for coverage
coverageProvider: "v8",

// A list of reporter names that Jest uses when writing coverage reports
// coverageReporters: [
// "json",
// "text",
// "lcov",
// "clover"
// ],

// An object that configures minimum threshold enforcement for coverage results
// coverageThreshold: undefined,

// A path to a custom dependency extractor
// dependencyExtractor: undefined,

// Make calling deprecated APIs throw helpful error messages
// errorOnDeprecated: false,

// Force coverage collection from ignored files using an array of glob patterns
// forceCoverageMatch: [],

// A path to a module which exports an async function that is triggered once before all test suites
// globalSetup: undefined,

// A path to a module which exports an async function that is triggered once after all test suites
// globalTeardown: undefined,

// A set of global variables that need to be available in all test environments
// globals: {},

// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
// maxWorkers: "50%",

// An array of directory names to be searched recursively up from the requiring module's location
// moduleDirectories: [
// "node_modules"
// ],

// An array of file extensions your modules use
// moduleFileExtensions: [
// "js",
// "jsx",
// "ts",
// "tsx",
// "json",
// "node"
// ],

// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
// moduleNameMapper: {},

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],

// Activates notifications for test results
// notify: false,

// An enum that specifies notification mode. Requires { notify: true }
// notifyMode: "failure-change",

// A preset that is used as a base for Jest's configuration
// preset: undefined,

// Run tests from one or more projects
// projects: undefined,

// Use this configuration option to add custom reporters to Jest
// reporters: undefined,

// Automatically reset mock state before every test
resetMocks: true,

// Reset the module registry before running each individual test
// resetModules: false,

// A path to a custom resolver
// resolver: undefined,

// Automatically restore mock state and implementation before every test
// restoreMocks: false,

// The root directory that Jest should scan for tests and modules within
// rootDir: undefined,

// A list of paths to directories that Jest should use to search for files in
// roots: [
// "<rootDir>"
// ],

// Allows you to use a custom runner instead of Jest's default test runner
// runner: "jest-runner",

// The paths to modules that run some code to configure or set up the testing environment before each test
// setupFiles: [],

// A list of paths to modules that run some code to configure or set up the testing framework before each test
// setupFilesAfterEnv: [],

// The number of seconds after which a test is considered as slow and reported as such in the results.
// slowTestThreshold: 5,

// A list of paths to snapshot serializer modules Jest should use for snapshot testing
// snapshotSerializers: [],

// The test environment that will be used for testing
// testEnvironment: "jest-environment-node",

// Options that will be passed to the testEnvironment
// testEnvironmentOptions: {},

// Adds a location field to test results
// testLocationInResults: false,

// The glob patterns Jest uses to detect test files
// testMatch: [
// "**/__tests__/**/*.[jt]s?(x)",
// "**/?(*.)+(spec|test).[tj]s?(x)"
// ],

// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
// testPathIgnorePatterns: [
// "/node_modules/"
// ],

// The regexp pattern or array of patterns that Jest uses to detect test files
// testRegex: [],

// This option allows the use of a custom results processor
// testResultsProcessor: undefined,

// This option allows use of a custom test runner
// testRunner: "jest-circus/runner",

// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
// testURL: "http://localhost",

// Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
// timers: "real",

// A map from regular expressions to paths to transformers
// transform: undefined,

// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
// transformIgnorePatterns: [
// "/node_modules/",
// "\\.pnp\\.[^\\/]+$"
// ],

// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
// unmockedModulePathPatterns: undefined,

// Indicates whether each individual test should be reported during the run
// verbose: undefined,

// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
// watchPathIgnorePatterns: [],

// Whether to use watchman for file crawling
// watchman: true,
resetMocks: true
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"@actions/github": "^5.0.0"
},
"scripts": {
"build": "ncc build src/index.js -o dist --source-map --license licenses.txt",
"build": "NODE_OPTIONS=--openssl-legacy-provider ncc build src/index.js -o dist --source-map --license licenses.txt",
"test": "jest"
},
"devDependencies": {
Expand Down
13 changes: 4 additions & 9 deletions src/run.bad_api_response.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
process.env.GITHUB_REPOSITORY = "someOwner/repository";
process.env.INPUT_GITHUB_TOKEN = "token"
process.env.INPUT_DESTINATION_BRANCH = "staging"
process.env.GITHUB_SHA = "xx";

const run = require("./run");
const nock = require("nock");
const assertWriteCalls = require("./run.shared-test-context");

describe("when it's merged", () => {
let oldWrite;
let mock;

beforeEach(() => {
mock = jest.fn();
oldWrite = process.stdout.write;
process.stdout.write = mock;
process.stdout.write = jest.fn();

nock("https://api.github.com").get("/repos/someOwner/repository/compare/staging...xx").reply(404, {
message: 'Not Found',
documentation_url: 'https://docs.github.com/rest/reference/repos#compare-two-commits'
Expand All @@ -23,8 +20,6 @@ describe("when it's merged", () => {
test("when there is no branch to compare", async () => {
await run();

process.stdout.write = oldWrite;

assertWriteCalls(["::error::HttpError: Not Found\n"], mock);
assertWriteCalls(["::error::HttpError: Not Found\n"]);
});
});
Loading

0 comments on commit ceed62a

Please sign in to comment.