-
Notifications
You must be signed in to change notification settings - Fork 26
Implement T9L1/create-pr-from-main #276
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
desmondwong1215
merged 8 commits into
git-mastery:main
from
desmondwong1215:exercise/create-pr-from-main
Apr 21, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ac15ba6
Implement download
desmondwong1215 ac05f49
Implement verify
desmondwong1215 f07693d
Implement test-verify
desmondwong1215 9d4c840
Remove unnecessary fields in test_verify
desmondwong1215 8c53ce1
Update test-verify
desmondwong1215 3f69dbd
Update test-verify
desmondwong1215 e004009
Address copilot's comments
desmondwong1215 980bf18
Fix typo
desmondwong1215 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,14 @@ | ||
| { | ||
| "exercise_name": "create-pr-from-main", | ||
| "tags": [], | ||
| "requires_git": true, | ||
| "requires_github": true, | ||
| "base_files": {}, | ||
| "exercise_repo": { | ||
| "repo_type": "remote", | ||
| "repo_name": "languages", | ||
| "repo_title": "gm-languages", | ||
| "create_fork": true, | ||
| "init": null | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| See https://git-mastery.org/lessons/prsCreate/exercise-create-pr-from-main.html |
Empty file.
This file contains hidden or 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,8 @@ | ||
| from pathlib import Path | ||
|
|
||
| from exercise_utils.exercise_config import add_pr_config | ||
| from exercise_utils.gitmastery import create_start_tag | ||
|
|
||
| def setup(verbose: bool = False): | ||
| create_start_tag(verbose) | ||
| add_pr_config(pr_repo_full_name="git-mastery/gm-languages", config_path=Path("../")) |
This file contains hidden or 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,115 @@ | ||
| from contextlib import contextmanager | ||
| from unittest.mock import PropertyMock, patch | ||
|
|
||
| import pytest | ||
| from exercise_utils.test import GitAutograderTestLoader, assert_output | ||
| from git_autograder import ( | ||
| GitAutograderStatus, | ||
| GitAutograderWrongAnswerException, | ||
| ) | ||
| from git_autograder.pr import GitAutograderPr | ||
|
|
||
| from .verify import ( | ||
| EXPECTED_CONTENT_STEP_3, | ||
| JAVA_FILE_MISSING, | ||
| JAVA_INVALID_CONTENT, | ||
| PR_MISSING, | ||
| WRONG_HEAD_BRANCH, | ||
| verify, | ||
| ) | ||
|
|
||
| REPOSITORY_NAME = "create-pr-from-main" | ||
|
|
||
| loader = GitAutograderTestLoader(REPOSITORY_NAME, verify) | ||
|
|
||
| # NOTE: This exercise is a special case where we do not require repo-smith. Instead, | ||
| # we directly mock function calls to verify that all branches are covered for us. | ||
|
|
||
|
|
||
| class FakeCommit: | ||
| def __init__(self, java_content: str | None) -> None: | ||
| self._java_content = java_content | ||
|
|
||
| @contextmanager | ||
| def file(self, file_path: str): | ||
| yield self._java_content | ||
|
|
||
|
|
||
| def _run_verify( | ||
| pr_numbers: list[int] | None = None, | ||
| head_branch: str = "", | ||
| java_content: str | None = None, | ||
| ): | ||
| if pr_numbers is None: | ||
| pr_numbers = [] | ||
| fake_commit = FakeCommit(java_content) | ||
| with ( | ||
| loader.start_mock_exercise( | ||
| has_pr_context=True, pr_number=1, pr_repo_full_name="dummy/repo" | ||
| ) as exercise, | ||
| patch( | ||
| "create_pr_from_main.verify.get_pr_numbers_by_author", | ||
| return_value=pr_numbers, | ||
| ), | ||
| patch("create_pr_from_main.verify.add_pr_config"), | ||
| patch.object(exercise, "fetch_pr", return_value=None), | ||
| patch.object( | ||
| GitAutograderPr, | ||
| "head_branch", | ||
| new_callable=PropertyMock, | ||
| return_value=head_branch, | ||
| ), | ||
| patch.object( | ||
| GitAutograderPr, | ||
| "last_user_commit", | ||
| new_callable=PropertyMock, | ||
| return_value=fake_commit, | ||
| ), | ||
| ): | ||
| return verify(exercise) | ||
|
|
||
|
|
||
| def test_success(): | ||
| output = _run_verify( | ||
| pr_numbers=[123], | ||
| head_branch="main", | ||
| java_content="\n".join(EXPECTED_CONTENT_STEP_3), | ||
| ) | ||
|
|
||
| assert_output(output, GitAutograderStatus.SUCCESSFUL) | ||
|
|
||
|
|
||
| def test_pr_missing(): | ||
| with pytest.raises(GitAutograderWrongAnswerException) as exception: | ||
| _run_verify() | ||
|
|
||
| assert exception.value.message == [PR_MISSING] | ||
|
|
||
|
|
||
| def test_wrong_head_branch(): | ||
| with pytest.raises(GitAutograderWrongAnswerException) as exception: | ||
| _run_verify(pr_numbers=[1], head_branch="feature/pr-branch") | ||
|
|
||
| assert exception.value.message == [WRONG_HEAD_BRANCH] | ||
|
|
||
|
|
||
| def test_java_file_missing(): | ||
| with pytest.raises(GitAutograderWrongAnswerException) as exception: | ||
| _run_verify( | ||
| pr_numbers=[1], | ||
| head_branch="main", | ||
| java_content=None, | ||
| ) | ||
|
|
||
| assert exception.value.message == [JAVA_FILE_MISSING] | ||
|
|
||
|
|
||
| def test_java_content_invalid(): | ||
| with pytest.raises(GitAutograderWrongAnswerException) as exception: | ||
| _run_verify( | ||
| pr_numbers=[1], | ||
| head_branch="main", | ||
| java_content="wrong content\n", | ||
| ) | ||
|
|
||
| assert exception.value.message == [JAVA_INVALID_CONTENT] |
This file contains hidden or 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,55 @@ | ||
| from pathlib import Path | ||
|
|
||
| from git_autograder import ( | ||
| GitAutograderOutput, | ||
| GitAutograderExercise, | ||
| GitAutograderStatus, | ||
| ) | ||
|
|
||
| from exercise_utils.exercise_config import add_pr_config | ||
| from exercise_utils.github_cli import get_github_username, get_pr_numbers_by_author | ||
|
|
||
|
|
||
| JAVA_FILE_MISSING = "Java.txt file is missing in the latest commit on main branch." | ||
| JAVA_INVALID_CONTENT = "The content in Java.txt in main branch is not correct." | ||
| MULTIPLE_PRS = "Multiple PRs found. The latest pr will be used in grading." | ||
| PR_MISSING = "No PR is found." | ||
| WRONG_HEAD_BRANCH = "The PR's head branch is not 'main'." | ||
|
|
||
|
|
||
| EXPECTED_CONTENT_STEP_3 = ["1995, by James Gosling"] | ||
|
|
||
|
|
||
| def verify(exercise: GitAutograderExercise) -> GitAutograderOutput: | ||
| username = get_github_username(False) | ||
| target_repo = f"git-mastery/{exercise.config.exercise_repo.repo_title}" | ||
| comments = [] | ||
|
|
||
| pr_numbers = get_pr_numbers_by_author(username, target_repo, False) | ||
| if not pr_numbers: | ||
| raise exercise.wrong_answer([PR_MISSING]) | ||
| if len(pr_numbers) > 1: | ||
| comments.append(MULTIPLE_PRS) | ||
| pr_number = pr_numbers[-1] | ||
|
|
||
| add_pr_config(pr_number=pr_number, config_path=Path("./")) | ||
| exercise.fetch_pr() | ||
|
|
||
| if exercise.repo.prs.pr.head_branch != "main": | ||
| comments.append(WRONG_HEAD_BRANCH) | ||
| raise exercise.wrong_answer(comments) | ||
|
desmondwong1215 marked this conversation as resolved.
|
||
|
|
||
| latest_user_commit = exercise.repo.prs.pr.last_user_commit | ||
| with latest_user_commit.file("Java.txt") as content: | ||
| if content is None: | ||
| comments.append(JAVA_FILE_MISSING) | ||
| raise exercise.wrong_answer(comments) | ||
| extracted_content = [ | ||
| line.strip() for line in content.splitlines() if line.strip() != "" | ||
| ] | ||
| if extracted_content != EXPECTED_CONTENT_STEP_3: | ||
| comments.append(JAVA_INVALID_CONTENT) | ||
| raise exercise.wrong_answer(comments) | ||
|
desmondwong1215 marked this conversation as resolved.
|
||
|
|
||
| comments.append("Good job creating the PR and pushing commits!") | ||
| return exercise.to_output(comments, GitAutograderStatus.SUCCESSFUL) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.