Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions release/release_info.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
"docs",
"tests"
],
"edit": {
".github/workflows/build.yml": "switch_workflow_to_pull_request_target",
".github/workflows/owners-redhat.yml": "switch_workflow_to_pull_request_target"
},
"ignore": [
".github/workflows/release.yml",
".github/workflows/nightly_test.yml",
Expand All @@ -36,6 +40,10 @@
"scripts",
"tests"
],
"edit": {
".github/workflows/build.yml": "switch_workflow_to_pull_request_target",
".github/workflows/owners-redhat.yml": "switch_workflow_to_pull_request_target"
},
"ignore": [
".github/workflows/release.yml",
".github/workflows/nightly_test.yml",
Expand Down
11 changes: 11 additions & 0 deletions scripts/src/release/release_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ def get_replaces(from_repo, to_repo, directory):
return []


def get_edits(from_repo, to_repo, directory):
print(f"get edits for {from_repo} to {to_repo} ")
info = _get_release_info(directory)
if from_repo in info:
if "edit" in info[from_repo][to_repo]:
print(f"edits found: {info[from_repo][to_repo]['edit']}")
return info[from_repo][to_repo]["edit"]
print("no edits found")
return {}


def get_merges(from_repo, to_repo, directory):
print(f"get merges for {from_repo} to {to_repo}")
info = _get_release_info(directory)
Expand Down
61 changes: 61 additions & 0 deletions scripts/src/release/releaser.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
"""

import argparse
import difflib
import fileinput
import os
import re
import shutil
import sys

Expand All @@ -37,6 +40,52 @@
STAGE_PR_BRANCH_NAME_PREFIX = "Release-"


def switch_workflow_to_pull_request_target(workflow_path: str):
"""Edit function that modifies the trigger of a workflow from pull_request
to pull_request_target.

It ensures that a single modification occurs and outputs a diff.

Args:
workflow_path (str): Path to the workflow to modify
"""
replaced = False
with fileinput.FileInput(workflow_path, inplace=True, backup=".bak") as file:
for line in file:
if replaced:
print(line, end="")
continue
new_line, count = re.subn(
"^ pull_request:$",
r" pull_request_target:",
line,
)
if count > 0:
replaced = True
print(new_line, end="")

with open(workflow_path, encoding="utf-8") as workflow_new_content:
with open(f"{workflow_path}.bak", encoding="utf-8") as workflow_bkp_content:
diff = difflib.unified_diff(
workflow_bkp_content.readlines(),
workflow_new_content.readlines(),
fromfile=f"{workflow_path}.bak",
tofile=workflow_path,
)
for line in diff:
print(line, end="")

os.remove(f"{workflow_path}.bak")


# Mapping of allowed edit functions
# This ensures only a predefined set of edit functions can be ran.
# This also acts as a cast of str to callable.
EDIT_FUNCTIONS = {
"switch_workflow_to_pull_request_target": switch_workflow_to_pull_request_target,
}


def make_required_changes(release_info_dir, origin, destination):
print(f"Make required changes from {origin} to {destination}")

Expand Down Expand Up @@ -68,6 +117,18 @@ def make_required_changes(release_info_dir, origin, destination):
print(f"Replace file {replace_this} with {with_this}")
shutil.copy2(with_this, replace_this)

edits = release_info.get_edits(from_repository, to_repository, release_info_dir)

for filename, function in edits.items():
to_edit = f"{destination}/{filename}"
try:
edit_function = EDIT_FUNCTIONS[function]
except KeyError as e:
print(f"Edit function {function} not allowed")
raise e
print(f"Run edit function {function} on {to_edit}")
edit_function(to_edit)

merges = release_info.get_merges(from_repository, to_repository, release_info_dir)

for merge in merges:
Expand Down
Loading