Skip to content

Commit 337af39

Browse files
authored
chore: fix ruleset updater workflow not working (#140)
1 parent ab4fe82 commit 337af39

File tree

1 file changed

+24
-40
lines changed

1 file changed

+24
-40
lines changed

.github/workflows/default-ruleset.yml

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ jobs:
1111
update:
1212
runs-on: ubuntu-latest
1313
name: Update
14-
outputs:
15-
mutation-happened: ${{ steps.detect.outputs.mutation-happened }}
14+
permissions:
15+
contents: write
1616
steps:
1717
- name: Checkout
1818
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
@@ -21,7 +21,7 @@ jobs:
2121
with:
2222
go-version: oldstable
2323
cache-dependency-path: _tools/ruleset-updater/go.mod
24-
- name: Generate a GitHub token
24+
- name: Generate a GitHub token (DataDog/appsec-event-rules)
2525
id: generate-token
2626
uses: actions/create-github-app-token@df432ceedc7162793a195dd1713ff69aefc7379e # v2.0.6
2727
with:
@@ -31,67 +31,51 @@ jobs:
3131
repositories: appsec-event-rules
3232
permission-contents: read
3333
- name: Update Default Ruleset
34-
run: go -C _tools/ruleset-updater run . -output=${{ github.workspace }}/internal/ruleset/recommended.json.gz
34+
id: ruleset
35+
run: |-
36+
go -C _tools/ruleset-updater run . -output="${{ github.workspace }}/internal/ruleset/recommended.json.gz"
37+
echo "version=$(jq -r '.metadata.rules_version' <(gunzip --to-stdout ${GITHUB_WORKSPACE}/internal/ruleset/recommended.json.gz))" >> "${GITHUB_OUTPUT}"
3538
env:
3639
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
3740
- name: Detect Mutation
3841
id: detect
3942
run: |-
4043
git add .
41-
git diff --staged --patch --exit-code > ${{ runner.temp }}/repo.patch || echo "mutation-happened=true" >> "${GITHUB_OUTPUT}"
42-
- name: Upload Patch
43-
if: steps.detect.outputs.mutation_happened
44-
uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0
45-
with:
46-
name: repo.patch
47-
path: ${{ runner.temp }}/repo.patch
48-
49-
pr:
50-
runs-on: ubuntu-latest
51-
name: Create PR
52-
needs: update
53-
if: needs.update.outputs.mutation-happened
54-
permissions:
55-
contents: write
56-
steps:
57-
- name: Checkout
58-
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
59-
- name: Download Patch
60-
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
61-
with:
62-
name: repo.patch
63-
path: ${{ runner.temp }}
64-
- name: Apply Patch
65-
id: apply
66-
run: |-
67-
git apply ${{ runner.temp }}/repo.patch
68-
echo "version=$(jq -r '.metadata.rules_version' < ./appsec/rules.json)" >> $GITHUB_OUTPUT
44+
git diff --staged --patch --exit-code || echo "mutation-happened=true" >> "${GITHUB_OUTPUT}"
6945
46+
##########################################################################
47+
# Creating a PR with the changes if there are any
7048
- name: Create PR Branch
49+
if: fromJson(steps.detect.outputs.mutation-happened)
7150
id: create-branch
7251
run: |-
7352
branch="automation/default-ruleset-update/${VERSION}"
74-
git push origin "${{ github.sha }}":"refs/heads/${branch}"
53+
git push --force origin "${{ github.sha }}":"refs/heads/${branch}"
7554
echo "branch=${branch}" >> "${GITHUB_OUTPUT}"
7655
git fetch origin "${branch}"
7756
env:
78-
VERSION: ${{ steps.apply.outputs.version }}
79-
- name: Generate a GitHub token
80-
id: generate-token
57+
VERSION: ${{ steps.ruleset.outputs.version }}
58+
- name: Generate a GitHub token (${{ github.repository_owner }}/go-libddwaf)
59+
if: fromJson(steps.detect.outputs.mutation-happened)
60+
id: generate-token-pr
8161
uses: actions/create-github-app-token@df432ceedc7162793a195dd1713ff69aefc7379e # v2.0.6
8262
with:
8363
app-id: ${{ vars.DD_K9_LIBRARY_GO_APP_ID }}
8464
private-key: ${{ secrets.DD_K9_LIBRARY_GO_APP_PRIVATE_KEY }}
65+
owner: ${{ github.repository_owner }}
66+
repositories: go-libddwaf
8567
# We use ghcommit to create signed commits directly using the GitHub API
8668
- name: Create Commit on PR Branch
69+
if: fromJson(steps.detect.outputs.mutation-happened)
8770
uses: planetscale/ghcommit-action@6a383e778f6620afde4bf4b45069d3c6983c1ae2 # v0.2.15
8871
with:
89-
commit_message: "chore: update default ruleset to ${{ steps.apply.outputs.version }}"
72+
commit_message: "chore: update default ruleset to ${{ steps.ruleset.outputs.version }}"
9073
branch: ${{ steps.create-branch.outputs.branch }}
9174
repo: ${{ github.repository }}
9275
env:
93-
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
76+
GITHUB_TOKEN: ${{ steps.generate-token-pr.outputs.token }}
9477
- name: Create PR
78+
if: fromJson(steps.detect.outputs.mutation-happened)
9579
run: |-
9680
git fetch origin "${{ steps.create-branch.outputs.branch }}"
9781
git reset --hard HEAD
@@ -100,5 +84,5 @@ jobs:
10084
--body "Updated default ruleset to ${VERSION}." \
10185
--head="${{ steps.create-branch.outputs.branch }}"
10286
env:
103-
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
104-
VERSION: ${{ steps.apply.outputs.version }}
87+
GITHUB_TOKEN: ${{ steps.generate-token-pr.outputs.token }}
88+
VERSION: ${{ steps.ruleset.outputs.version }}

0 commit comments

Comments
 (0)