Skip to content

Run and upload primer over packages #6714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 28, 2022
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
12 changes: 12 additions & 0 deletions .github/workflows/primer_run_main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,15 @@ jobs:
with:
name: primer_commitstring
path: .pylint_primer_tests/commit_string.txt

# Run primer
- name: Run pylint primer
run: |
. venv/bin/activate
pip install -e .
python tests/primer/primer_tool.py run --type=main
- name: Upload output
uses: actions/upload-artifact@v3
with:
name: primer_output
path: .pylint_primer_tests/output_main.txt
53 changes: 49 additions & 4 deletions .github/workflows/primer_run_pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ jobs:
steps:
- name: Check out code from GitHub
uses: actions/[email protected]
with:
fetch-depth: 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ‘

- name: Set up Python ${{ matrix.python-version }}
id: python
uses: actions/[email protected]
with:
python-version: ${{ matrix.python-version }}
- uses: actions/setup-node@v1
- uses: actions/setup-node@v3
with:
version: 12
node-version: 16
- run: npm install @octokit/rest

# Restore cached Python environment
Expand Down Expand Up @@ -62,7 +64,8 @@ jobs:
pip install -U -r requirements_test.txt

# Cache primer packages
- name: Download diffs
- name: Download last 'main' run info
id: download-main-run
uses: actions/github-script@v6
with:
script: |
Expand All @@ -86,6 +89,8 @@ jobs:
repo: context.repo.repo,
run_id: lastRunMain.id,
});

// Get commitstring
const [matchArtifactMain] = artifacts_main.data.artifacts.filter((artifact) =>
artifact.name == "primer_commitstring");
const downloadWorkflow = await github.rest.actions.downloadArtifact({
Expand All @@ -95,10 +100,24 @@ jobs:
archive_format: "zip",
});
fs.writeFileSync("primer_commitstring.zip", Buffer.from(downloadWorkflow.data));

// Get output
const [matchArtifactMainOutput] = artifacts_main.data.artifacts.filter((artifact) =>
artifact.name == "primer_output");
const downloadWorkflowTwo = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifactMainOutput.id,
archive_format: "zip",
});
fs.writeFileSync("primer_output_main.zip", Buffer.from(downloadWorkflowTwo.data));
return lastRunMain.head_sha;
- name: Copy and unzip the commit string
run: |
unzip primer_commitstring.zip
cp commit_string.txt .pylint_primer_tests/commit_string.txt
- name: Unzip the output of 'main'
run: unzip primer_output_main.zip
- name: Get commit string
id: commitstring
run: |
Expand All @@ -118,11 +137,37 @@ jobs:
. venv/bin/activate
python tests/primer/primer_tool.py prepare --check

# Merge the 'main' commit of last successful run
- name: Pull 'main'
shell: bash
run: |
git config --global user.email "[email protected]"
git config --global user.name "Pylint Primer"
git pull origin ${{ steps.download-main-run.outputs.result }} --no-edit --no-commit --no-rebase

# Run primer
- name: Run pylint primer
run: |
. venv/bin/activate
pip install -e .
python tests/primer/primer_tool.py run --type=pr
- name: Upload output of PR
uses: actions/upload-artifact@v3
with:
name: primer_output_pr
path: .pylint_primer_tests/output_pr.txt
- name: Upload output of 'main'
uses: actions/upload-artifact@v3
with:
name: primer_output_main
path: output_main.txt

# Save PR number so we know which PR to comment on
- name: Save PR number
run: |
echo ${{ github.event.pull_request.number }} | tee pr_number.txt
- name: Upload PR number
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: primer_pylint_output_workflow
path: pr_number.txt
3 changes: 2 additions & 1 deletion tests/primer/packages_to_prime.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"astroid": {
"branch": "main",
"directories": ["astroid"],
"url": "https://github.com/PyCQA/astroid"
"url": "https://github.com/PyCQA/astroid",
"pylintrc_relpath": "pylintrc"
},
"black": {
"branch": "main",
Expand Down
37 changes: 37 additions & 0 deletions tests/primer/primer_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@

import argparse
import json
from io import StringIO
from pathlib import Path

import git

from pylint.lint import Run
from pylint.reporters import JSONReporter
from pylint.testutils.primer import PackageToLint

MAIN_DIR = Path(__file__).parent.parent.parent
Expand Down Expand Up @@ -49,6 +52,12 @@ def __init__(self, json_path: Path) -> None:
default=False,
)

# All arguments for the run parser
run_parser = self._subparsers.add_parser("run")
run_parser.add_argument(
"--type", choices=["main", "pr"], required=True, help="Type of primer run."
)

# Storing arguments
self.config = self._argument_parser.parse_args()

Expand All @@ -58,6 +67,8 @@ def __init__(self, json_path: Path) -> None:
def run(self) -> None:
if self.config.command == "prepare":
self._handle_prepare_command()
if self.config.command == "run":
self._handle_run_command()

def _handle_prepare_command(self) -> None:
commit_string = ""
Expand Down Expand Up @@ -88,6 +99,32 @@ def _handle_prepare_command(self) -> None:
) as f:
f.write(commit_string)

def _handle_run_command(self) -> None:
packages: dict[str, list[dict[str, str | int]]] = {}

for package, data in self.packages.items():
output = self._lint_package(data)
packages[package] = output
print(f"Successfully primed {package}.")

with open(
PRIMER_DIRECTORY / f"output_{self.config.type}.txt", "w", encoding="utf-8"
) as f:
json.dump(packages, f)

def _lint_package(self, data: PackageToLint) -> list[dict[str, str | int]]:
# We want to test all the code we can
enables = ["--enable-all-extensions", "--enable=all"]
# Duplicate code takes too long and is relatively safe
disables = ["--disable=duplicate-code"]
arguments = data.directories + data.pylint_args + enables + disables
if data.pylintrc_relpath:
arguments += [f"--rcfile={data.pylintrc_relpath}"]
output = StringIO()
reporter = JSONReporter(output)
Run(arguments, reporter=reporter, do_exit=False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to lint every primer package twice, so I think we will need to turn off the python test that checks for crashes, yeah?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Although this still needs some work before we get there. We don't actually prime on astroid and black doesn't take too long. So for now I think this is fine.

return json.loads(output.getvalue())

@staticmethod
def _get_packages_to_lint_from_json(json_path: Path) -> dict[str, PackageToLint]:
with open(json_path, encoding="utf8") as f:
Expand Down