Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/mak-add-prdoc-command'
Browse files Browse the repository at this point in the history
  • Loading branch information
mordamax committed Sep 10, 2024
2 parents d887804 + 77dad4f commit e75726c
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 43 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.parse_args(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()
23 changes: 17 additions & 6 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()
# 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)
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()

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
53 changes: 27 additions & 26 deletions .github/workflows/cmd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Command

on:
issue_comment: # listen for comments on issues
types: [ created ]
types: [created]

permissions: # allow the action to comment on the PR
contents: write
Expand Down Expand Up @@ -35,14 +35,14 @@ jobs:
try {
const org = '${{ github.event.repository.owner.login }}';
const username = '${{ github.event.comment.user.login }}';
const membership = await github.rest.orgs.checkMembershipForUser({
org: org,
username: username
});
console.log(membership, membership.status, membership.status === 204);
if (membership.status === 204) {
return 'true';
} else {
Expand All @@ -52,7 +52,7 @@ jobs:
} catch (error) {
console.log(error)
}
return 'false';
reject-non-members:
Expand Down Expand Up @@ -140,12 +140,14 @@ jobs:
}
})
help:
needs: [ clean, is-org-member ]
needs: [clean, is-org-member]
if: ${{ startsWith(github.event.comment.body, '/cmd') && contains(github.event.comment.body, '--help') && needs.is-org-member.outputs.member == 'true' }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}

- name: Get command
uses: actions-ecosystem/action-regex-match@v2
Expand Down Expand Up @@ -173,11 +175,11 @@ jobs:
owner: context.repo.owner,
repo: context.repo.repo,
body: `<details><summary>Command help:</summary>
\`\`\`
${{ steps.help.outputs.help }}
\`\`\`
</details>`
})
Expand Down Expand Up @@ -208,7 +210,7 @@ jobs:
})
set-image:
needs: [ clean, is-org-member ]
needs: [clean, is-org-member]
if: ${{ startsWith(github.event.comment.body, '/cmd') && !contains(github.event.comment.body, '--help') && needs.is-org-member.outputs.member == 'true' }}
runs-on: ubuntu-latest
outputs:
Expand All @@ -222,13 +224,13 @@ jobs:
run: |
BODY=$(echo "${{ github.event.comment.body }}" | xargs)
IMAGE_OVERRIDE=$(echo $BODY | grep -oe 'docker.io/paritytech/ci-unified:.*\s' | xargs)
cat .github/env >> $GITHUB_OUTPUT
if [ -n "$IMAGE_OVERRIDE" ]; then
echo "IMAGE=$IMAGE_OVERRIDE" >> $GITHUB_OUTPUT
fi
if [[ $BODY == "/cmd bench"* ]]; then
echo "RUNNER=arc-runners-polkadot-sdk-benchmark" >> $GITHUB_OUTPUT
elif [[ $BODY == "/cmd update-ui"* ]]; then
Expand All @@ -239,7 +241,7 @@ jobs:
# Get PR branch name, because the issue_comment event does not contain the PR branch name
get-pr-branch:
needs: [ set-image ]
needs: [set-image]
runs-on: ubuntu-latest
outputs:
pr-branch: ${{ steps.get-pr.outputs.pr_branch }}
Expand Down Expand Up @@ -278,9 +280,9 @@ jobs:
echo "The repository is ${{ steps.get-pr.outputs.repo }}"
cmd:
needs: [ set-image, get-pr-branch ]
needs: [set-image, get-pr-branch]
env:
JOB_NAME: 'cmd'
JOB_NAME: "cmd"
runs-on: ${{ needs.set-image.outputs.RUNNER }}
container:
image: ${{ needs.set-image.outputs.IMAGE }}
Expand All @@ -301,26 +303,25 @@ jobs:
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.run_id }}/jobs | jq '.jobs[] | select(.name | contains("${{ env.JOB_NAME }}")) | .html_url')
runLink=$(curl -s \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.run_id }} | jq '.html_url')
echo "job_url=${jobLink}"
echo "run_url=${runLink}"
echo "job_url=$jobLink" >> $GITHUB_OUTPUT
echo "run_url=$runLink" >> $GITHUB_OUTPUT
- name: Comment PR (Start)
if: ${{ !contains(github.event.comment.body, '--quiet') }}
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
let job_url = ${{ steps.build-link.outputs.job_url }}
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
Expand All @@ -331,8 +332,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v4
with:
repository: ${{ needs.get-pr-branch.outputs.repo }}
ref: ${{ needs.get-pr-branch.outputs.pr-branch }}
ref: ${{ github.event.pull_request.head.sha }}

- name: Install dependencies for bench
if: startsWith(steps.get-pr-comment.outputs.group2, 'bench')
Expand All @@ -348,6 +348,7 @@ jobs:
# Fixes "detected dubious ownership" error in the ci
git config --global --add safe.directory '*'
git remote -v
python3 -m pip install -r .github/scripts/generate-prdoc.requirements.txt
python3 .github/scripts/cmd/cmd.py $CMD
git status
git diff
Expand All @@ -357,7 +358,7 @@ jobs:
if [ -n "$(git status --porcelain)" ]; then
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git pull --rebase origin ${{ needs.get-pr-branch.outputs.pr-branch }}
git add .
git restore --staged Cargo.lock # ignore changes in Cargo.lock
Expand All @@ -381,7 +382,7 @@ jobs:
--change added changed \
--ignore-errors \
refs/remotes/origin/master refs/heads/${{ needs.get-pr-branch.outputs.pr-branch }})
# Save the multiline result to the output
{
echo "result<<EOF"
Expand All @@ -393,17 +394,17 @@ jobs:
if: ${{ !failure() && !contains(github.event.comment.body, '--quiet') }}
uses: actions/github-script@v7
env:
SUBWEIGHT: '${{ steps.subweight.outputs.result }}'
SUBWEIGHT: "${{ steps.subweight.outputs.result }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
let runUrl = ${{ steps.build-link.outputs.run_url }}
let subweight = process.env.SUBWEIGHT;
let subweightCollapsed = subweight
? `<details>\n\n<summary>Subweight results:</summary>\n\n${subweight}\n\n</details>`
: '';
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
Expand Down
5 changes: 4 additions & 1 deletion docs/contributor/commands-readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ Note: it works only for members of the `paritytech` organization.

`/cmd <command> --help` to see the usage of a specific command


### Commands

- `/cmd fmt` to format the code in the PR. It commits back with the formatted code (fmt) and configs (taplo).

- `/cmd bench` to generate weights for a runtime. Read more about [Weight Generation](weight-generation.md)

- `/cmd prdoc` to generate a prdoc for a PR. Read more about [PRDoc](prdoc.md)

### Flags

1.`--quiet` to suppress the output of the command in the comments.
Expand All @@ -32,12 +33,14 @@ The pipeline logs will include what is failed (like which runtimes/pallets), the
or they keep failing, and you're rerunning them again, it's handy to add this flag to keep a PR clean.

### Adding new Commands

Feel free to add new commands to the workflow, however **_note_** that triggered workflows will use the actions
from `main` (default) branch, meaning they will take effect only after the PR with new changes/command is merged.
If you want to test the new command, it's better to test in your fork and local-to-fork PRs, where you control
the default branch.

### Examples

The regex in cmd.yml is: `^(\/cmd )([-\/\s\w.=:]+)$` accepts only alphanumeric, space, "-", "/", "=", ":", "." chars.

`/cmd bench --runtime bridge-hub-westend --pallet=pallet_name`
Expand Down
Loading

0 comments on commit e75726c

Please sign in to comment.