Skip to content

Commit 9dc9306

Browse files
authored
Merge pull request #16 from arilivigni/fix-test-run-issues
Fix test run issues
2 parents c1cde0f + 7daebae commit 9dc9306

11 files changed

Lines changed: 155 additions & 92 deletions

.devcontainer/devcontainer.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,11 @@
3434
"portAttributes": {
3535
"3000": { // React port attributes
3636
"label": "octofit-tracker",
37-
"requireLocalPort": true,
38-
"visibility": "public"
37+
"requireLocalPort": true
3938
},
4039
"8000": { // Django port attributes
4140
"label": "octofit-api",
42-
"requireLocalPort": true,
43-
"visibility": "public"
41+
"requireLocalPort": true
4442
}
4543
}
4644
}

.devcontainer/post_create.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb
77
sudo apt-get update
88
sudo apt-get install -y python3-venv
99
sudo apt-get install -y mongodb-org
10+
sudo apt-get install -y mongodb-clients
1011
sudo cp --force ./.devcontainer/welcome-message.txt /usr/local/etc/vscode-dev-containers/first-run-notice.txt

.devcontainer/post_start.sh

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,100 @@
11
#!/bin/bash
2-
# This script is run after the container is startup.
2+
# This script is run after the container starts up.
33

4-
# open ports for Python Django server and React app
5-
gh cs ports visibility 8000:public -c $CODESPACE_NAME
6-
gh cs ports visibility 3000:public -c $CODESPACE_NAME
4+
set -euo pipefail
5+
6+
die() {
7+
echo "ERROR: $@" >&2
8+
exit 1
9+
}
10+
11+
# Ensure CODESPACE_NAME is set
12+
: "${CODESPACE_NAME:?CODESPACE_NAME environment variable not set. This script should be run in a GitHub Codespace environment.}"
13+
14+
# Port visibility setup
15+
echo "Setting port visibility..."
16+
gh cs ports visibility 8000:public -c "$CODESPACE_NAME" || die "Failed to set 8000 public"
17+
gh cs ports visibility 3000:public -c "$CODESPACE_NAME" || die "Failed to set 3000 public"
18+
19+
echo "Preparing MongoDB data dir..."
20+
sudo mkdir -p /data/db || die "mkdir failed"
21+
sudo chmod 777 /data/db || die "chmod failed"
22+
23+
LOGFILE=/tmp/mongod.log
24+
MAX_START_TRIES=3 # How many times to attempt (re)starting mongod
25+
READY_CHECK_RETRIES=15 # How many readiness checks per start attempt
26+
READY_CHECK_INTERVAL=1 # Seconds between readiness checks
27+
is_running() {
28+
pgrep -x mongod >/dev/null 2>&1
29+
}
30+
31+
start_mongod() {
32+
if is_running; then
33+
echo "mongod already running"
34+
return 0
35+
fi
36+
# Clean old log (keep last one for inspection)
37+
> "$LOGFILE"
38+
echo "Launching mongod (dbpath=/data/db, log=$LOGFILE)..."
39+
mongod --dbpath /data/db --fork --logpath "$LOGFILE"
40+
}
41+
42+
ready_check() {
43+
# Success if log shows ready OR port is open
44+
if grep -q "Waiting for connections" "$LOGFILE" 2>/dev/null; then
45+
return 0
46+
fi
47+
if command -v nc >/dev/null 2>&1; then
48+
if nc -z 127.0.0.1 27017 2>/dev/null; then
49+
return 0
50+
fi
51+
fi
52+
# If mongo/mongosh client exists, try a ping (quietly)
53+
if command -v mongosh >/dev/null 2>&1; then
54+
mongosh --quiet --eval 'db.runCommand({ping:1})' >/dev/null 2>&1 && return 0 || true
55+
elif command -v mongo >/dev/null 2>&1; then
56+
mongo --quiet --eval 'db.runCommand({ping:1})' >/dev/null 2>&1 && return 0 || true
57+
fi
58+
return 1
59+
}
60+
61+
wait_for_ready() {
62+
for ((i=1; i<=READY_CHECK_RETRIES; i++)); do
63+
if ready_check; then
64+
echo "mongod is ready (after $i checks)."
65+
return 0
66+
fi
67+
sleep "$READY_CHECK_INTERVAL"
68+
done
69+
return 1
70+
}
71+
72+
echo "Starting MongoDB with retries..."
73+
for ((attempt=1; attempt<=MAX_START_TRIES; attempt++)); do
74+
echo "Start attempt $attempt/$MAX_START_TRIES"
75+
if start_mongod; then
76+
if wait_for_ready; then
77+
tail -20 "$LOGFILE" || true
78+
echo "MongoDB started successfully."
79+
break
80+
else
81+
echo "Readiness check failed for attempt $attempt."
82+
fi
83+
else
84+
echo "mongod launch command failed on attempt $attempt."
85+
fi
86+
87+
if (( attempt == MAX_START_TRIES )); then
88+
tail -40 "$LOGFILE" || true
89+
die "MongoDB failed to start after $MAX_START_TRIES attempts"
90+
fi
91+
92+
echo "Cleaning up before next attempt..."
93+
if is_running; then
94+
pkill -x mongod || true
95+
sleep 2
96+
fi
97+
done
98+
99+
echo "post_start.sh completed successfully."
100+
exit 0

