diff --git a/.github/backport-mapping.js b/.github/backport-mapping.js new file mode 100644 index 0000000000..8eff63bf38 --- /dev/null +++ b/.github/backport-mapping.js @@ -0,0 +1,14 @@ +const process = require('process'); +const fs = require('fs'); +const yaml = require('js-yaml'); + +// Create a backport conversion table from the backport-mapping.yml file +// This is used to map backport labels to directories that should be deleted +let conversion_table; + +try { + conversion_table = yaml.load(fs.readFileSync('.github/backport-mapping.yml', 'utf8')); +} catch (error) { + process.stderr(error); + process.exit(1) +} diff --git a/.github/backport-mapping.yml b/.github/backport-mapping.yml new file mode 100644 index 0000000000..e72843a90a --- /dev/null +++ b/.github/backport-mapping.yml @@ -0,0 +1,2 @@ +"[7,8].*": + "serverless" \ No newline at end of file diff --git a/.github/label-mapping.js b/.github/label-mapping.js new file mode 100644 index 0000000000..385d7b1ad6 --- /dev/null +++ b/.github/label-mapping.js @@ -0,0 +1,47 @@ +const process = require('process'); +const fs = require('fs'); +const yaml = require('js-yaml'); + +// Create a label conversion table from the label-mapping.yml file +// This is used to map existing labels to new labels +let conversion_table; + +try { + conversion_table = yaml.load(fs.readFileSync('.github/label-mapping.yml', 'utf8')); +} catch (error) { + process.stderr(error); + process.exit(1) +} + +// Take existing labels and see if they exist in the yml file +// If they do, keep them. If they don't, remove them from the array +let existing_labels = (process.argv.slice(2)[0]).split(' '); // [ 'stateful serverless something' ] +let labels_that_matter = []; + +existing_labels.forEach((label) => { + // Check to see if the label exists in label-mapping.yml + if (label in conversion_table) { + labels_that_matter.push(label) + } +}) + +// For each existing label that matters, map it to the +// correct backport label +let labels_to_add = []; + +labels_that_matter.forEach((label) => { + labels_to_add.push(...conversion_table[label]) +}) + +// This is hard coded and should be fixed +// Look for `backport-skip` and remove it if it's the only new label to add +const skip = "backport-skip" +if (labels_to_add.includes(skip) && labels_to_add.length != 1){ + labels_to_add.splice(labels_to_add.indexOf(skip), 1) +} + +process.stdout.write(`{ + "remove": "${labels_that_matter}", + "add": "${labels_to_add}" +}`) +process.exit(0) \ No newline at end of file diff --git a/.github/label-mapping.yml b/.github/label-mapping.yml new file mode 100644 index 0000000000..df596062a7 --- /dev/null +++ b/.github/label-mapping.yml @@ -0,0 +1,13 @@ +apm-agent-java: +- 1.x-java +apm-agent-node: +- 5.x-node +cloud: +- ms200 +- ms201 +- ms300 +stateful: +- backport-8.13 +- backport-8.12 +serverless: +- backport-skip diff --git a/.github/workflows/add-backport-labels.yml b/.github/workflows/add-backport-labels.yml new file mode 100644 index 0000000000..059c599f99 --- /dev/null +++ b/.github/workflows/add-backport-labels.yml @@ -0,0 +1,78 @@ +name: Add backport labels + +on: + pull_request: + types: [closed] + +jobs: + remove_and_add_labels: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Get existing labels + id: get_labels + uses: actions/github-script@v7 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const { data: labels } = await github.issues.listLabelsOnIssue({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number + }); + const labelNames = labels.map(label => label.name); + console.log(labelNames.join(',')); + + echo "labelNames=$labelNames" >> "$GITHUB_ENV" + + - name: Map to backport labels + id: map_backport_labels + run: | + # Run node script to map existing labels to backport labels + val=$(node .github/label-mapping.js "$labelNames") + + # Use jq to extract the values as arrays + labels_to_remove=$(jq -r '.remove | split(",")[]' <<< "$val") + labels_to_add=$(jq -r '.add | split(",")[]' <<< "$val") + + # Print the arrays + echo "The following labels will be removed: ${labels_to_remove[@]}" + echo "The following labels will be added: ${labels_to_add[@]}" + + # Save the arrays for a later step + echo "labels_to_remove=$labels_to_remove" >> "$GITHUB_ENV" + echo "labels_to_add=$labels_to_add" >> "$GITHUB_ENV" + # echo "::set-output name=label::${old_labels[random_index]}" + + - name: Remove deployment labels + uses: actions/github-script@v5 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + ${{ labels_to_remove }}.foreach((label) => { + await github.issues.removeLabel({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number, + name: label + }); + }) + + - name: Add backport labels + uses: actions/github-script@v5 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const { data: pr } = await github.pulls.get({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.issue.number + }); + await github.issues.addLabels({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number, + labels: ${{ labels_to_add }} + }); \ No newline at end of file diff --git a/.github/workflows/clean-backports.yml b/.github/workflows/clean-backports.yml new file mode 100644 index 0000000000..a5634405cf --- /dev/null +++ b/.github/workflows/clean-backports.yml @@ -0,0 +1,53 @@ +name: Delete Unused Directories + +on: + pull_request: + types: [opened] + branches: + - "!main" + +jobs: + delete_serverless_dir: + runs-on: ubuntu-latest + if: ${{ github.event.pull_request.head.ref =~ '[7,8].*' }} + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: map dirs + run: | + val=$(node .github/backport-mapping.js "${{ github.event.pull_request.head.ref }}") + + # Use jq to extract the values as arrays + labels_to_remove=$(jq -r '.remove | split(",")[]' <<< "$val") + labels_to_add=$(jq -r '.add | split(",")[]' <<< "$val") + + # Print the arrays + echo "The following labels will be removed: ${labels_to_remove[@]}" + echo "The following labels will be added: ${labels_to_add[@]}" + + # Save the arrays for a later step + echo "labels_to_remove=$labels_to_remove" >> "$GITHUB_ENV" + echo "labels_to_add=$labels_to_add" >> "$GITHUB_ENV" + # echo "::set-output name=label::${old_labels[random_index]}" + + - name: Check if serverless directory exists + id: check_serverless_dir + run: | + if [ -d "serverless" ]; then + echo "::set-output name=dir_exists::true" + else + echo "::set-output name=dir_exists::false" + fi + + - name: Delete serverless directory if exists + if: steps.check_serverless_dir.outputs.dir_exists == 'true' + run: rm -rf serverless + + - name: Commit and push changes + if: steps.check_serverless_dir.outputs.dir_exists == 'true' + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: "Delete serverless directory" + commit_user_name: "GitHub Actions" + commit_user_email: "actions@github.com" \ No newline at end of file