-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python conversion of maintenance commands (#2739)
* updating atomics count in README.md [ci skip] * converting python * rename * fix path * minor refactor --------- Co-authored-by: publish bot <[email protected]>
- Loading branch information
Showing
17 changed files
with
900 additions
and
751 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ name: generate-svg-counter | |
|
||
on: | ||
push: | ||
branches: ["master"] | ||
branches: [ "master" ] | ||
|
||
jobs: | ||
generate-counter: | ||
|
@@ -15,26 +15,28 @@ jobs: | |
run: pipx install poetry | ||
- uses: actions/setup-python@v5 | ||
with: | ||
python-version: '3.11.2' | ||
python-version: '3.11.2' | ||
cache: 'poetry' | ||
- name: Install dependencies | ||
run: poetry install --no-interaction --no-root | ||
- name: Generate shields.io URL | ||
run: poetry run python bin/generate_counter.py -f atomics/ | ||
run: poetry run python atomic_red_team/runner.py generate-counter | ||
id: counter | ||
- name: Update README | ||
run: | | ||
echo ${{ steps.counter.outputs.result }} | ||
sed -i "s|https://img.shields.io/badge/Atomics-.*-flat.svg|${{ steps.counter.outputs.result }}|" README.md | ||
shell: bash | ||
- name: Generate and commit unique GUIDs for each atomic test | ||
run: poetry run python atomic_red_team/runner.py generate-guids | ||
- name: update github with new site | ||
run: | | ||
# configure git to prep for commit | ||
git config user.email "[email protected]" | ||
git config user.name "publish bot" | ||
git config --global push.default simple | ||
git add README.md | ||
git commit --allow-empty -m "updating atomics count in README.md [ci skip]" | ||
git commit --allow-empty -m "updating atomics count and guids [ci skip]" | ||
# push quietly to prevent showing the token in log | ||
# no need to provide any credentials | ||
git push --force |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name: generate-docs | ||
on: | ||
push: | ||
branches: ["master"] | ||
branches: [ "master" ] | ||
|
||
jobs: | ||
generate-docs: | ||
|
@@ -15,29 +15,7 @@ jobs: | |
uses: ruby/setup-ruby@v1 | ||
with: | ||
ruby-version: 2.7 | ||
bundler-cache: true | ||
|
||
- name: Generate and commit unique GUIDs for each atomic test | ||
run: | | ||
bin/generate-guids.rb | ||
echo "" | ||
echo "" | ||
git status | ||
echo "" | ||
echo "" | ||
git diff-index HEAD -- | ||
if git diff-index --quiet HEAD -- ; then | ||
echo "Not committing GUID changes because there are no changes" | ||
else | ||
git config credential.helper 'cache --timeout=120' | ||
git config user.email "[email protected]" | ||
git config user.name "Atomic Red Team GUID generator" | ||
git add atomics | ||
git commit -am "Generate GUIDs from job=$GITHUB_JOB branch=$GITHUB_REF_NAME [skip ci]" | ||
git push origin $GITHUB_REF_NAME -f | ||
fi | ||
bundler-cache: true | ||
|
||
- name: generate markdown docs for atomics | ||
run: | | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from os.path import dirname, realpath | ||
|
||
base_path = dirname(dirname(realpath(__file__))) | ||
atomics_path = f"{base_path}/atomics" | ||
used_guids_file = f"{atomics_path}/used_guids.txt" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import re | ||
import uuid | ||
from typing import List | ||
|
||
from ruamel.yaml import YAML | ||
|
||
from common import used_guids_file | ||
|
||
yaml = YAML(typ="safe") | ||
|
||
|
||
def get_unique_guid(guids: List[str]): | ||
# This function should return a unique GUID that's not in the used_guids_file. | ||
guid = str(uuid.uuid4()) | ||
if guid not in guids: | ||
with open(used_guids_file, "a") as f: # append mode | ||
f.write(guid) | ||
return guid | ||
else: | ||
return get_unique_guid(guids) | ||
|
||
|
||
def generate_guids_for_yaml(path, get_guid): | ||
with open(path, "r") as file: | ||
og_text = file.read() | ||
|
||
# Add the "auto_generated_guid:" element after the "- name:" element if it isn't already there | ||
text = re.sub( | ||
r"(?i)(^([ \t]*-[ \t]*)name:.*$(?!\s*auto_generated_guid))", | ||
lambda m: f"{m.group(1)}\n{m.group(2).replace('-', ' ')}auto_generated_guid:", | ||
og_text, | ||
flags=re.MULTILINE, | ||
) | ||
|
||
# Fill the "auto_generated_guid:" element in if it doesn't contain a guid | ||
text = re.sub( | ||
r"(?i)^([ \t]*auto_generated_guid:)(?!([ \t]*[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12})).*$", | ||
lambda m: f"{m.group(1)} {get_guid()}", | ||
text, | ||
flags=re.MULTILINE, | ||
) | ||
if text != og_text: | ||
with open(path, "wb") as file: | ||
# using wb mode instead of w. If not, the end of line characters are auto-converted to OS specific ones. | ||
file.write(text.encode()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.