Skip to content

Create github repo on course creation #482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
101 changes: 100 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ opentelemetry-distro = "*"
opentelemetry-instrumentation-django = "*"
opentelemetry-exporter-richconsole = "*"
opentelemetry-exporter-otlp-proto-http = "*"
openedx-events = "9.14.0"

[tool.poetry.group.dev.dependencies]
pre-commit = "^3.0.0"
Expand Down
3 changes: 3 additions & 0 deletions src/ol_openedx_git_auto_export/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ python_sources(
name="git_auto_export",
dependencies=[
"src/ol_openedx_git_auto_export/settings:git_auto_export_settings",
"src/ol_openedx_git_auto_export/migrations:git_auto_export_migrations",
"src/ol_openedx_git_auto_export/management/commands:git_auto_export_management_commands",
"//:external_dependencies#edx-opaque-keys",
"//:external_dependencies#celery",
"//:external_dependencies#openedx-events",
],
)

Expand Down
21 changes: 21 additions & 0 deletions src/ol_openedx_git_auto_export/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Django admin pages for git-auto-export plugin
"""

from django.contrib import admin
from ol_openedx_git_auto_export.models import CourseGitRepo


class CourseGitRepoAdmin(admin.ModelAdmin):
"""
Admin interface for the CourseGitRepo model.
"""

list_display = ("course_id", "git_url")
search_fields = ("course_id", "git_url")
list_filter = ("course_id",)
ordering = ("course_id",)
list_per_page = 20


admin.site.register(CourseGitRepo, CourseGitRepoAdmin)
7 changes: 6 additions & 1 deletion src/ol_openedx_git_auto_export/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ class GitAutoExportConfig(AppConfig):
PluginSignals.RECEIVER_FUNC_NAME: "listen_for_course_publish",
PluginSignals.SIGNAL_PATH: "xmodule.modulestore.django.COURSE_PUBLISHED", # noqa: E501
PluginSignals.DISPATCH_UID: "ol_openedx_git_auto_export.signals.listen_for_course_publish", # noqa: E501
}
},
{
PluginSignals.RECEIVER_FUNC_NAME: "listen_for_course_created",
PluginSignals.SIGNAL_PATH: "openedx_events.content_authoring.signals.COURSE_CREATED", # noqa: E501
PluginSignals.DISPATCH_UID: "ol_openedx_git_auto_export.signals.listen_for_course_created", # noqa: E501
},
],
},
},
Expand Down
3 changes: 3 additions & 0 deletions src/ol_openedx_git_auto_export/constants.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
ENABLE_GIT_AUTO_EXPORT = "ENABLE_GIT_AUTO_EXPORT"
ENABLE_AUTO_GITHUB_REPO_CREATION = "ENABLE_AUTO_GITHUB_REPO_CREATION"
GITHUB_ORG = "GITHUB_ORG"
GITHUB_ACCESS_TOKEN = "GITHUB_ACCESS_TOKEN" # noqa: S105
Empty file.
1 change: 1 addition & 0 deletions src/ol_openedx_git_auto_export/management/commands/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python_sources(name="git_auto_export_management_commands")
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from django.core.management.base import BaseCommand
from ol_openedx_git_auto_export.models import CourseGitRepo
from ol_openedx_git_auto_export.signals import listen_for_course_created
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
from openedx_events.content_authoring.data import CourseData
from xmodule.modulestore.django import modulestore


class Command(BaseCommand):
help = "Migrate git URLs from courses to CourseGitRepo model"

def add_arguments(self, parser):
parser.add_argument(
"course_ids",
nargs="*",
type=str,
help="List of course IDs to migrate their giturl",
)

def handle(self, *args, **options): # noqa: ARG002
course_ids = options.get("course_ids")
courses = CourseOverview.objects.all()
if course_ids:
courses = courses.filter(id__in=course_ids)

seen_giturls = set()
for course in courses:
course_module = modulestore().get_course(course.id, depth=1)
giturl = course_module.giturl
if giturl and giturl not in seen_giturls:
seen_giturls.add(giturl)
self.stdout.write(
self.style.SUCCESS(f"Course {course.id} has giturl: {giturl}")
)
CourseGitRepo.objects.get_or_create(
course_id=course.id,
defaults={"git_url": giturl},
)
elif giturl and giturl in seen_giturls:
self.stdout.write(
self.style.WARNING(
f"Course {course.id} has a duplicate giturl: {giturl}"
)
)
ssh_url = listen_for_course_created(CourseData(course_key=course.id))
if ssh_url:
seen_giturls.add(ssh_url)
else:
self.stdout.write(
self.style.WARNING(f"Course {course.id} does not have a giturl.")
)

self.stdout.write(self.style.SUCCESS("Git URLs migrated successfully."))
31 changes: 31 additions & 0 deletions src/ol_openedx_git_auto_export/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 4.2.20 on 2025-04-18 10:05

from django.db import migrations, models


class Migration(migrations.Migration):
initial = True

dependencies = [] # type: ignore # noqa: PGH003

operations = [
migrations.CreateModel(
name="CourseGitRepo",
fields=[
(
"course_id",
models.CharField(
max_length=255, primary_key=True, serialize=False, unique=True
),
),
("git_url", models.CharField(max_length=255)),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
],
options={
"verbose_name": "Course Git Repository",
"verbose_name_plural": "Course Git Repositories",
"ordering": ["-created_at"],
},
),
]
1 change: 1 addition & 0 deletions src/ol_openedx_git_auto_export/migrations/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python_sources(name="git_auto_export_migrations")
Empty file.
20 changes: 20 additions & 0 deletions src/ol_openedx_git_auto_export/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.db import models


class CourseGitRepo(models.Model):
"""
Model to store Git repository information for courses.
"""

course_id = models.CharField(max_length=255, unique=True, primary_key=True)
git_url = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

class Meta:
verbose_name = "Course Git Repository"
verbose_name_plural = "Course Git Repositories"
ordering = ["-created_at"]

def __str__(self):
return f"CourseGitRepo(course_id={self.course_id}, git_url={self.git_url})"
8 changes: 7 additions & 1 deletion src/ol_openedx_git_auto_export/settings/common.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
"""Common settings unique to the git auto export plugin."""

from ol_openedx_git_auto_export.constants import ENABLE_GIT_AUTO_EXPORT
from ol_openedx_git_auto_export.constants import (
ENABLE_AUTO_GITHUB_REPO_CREATION,
ENABLE_GIT_AUTO_EXPORT,
)


def plugin_settings(settings):
"""Settings for the git auto export plugin.""" # noqa: D401
settings.GIT_REPO_EXPORT_DIR = "/edx/var/edxapp/export_course_repos"
settings.GITHUB_ORG_API_URL = "https://api.github.com/orgs/edx"
settings.GITHUB_ACCESS_TOKEN = "token" # noqa: S105
settings.FEATURES[ENABLE_GIT_AUTO_EXPORT] = True
settings.FEATURES[ENABLE_AUTO_GITHUB_REPO_CREATION] = False
8 changes: 7 additions & 1 deletion src/ol_openedx_git_auto_export/settings/production.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
"""Production settings unique to the git auto export plugin."""

from ol_openedx_git_auto_export.constants import ENABLE_GIT_AUTO_EXPORT
from ol_openedx_git_auto_export.constants import (
ENABLE_AUTO_GITHUB_REPO_CREATION,
ENABLE_GIT_AUTO_EXPORT,
)


def plugin_settings(settings):
"""Settings for the git auto export plugin.""" # noqa: D401
settings.GIT_REPO_EXPORT_DIR = "/edx/var/edxapp/export_course_repos"
settings.GITHUB_ORG_API_URL = "https://api.github.com/orgs/edx"
settings.GITHUB_ACCESS_TOKEN = "token" # noqa: S105
settings.FEATURES[ENABLE_GIT_AUTO_EXPORT] = True
settings.FEATURES[ENABLE_AUTO_GITHUB_REPO_CREATION] = False
Loading
Loading