Skip to content

Commit

Permalink
Updates (#2)
Browse files Browse the repository at this point in the history
* Updates and Minor Fixes

* Add Output

* Update Color
  • Loading branch information
smashedr authored Jul 21, 2024
1 parent 09db9e9 commit 02aa6d9
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 68 deletions.
37 changes: 37 additions & 0 deletions .github/ISSUE_TEMPLATE/0-bug.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: "⚠️ Report an Issue"
description: "Something Not Working Right? Please let us know..."
labels: ["bug"]
assignees:
- smashedr

body:
- type: input
id: website
validations:
required: false
attributes:
label: Repo Link
description: Please provide a link to the repository or workflow you are having issues with if possible.

- type: textarea
id: description
validations:
required: true
attributes:
label: Details
description: Please describe the issue you are experiencing and how to reproduce.
placeholder: Provide as many details as you can...

- type: textarea
id: logs
validations:
required: false
attributes:
label: Log Output
description: Paste any relevant logs or output in this box.
render: shell

- type: markdown
attributes:
value: |
All issues/bugs that we can verify will be fixed. Thank you for taking the time to make this report!
17 changes: 17 additions & 0 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
blank_issues_enabled: true
contact_links:
- name: '💡 Request a Feature'
about: Request a New Feature or Enhancement in the Discussions.
url: https://github.com/cssnr/update-version-tags-action/discussions/new?category=feature-requests

- name: '❔ Ask a Question'
about: Ask a General Question or start a Discussions.
url: https://github.com/cssnr/update-version-tags-action/discussions/new?category=q-a

- name: '💬 Join Discord'
about: Chat with us about Issues, Features, Questions and More.
url: https://discord.gg/wXy6m2X8wY

- name: '📝 Submit Feedback'
about: Send General Feedback.
url: https://cssnr.github.io/feedback/?app=Update%20JSON%20Value
4 changes: 2 additions & 2 deletions .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
"singleQuote": true,
"overrides": [
{
"files": ["**/*.json", "**/*.yaml"],
"files": ["**/*.json", "**/*.yaml", "**/*.yml"],
"options": {
"singleQuote": false
}
},
{
"files": ["**/*.json", "**/*.yaml"],
"files": ["**/*.json", "**/*.yaml", "**/*.yml"],
"options": {
"tabWidth": 2
}
Expand Down
72 changes: 57 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
[![Tags](https://img.shields.io/github/actions/workflow/status/cssnr/update-json-value-action/tags.yaml?logo=github&logoColor=white&label=tags)](https://github.com/cssnr/update-json-value-action/actions/workflows/tags.yaml)
[![CSSNR Website](https://img.shields.io/badge/pages-website-blue?logo=github&logoColor=white&color=blue)](https://cssnr.github.io/)
[![GitHub Release Version](https://img.shields.io/github/v/release/cssnr/update-json-value-action?logo=github)](https://github.com/cssnr/update-json-value-action/releases/latest)
[![GitHub Top Language](https://img.shields.io/github/languages/top/cssnr/update-json-value-action?logo=htmx&logoColor=white)](https://github.com/cssnr/update-json-value-action)
[![GitHub Org Stars](https://img.shields.io/github/stars/cssnr?style=flat&logo=github&logoColor=white)](https://cssnr.github.io/)
[![Discord](https://img.shields.io/discord/899171661457293343?logo=discord&logoColor=white&label=discord&color=7289da)](https://discord.gg/wXy6m2X8wY)

# Update JSON Value Action

Update JSON file Value(s) for Publishing.

Zero configuration to update a `manifest.json` file `version` value to a release tag.
Allows setting multiple key/value pairs and setting nested keys. Arrays are not supported yet.

* [Inputs](#Inputs)
* [Examples](#Examples)
* [Support](#Support)
* [Inputs](#Inputs)
* [Outputs](#Outputs)
* [Examples](#Examples)
* [Support](#Support)
* [Contributing](#Contributing)

> [!NOTE]
> Please submit a
Expand All @@ -19,24 +24,32 @@ Allows setting multiple key/value pairs and setting nested keys. Arrays are not
## Inputs

| input | required | default | description |
|--------|----------|------------------|-----------------------------------|
| file | No | manifest.json | JSON File Path |
| keys | No | version | JSON Keys to Update, One per Line |
| values | No | $GITHUB_REF_NAME | Values to Update, One per Line |

If no options are passed, it will update the `manifest.json` file key `version` to the value of `GITHUB_REF_NAME`.
For multiple `keys` and `values` use new lines. Nested keys are specified using `.` notation.
| input | required | default | description |
|--------|----------|--------------------|-----------------------------------|
| file | No | `manifest.json` | JSON File Path |
| keys | No | `version` | JSON Keys to Update, One per Line |
| values | No | `$GITHUB_REF_NAME` | Values to Update, One per Line |
| write | No | `true` | Write Updates to `file` |

## Examples
If no options are passed, it will update the `manifest.json` file's key `version` to the value of `GITHUB_REF_NAME`.
For multiple `keys` and `values` use new lines with a yaml `|`.
Nested keys are specified using `.` notation.

Update the `manifest.json` file key `version` to the current `GITHUB_REF_NAME`.
```yaml
- name: 'Update JSON'
uses: cssnr/update-json-value-action@v1
```
## Outputs
| output | description |
|--------|---------------------|
| result | Updated JSON String |
## Examples
Same as above but manually setting values and only running on `release` events.

```yaml
- name: 'Update JSON'
uses: cssnr/update-json-value-action@v1
Expand All @@ -48,6 +61,7 @@ Same as above but manually setting values and only running on `release` events.
```

Same as above but also setting an additional key value pair.

```yaml
- name: 'Update JSON'
uses: cssnr/update-json-value-action@v1
Expand All @@ -63,6 +77,7 @@ Same as above but also setting an additional key value pair.
```

Set a nested key and use file from different directory.

```yaml
- name: 'Update JSON'
uses: cssnr/update-json-value-action@v1
Expand All @@ -75,6 +90,17 @@ Set a nested key and use file from different directory.
"Release ${{ github.ref_name }}"
```

Use the Output.

```yaml
- name: 'Update JSON'
id: json
uses: cssnr/update-json-value-action@v1
- name: "Echo Result"
run: echo '${{ steps.json.outputs.result }}'
```

# Support

For general help or to request a feature, see:
Expand All @@ -86,4 +112,20 @@ If you are experiencing an issue/bug or getting unexpected results, you can:

- Report an Issue: https://github.com/cssnr/update-json-value-action/issues
- Chat with us on Discord: https://discord.gg/wXy6m2X8wY
- Provide General Feedback: [https://cssnr.github.io/feedback/](https://cssnr.github.io/feedback/?app=Update%20JSON%20Value)
- Provide General
Feedback: [https://cssnr.github.io/feedback/](https://cssnr.github.io/feedback/?app=Update%20JSON%20Value)

# Contributing

Currently, the best way to contribute to this project is to star this project on GitHub.

Additionally, you can support other GitHub Actions I have published:

- [VirusTotal Action](https://github.com/cssnr/virustotal-action)
- [Update Version Tags Action](https://github.com/cssnr/update-version-tags-action)
- [Update JSON Value Action](https://github.com/cssnr/update-json-value-action)
- [Parse Issue Form Action](https://github.com/cssnr/parse-issue-form-action)
- [Portainer Stack Deploy](https://github.com/cssnr/portainer-stack-deploy-action)
- [Mozilla Addon Update Action](https://github.com/cssnr/mozilla-addon-update-action)

For a full list of current projects to support visit: [https://cssnr.github.io/](https://cssnr.github.io/)
10 changes: 9 additions & 1 deletion action.yaml → action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ branding:
inputs:
file:
description: "JSON File"
required: true
required: false
default: "manifest.json"
keys:
description: "Keys to Update"
Expand All @@ -17,6 +17,14 @@ inputs:
values:
description: "Values to Set"
required: false
write:
description: "Write Result to File"
required: false
default: "true"

outputs:
result:
description: "JSON Output Results"

runs:
using: "node20"
Expand Down
4 changes: 4 additions & 0 deletions commit-checklist.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
{ "value": "Run Build", "fileMask": "*src\\*.js" },
{ "value": "Check main in action.yml", "fileMask": "*action.yml" }
]
55 changes: 30 additions & 25 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26813,49 +26813,54 @@ const fs = __nccwpck_require__(7147)
try {
// Parse Inputs
const inputFile = core.getInput('file')
console.log('file:', inputFile)
console.log('inputFile:', inputFile)
const inputKeys = core.getInput('keys')
console.log('inputKeys:', inputKeys)
const inputValues = core.getInput('values')
const inputValues =
core.getInput('values') || process.env.GITHUB_REF_NAME
console.log('inputValues:', inputValues)
const writeFile = core.getBooleanInput('write')
console.log('writeFile:', writeFile)

// Parse Keys
if (!inputKeys) {
return core.setFailed('No Keys Found.')
}
const parsedKeys = inputKeys.split('\n')
console.log('parsedKeys:', parsedKeys)
const keys = inputKeys.split('\n')
console.log('keys:', keys)

// Parse Values
const parsedValues = []
if (!inputValues) {
parsedValues.push(process.env.GITHUB_REF_NAME)
} else {
parsedValues.push(...inputValues.split('\n'))
}
console.log('parsedValues:', parsedValues)
const values = inputValues.split('\n')
console.log('values:', values)

// Validate Parsed Inputs
if (parsedKeys.length !== parsedValues.length) {
return core.setFailed('Keys and Values are not equal in length.')
// Validate Inputs
if (keys.length !== values.length) {
return core.setFailed('Keys and Values length are not equal.')
}

// Update JSON
let file = fs.readFileSync(inputFile)
let data = JSON.parse(file.toString())
for (let i = 0; i < parsedKeys.length; i++) {
const key = parsedKeys[i]
const value = parsedValues[i]
console.log(`-- ${i} -- ${key}: ${value}`)
const file = fs.readFileSync(inputFile)
const data = JSON.parse(file.toString())
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
const value = values[i]
console.log(`--- ${i + 1}: ${key}: ${value}`)
setNestedValue(data, key, value)
}
let result = JSON.stringify(data, null, 2)
fs.writeFileSync(inputFile, result)

// Display Result
const result = JSON.stringify(data, null, 2)
console.log('-'.repeat(40))
console.log(result)
console.log('-'.repeat(40))

// Write File
if (writeFile) {
core.info(`\u001b[32;1mWriting result to file: ${inputFile}`)
fs.writeFileSync(inputFile, result)
} else {
core.info('\u001b[33;1mNot writing file because write is false.')
}

// Set Output
core.setOutput('result', JSON.stringify(data))
} catch (e) {
core.debug(e)
core.info(e.message)
Expand Down
55 changes: 30 additions & 25 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,54 @@ const fs = require('fs')
try {
// Parse Inputs
const inputFile = core.getInput('file')
console.log('file:', inputFile)
console.log('inputFile:', inputFile)
const inputKeys = core.getInput('keys')
console.log('inputKeys:', inputKeys)
const inputValues = core.getInput('values')
const inputValues =
core.getInput('values') || process.env.GITHUB_REF_NAME
console.log('inputValues:', inputValues)
const writeFile = core.getBooleanInput('write')
console.log('writeFile:', writeFile)

// Parse Keys
if (!inputKeys) {
return core.setFailed('No Keys Found.')
}
const parsedKeys = inputKeys.split('\n')
console.log('parsedKeys:', parsedKeys)
const keys = inputKeys.split('\n')
console.log('keys:', keys)

// Parse Values
const parsedValues = []
if (!inputValues) {
parsedValues.push(process.env.GITHUB_REF_NAME)
} else {
parsedValues.push(...inputValues.split('\n'))
}
console.log('parsedValues:', parsedValues)
const values = inputValues.split('\n')
console.log('values:', values)

// Validate Parsed Inputs
if (parsedKeys.length !== parsedValues.length) {
return core.setFailed('Keys and Values are not equal in length.')
// Validate Inputs
if (keys.length !== values.length) {
return core.setFailed('Keys and Values length are not equal.')
}

// Update JSON
let file = fs.readFileSync(inputFile)
let data = JSON.parse(file.toString())
for (let i = 0; i < parsedKeys.length; i++) {
const key = parsedKeys[i]
const value = parsedValues[i]
console.log(`-- ${i} -- ${key}: ${value}`)
const file = fs.readFileSync(inputFile)
const data = JSON.parse(file.toString())
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
const value = values[i]
console.log(`--- ${i + 1}: ${key}: ${value}`)
setNestedValue(data, key, value)
}
let result = JSON.stringify(data, null, 2)
fs.writeFileSync(inputFile, result)

// Display Result
const result = JSON.stringify(data, null, 2)
console.log('-'.repeat(40))
console.log(result)
console.log('-'.repeat(40))

// Write File
if (writeFile) {
core.info(`\u001b[32;1mWriting result to file: ${inputFile}`)
fs.writeFileSync(inputFile, result)
} else {
core.info('\u001b[33;1mNot writing file because write is false.')
}

// Set Output
core.setOutput('result', JSON.stringify(data))
} catch (e) {
core.debug(e)
core.info(e.message)
Expand Down

0 comments on commit 02aa6d9

Please sign in to comment.