Skip to content

Commit

Permalink
draft backport automation
Browse files Browse the repository at this point in the history
  • Loading branch information
bmorelli25 committed May 14, 2024
1 parent d8aad09 commit 5e09404
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .github/backport-mapping.js
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 2 additions & 0 deletions .github/backport-mapping.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"[7,8].*":
"serverless"
47 changes: 47 additions & 0 deletions .github/label-mapping.js
Original file line number Diff line number Diff line change
@@ -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)
13 changes: 13 additions & 0 deletions .github/label-mapping.yml
Original file line number Diff line number Diff line change
@@ -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
78 changes: 78 additions & 0 deletions .github/workflows/add-backport-labels.yml
Original file line number Diff line number Diff line change
@@ -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 }}
});
53 changes: 53 additions & 0 deletions .github/workflows/clean-backports.yml
Original file line number Diff line number Diff line change
@@ -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: "[email protected]"

0 comments on commit 5e09404

Please sign in to comment.