Update Forms JSON #75
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
| name: Update Forms JSON | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| formData: | |
| description: "Form data JSON" | |
| required: true | |
| type: string | |
| jobs: | |
| update-json: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| steps: | |
| - name: Checkout Repository | |
| uses: actions/checkout@v3 | |
| - name: Set Up Node.js | |
| uses: actions/setup-node@v3 | |
| with: | |
| node-version: "18" | |
| - name: Update FormFieldsInfo.json | |
| run: | | |
| # Save the input formData as formData.json | |
| echo '${{ inputs.formData }}' > formData.json | |
| # Load the existing FormFieldsInfo.json (if file is empty, use {} as default) | |
| if [ -s src/data/FormFieldsInfo.json ]; then | |
| existingJson=$(cat src/data/FormFieldsInfo.json) | |
| else | |
| existingJson="{}" | |
| fi | |
| # Load the new form data | |
| newFormData=$(cat formData.json) | |
| # Get the id from the new form data (assuming the id is the first key in the object) | |
| formId=$(echo "$newFormData" | jq -r 'keys[0]') | |
| echo "Form ID: $formId" | |
| # Check if the id exists in the existing JSON and update or append properly | |
| if echo "$existingJson" | jq -e --arg formId "$formId" 'has($formId)' > /dev/null; then | |
| echo "Form ID exists. Updating entry." | |
| # Replace the existing entry | |
| updatedJson=$(jq -s ".[0] * .[1]" <(echo "$existingJson") <(echo "$newFormData")) | |
| else | |
| echo "Form ID does not exist. Adding new entry." | |
| # Add new entry to existing JSON | |
| updatedJson=$(jq -s ".[0] + .[1]" <(echo "$existingJson") <(echo "$newFormData")) | |
| fi | |
| # Save the updated JSON back to FormFieldsInfo.json (formatted) | |
| echo "$updatedJson" | jq '.' > src/data/FormFieldsInfo.json | |
| # Display the updated JSON for verification | |
| cat src/data/FormFieldsInfo.json | |
| - name: Commit and Push Changes | |
| run: | | |
| git config --global user.name "github-actions" | |
| git config --global user.email "actions@github.com" | |
| git add src/data/FormFieldsInfo.json | |
| git commit -m "Updated FormFieldsInfo.json with new or updated submission" || echo "No changes to commit" | |
| git push | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |