|
| 1 | +# Step 1: Protect your code |
| 2 | + |
| 3 | +It's been a busy month at Mergington High! Your simple website for managing extra-curricular activities has really taken off. What started as a basic sign-up form for a few activities has grown into the go-to place for half the school activities. 📚✨ |
| 4 | + |
| 5 | +Principal Martinez was so impressed with your work that they announced at the last staff meeting that ALL clubs should start using the website. While this is exciting, you're a bit nervous - the last thing you want is an accidental change breaking the system right before the big Fall Activities Fair! 😰 |
| 6 | + |
| 7 | +When more teachers start helping with the Mergington High activities website, it's important to add some safeguards. Thankfully, GitHub provides several ways to protect your repository: |
| 8 | + |
| 9 | +1. **Repository Rulesets** - These provide safeguards to limit: |
| 10 | + |
| 11 | + - Pushing code directly to important branches |
| 12 | + - Deleting or renaming branches |
| 13 | + - Force pushing (which can overwrite history) |
| 14 | + - (and much more) |
| 15 | + |
| 16 | +1. **`.gitignore`** - This special file tells Git which files it should NOT track, like: |
| 17 | + |
| 18 | + - Temporary files that your code creates while running |
| 19 | + - Secret configuration files with sensitive information |
| 20 | + - System files that other developers don't need |
| 21 | + |
| 22 | +> [!TIP] |
| 23 | +> Think of these settings like the editorial process of a school yearbook. Various student committees will take photos and write articles, then the yearbook president will make adjustments to make sure everything flows together properly. Finally, a teacher/advisor will sign off that all content is appropriate. |
| 24 | +
|
| 25 | +## ⌨️ Activity: (optional) Get to know our extracurricular activities site |
| 26 | + |
| 27 | +<details> |
| 28 | +<summary>Show Steps</summary> |
| 29 | + |
| 30 | +In other exercise, we have been developing the Extracurricular activities website. You can follow these steps to start up the development environment and try it out. |
| 31 | + |
| 32 | +> ! **Important:** Opening a development environment and running the application is not necessary to complete this exercise. You can skip this activity if desired. |
| 33 | +
|
| 34 | +1. Right-click the below button to open the **Create Codespace** page in a new tab. Use the default configuration. |
| 35 | + |
| 36 | + [](https://codespaces.new/{{full_repo_name}}?quickstart=1) |
| 37 | + |
| 38 | +1. Wait some time for the environment to be prepared. It will automatically install all requirements and services. |
| 39 | + |
| 40 | +1. Validate the **GitHub Copilot** and **Python** extensions are installed and enabled. |
| 41 | + |
| 42 | + <img width="300" alt="copilot extension for VS Code" src="https://github.com/user-attachments/assets/ef1ef984-17fc-4b20-a9a6-65a866def468" /><br/> |
| 43 | + <img width="300" alt="python extension for VS Code" src="https://github.com/user-attachments/assets/3040c0f5-1658-47e2-a439-20504a384f77" /> |
| 44 | + |
| 45 | +1. Try running the the application. In the left sidebar, select the **Run and Debug** tab and then press the **Start Debugging** icon. |
| 46 | + |
| 47 | + <details> |
| 48 | + <summary>📸 Show screenshot</summary><br/> |
| 49 | + |
| 50 | + <img width="300" alt="run and debug" src="https://github.com/user-attachments/assets/50b27f2a-5eab-4827-9343-ab5bce62357e" /> |
| 51 | + |
| 52 | + </details> |
| 53 | + |
| 54 | + <details> |
| 55 | + <summary>🤷 Having trouble?</summary><br/> |
| 56 | + |
| 57 | + If the **Run and Debug** area is empty, try reloading VS Code: Open the command palette (`Ctrl`+`Shift`+`P`) and search for `Developer: Reload Window`. |
| 58 | + |
| 59 | + <img width="300" alt="empty run and debug panel" src="https://github.com/user-attachments/assets/0dbf1407-3a97-401a-a630-f462697082d6" /> |
| 60 | + |
| 61 | + </details> |
| 62 | + |
| 63 | +1. Use the **Ports** tab to find the webpage address, open it, and verify it is running. |
| 64 | + |
| 65 | + <details> |
| 66 | + <summary>📸 Show screenshot</summary><br/> |
| 67 | + |
| 68 | + <img width="350" alt="ports tab" src="https://github.com/user-attachments/assets/8d24d6b5-202d-4109-8174-2f0d1e4d8d44" /> |
| 69 | + |
| 70 | +  |
| 71 | + |
| 72 | + </details> |
| 73 | + |
| 74 | +</details> |
| 75 | + |
| 76 | +## ⌨️ Activity: Add a branch ruleset |
| 77 | + |
| 78 | +To get started, let's add some protections so that no one accidentally breaks the club registration system. |
| 79 | + |
| 80 | +1. If necessary, open another tab and navigate to this repository. We will start on the **Settings** tab. |
| 81 | + |
| 82 | +1. In the left navigation, expand the **Rules** area and select **Rulesets**. |
| 83 | + |
| 84 | +1. Click the **New ruleset** dropdown and select **New branch ruleset**. |
| 85 | + |
| 86 | + <img width="250" alt="image" src="https://github.com/user-attachments/assets/1e9fd519-1421-4d6b-b654-a3fe53a8fb75" /> |
| 87 | + |
| 88 | +1. Set the **Ruleset Name** as `Protect main` and change the **Enforcement status** to `Active`. |
| 89 | + |
| 90 | + <img width="250" alt="image" src="https://github.com/user-attachments/assets/ce30fd34-39b5-4e22-b348-4af61fd05cd1" /> |
| 91 | + |
| 92 | +1. Find the **Targets** section and use the **Add target** dropdown to add 2 entries: |
| 93 | + |
| 94 | + 1. Add the **Include default branch** option to ensure protections aren't bypassed by switching the default branch. |
| 95 | + |
| 96 | + <img width="250" alt="image" src="https://github.com/user-attachments/assets/217263cc-d5c2-4ac0-b03c-a72494e5c812" /> |
| 97 | + |
| 98 | + 1. Use the **include by pattern** option and enter the pattern `main`. |
| 99 | + |
| 100 | + <img width="250" alt="image" src="https://github.com/user-attachments/assets/968c9ed8-b051-44eb-af42-d99670ad31fd" /> |
| 101 | + |
| 102 | + <img width="250" alt="image" src="https://github.com/user-attachments/assets/ddc52767-d93e-4c9e-a77a-90c3b5c08fb5" /> |
| 103 | + |
| 104 | +1. Find the **Rules** section and ensure the following items are checked. |
| 105 | + |
| 106 | + - [x] Restrict deletions |
| 107 | + - [x] Require a pull request before merging |
| 108 | + - Required approvals: `0` |
| 109 | + - [x] Require review from Code Owners |
| 110 | + - [x] Block force pushes |
| 111 | + |
| 112 | +1. Scroll to the bottom and click the **Create** button to save the ruleset. |
| 113 | + |
| 114 | +## ⌨️ Activity: Create a `.gitignore` file |
| 115 | + |
| 116 | +We know many teachers use different tools, so let's make sure they don't accidentally commit unnecessary files. |
| 117 | + |
| 118 | +1. At the top navigation, return to the **Code** tab and verify you are on the `main` branch. |
| 119 | + |
| 120 | +1. Above the list of files, click the **Add file** dropdown and select **Create new file**. |
| 121 | + |
| 122 | + <img width="300" alt="New file button" src="https://github.com/user-attachments/assets/8f3f8da8-1471-485a-9df5-8c03ecba2d8e"/> |
| 123 | + |
| 124 | +1. Enter the file name `.gitignore`. We will ignore the template selector for now and make our own. Copy the below example content into it. |
| 125 | + |
| 126 | + <img width="350" alt="preview of new file" src="https://github.com/user-attachments/assets/580d1a63-a264-4d44-8901-50ad708b8822"/> |
| 127 | + |
| 128 | + ```gitignore |
| 129 | + # Python backend for club management |
| 130 | + __pycache__/ |
| 131 | + *.py[cod] # Python compiled files |
| 132 | + *$py.class |
| 133 | + *.so |
| 134 | + .Python |
| 135 | + env/ |
| 136 | + .env # Where database passwords are stored |
| 137 | + venv/ # Virtual environment for testing |
| 138 | + .venv |
| 139 | +
|
| 140 | + # Teacher IDE settings |
| 141 | + .vscode/ # Ms. Rodriguez uses VS Code |
| 142 | + .idea/ # Mr. Chen uses PyCharm |
| 143 | +
|
| 144 | + # Local development & testing |
| 145 | + instance/ |
| 146 | + .pytest_cache/ |
| 147 | + .coverage # Test coverage reports |
| 148 | + htmlcov/ |
| 149 | +
|
| 150 | + # Staff computer files |
| 151 | + .DS_Store # For teachers with Macs |
| 152 | + Thumbs.db # For teachers with Windows |
| 153 | + ``` |
| 154 | + |
| 155 | +1. In the top right, select the **Commit changes...** button. Notice that it won't let us commit to the `main` branch! Our ruleset is working! Nice! |
| 156 | + |
| 157 | + <img width="400" alt="image" src="https://github.com/user-attachments/assets/4e85948d-75c8-4c13-8ddd-4707bf9b0805" /> |
| 158 | + |
| 159 | +1. Enter `prepare-to-collaborate` for the branch name then click the **Propose changes** button. You will be forwarded to a new page to start a new pull request. |
| 160 | + |
| 161 | +1. Set the title to `Prepare to collaborate` and click the **Create pull request** button. **Do NOT merge yet**, since we will be adding more collaboration related changes. |
| 162 | + |
| 163 | +1. With the file committed, wait a moment for Mona to check your work, provide feedback, and share the next lesson. |
| 164 | + |
| 165 | +> [!TIP] |
| 166 | +> GitHub and the community have built a repository with [sample `.gitignore` files](https://github.com/github/gitignore) for many situations. Make sure to check it out! |
| 167 | +
|
| 168 | +<details> |
| 169 | +<summary>🤷 Having trouble?</summary><br/> |
| 170 | + |
| 171 | +Make sure you pushed the `.gitignore` file to `prepare-to-collaborate` branch. Exact naming for both matters! |
| 172 | + |
| 173 | +</details> |
0 commit comments