Skip to content

Commit 1f4722b

Browse files
Mikaayensongithub-actions[bot]
authored andcommitted
test automatically prevent future merges when a backport fails (#1909)
automatically prevent future merges when a backport fails (cherry picked from commit 4fdd978)
1 parent 429c1c3 commit 1f4722b

File tree

5 files changed

+93
-7
lines changed

5 files changed

+93
-7
lines changed

.github/workflows/backport.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ on:
1111
- closed
1212

1313
jobs:
14+
get-branches:
15+
uses: ./.github/workflows/get-target-branches.yml
16+
1417
label:
1518
runs-on: ubuntu-latest
1619
if: |
@@ -51,12 +54,13 @@ jobs:
5154
(github.event.action == 'labeled' && github.event.label.name == 'backport: auto')
5255
|| (github.event.action == 'closed')
5356
)
57+
needs: get-branches
5458
runs-on: ubuntu-latest
5559
strategy:
5660
max-parallel: 1
5761
matrix:
5862
# 7.17 was intentionally skipped because it was added late and was bug fix only
59-
target_branch: [7.16, '8.0', 8.1, 8.2, 8.3]
63+
target_branch: ${{ fromJSON(needs.get-branches.outputs.branches) }}
6064

6165
steps:
6266
- name: Checkout repo
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Branch Version Status Checks
2+
3+
on:
4+
push:
5+
branches: [ "main", "7.*", "8.*" ]
6+
pull_request:
7+
branches: [ "*" ]
8+
9+
jobs:
10+
get-branches:
11+
uses: ./.github/workflows/get-target-branches.yml
12+
13+
branch-status-checks:
14+
needs: get-branches
15+
runs-on: ubuntu-latest
16+
strategy:
17+
matrix:
18+
target_branch: ${{ fromJSON(needs.get-branches.outputs.branches) }}
19+
20+
steps:
21+
- name: Get Backport Status
22+
id: get_backport_status
23+
uses: fjogeleit/http-request-action@v1
24+
with:
25+
url: "https://api.github.com/repos/elastic/detection-rules/actions/workflows/pythonpackage.yml/runs?per_page=1&branch=${{matrix.target_branch}}"
26+
method: 'GET'
27+
28+
- name: Check Backport Status
29+
uses: actions/github-script@v6
30+
with:
31+
script: |
32+
const workflow_status = ${{ toJSON(fromJSON(steps.get_backport_status.outputs.response).workflow_runs[0].status) }}
33+
const workflow_conclusion = ${{ toJSON(fromJSON(steps.get_backport_status.outputs.response).workflow_runs[0].conclusion) }}
34+
if (workflow_status != 'completed' ||
35+
workflow_conclusion != 'success') {
36+
core.setFailed('Recent Backport status: ' + workflow_status + ', conclusion: ' + workflow_conclusion)
37+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: List Target Branches
2+
3+
on:
4+
workflow_call:
5+
# Map the workflow outputs to job outputs
6+
outputs:
7+
branches:
8+
description: "List of target branches"
9+
value: ${{ jobs.list-target-branches.outputs.matrix }}
10+
11+
jobs:
12+
list-target-branches:
13+
runs-on: ubuntu-latest
14+
outputs:
15+
matrix: ${{ steps.get-branch-list.outputs.matrix }}
16+
steps:
17+
- uses: actions/checkout@v2
18+
19+
- name: Set up Python 3.8
20+
uses: actions/setup-python@v2
21+
with:
22+
python-version: 3.8
23+
24+
- name: Install dependencies
25+
run: |
26+
python -m pip install --upgrade pip
27+
pip install -r requirements.txt
28+
29+
- id: get-branch-list
30+
run: |
31+
python -m detection_rules dev utils get-branches
32+
echo "::set-output name=matrix::$(cat ./detection_rules/etc/target-branches.yml)"

.github/workflows/pythonpackage.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ jobs:
3131
- name: Python License Check
3232
run: |
3333
python -m detection_rules dev license-check
34-
34+
35+
- name: Unit tests
36+
run: |
37+
python -m detection_rules test
38+
3539
- name: Build release package
3640
env:
3741
# only generate the navigator files on push events to main
@@ -48,10 +52,6 @@ jobs:
4852
path: |
4953
releases
5054
51-
- name: Unit tests
52-
run: |
53-
python -m detection_rules test
54-
5555
- name: Update navigator gist files
5656
env:
5757
GITHUB_TOKEN: "${{ secrets.NAVIGATOR_GIST_TOKEN }}"

detection_rules/devtools.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
from .rule_loader import RuleCollection, production_filter
4040
from .schemas import definitions, get_stack_versions
4141
from .semver import Version
42-
from .utils import dict_hash, get_path, load_dump
42+
from .utils import dict_hash, get_path, get_etc_path, load_dump
4343

4444
RULES_DIR = get_path('rules')
4545
GH_CONFIG = Path.home() / ".config" / "gh" / "hosts.yml"
@@ -1075,3 +1075,16 @@ def rule_survey(ctx: click.Context, query, date_range, dump_file, hide_zero_coun
10751075
json.dump(details, f, indent=2, sort_keys=True)
10761076

10771077
return survey_results
1078+
1079+
1080+
@dev_group.group('utils')
1081+
def utils_group():
1082+
"""Commands for dev utility methods."""
1083+
1084+
1085+
@utils_group.command('get-branches')
1086+
@click.option('--outfile', '-o', type=Path, default=get_etc_path("target-branches.yml"), help='File to save output to')
1087+
def get_branches(outfile: Path):
1088+
branch_list = get_stack_versions(drop_patch=True)
1089+
target_branches = json.dumps(branch_list[:-1]) + "\n"
1090+
outfile.write_text(target_branches)

0 commit comments

Comments
 (0)