Skip to content

Commit

Permalink
Add key input variable
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisBoudreau committed Dec 2, 2021
1 parent b17f045 commit 81868d3
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 109 deletions.
7 changes: 0 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,3 @@ jobs:
yarn
- run: |
yarn all
test: # make sure the action works on a clean machine without building
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ./
with:
milliseconds: 1000
120 changes: 21 additions & 99 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,105 +1,27 @@
# Create a JavaScript Action using TypeScript
# Merge JSON files

Use this template to bootstrap the creation of a TypeScript action.:rocket:
This action merges two json array of objects living in two separate files.
If a collision occurs between two json objects, the priority is given to the object of `file_2_path`.

This template includes compilation support, tests, a validation workflow, publishing, and versioning guidance.
See [action.yml](./action.yml) for the list of `inputs` and `outputs`.

If you are new, there's also a simpler introduction. See the [Hello World JavaScript Action](https://github.com/actions/hello-world-javascript-action)

## Create an action from this template

Click the `Use this Template` and provide the new repo details for your action

## Code in Main

> First, you'll need to have a reasonably modern version of `node` handy. This won't work with versions older than 9, for instance.
Install the dependencies
```bash
$ yarn install
```

Build the typescript and package it for distribution
```bash
$ yarn build && yarn package
```

Run the tests :heavy_check_mark:
```bash
$ yarn test

PASS ./index.test.js
✓ throws invalid number (3ms)
wait 500 ms (504ms)
test runs (95ms)

...
```

## Change action.yml

The action.yml contains defines the inputs and output for your action.

Update the action.yml with your name, description, inputs and outputs for your action.

See the [documentation](https://help.github.com/en/articles/metadata-syntax-for-github-actions)

## Change the Code

Most toolkit and CI/CD operations involve async operations so the action is run in an async function.

```javascript
import * as core from '@actions/core';
...

async function run() {
try {
...
}
catch (error) {
core.setFailed(error.message);
}
}

run()
```

See the [toolkit documentation](https://github.com/actions/toolkit/blob/master/README.md#packages) for the various packages.

## Publish to a distribution branch

Actions are run from GitHub repos so we will checkin the packed dist folder.

Then run [ncc](https://github.com/zeit/ncc) and push the results:
```bash
$ yarn package
$ git add dist
$ git commit -a -m "prod dependencies"
$ git push origin releases/v1
```

Note: We recommend using the `--license` option for ncc, which will create a license file for all of the production node modules used in your project.

Your action is now published! :rocket:

See the [versioning documentation](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md)

## Validate

You can now validate the action by referencing `./` in a workflow in your repo (see [test.yml](.github/workflows/test.yml))
## Example usage

```yaml
uses: ./
with:
milliseconds: 1000
build-secrets:
name: Build secrets file
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Merge secrets
uses: agendrix/merge-json-files-action@v1.0.3
with:
key: "id"
file_1_path: "./secrets/shared.json"
file_2_path: "./secrets/staging.json"
```
See the [actions tab](https://github.com/actions/typescript-action/actions) for runs of this action! :rocket:
## Usage:
After testing you can [create a v1 tag](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md) to reference the stable and latest V1 action
## Helpers
If you created helpers that you consider could benefit to other actions, please consider contributing by adding them to the `helpers` folder.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ name: Merge json files
description: Merge two json files into one. If a key collision occurs, the priority is given to file 2.
author: 'l.boudreau@agendrix.com'
inputs:
key:
required: false
description: "Name of the field that uniquely identifies an item"
default: "name"
file_1_path:
required: false
description: Path of the first json file
Expand Down
3 changes: 2 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ async function run(): Promise<void> {

const fileOnepath = core.getInput("file_1_path", { required: false });
const fileTwopath = core.getInput("file_2_path", { required: false });
const key = core.getInput("key", { required: false });

const fileOneValues = fileValues(fileOnepath);
const fileTwoValues = fileValues(fileTwopath);

const mergedFile = Object();
[...fileOneValues, ...fileTwoValues].forEach(item => {
mergedFile[item.name] = item;
mergedFile[item[key]] = item;
});

const tmpFilePath = `/tmp/${randomBytes(16).toString("hex")}.json`;
Expand Down

0 comments on commit 81868d3

Please sign in to comment.