diff --git a/.github/workflows/first-commit.yml b/.github/workflows/first-commit.yml index 3bd3037..920ad7b 100644 --- a/.github/workflows/first-commit.yml +++ b/.github/workflows/first-commit.yml @@ -1,11 +1,18 @@ name: Validate first commit # pull_request_target runs this workflow from the base branch with a write -# token, so a fork's PR can be commented on. A maintainer reviews and merges. +# token, so a fork's PR can be commented on and, when it passes, merged. # To stay safe, the PR's # code is never checked out or executed: the validator comes from the base # branch and only reads the learner's file with git show + ast.parse. # PRs that don't touch first-commits/.py (status 3) get no comment. +# +# A PR that passes is merged here, so a learner's first pull request never +# waits for a maintainer. It is safe to merge unread because the validator +# allows exactly one ordinary file, first-commits/.py, holding three +# short strings with no links or mentions. The merge is a merge commit (a +# squash would leave the learner's main out of step with ours), and only of +# the commit that was checked. This is the only bot that merges. on: pull_request_target: @@ -14,7 +21,7 @@ on: paths: ["first-commits/**"] permissions: - contents: read + contents: write # to merge a pull request that passed pull-requests: write concurrency: @@ -59,6 +66,18 @@ jobs: PR: ${{ github.event.pull_request.number }} run: gh pr comment "$PR" --repo "$GITHUB_REPOSITORY" --body-file comment.md + - name: Merge the pull request that passed + if: steps.check.outputs.status == '0' + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PR: ${{ github.event.pull_request.number }} + HEAD: ${{ github.event.pull_request.head.sha }} + run: | + if ! gh pr merge "$PR" --repo "$GITHUB_REPOSITORY" --merge --match-head-commit "$HEAD"; then + gh pr comment "$PR" --repo "$GITHUB_REPOSITORY" --body \ + "I couldn't merge this by myself: the branch may have changed while I was checking, or it has a conflict. A maintainer will take a look." + fi + - name: Fail the check when the file needs fixes if: steps.check.outputs.status != '0' && steps.check.outputs.status != '3' run: exit 1 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 41452f4..fdd7937 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -9,8 +9,8 @@ Thanks for helping! There are two kinds of contribution here. - **Lesson 05:** add `practice/greetings/YOUR_NAME.py` and its test. See [the lesson](lessons/05-second-pr-from-terminal/). -Bots check these PRs, and a maintainer reviews and merges them. Change only -your own files. +A bot checks these PRs. It merges a first commit that passes; a maintainer +reviews and merges the Lesson 05 ones. Change only your own files. ## 2. Improving the course diff --git a/CURRICULUM.md b/CURRICULUM.md index 29259c7..ac291b9 100644 --- a/CURRICULUM.md +++ b/CURRICULUM.md @@ -41,7 +41,7 @@ Each long video also gets one 60-second clip for Shorts/Reels. | # | Lesson | Hands-on | Status | |---|---|---|---| | 01 | [What counts as a contribution](lessons/01-what-counts/) | Pick the kind of contribution you'll make first | Drafted | -| 02 | [Star, fork, and your first commit, in the browser](lessons/02-first-pr-in-browser/) | Add `first-commits/YOU.py` to this repo; the bot checks it and a maintainer merges it | Drafted | +| 02 | [Star, fork, and your first commit, in the browser](lessons/02-first-pr-in-browser/) | Add `first-commits/YOU.py` to this repo; the bot checks it and merges it | Drafted | ## Part 2: Real tools diff --git a/README.md b/README.md index ac5ec40..57ac25e 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ A free video course that takes you from "I've never opened a pull request" to a merged contribution to a real Python project. - **Lesson 02 needs no install.** You open your first pull request in the - browser. A bot checks it within minutes and a maintainer merges it. + browser. A bot checks it and merges it within a minute. - **Then the real tools:** git, GitHub CLI, virtualenvs, pytest, ruff, pre-commit — the things maintainers will actually check. - **Then a real project:** finding an issue nobody else is working on, @@ -18,7 +18,7 @@ to a merged contribution to a real Python project. The hands-on exercises happen right here: in Lesson 02 you star and fork this repo, then add your own file to [`first-commits/`](first-commits/) as -your first commit. A bot checks it, then a maintainer reviews and merges it. +your first commit. A bot checks it and, when it's right, merges it. ## How this course is made diff --git a/first-commits/README.md b/first-commits/README.md index 5e2e307..2db439b 100644 --- a/first-commits/README.md +++ b/first-commits/README.md @@ -9,5 +9,5 @@ github = "your-github-username" message = "Why you're learning Python" ``` -A bot checks your file with Python's parser (it never runs it), comments on -your pull request. If everything's right, a maintainer reviews and merges it. +A bot checks your file with Python's parser (it never runs it) and comments on +your pull request. If everything's right, the bot merges it. diff --git a/lessons/02-first-pr-in-browser/README.md b/lessons/02-first-pr-in-browser/README.md index cfe9542..4421527 100644 --- a/lessons/02-first-pr-in-browser/README.md +++ b/lessons/02-first-pr-in-browser/README.md @@ -71,9 +71,9 @@ Within a minute or two: - A check called **Validate first commit** runs. Scroll down on your PR to see it. -- If your file is right, the bot comments 🎉, and a **maintainer reviews - and merges your PR**, usually within a day. The purple **Merged** badge - means your commit is now part of the project. +- If your file is right, the bot comments 🎉 and **merges your PR**, usually + within a minute. The purple **Merged** badge means your commit is now part + of the project. - If something's wrong, the bot comments with exactly what to fix. Fix it in your fork (open your file, click the ✏️ pencil, commit). Your PR updates automatically and the check runs again. @@ -82,8 +82,9 @@ Within a minute or two: That's the same workflow behind every open source contribution: **fork → commit → pull request → automated checks → merge**. In real projects a human -reviews it too, and the checks run tests instead of reading three lines. You -will do both in Part 3. +reviews it and merges it, and the checks run tests instead of reading three +lines. Here a bot merges your first commit, so you don't have to wait. You +meet a human reviewer in your next pull request, in Lesson 05. ## Common problems @@ -93,5 +94,6 @@ will do both in Part 3. | Changes files outside `first-commits/` | You edited another file too. Undo that change in your fork. | | Isn't valid Python | Usually a missing quote. Each value needs `"` at both ends. | | `github` doesn't match | The `github = "..."` value must be your username. | +| Can't contain links or @mentions | Nobody reads your file before the bot merges it, so `name` and `message` must be plain words. | Next: [Lesson 03: set up your machine](../03-set-up-your-machine/). diff --git a/lessons/02-first-pr-in-browser/exercise.md b/lessons/02-first-pr-in-browser/exercise.md index 58954f7..b968b7b 100644 --- a/lessons/02-first-pr-in-browser/exercise.md +++ b/lessons/02-first-pr-in-browser/exercise.md @@ -12,7 +12,7 @@ - [ ] Open a pull request to `thejesh23/first-python-contribution` - [ ] Wait for the **Validate first commit** check and the bot's comment -- [ ] See the purple **Merged** badge once a maintainer merges it +- [ ] See the purple **Merged** badge: the bot merges your pull request when the check passes **Stuck?** Read the bot's comment first: it names the problem. Then check the "Common problems" table in the [lesson](README.md). Still stuck? diff --git a/lessons/02-first-pr-in-browser/script.md b/lessons/02-first-pr-in-browser/script.md index bddd8cb..db6efea 100644 --- a/lessons/02-first-pr-in-browser/script.md +++ b/lessons/02-first-pr-in-browser/script.md @@ -131,7 +131,7 @@ highlight: 56,99,260,36 ## Scene 9: The bot visual: browser url: https://github.com/thejesh23/first-python-contribution/pull/27 -caption: The bot explains what to fix, then hands over to a maintainer +caption: The bot explains what to fix. When your file is right, it merges. image: captures/bot-review.png height: 2600 crop: 100,528,900,552 @@ -140,9 +140,8 @@ crop: 100,528,900,552 > runs. It reads your file with Python's own parser, the same way Python > reads your code before running it. -> If your file is right, the bot says so, and a maintainer reviews and -> merges your pull request. If something is wrong, the bot tells you -> exactly what to fix. Fix it in your +> If your file is right, the bot says so, and merges your pull request. If +> something is wrong, the bot tells you exactly what to fix. Fix it in your > fork, and commit. Your pull request updates automatically, and the check > runs again. @@ -158,12 +157,12 @@ bullets: - Automated checks - Merge -> When the maintainer merges it, you get the purple Merged badge. Your -> commit is now part of a public project. +> When the bot merges it, you get the purple Merged badge. Your commit is +> now part of a public project. > And that is the same workflow behind every open source contribution. Fork, > commit, pull request, automated checks, merge. -> Real projects work the same way: the checks run first, then a human -> reviews and merges. Their checks also run real tests. You'll write some -> later in this course. Next, we set up your own machine. +> In real projects, the checks run first, then a human reviews and merges. +> Their checks also run real tests. You'll write some, and meet a human +> reviewer, later in this course. Next, we set up your own machine. diff --git a/lessons/04-just-enough-git/README.md b/lessons/04-just-enough-git/README.md index c86f443..f3b47dd 100644 --- a/lessons/04-just-enough-git/README.md +++ b/lessons/04-just-enough-git/README.md @@ -110,8 +110,8 @@ Because `main` hadn't changed since `shout` branched off, git just moves `main` forward to C. That's called a **fast-forward**. In real projects you rarely merge into `main` yourself. You push a branch and -open a pull request, and the **maintainer** merges it. That's exactly what -happened to your first commit in Lesson 02. +open a pull request, and the **maintainer** merges it. For your first commit +in Lesson 02, a bot did that for you. ## 6. Remotes diff --git a/lessons/04-just-enough-git/script.md b/lessons/04-just-enough-git/script.md index 38a7eec..c8ac864 100644 --- a/lessons/04-just-enough-git/script.md +++ b/lessons/04-just-enough-git/script.md @@ -186,8 +186,8 @@ $ git log --oneline --graph --all > Look at the log. Main and shout now point at the same commit. > In real projects, you rarely merge into main yourself. You push a branch, -> and open a pull request. The maintainer merges it. That is exactly what -> happened to your first commit, in lesson two. +> and open a pull request. A maintainer merges it. For your first commit, in +> lesson two, a bot did that for you. ## Scene 9: Remotes visual: slide diff --git a/lessons/05-second-pr-from-terminal/README.md b/lessons/05-second-pr-from-terminal/README.md index 7ae1e47..db0ad5f 100644 --- a/lessons/05-second-pr-from-terminal/README.md +++ b/lessons/05-second-pr-from-terminal/README.md @@ -196,11 +196,12 @@ runs `ruff format --check`, `ruff check` and `pytest` on your code, exactly like Steps 5 and 6. > **No checks yet?** GitHub doesn't run a newcomer's checks until a -> maintainer approves them. If your Lesson 02 pull request hasn't been -> merged yet, this check waits too: `gh pr checks` reports no checks, and -> the pull request page says a workflow is awaiting approval. You did -> nothing wrong. The check starts when a maintainer approves it, usually -> within a day. Run `gh pr checks --watch` again then. +> maintainer approves them. If your Lesson 02 pull request isn't merged +> (you skipped it, or the bot is still waiting for a fix), this check waits +> too: `gh pr checks` reports no checks, and the pull request page says a +> workflow is awaiting approval. Finish Lesson 02 first, or wait: a +> maintainer approves the run, usually within a day. Then run +> `gh pr checks --watch` again. When it finishes, a separate review bot checks your file names and that your code only contains what it should. It doesn't appear in the checks list: diff --git a/publishing/promo/script.md b/publishing/promo/script.md index ff186b3..6bc9250 100644 --- a/publishing/promo/script.md +++ b/publishing/promo/script.md @@ -29,13 +29,13 @@ crop: 15,85,1260,245 ## Scene 3: What you'll do visual: browser url: https://github.com/thejesh23/first-python-contribution/pull/27 -caption: Your first pull request: a bot checks it, a maintainer merges it +caption: Your first pull request: a bot checks it, and merges it within a minute image: captures/first-pr.png height: 2600 crop: 112,836,880,215 > Your first pull request lands in about fifteen minutes, right in your -> browser. A bot checks it within minutes, and a maintainer merges it. +> browser. A bot checks it, and merges it within a minute. > By the end, you'll have a pull request merged in a real open source > project. One you chose yourself. diff --git a/publishing/udemy-landing.md b/publishing/udemy-landing.md index 8a762dd..4b33e11 100644 --- a/publishing/udemy-landing.md +++ b/publishing/udemy-landing.md @@ -19,7 +19,7 @@ This course contains the use of artificial intelligence. Never opened a pull request? This course takes you from zero to a merged contribution in a real open source Python project, one small, practical step at a time. -You start with a win in the first 15 minutes: you star and fork the course's GitHub repository and make your first commit, entirely in the browser. A bot checks your work within minutes, and a maintainer merges it. Then you set up the real tools (git, the GitHub CLI and Python), learn just enough git to never feel lost, and open a second pull request from the terminal with a Python function and its test. That one has to pass CI before it's merged. +You start with a win in the first 15 minutes: you star and fork the course's GitHub repository and make your first commit, entirely in the browser. A bot checks your work and merges it within a minute. Then you set up the real tools (git, the GitHub CLI and Python), learn just enough git to never feel lost, and open a second pull request from the terminal with a Python function and its test. That one has to pass CI before it's merged. Then you go out into the real world. You learn the house rules every project has (CONTRIBUTING files, Contributor License Agreements, DCO sign-offs and AI policies), how to set up any Python project whatever tools it uses, how to find an issue nobody else is working on, how to report a bug so it gets fixed (you hunt for bugs planted in a small practice program), how to make a change maintainers want to merge, and how to write a pull request that's easy to review. You practise rebasing, merge conflicts and code review in a safe lab on your own computer, and you learn how to use AI tools honestly and well. diff --git a/publishing/udemy-messages.md b/publishing/udemy-messages.md index 8cb3936..41ad23e 100644 --- a/publishing/udemy-messages.md +++ b/publishing/udemy-messages.md @@ -13,7 +13,7 @@ By the end of this course you'll have a pull request merged in a real open sourc 1. Open the resources of any lecture. Each one links to the written lesson, the exercise and the quiz in the course's GitHub repository. 2. Do the exercises. You learn this by doing it, not by watching: your first pull request takes about 15 minutes, right in the browser, in Lecture 2. -3. A bot checks your work within minutes, and I review and merge it myself. +3. A bot checks your work within minutes. It merges your first pull request itself, and I review and merge the later ones. 4. Stuck? Ask in the Q&A, or open an issue in the repository. That's good practice too. You can do the whole course in a day: about an hour of video and six hours of exercises. Only the capstone takes longer, because a real maintainer has to review your pull request. diff --git a/tests/test_check_first_commit.py b/tests/test_check_first_commit.py index 9db9782..5716af0 100644 --- a/tests/test_check_first_commit.py +++ b/tests/test_check_first_commit.py @@ -36,6 +36,10 @@ def test_comments_are_allowed(): ('name = "Ada"\ngithub = "someone-else"\nmessage = "hi"\n', "must match"), ('name = "Ada"\ngithub = "ada"\nmessage = "' + "x" * 300 + '"\n', "too long"), ("x" * 3000, "too long"), + ('name = "Ada"\ngithub = "ada"\nmessage = "see https://example.com"\n', "links or @mentions"), + ('name = "Ada"\ngithub = "ada"\nmessage = "Visit WWW.example.com"\n', "links or @mentions"), + ('name = "Ada"\ngithub = "ada"\nmessage = "hi @someone"\n', "links or @mentions"), + ('name = "ada@example.com"\ngithub = "ada"\nmessage = "hi"\n', "links or @mentions"), ], ) def test_invalid_sources(source, expected): @@ -72,7 +76,8 @@ def test_deleting_own_file_rejected(): def test_comment_mentions_user(): - assert "@ada" in format_comment(check_source(GOOD, "ada"), "ada") + passed = format_comment(check_source(GOOD, "ada"), "ada") + assert "@ada" in passed and "merging your pull request now" in passed and "maintainer" not in passed assert "fix" in format_comment(check_source("", "ada"), "ada") @@ -153,3 +158,27 @@ def test_a_syntax_error_says_its_line_once(): "Your file isn't valid Python: unterminated string literal (line 3). " 'A missing quote `"` is the most common cause.' ] + + +def test_only_an_ordinary_file_passes(tmp_path, monkeypatch): + """The file is merged unread, so a symlink or an executable must not get through.""" + + def git(*args): + return subprocess.run(["git", *args], cwd=tmp_path, check=True, capture_output=True, text=True).stdout.strip() + + git("init", "-q", "-b", "main") + git("config", "user.email", "t@example.com") + git("config", "user.name", "t") + (tmp_path / "first-commits").mkdir() + (tmp_path / "first-commits" / "README.md").write_text("hi\n") + git("add", ".") + git("commit", "-qm", "base") + base = git("rev-parse", "HEAD") + (tmp_path / "first-commits" / "ada.py").write_text(GOOD) + git("add", ".") + git("update-index", "--chmod=+x", "first-commits/ada.py") + git("commit", "-qm", "an executable file") + monkeypatch.chdir(tmp_path) + + errors = validate("ada", base, git("rev-parse", "HEAD")).errors + assert errors == ["`first-commits/ada.py` must be an ordinary text file (not a link or a program)."] diff --git a/tests/test_workflows.py b/tests/test_workflows.py index d738de0..56c48d5 100644 --- a/tests/test_workflows.py +++ b/tests/test_workflows.py @@ -4,11 +4,27 @@ from pathlib import Path WORKFLOWS = Path(__file__).resolve().parent.parent / ".github" / "workflows" -BOTS = ["first-commit.yml", "exercise-2-merge.yml", "graduate.yml"] +BOTS = ["exercise-2-merge.yml", "graduate.yml"] -def test_bots_only_check_and_comment(): - """The bots never merge: a maintainer reviews and merges every learner PR.""" +def test_only_the_first_commit_bot_merges(): + """A first pull request that passes is merged by the bot, so nobody waits for a maintainer. + + It must merge only the commit it checked, with a merge commit (a squash + would leave the learner's main out of step), and never run the PR's code. + """ + text = (WORKFLOWS / "first-commit.yml").read_text(encoding="utf-8") + merges = re.findall(r"gh pr merge .*", text) + assert len(merges) == 1 + assert "--merge" in merges[0] and '--match-head-commit "$HEAD"' in merges[0] + assert "--squash" not in text and "--rebase" not in text and "--admin" not in text + assert "steps.check.outputs.status == '0'" in text + assert "persist-credentials: false" in text + assert "git checkout" not in text and "ref: ${{" not in text, "the PR's code must never be checked out" + + +def test_the_other_bots_only_check_and_comment(): + """Exercise 2 and graduate pull requests are merged by a maintainer.""" for name in BOTS: text = (WORKFLOWS / name).read_text(encoding="utf-8") assert "gh pr merge" not in text, f"{name} must not merge" @@ -16,15 +32,31 @@ def test_bots_only_check_and_comment(): assert "gh pr comment" in text -def test_no_lesson_says_a_bot_merges(): - """The lessons and videos must say what the bots do: check and comment.""" +def test_lessons_say_who_merges(): + """The bot merges the first pull request (Lesson 02). A maintainer merges Exercise 2 and graduates.""" root = WORKFLOWS.parent.parent - pages = [*root.glob("*.md"), *root.glob("lessons/*/*.md"), *root.glob("publishing/**/*.md")] - pages += [*root.glob("first-commits/README.md"), *root.glob("practice/README.md")] - claim = re.compile(r"bots? (?:check(?:s)? and )?merges?\b|it comments,? (?:and )?merges", re.IGNORECASE) - for page in pages: - text = " ".join(page.read_text(encoding="utf-8").replace(">", " ").split()) - assert not claim.search(text), f"{page.relative_to(root)} says a bot merges; a maintainer does" + + def words(path: str) -> str: + return " ".join((root / path).read_text(encoding="utf-8").replace(">", " ").split()) + + bot_merges = re.compile(r"bots? (?:check(?:s)? (?:it )?,? ?and )?merges?\b|it comments,? (?:and )?merges", re.I) + for page in ( + "lessons/05-second-pr-from-terminal/README.md", + "lessons/05-second-pr-from-terminal/script.md", + "lessons/15-capstone/README.md", + "lessons/15-capstone/script.md", + "practice/README.md", + ): + assert not bot_merges.search(words(page)), f"{page} says a bot merges; a maintainer does" + maintainer_merges = re.compile(r"maintainer (?:reviews (?:it )?and )?merges", re.I) + for page in ( + "lessons/02-first-pr-in-browser/README.md", + "lessons/02-first-pr-in-browser/script.md", + "lessons/02-first-pr-in-browser/exercise.md", + "first-commits/README.md", + "publishing/promo/script.md", + ): + assert not maintainer_merges.search(words(page)), f"{page} says a maintainer merges the first PR; the bot does" def test_every_action_is_pinned_to_a_commit(): diff --git a/tools/check_first_commit.py b/tools/check_first_commit.py index 049d759..3d97700 100644 --- a/tools/check_first_commit.py +++ b/tools/check_first_commit.py @@ -11,6 +11,8 @@ python tools/check_first_commit.py --login LOGIN --base BASE_SHA --head HEAD_SHA Prints a Markdown comment for the PR and exits 0 if the PR is valid, 1 if not. +A valid PR is merged by the workflow without a human reading it, so the rules +are strict: one ordinary file, three short strings, no links or mentions. Exits 3, printing nothing, when the PR doesn't add or change any ``first-commits/.py`` (for example a maintainer editing the folder's README), so the bot stays quiet. It also stays quiet on an Exercise 2 pull @@ -32,6 +34,8 @@ MAX_BYTES = 2_000 MAX_LEN = {"name": 80, "github": 39, "message": 280} LOGIN_RE = re.compile(r"^[A-Za-z0-9](?:[A-Za-z0-9-]{0,37}[A-Za-z0-9])?$") +LINK_RE = re.compile(r"https?://|www\.|@", re.IGNORECASE) # nobody reads the file before it's merged +REGULAR_FILE = "100644" NOT_A_LEARNER_PR = 3 @@ -155,6 +159,8 @@ def check_source(source: str, login: str) -> Result: result.errors.append(f"`{var}` is too long ({len(stripped)} characters, maximum {MAX_LEN[var]}).") if "\n" in value or "\r" in value: result.errors.append(f"`{var}` must be on one line.") + if var != "github" and LINK_RE.search(value): + result.errors.append(f"`{var}` can't contain links or @mentions. Use plain words.") placeholder = {"name": "Your Name", "github": "your-github-username"} for var, text in placeholder.items(): @@ -171,10 +177,10 @@ def check_source(source: str, login: str) -> Result: def format_comment(result: Result, login: str) -> str: if result.ok: return ( - f"🎉 Looks great, @{login}! Your file passed every check. A maintainer " - "will review your pull request and merge it soon.\n\n" - "When it's merged, you'll see the purple **Merged** badge: your commit is " - "then part of a public project. Next up: Lesson 03, setting up git on your " + f"🎉 Looks great, @{login}! Your file passed every check, so I'm merging " + "your pull request now.\n\n" + "In a moment you'll see the purple **Merged** badge: your commit is then " + "part of a public project. Next up: Lesson 03, setting up git on your " "own machine." ) lines = "\n".join(f"- {e}" for e in result.errors) @@ -199,6 +205,10 @@ def validate(login: str, base: str, head: str) -> Result: result, path = check_changes(changes, login) # type: ignore[arg-type] if path is None: return result + mode = _git("ls-tree", head, "--", path).split(" ", 1)[0] + if mode != REGULAR_FILE: + result.errors.append(f"`{path}` must be an ordinary text file (not a link or a program).") + return result source = _git("show", f"{head}:{path}") result.errors.extend(check_source(source, login).errors) return result