Skip to content

Commit

Permalink
Add prdoc command to /cmd (#5661)
Browse files Browse the repository at this point in the history
Fixes #5647

- [x]  create new command (reusing original py module)
- [x]  add unit-test cases (just the fact of proxy)
- [x]  update docs
  • Loading branch information
mordamax authored Sep 10, 2024
1 parent 12eeb5d commit f0e420a
Show file tree
Hide file tree
Showing 15 changed files with 149 additions and 49 deletions.
21 changes: 19 additions & 2 deletions .github/scripts/cmd/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
import json
import argparse
import _help
import importlib.util

_HelpAction = _help._HelpAction

f = open('.github/workflows/runtimes-matrix.json', 'r')
runtimesMatrix = json.load(f)

print(f'runtimesMatrix: {runtimesMatrix}\n')

runtimeNames = list(map(lambda x: x['name'], runtimesMatrix))

common_args = {
Expand Down Expand Up @@ -69,6 +68,17 @@
for arg, config in common_args.items():
parser_ui.add_argument(arg, **config)

"""
PRDOC
"""
# Import generate-prdoc.py dynamically
spec = importlib.util.spec_from_file_location("generate_prdoc", ".github/scripts/generate-prdoc.py")
generate_prdoc = importlib.util.module_from_spec(spec)
spec.loader.exec_module(generate_prdoc)

parser_prdoc = subparsers.add_parser('prdoc', help='Generates PR documentation')
generate_prdoc.setup_parser(parser_prdoc)

def main():
global args, unknown, runtimesMatrix
args, unknown = parser.parse_known_args()
Expand Down Expand Up @@ -215,6 +225,13 @@ def main():
print('❌ Failed to format code')
sys.exit(1)

elif args.command == 'prdoc':
# Call the main function from ./github/scripts/generate-prdoc.py module
exit_code = generate_prdoc.main(args)
if exit_code != 0 and not args.continue_on_fail:
print('❌ Failed to generate prdoc')
sys.exit(exit_code)

print('🚀 Done')

if __name__ == '__main__':
Expand Down
18 changes: 18 additions & 0 deletions .github/scripts/cmd/test_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@ def setUp(self):
self.patcher3 = patch('argparse.ArgumentParser.parse_known_args')
self.patcher4 = patch('os.system', return_value=0)
self.patcher5 = patch('os.popen')
self.patcher6 = patch('importlib.util.spec_from_file_location', return_value=MagicMock())
self.patcher7 = patch('importlib.util.module_from_spec', return_value=MagicMock())
self.patcher8 = patch('cmd.generate_prdoc.main', return_value=0)

self.mock_open = self.patcher1.start()
self.mock_json_load = self.patcher2.start()
self.mock_parse_args = self.patcher3.start()
self.mock_system = self.patcher4.start()
self.mock_popen = self.patcher5.start()
self.mock_spec_from_file_location = self.patcher6.start()
self.mock_module_from_spec = self.patcher7.start()
self.mock_generate_prdoc_main = self.patcher8.start()

# Ensure that cmd.py uses the mock_runtimes_matrix
import cmd
Expand All @@ -48,6 +54,9 @@ def tearDown(self):
self.patcher3.stop()
self.patcher4.stop()
self.patcher5.stop()
self.patcher6.stop()
self.patcher7.stop()
self.patcher8.stop()

def test_bench_command_normal_execution_all_runtimes(self):
self.mock_parse_args.return_value = (argparse.Namespace(
Expand Down Expand Up @@ -317,5 +326,14 @@ def test_update_ui_command(self, mock_system, mock_parse_args):
mock_exit.assert_not_called()
mock_system.assert_called_with('sh ./scripts/update-ui-tests.sh')

@patch('argparse.ArgumentParser.parse_known_args', return_value=(argparse.Namespace(command='prdoc', continue_on_fail=False), []))
@patch('os.system', return_value=0)
def test_prdoc_command(self, mock_system, mock_parse_args):
with patch('sys.exit') as mock_exit:
import cmd
cmd.main()
mock_exit.assert_not_called()
self.mock_generate_prdoc_main.assert_called_with(mock_parse_args.return_value[0])

if __name__ == '__main__':
unittest.main()
31 changes: 21 additions & 10 deletions .github/scripts/generate-prdoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,28 @@ def yaml_multiline_string_presenter(dumper, data):

yaml.add_representer(str, yaml_multiline_string_presenter)

def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--pr", type=int, required=True)
parser.add_argument("--audience", type=str, default="TODO")
parser.add_argument("--bump", type=str, default="TODO")
parser.add_argument("--force", type=str)
return parser.parse_args()
# parse_args is also used by cmd/cmd.py
def setup_parser(parser=None):
if parser is None:
parser = argparse.ArgumentParser()
parser.add_argument("--pr", type=int, required=True, help="The PR number to generate the PrDoc for." )
parser.add_argument("--audience", type=str, default="TODO", help="The audience of whom the changes may concern.")
parser.add_argument("--bump", type=str, default="TODO", help="A default bump level for all crates.")
parser.add_argument("--force", type=str, help="Whether to overwrite any existing PrDoc.")

return parser

if __name__ == "__main__":
args = parse_args()
def main(args):
force = True if (args.force or "false").lower() == "true" else False
print(f"Args: {args}, force: {force}")
setup_yaml()
from_pr_number(args.pr, args.audience, args.bump, force)
try:
from_pr_number(args.pr, args.audience, args.bump, force)
return 0
except Exception as e:
print(f"Error generating prdoc: {e}")
return 1

if __name__ == "__main__":
args = setup_parser().parse_args()
main(args)
6 changes: 6 additions & 0 deletions .github/scripts/generate-prdoc.requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
requests
cargo-workspace
PyGithub
whatthepatch
pyyaml
toml
4 changes: 4 additions & 0 deletions .github/workflows/check-cargo-check-runtimes.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
name: Check Cargo Check Runtimes

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/check-features.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
check-features:
runs-on: ubuntu-latest
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/check-labels.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
name: Check labels

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

on:
pull_request:
types: [labeled, opened, synchronize, unlabeled]
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/check-licenses.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ on:
pull_request:
merge_group:

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

permissions:
packages: read

Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/check-links.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ on:
types: [opened, synchronize, reopened, ready_for_review]
merge_group:

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

permissions:
packages: read

Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/check-prdoc.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
name: Check PRdoc

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

on:
pull_request:
types: [labeled, opened, synchronize, unlabeled]
Expand Down
8 changes: 6 additions & 2 deletions .github/workflows/cmd-tests.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
name: Command Bot Tests

on:
pull_request:
pull_request:

permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: python3 .github/scripts/cmd/test_cmd.py
- run: python3 .github/scripts/cmd/test_cmd.py
Loading

0 comments on commit f0e420a

Please sign in to comment.