From 199a1c5c614513c3a3458ecdb21763c98484be54 Mon Sep 17 00:00:00 2001 From: Ankita Sahu <71656941+SAHU-01@users.noreply.github.com> Date: Mon, 21 Oct 2024 08:36:36 +0530 Subject: [PATCH] Workflow updated Signed-off-by: Ankita Sahu <71656941+SAHU-01@users.noreply.github.com> --- .github/workflows/feature-list.yml | 87 +++++++++++++++++++++++++ .github/workflows/new-feature-check.yml | 71 ++++++++++++++++++++ .github/workflows/pricing-list.yml | 57 ---------------- 3 files changed, 158 insertions(+), 57 deletions(-) create mode 100644 .github/workflows/feature-list.yml create mode 100644 .github/workflows/new-feature-check.yml delete mode 100644 .github/workflows/pricing-list.yml diff --git a/.github/workflows/feature-list.yml b/.github/workflows/feature-list.yml new file mode 100644 index 00000000..812fb4f8 --- /dev/null +++ b/.github/workflows/feature-list.yml @@ -0,0 +1,87 @@ +name: Feature List + +on: + workflow_dispatch: + inputs: + spreadsheet_uri: + description: 'Link of the spreadsheet containing subscription details.' + type: string + required: true + +jobs: + update-feature-data: + runs-on: ubuntu-latest + env: + FEATURES_FILE: 'data/features.json' + + steps: + - name: Trigger Feature List workflow and wait for completion + uses: actions/github-script@v6 + with: + github-token: ${{secrets.GH_ACCESS_TOKEN}} + script: | + const result = await github.rest.actions.createWorkflowDispatch({ + owner: 'layer5labs', + repo: 'meshery-extensions-packages', + workflow_id: 'generate-feature-list.yml', + ref: 'master', + inputs: { + spreadsheet_uri: '${{ secrets.INPUT_SPREADSHEET_URI }}' + } + }); + + console.log("Triggered workflow, waiting for completion..."); + + while (true) { + const runs = await github.rest.actions.listWorkflowRuns({ + owner: 'layer5labs', + repo: 'meshery-extensions-packages', + workflow_id: 'generate-feature-list.yml' + }); + + if (runs.data.workflow_runs[0].status === 'completed') { + console.log("Workflow completed"); + break; + } + + console.log("Waiting for workflow to complete..."); + await new Promise(resolve => setTimeout(resolve, 30000)); + } + + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Create data directory if it doesn't exist + run: mkdir -p data + + - name: Download new feature data + uses: actions/github-script@v6 + with: + github-token: ${{secrets.GH_ACCESS_TOKEN}} + script: | + const fs = require('fs'); + const path = require('path'); + + const { data } = await github.rest.repos.getContent({ + owner: 'layer5labs', + repo: 'meshery-extensions-packages', + path: 'feature-data.json' + }); + + const newContent = Buffer.from(data.content, 'base64').toString('utf-8'); + fs.writeFileSync('${{ env.FEATURES_FILE }}', newContent); + + - name: Update or create features.json + run: | + if [ -f "${{ env.FEATURES_FILE }}" ]; then + echo "Updating existing features.json" + else + echo "Creating new features.json" + cp "${{ env.FEATURES_FILE }}" data/features.json + fi + + - name: Commit changes + uses: stefanzweifel/git-auto-commit-action@v5 + with: + commit_message: Updated feature data + file_pattern: ${{ env.FEATURES_FILE }} \ No newline at end of file diff --git a/.github/workflows/new-feature-check.yml b/.github/workflows/new-feature-check.yml new file mode 100644 index 00000000..b4660e14 --- /dev/null +++ b/.github/workflows/new-feature-check.yml @@ -0,0 +1,71 @@ +name: Nightly Google Sheet Check + +on: + schedule: + - cron: '0 0 * * *' # Run every day at midnight UTC + +jobs: + check-for-updates: + runs-on: ubuntu-latest + steps: + - name: Check for Google Sheet updates + id: sheet-check + uses: actions/github-script@v6 + with: + github-token: ${{secrets.GH_ACCESS_TOKEN}} + script: | + const { GoogleSpreadsheet } = require('google-spreadsheet'); + + async function checkForUpdates() { + const doc = new GoogleSpreadsheet('${{ secrets.INPUT_SPREADSHEET_URI }}'); + await doc.useServiceAccountAuth({ + client_email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL, + private_key: process.env.GOOGLE_PRIVATE_KEY, + }); + + await doc.loadInfo(); + const sheet = doc.sheetsByIndex[0]; + const lastModified = sheet.lastModified; + + const cache = await github.rest.actions.getActionsCacheList({ + owner: context.repo.owner, + repo: context.repo.repo, + }); + + let lastKnownModified = null; + if (cache.data.actions_caches.length > 0) { + lastKnownModified = new Date(cache.data.actions_caches[0].last_accessed_at); + } + + if (!lastKnownModified || lastModified > lastKnownModified) { + await github.rest.actions.createActionsCacheEntry({ + owner: context.repo.owner, + repo: context.repo.repo, + key: 'last-modified-time', + ref: context.ref, + cache_data: lastModified.toISOString(), + }); + return true; + } + + return false; + } + + const hasUpdates = await checkForUpdates(); + core.setOutput('has-updates', hasUpdates); + + - name: Trigger Subscriptions Plan workflow + if: steps.sheet-check.outputs.has-updates == 'true' + uses: actions/github-script@v6 + with: + github-token: ${{secrets.GH_ACCESS_TOKEN}} + script: | + github.rest.actions.createWorkflowDispatch({ + owner: context.repo.owner, + repo: context.repo.repo, + workflow_id: 'feature-list.yml', + ref: 'main', + inputs: { + spreadsheet_uri: '${{ secrets.INPUT_SPREADSHEET_URI }}' + } + }); \ No newline at end of file diff --git a/.github/workflows/pricing-list.yml b/.github/workflows/pricing-list.yml deleted file mode 100644 index 9fc94ac3..00000000 --- a/.github/workflows/pricing-list.yml +++ /dev/null @@ -1,57 +0,0 @@ -name: Subscriptions Plan - -on: - workflow_dispatch: - inputs: - spreadsheet_uri: - description: 'Link of the spreadsheet containing subscription details.' - type: string - required: true - -jobs: - update-pricing-and-frontmatter: - runs-on: ubuntu-latest - env: - PRICING_FILE: 'pricing-data.json' - - steps: - - name: Trigger Pricing List workflow and wait for completion - id: trigger-workflow - run: | - WORKFLOW_ID=$(curl -X POST \ - -H "Accept: application/vnd.github+json" \ - -H "Authorization: token ${{ secrets.GH_ACCESS_TOKEN }}" \ - https://api.github.com/repos/layer5labs/meshery-extensions-packages/actions/workflows/generate-pricing-list.yml/dispatches \ - -d '{"ref":"master", "inputs":{"spreadsheet_uri":"${{ inputs.spreadsheet_uri }}"}}' \ - -s -o /dev/null -w "%{http_code}") - - echo "Triggered workflow, waiting for completion..." - - while true; do - STATUS=$(curl -H "Authorization: token ${{ secrets.GH_ACCESS_TOKEN }}" \ - https://api.github.com/repos/layer5labs/meshery-extensions-packages/actions/runs | jq '.workflow_runs[0].status' -r) - if [ "$STATUS" = "completed" ]; then - echo "Workflow completed" - break - fi - echo "Waiting for workflow to complete..." - sleep 30 - done - - - name: Checkout repository - uses: actions/checkout@v4 - - - name: Setup Go - uses: actions/setup-go@v5 - with: - go-version: '1.21' - - - name: Run update_frontmatter.go - run: go run update_frontmatter.go "${{ env.PRICING_FILE }}" - - - name: Commit and push if there are changes - run: | - git config --local user.email "action@github.com" - git config --local user.name "GitHub Action" - git add ${{ env.PRICING_FILE }} - git diff --quiet && git diff --staged --quiet || (git commit -m "Update front matter based on new pricing data" && git push) \ No newline at end of file