Skip to content
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

feat: Link Datadoc to GitHub #3

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "querybook",
"version": "3.34.2",
"version": "3.35.0",
"description": "A Big Data Webapp",
"private": true,
"scripts": {
Expand Down
52 changes: 52 additions & 0 deletions querybook/migrations/versions/a8bb37120302_add_github_link.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Add Github Link

Revision ID: a8bb37120302
Revises: f7b11b3e3a95
Create Date: 2024-10-18 15:50:51.471760

"""

from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "a8bb37120302"
down_revision = "f7b11b3e3a95"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"github_link",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("datadoc_id", sa.Integer(), nullable=False),
sa.Column("user_id", sa.Integer(), nullable=False),
sa.Column("repo_url", sa.String(length=255), nullable=False),
sa.Column("branch", sa.String(length=255), nullable=False),
sa.Column("file_path", sa.String(length=255), nullable=False),
sa.Column(
"created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False
),
sa.Column(
"updated_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False
),
sa.ForeignKeyConstraint(
["datadoc_id"],
["data_doc.id"],
),
sa.ForeignKeyConstraint(
["user_id"],
["user.id"],
),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("datadoc_id"),
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table("github_link")
# ### end Alembic commands ###
18 changes: 18 additions & 0 deletions querybook/server/datasources/github.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from app.datasource import register
from lib.github_integration.github_integration import get_github_manager
from typing import Dict
from logic import github as logic
from flask_login import current_user


@register("/github/auth/", methods=["GET"])
Expand All @@ -14,3 +16,19 @@ def is_github_authenticated() -> str:
github_manager = get_github_manager()
is_authenticated = github_manager.get_github_token() is not None
return {"is_authenticated": is_authenticated}


@register("/github/datadocs/<int:datadoc_id>/link/", methods=["POST"])
def link_datadoc_to_github(
datadoc_id: int,
repo_url: str,
branch: str,
file_path: str,
) -> Dict:
return logic.create_repo_link(
datadoc_id=datadoc_id,
user_id=current_user.id,
repo_url=repo_url,
branch=branch,
file_path=file_path,
)
44 changes: 44 additions & 0 deletions querybook/server/logic/github.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from app.db import with_session
from models.github import GitHubLink
from models.datadoc import DataDoc


@with_session
def create_repo_link(
datadoc_id: int,
user_id: int,
repo_url: str,
branch: str,
file_path: str,
commit=True,
session=None,
):
datadoc = DataDoc.get(id=datadoc_id, session=session)
assert datadoc is not None, f"DataDoc with id {datadoc_id} not found"

github_link = GitHubLink.get(datadoc_id=datadoc_id, session=session)
assert (
github_link is None
), f"GitHub link for DataDoc with id {datadoc_id} already exists"

github_link = GitHubLink.create(
{
"datadoc_id": datadoc_id,
"user_id": user_id,
"repo_url": repo_url,
"branch": branch,
"file_path": file_path,
},
commit=commit,
session=session,
)
return github_link


@with_session
def get_repo_link(datadoc_id: int, session=None):
github_link = GitHubLink.get(datadoc_id=datadoc_id, session=session)
assert (
github_link is not None
), f"GitHub link for DataDoc with id {datadoc_id} not found"
return github_link
1 change: 1 addition & 0 deletions querybook/server/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
from .data_element import *
from .comment import *
from .survey import *
from .github import *
41 changes: 41 additions & 0 deletions querybook/server/models/github.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import sqlalchemy as sql
from sqlalchemy.sql import func
from lib.sqlalchemy import CRUDMixin
from sqlalchemy.orm import backref, relationship
from app import db

Base = db.Base


class GitHubLink(Base, CRUDMixin):
__tablename__ = "github_link"
id = sql.Column(sql.Integer, primary_key=True, autoincrement=True)
datadoc_id = sql.Column(
sql.Integer, sql.ForeignKey("data_doc.id"), nullable=False, unique=True
)
user_id = sql.Column(sql.Integer, sql.ForeignKey("user.id"), nullable=False)
repo_url = sql.Column(sql.String(255), nullable=False)
branch = sql.Column(sql.String(255), nullable=False)
file_path = sql.Column(sql.String(255), nullable=False)
created_at = sql.Column(sql.DateTime, server_default=func.now(), nullable=False)
updated_at = sql.Column(
sql.DateTime, server_default=func.now(), onupdate=func.now(), nullable=False
)

datadoc = relationship(
"DataDoc",
backref=backref("github_link", uselist=False, cascade="all, delete-orphan"),
)
user = relationship("User", backref=backref("github_link", uselist=False))

def to_dict(self):
return {
"id": self.id,
"datadoc_id": self.datadoc_id,
"user_id": self.user_id,
"repo_url": self.repo_url,
"branch": self.branch,
"file_path": self.file_path,
"created_at": self.created_at,
"updated_at": self.updated_at,
}