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(app_source): Handle apps hosted on GitLab instances #1725

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
11 changes: 1 addition & 10 deletions press/press/doctype/app_release/app_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import frappe
from frappe.model.document import Document
from press.api.github import get_access_token
from press.press.doctype.app_source.app_source import AppSource
from press.utils import log_error

Expand Down Expand Up @@ -222,15 +221,7 @@ def _clone_repo(self):
self.output += self.run(f"git reset --hard {self.hash}")

def _get_repo_url(self, source: "AppSource") -> str:
if not source.github_installation_id:
return source.repository_url

token = get_access_token(source.github_installation_id)
if token is None:
# Do not edit without updating deploy_notifications.py
raise Exception("App installation token could not be fetched", self.app)

return f"https://x-access-token:{token}@github.com/{source.repository_owner}/{source.repository}"
return source.get_repo_url()

def on_trash(self):
if self.clone_directory and os.path.exists(self.clone_directory):
Expand Down
13 changes: 13 additions & 0 deletions press/press/doctype/app_source/app_source.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"column_break_11",
"github_installation_id",
"uninstalled",
"gitlab_project_id",
"gitlab_access_token",
"section_break_12",
"versions",
"github_section",
Expand Down Expand Up @@ -77,6 +79,17 @@
"fieldtype": "Data",
"label": "GitHub Installation ID"
},
{
"fieldname": "gitlab_project_id",
"fieldtype": "Data",
"label": "GitLab Project ID"
},
{
"depends_on": "eval:doc.gitlab_project_id",
"fieldname": "gitlab_access_token",
"fieldtype": "Password",
"label": "GitLab Access Token"
},
{
"fieldname": "data_3",
"fieldtype": "Column Break"
Expand Down
59 changes: 59 additions & 0 deletions press/press/doctype/app_source/app_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class AppSource(Document):
enabled: DF.Check
frappe: DF.Check
github_installation_id: DF.Data | None
gitlab_access_token: DF.Password | None
gitlab_project_id: DF.Data | None
last_github_poll_failed: DF.Check
last_github_response: DF.Code | None
last_synced: DF.Datetime | None
Expand Down Expand Up @@ -99,12 +101,64 @@ def before_save(self):
_, self.repository_owner, self.repository = self.repository_url.rsplit("/", 2)
# self.create_release()

def create_gitlab_release(self, force=False):
from urllib.parse import urlparse

repo_url = urlparse(self.repository_url)
api_path = (
f"/api/v4/projects/{self.gitlab_project_id}/repository/branches/{self.branch}"
)
url = f"{repo_url.scheme}://{repo_url.netloc}{api_path}"

headers = {}

gitlab_token = ""
if self.get("gitlab_access_token"):
gitlab_token = gitlab_token or self.get_password("gitlab_access_token")

if gitlab_token:
headers["Authorization"] = f"Bearer {gitlab_token}"

res = requests.get(url, headers=headers).json()

frappe.db.set_value(
"App Source",
self.name,
{
"last_github_response": "GitLab",
"last_github_poll_failed": False,
"last_synced": frappe.utils.now(),
},
)
commit_hash = res["commit"]["id"]
commit_message = res["commit"]["message"]
commit_author = res["commit"]["author_name"]
if not frappe.db.exists(
"App Release", {"app": self.app, "source": self.name, "hash": commit_hash}
):
frappe.get_doc(
{
"doctype": "App Release",
"app": self.app,
"source": self.name,
"hash": commit_hash,
"team": self.team,
"message": commit_message,
"author": commit_author,
"deployable": False,
}
).insert(set_name=f"{self.name}-{commit_hash}")

@frappe.whitelist()
def create_release(
self,
force: bool = False,
commit_hash: str | None = None,
):
if self.gitlab_project_id:
self.create_gitlab_release(force)
return

if self.last_github_poll_failed and not force:
return

Expand Down Expand Up @@ -234,6 +288,11 @@ def get_access_token(self) -> Optional[str]:
return frappe.get_value("Press Settings", None, "github_access_token")

def get_repo_url(self) -> str:
if self.get("gitlab_access_token"):
return self.repository_url.replace(
"https://", f"https://oauth2:{self.get_password('gitlab_access_token')}@"
)

if not self.github_installation_id:
return self.repository_url

Expand Down
Loading