From 4916c97ffd4998824d5e67201ed742c075c3dcb3 Mon Sep 17 00:00:00 2001 From: Aleksey Khoroshilov Date: Tue, 9 Jul 2024 21:27:59 +0700 Subject: [PATCH] Add PR seed upload workflow. --- .github/workflows/scripts/comment.js | 37 +++++++ .github/workflows/upload-test-seed.yml | 99 +++++++++++++++++++ src/seed_tools/commands/create_seed.ts | 1 + .../AdBlockComponentUpdateIntervalStudy.json | 35 +++++++ 4 files changed, 172 insertions(+) create mode 100644 .github/workflows/scripts/comment.js create mode 100644 .github/workflows/upload-test-seed.yml create mode 100644 studies/AdBlockComponentUpdateIntervalStudy.json diff --git a/.github/workflows/scripts/comment.js b/.github/workflows/scripts/comment.js new file mode 100644 index 00000000..b97e1b32 --- /dev/null +++ b/.github/workflows/scripts/comment.js @@ -0,0 +1,37 @@ +// Creates or updates a single comment which is looked up by the workflow +// reference. Workflow reference is unique for each workflow, but is persistent +// across runs. + +module.exports = async (github, context, commentBody) => { + const assert = require('assert'); + assert(commentBody); + + const uniqueCommentTag = ``; + commentBody += `\n${uniqueCommentTag}`; + + const comments = await github.rest.issues.listComments({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + }); + + const existingComment = comments.data.find((comment) => + comment.body.includes(uniqueCommentTag), + ); + + if (existingComment) { + await github.rest.issues.updateComment({ + comment_id: existingComment.id, + owner: context.repo.owner, + repo: context.repo.repo, + body: commentBody, + }); + } else { + await github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: commentBody, + }); + } +}; diff --git a/.github/workflows/upload-test-seed.yml b/.github/workflows/upload-test-seed.yml new file mode 100644 index 00000000..f5ea666e --- /dev/null +++ b/.github/workflows/upload-test-seed.yml @@ -0,0 +1,99 @@ +name: Upload Test Seed + +on: + pull_request: + paths: + - 'studies/**' + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4 + + - name: Set upload in progress status + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 + with: + script: | + const commentBody = + `## 🔄 Test Seed Upload In Progress + + A new seed file is currently being uploaded for this pull request. + + ### What's Next? + + - The upload process typically takes a few minutes. + - Once the upload is complete, a follow-up comment will provide the seed details and testing instructions. + ` + const comment = require('.github/workflows/scripts/comment.js') + await comment(github, context, commentBody) + + - name: Install dependencies and run tests + run: | + npm install + npm run typecheck:scripts + npm run build:proto + npm run typecheck + npm run test + + - name: Generate seed + run: | + npm run seed_tools -- create_seed studies seed.bin + + - name: Upload seed + env: + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_PRODUCTION_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_PRODUCTION_SECRET_ACCESS_KEY }} + AWS_REGION: us-west-2 + CLOUDFRONT_DISTRIBUTION_ID: ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }} + REMOTE_SEED_PATH: 'pull/${{github.event.pull_request.number}}/seed' + run: | + gzip -c seed.bin | aws s3 cp - "s3://brave-production-griffin-origin/$REMOTE_SEED_PATH" \ + --content-type application/octet-stream \ + --content-encoding gzip + + INVALIDATION_ID=$(aws cloudfront create-invalidation --distribution-id "$CLOUDFRONT_DISTRIBUTION_ID" --paths "/$REMOTE_SEED_PATH" --query 'Invalidation.Id' --output text) + aws cloudfront wait invalidation-completed --distribution-id "$CLOUDFRONT_DISTRIBUTION_ID" --id "$INVALIDATION_ID" + + - name: Set uploaded status + uses: actions/github-script@v7 + with: + script: | + console.log(context) + console.log(process.env) + const fs = require('fs'); + const variationsServerURL = `https://griffin.brave.com/pull/${{github.event.pull_request.number}}/seed`; + const serialNumberContent = fs.readFileSync('serialnumber', 'utf8'); + const commentBody = + `## 🚀 Test Seed Upload Successful + + To test the new seed, launch the browser with the following command line: + \`\`\` + --accept-empty-variations-seed-signature --variations-server-url=${variationsServerURL} + \`\`\` + + #### Seed Details + - Serial Number: \`${serialNumberContent}\` + - Upload Date: \`${new Date().toISOString()}\` + ` + const comment = require('.github/workflows/scripts/comment.js') + await comment(github, context, commentBody) + + - name: Set failed status + if: failure() + uses: actions/github-script@v7 + with: + script: | + const failureCommentBody = + `## ❌ Test Seed Upload Failed + + Unfortunately, the seed upload failed. + + ### What's Next? + + - Please review Pull Request checks statuses for more information. + - Push a new commit to trigger a new upload attempt. + ` + const comment = require('.github/workflows/scripts/comment.js') + await comment(github, context, commentBody) diff --git a/src/seed_tools/commands/create_seed.ts b/src/seed_tools/commands/create_seed.ts index d00da3fe..0b6c5bfa 100644 --- a/src/seed_tools/commands/create_seed.ts +++ b/src/seed_tools/commands/create_seed.ts @@ -24,6 +24,7 @@ export default new Command('create_seed') .option( '--serial_number_path ', 'file path to write the serial number to', + './serialnumber', ) .option('--mock_serial_number ', 'mock serial number') .action(main); diff --git a/studies/AdBlockComponentUpdateIntervalStudy.json b/studies/AdBlockComponentUpdateIntervalStudy.json new file mode 100644 index 00000000..816851c8 --- /dev/null +++ b/studies/AdBlockComponentUpdateIntervalStudy.json @@ -0,0 +1,35 @@ +[ + { + "name": "AdBlockComponentUpdateIntervalStudy", + "experiment": [ + { + "name": "Enabled", + "probability_weight": 100, + "feature_association": { + "enable_feature": [ + "AdBlockDefaultResourceUpdateInterval" + ] + }, + "param": [ + { + "name": "update_interval_mins", + "value": "1" + } + ] + } + ], + "filter": { + "channel": [ + "NIGHTLY", + "BETA", + "RELEASE" + ], + "platform": [ + "PLATFORM_WINDOWS", + "PLATFORM_MAC", + "PLATFORM_LINUX", + "PLATFORM_ANDROID" + ] + } + } +]