-
Notifications
You must be signed in to change notification settings - Fork 0
feat(update-offer): add action to update existing SPCTL offers with new configuration #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DmitrySmv
wants to merge
4
commits into
v1
Choose a base branch
from
feature/SP-6620
base: v1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
eef0bc0
feat(update-offer): add action to update existing SPCTL offers with n…
DmitrySmv 9b7ad18
feat(update-offer): rewrite to node.js (SP-6620)
DmitrySmv 5bd9bc9
feat(update-offer): add @vercel/ncc as a dev dependency, update packa…
DmitrySmv b6225b5
feat(update-offer): update build script in package.json to target ES2…
DmitrySmv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| node_modules |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| # Update Offer Action | ||
|
|
||
| This GitHub Action updates an existing Super Protocol offer using the `spctl` utility. It is written in Node.js to ensure cross-platform compatibility and reliability. | ||
|
|
||
| This action executes the command: `./spctl offers update value <offer_id> --configuration <file_path>`. | ||
|
|
||
| ## Features | ||
|
|
||
| - **Cross-Platform:** Works on runners using Linux, macOS, and Windows. | ||
| - **Validation:** Automatically validates the existence and JSON validity of the configuration file. | ||
| - **Secure:** Follows security best practices. | ||
| - **Simple:** Accepts the minimum required inputs to perform its task. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| This action requires the `spctl` utility to be installed and available in the `PATH` or in the workflow's root directory. It is recommended to use the official `download-spctl` action in a previous step. | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Inputs | ||
|
|
||
| | Parameter | Description | Required | Default | | ||
| | ------------------------- | ------------------------------------------------------------- | -------- | --------------- | | ||
| | `offer_id` | The numeric ID of the offer to be updated. | `true` | - | | ||
| | `configuration_file_path` | The path to the new offer configuration file in JSON format. | `true` | - | | ||
| | `spctl_config_file` | The path to the `spctl` configuration file (e.g., `config.json`). | `false` | `./config.json` | | ||
|
|
||
| ### Outputs | ||
|
|
||
| This action has no outputs. | ||
|
|
||
| ### Example Workflow | ||
|
|
||
| ```yaml | ||
| # .github/workflows/update-my-offer.yml | ||
| name: Update My Special Offer | ||
|
|
||
| on: | ||
| workflow_dispatch: | ||
| inputs: | ||
| target_offer_id: | ||
| description: 'ID of the offer to update' | ||
| required: true | ||
|
|
||
| jobs: | ||
| update_offer: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout repository | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Prepare configuration files | ||
| run: | | ||
| echo "${{ secrets.SPCTL_CONFIG_BASE64 }}" | base64 --decode > ./spctl-config.json | ||
| echo '{ "attributes": { "new_attribute": "new_value" } }' > ./new-offer-config.json | ||
|
|
||
| - name: Download and install SPCTL | ||
| uses: Super-Protocol/sp-build-tools/actions/download-spctl@v1 | ||
| # ... other inputs | ||
|
|
||
| - name: Update Offer Configuration | ||
| uses: Super-Protocol/sp-build-tools/actions/update-offer@main | ||
| with: | ||
| offer_id: ${{ inputs.target_offer_id }} | ||
| configuration_file_path: ./new-offer-config.json | ||
| spctl_config_file: ./spctl-config.json | ||
| ``` | ||
|
|
||
| ## Contributing and Development | ||
|
|
||
| This section provides instructions for developers who want to modify or contribute to this action. | ||
|
|
||
| ### Building | ||
|
|
||
| **This is the most important step after making code changes.** | ||
|
|
||
| This action uses [`@vercel/ncc`](https://github.com/vercel/ncc) to compile the code and all its dependencies into a single file located in the `dist` directory. This is necessary because the `node_modules` directory is not checked into the repository. | ||
|
|
||
| After making any changes to `index.js` or installing new dependencies in `package.json`, you must re-compile the action. | ||
|
|
||
| 1. **Install dependencies (if you haven't already):** | ||
| ```bash | ||
| npm install | ||
| ``` | ||
| 2. **Run the build command:** | ||
| ```bash | ||
| npm run build | ||
| ``` | ||
| 3. **Commit the result:** This command will generate a new `dist/index.js` file. You must commit this file to the repository for your changes to take effect in workflows. | ||
|
|
||
| ### Local Debugging | ||
|
|
||
| You can easily debug this action locally using VS Code. | ||
|
|
||
| 1. **Setup Test Files:** In the action's directory, create the files needed for the test run: | ||
| - `test-config.json`: | ||
| ```json | ||
| { "description": "This is a test config" } | ||
| ``` | ||
| - A mock executable file named `spctl`: | ||
| ```bash | ||
| #!/bin/bash | ||
| echo "Mock spctl executed with args: $@" | ||
| exit 0 | ||
| ``` | ||
| Make it executable: `chmod +x spctl`. | ||
|
|
||
| 2. **Create `launch.json`:** In the `.vscode` directory, create a `launch.json` file: | ||
| ```json | ||
| { | ||
| "version": "0.2.0", | ||
| "configurations": [ | ||
| { | ||
| "type": "node", | ||
| "request": "launch", | ||
| "name": "Debug Local Action", | ||
| "program": "${workspaceFolder}/index.js", | ||
| "env": { | ||
| "INPUT_OFFER_ID": "12345", | ||
| "INPUT_CONFIGURATION_FILE_PATH": "${workspaceFolder}/test-config.json", | ||
| "INPUT_SPCTL_CONFIG_FILE": "${workspaceFolder}/spctl-config.json" | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
| 3. **Run the Debugger:** Open `index.js`, set a breakpoint, and press F5. | ||
|
|
||
| ## License | ||
|
|
||
| This project is licensed under the [Business Source License 1.1](https://mariadb.com/bsl11/). | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| name: "Update offer" | ||
| description: "Updates an existing SPCTL offer by providing a new configuration file." | ||
|
|
||
| inputs: | ||
| spctl_config_file: | ||
| description: "Path to spctl config file" | ||
| default: "./config.json" | ||
| offer_id: | ||
| description: "The ID of the offer to update." | ||
| required: true | ||
| configuration_file_path: | ||
| description: "Path to the new JSON configuration file for the offer." | ||
| required: true | ||
|
|
||
| runs: | ||
| using: "node20" | ||
| main: "dist/index.js" |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| const core = require('@actions/core'); | ||
| const exec = require('@actions/exec'); | ||
| const fs = require('fs'); | ||
|
|
||
| async function run() { | ||
| try { | ||
| const offerId = core.getInput('offer_id', { required: true }); | ||
| const configFilePath = core.getInput('configuration_file_path', { required: true }); | ||
| const spctlConfigFile = core.getInput('spctl_config_file'); | ||
|
|
||
| core.startGroup('Validating inputs'); | ||
| if (!fs.existsSync(configFilePath)) { | ||
| throw new Error(`Configuration file does not exist at '${configFilePath}'.`); | ||
| } | ||
| core.info(`✅ Configuration file found.`); | ||
| try { | ||
| JSON.parse(fs.readFileSync(configFilePath, 'utf8')); | ||
| core.info('✅ File content is valid JSON.'); | ||
| } catch (error) { | ||
| throw new Error(`The file '${configFilePath}' does not contain valid JSON. Error: ${error.message}`); | ||
| } | ||
| core.endGroup(); | ||
|
|
||
| core.startGroup(`Updating offer ${offerId}`); | ||
| const args = [ | ||
| 'offers', 'update', 'value', offerId, | ||
| '--configuration', configFilePath, | ||
| '--config', spctlConfigFile | ||
| ]; | ||
DmitrySmv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| core.info(`Running command: ./spctl ${args.join(' ')}`); | ||
| await exec.exec('./spctl', args); | ||
DmitrySmv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| core.info(`Offer ${offerId} updated successfully.`); | ||
| core.endGroup(); | ||
|
|
||
| } catch (error) { | ||
| core.setFailed(error.message); | ||
| } | ||
| } | ||
|
|
||
| run(); | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "name": "update-offer-action-nodejs", | ||
| "version": "1.0.0", | ||
| "description": "Updates an SPCTL offer using a Node.js action.", | ||
| "author": "SuperProtocol", | ||
| "license": "BSL-1.1", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "build": "ncc build index.js -o dist --target es2022 -m" | ||
| }, | ||
| "dependencies": { | ||
| "@actions/core": "1.11.0", | ||
| "@actions/exec": "1.1.1" | ||
| }, | ||
| "devDependencies": { | ||
| "@vercel/ncc": "0.38.3" | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.