.github/instructions/octofit_tracker.instructions.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,35 @@ I want to build an Octofit Tracker app that will include the following:
1313
* Competitive leader board
1414
* Personalized workout suggestions
1515

16+
## Forwarded ports
17+
18+
- 8000: public
19+
- 3000: public
20+
- 27017: private
21+
22+
Do not propose any other ports to forward or to make public
23+
24+
## OctoFit Codespace variable for the API URL
25+
26+
Always set the `OCTOFIT_API_BASE_URL` environment variable to your codespace URL. This will be used in your API endpoints.
27+
28+
```bash
29+
export OCTOFIT_API_BASE_URL="$CODESPACE_NAME-8000.app.github.dev"
30+
```
31+
1632
## OctoFit Tracker App structure
1733

1834
The section defines the OctoFit Tracker App's structure
1935

2036

2137
The directory tree for the OctoFit Tracker App
38+
39+
```text
2240
octofit-tracker/
2341
├── backend/
2442
│ ├── venv/
2543
└── frontend/
26-
44+
```
2745

2846
## Python virtual environment and requirements
2947

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
---
22
mode: 'agent'
33
model: GPT-4.1
4-
description: 'Initialize and populate the octofit_db database'
4+
description: 'Setup, configure, and populate the octofit_db database with test data for the Octofit Tracker Django app.'
55
---
66

7-
- Your task is to create the Django project in octofit-tracker/backend/octofit_tracker directory
8-
- Activate the Python existing virtual environment octofit_tracker/backend/venv/bin/activate.
9-
- mongodb-org-shell is already installed
10-
- use mongosh as the client tool to talk to mongodb
7+
# Environment Setup
8+
- Use the existing Python virtual environment in `octofit-tracker/backend/venv`.
9+
- Do not create a new Python virtual environment.
10+
- Activate with: `source octofit-tracker/backend/venv/bin/activate`
11+
- `mongodb-org-shell` is already installed; use `mongosh` to interact with MongoDB.
1112

13+
# Database Initialization & Population
14+
1. Ensure the MongoDB service is running.
15+
2. Configure Django in `octofit-tracker/backend/octofit_tracker/settings.py` to connect to the `octofit_db` database using Djongo, with no authentication.
16+
3. Make sure `octofit_tracker`, `rest_framework`, and `djongo` are in `INSTALLED_APPS`.
17+
4. Enable CORS in `settings.py` to allow all origins, methods, and headers. Allow all hosts.
18+
5. Install and configure CORS middleware components.
19+
6. Run `makemigrations` and `migrate` in the Python virtual environment.
20+
7. Initialize the `octofit_db` database and create collections for users, teams, activities, leaderboard, and workouts.
21+
8. Ensure a unique index on the `email` field for the user collection (e.g., `db.users.createIndex({ "email": 1 }, { unique: true })`).
22+
9. Populate the database with test data for all collections using the Django management command in `octofit-tracker/backend/octofit_tracker/management/commands/populate_db.py` (help message: 'Populate the octofit_db database with test data').
23+
10. Verify the database and collections were created and populated successfully using `mongosh`.
24+
11. List the collections in the `octofit_db` database and show sample documents from each.
1225

