PR preview environment
Implement `preview_name(pr_number, branch, max_length)` — the name of a preview
environment for a pull request. The function returns `Preview(name, url,
ttl_hours)`.
Rules:
- `pr_number` is an integer greater than zero; `bool` does not count as a
number;
- `branch` is a non-empty string; surrounding whitespace is stripped;
- branch slug: lowercase, every character except `a-z0-9` becomes `-`, repeated
`-` collapse, edges are trimmed; if nothing is left, use `branch`;
- branch hash — `sha256(branch)[:6]` of the stripped string;
- name: `pr-<pr_number>-<slug>-<hash>`; the hash keeps `feature/login` and
`feature-login` apart;
- if the name is longer than `max_length`, only the slug is truncated; the
`-<hash>` tail is preserved; a dangling `-` is removed;
- `max_length` is an integer from 20 to 63 (the DNS-label limit); otherwise
`ValueError`;
- if `max_length` cannot fit even `pr-<number>-` and `-<hash>`, raise
`ValueError`;
- `url` — `https://<name>.preview.qa-lab.example`;
- `ttl_hours` — `PREVIEW_TTL_HOURS` (72);
- the same arguments always produce the same result.
Example:
```python
preview = preview_name(7, "feature/login", 63)
preview.name # "pr-7-feature-login-<hash>"
preview.url # "https://pr-7-feature-login-<hash>.preview.qa-lab.example"
preview.ttl_hours # 72
```
Hidden tests cover a normal branch, special characters, a long branch, an empty
branch, similar names across PRs and branches, and `max_length` boundaries.
The AI mentor is available after you log in: it sees your attempt history and does not spend resources on anonymous requests.
Log in and askHint 1
What makes a name safe for DNS and unique for a branch at the same time? Why is the branch slug alone not enough?