Skip to content
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
23 changes: 21 additions & 2 deletions .github/workflows/first-commit.yml
Original file line number Diff line number Diff line change
@@ -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/<name>.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/<login>.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:
Expand All @@ -14,7 +21,7 @@ on:
paths: ["first-commits/**"]

permissions:
contents: read
contents: write # to merge a pull request that passed
pull-requests: write

concurrency:
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion CURRICULUM.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions first-commits/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
12 changes: 7 additions & 5 deletions lessons/02-first-pr-in-browser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand All @@ -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/).
2 changes: 1 addition & 1 deletion lessons/02-first-pr-in-browser/exercise.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
17 changes: 8 additions & 9 deletions lessons/02-first-pr-in-browser/script.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.

Expand All @@ -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.
4 changes: 2 additions & 2 deletions lessons/04-just-enough-git/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lessons/04-just-enough-git/script.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions lessons/05-second-pr-from-terminal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions publishing/promo/script.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion publishing/udemy-landing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion publishing/udemy-messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
31 changes: 30 additions & 1 deletion tests/test_check_first_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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")


Expand Down Expand Up @@ -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)."]
54 changes: 43 additions & 11 deletions tests/test_workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,59 @@
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"
assert "contents: read" in text and "contents: write" not in text, f"{name} needs no write access to code"
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():
Expand Down
Loading
Loading