13-
To init and populate the octofit_db database follow these steps.
14-
1. Make sure the mongodb service is running.
15-
2. Initialize the mongodb octofit_db database.
16-
3. Create a correct table structure for users, teams, activities, leaderboard, and workouts collections.
17-
4. Make sure there is a unique ID for the primary key for the user collection.
18-
ex. db.users.createIndex({ "email": 1 }, { unique: true })
19-
4. Execute the command for me to create the database.
20-
5. Verify that the database was created successfully.
21-
6. List the collections in the octofit_db database.
26+
# Verification
27+
- After population, verify with `mongosh` that the `octofit_db` database contains the correct collections and test data.
28+
- Confirm Django REST API endpoints are available for all collections.

.github/steps/1-preparing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Great work! Let's ask copilot for help starting a branch so we can do some custo
6565
> ![Static Badge](https://img.shields.io/badge/-Prompt-text?style=flat-square&logo=github%20copilot&labelColor=512a97&color=ecd8ff)
6666
>
6767
> ```prompt
68-
> Create and publish a new Git branch called build-octofit-app?
68+
> Please create and publish a new Git branch called build-octofit-app
6969
> ```
7070
7171
Copilot agent mode will respond and prompt you to **continue** to execute the command.<br/>

.github/steps/2-application-initial-setup.md

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,6 @@ In this step, we will accomplish the following:
2929
> - Install the Python requirements from the file created
3030
>```
3131
32-
### :keyboard: Activity: Let's start and verify MongoDB is running
33-
34-
> ![Static Badge](https://img.shields.io/badge/-Prompt-text?style=flat-square&logo=github%20copilot&labelColor=512a97&color=ecd8ff)
35-
>
36-
> ```prompt
37-
> Let's install, start, and verify mongodb-org service for Ubuntu 22.
38-
>
39-
> 1. Start mongo db manually
40-
> 2. Verify mongodb is running manually
41-
>
42-
>```
43-
4432
1. Now that we have created the app directory structure, setup a Python virtual environment, and Copilot agent mode helped write a `requirements.txt` to install all project dependencies, let's check our changes in to our `build-octofit-app` branch.
4533
4634
1. With our new changes complete, please **commit** and **push** the changes to branch `build-octofit-app`.

.github/steps/3-django-project-setup.md

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ In this activity we will leverage a feature in vscode called prompt files. A pro
3333
> [!IMPORTANT]
3434
> - Don't start the Python Django app in the way that GitHub Copilot agent mode suggests hit **cancel**.
3535
36-
### :keyboard: Activity: Initialize and create the octofit_db MongoDB database
36+
### :keyboard: Activity: Initialize, create, and populate the octofit_db MongoDB database
3737
3838
Let's continue to leverage a prompt file that has been created by the IT department for us to initialize and create the octofit_db MongoDB database. Copy/paste the following prompt in the GitHub Copilot Chat and select the "Agent" instead of "Ask" or "Edit" from the drop down where you are inserting the prompt.
3939
@@ -53,26 +53,10 @@ Now let's create a prompt file of our own that we can share with other staff to
5353
> ```prompt
5454
> Let's add the following to a prompt file called `update-octofit-tracker-app.prompt.md` in the `.github/prompts` directory and add mode: 'agent' and model: GPT-4.1 to the prompt file.
5555
>
56-
> ## Use the existing Python virtual environment
57-
>
58-
> - Use the existing Python virtual environment we already created in directory octofit-tracker/backend/venv.
59-
> - Do not create a new Python virtual environment as part of this process.
60-
> source octofit-tracker/backend/venv/bin/activate
61-
>
62-
> ## Update octofit-tracker/backend/octofit_tracker app files
63-
>
64-
> 1. Update the octofit-tracker/backend/octofit_tracker/settings.py file to include the MongoDB database connections for octofit_db and djongo with no authentication.
65-
> 2. Update the octofit-tracker/backend/octofit_tracker/models.py file to include the models for users, teams, activities, leaderboard, and workouts collections.
66-
> 3. Update the octofit-tracker/backend/octofit_tracker/serializers.py file to include the serializers for users, teams, activities, leaderboard, and workouts collections.
67-
> 4. Update the octofit-tracker/backend/octofit_tracker/urls.py file to include the URLs for users, teams, activities, leaderboard, and workouts collections.
68-
> 5. Update the octofit-tracker/backend/octofit_tracker/views.py file to include the views for users, teams, activities, leaderboard, and workouts collections.
69-
> 6. Update the octofit-tracker/backend/octofit_tracker/tests.py file to include the tests for users, teams, activities, leaderboard, and workouts collections.
70-
> 7. Update the octofit-tracker/backend/octofit_tracker/admin.py file to include the admin for users, teams, activities, leaderboard, and workouts collections.
71-
> 8. Make sure api_root is in octofit-tracker/backend/octofit_tracker/urls.py
72-
> 9. Enable CORS in the octofit-tracker/backend/octofit_tracker/settings.py file to allow cross-origin requests from the frontend React app and allow all origins, methods, and headers.
73-
> 10. Allow all hosts in the settings.py file.
74-
> 11. Install CORS middleware components.
75-
> 12. Make sure there are apps for octofit_tracker, rest_framework, and djongo in the INSTALLED_APPS setting.
56+
> # Django App Updates
57+
> 1. Update `settings.py` for MongoDB connection and CORS.
58+
> 2. Update `models.py`, `serializers.py`, `urls.py`, `views.py`, `tests.py`, and `admin.py` to support users, teams, activities, leaderboard, and workouts collections.
59+
> 3. Ensure `api_root` is present in `urls.py`.
7660
> ```
7761
7862
> [!TIP]
@@ -95,29 +79,6 @@ Copy/paste the following prompt in the GitHub Copilot Chat and select the "Agent
9579
9680
> ❕ **Important:** Don't start the Python Django app in the way that GitHub Copilot agent mode suggests hit **cancel**.
9781
98-
### :keyboard: Activity: Populate the octofit_db database with test data from Django project/app files
99-
100-
> ![Static Badge](https://img.shields.io/badge/-Prompt-text?style=flat-square&logo=github%20copilot&labelColor=512a97&color=ecd8ff)
101-
>
102-
> ```prompt
103-
> Let's populate the octofit_db database with test data.
104-
>
105-
> - Create test data for users, teams, activities, leaderboard, and workouts collections.
106-
>
107-
> - Activate the Python existing virtual environment octofit-tracker/backend/venv/bin/activate.
108-
>
109-
> 1. Run makemigrations and migrate the database in a Python virtual environment.
110-
> 2. Populate the octofit_db database with test data for users, teams, activities, leaderboard, and workouts collections based on test data in our instructions to octofit-tracker/backend/octofit_tracker/management/commands/populate_db.py.
111-
> 3. Help message in populate_db.py contains 'Populate the octofit_db database with test data'
112-
> 4. Verify the test data is populated in the octofit_db database.
113-
>
114-
> ```
115-
116-
> ❕ **Important:**
117-
- Don't start the Python Django app in the way that GitHub Copilot agent mode suggests hit **cancel**.
118-
- If there is no `Continue` button, just pull the left side of the GitHub Copilot Chat panel over to the left, and it should appear.
119-
- If this doesn't work, you may need to copy and paste the response in the terminal if there is no `Continue` button.
120-
12182
1. Now that we have created the database structure, updated our Django project files, and populated the database, let's check our changes into our `build-octofit-app` branch.
12283
12384
1. With our new changes complete, please **commit** and **push** the changes to GitHub.

.github/steps/4-setup-django-rest-framework.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,15 @@ Copy and paste the following prompt(s) in the GitHub Copilot Chat and select the
1414
> - Keep files created and updated by Copilot agent mode until it is finished.
1515
> - Agent mode has the ability to evaluate your code base and execute commands and add/refactor/delete parts of your code base and automatically self heal if it or you makes a mistake in the process.
1616
17-
### :keyboard: Activity: Setup Django REST Framework, restart the server, and test the API
18-
19-
> [!NOTE]
20-
> - Make sure to replace [REPLACE-THIS-WITH-YOUR-CODESPACE-NAME] with your codespace name.
21-
> - ex. redesigned-spork-g6pj46rr9hpp6x
22-
> - You can get the codespace name by running the following command in the terminal: `echo $CODESPACE_NAME`.
17+
### :keyboard: Activity: Setup Django REST Framework and test the REST API endpoints
2318

2419
> ![Static Badge](https://img.shields.io/badge/-Prompt-text?style=flat-square&logo=github%20copilot&labelColor=512a97&color=ecd8ff)
2520
>
2621
> ```prompt
2722
> Let's setup codespace for the URL, start the server via VS Code launch.json, and test the API.
28-
>
29-
> 1. Create an environment variable `OCTOFIT_API_BASE_URL` with the value `[REPLACE-THIS-WITH-YOUR-CODESPACE-NAME]-8000.app.github.dev` for use in your API endpoints in `settings.py`, `urls.py`, and `views.py`.
30-
> 2. Update `urls.py` to use the value of the environment variable `OCTOFIT_API_BASE_URL` in your REST API URL endpoints (e.g., `https://${OCTOFIT_API_BASE_URL}`) for Django, to avoid certificate HTTPS issues.
23+
>
24+
> 1. Create an environment variable OCTOFIT_API_BASE_URL by executing this command in your terminal: `export OCTOFIT_API_BASE_URL="$CODESPACE_NAME-8000.app.github.dev"`
25+
> 2. Update views.py and urls.py to replace the return for the REST API URL endpoints with the environment variable OCTOFIT_API_BASE_URL https://$OCTOFIT_API_BASE_URL for Django and avoid certificate HTTPS issues.
3126
> 3. Make sure the Django backend works on your codespace URL (i.e., the value of `OCTOFIT_API_BASE_URL`) by updating `ALLOWED_HOSTS` in `octofit-tracker/backend/octofit_tracker/settings.py`.
3227
> 4. Test the API endpoints using curl command.
3328
>```

.github/steps/5-setup-frontend-react-framework.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ Copy and paste the following prompt(s) in the GitHub Copilot Chat and select the
4949
> ```prompt
5050
> Let's update the octofit-tracker frontend React components.
5151
>
52-
> - Update the following components to include the React framework to point to the backend API:
52+
> - Create an environment variable OCTOFIT_API_BASE_URL by executing this command in your terminal: `export OCTOFIT_API_BASE_URL="$CODESPACE_NAME-8000.app.github.dev"`
53+
> - Update the following components to include the React framework to point to the backend REST API:
5354
> - src/App.js
5455
> - src/index.js
5556
> - src/components/Activities.js
@@ -58,7 +59,7 @@ Copy and paste the following prompt(s) in the GitHub Copilot Chat and select the
5859
> - src/components/Users.js
5960
> - src/components/Workouts.js
6061
> - In each component replace the fetch url with the codespace url
61-
> https://[REPLACE-THIS-WITH-YOUR-CODESPACE-NAME]-8000.app.github.dev/api/<component>
62+
> https://{OCTOFIT_API_BASE_URL}/api/<component>
6263
> for the Django rest framework backend.
6364
> - Make sure to use the correct port and protocol http or https.
6465
> - Update src/App.js to include the main navigation for all components.

0 commit comments

Comments
 (0)