From db31a91f48a33bcff74cffda918da5c6fabe8d30 Mon Sep 17 00:00:00 2001 From: Morg42 <43153739+Morg42@users.noreply.github.com> Date: Thu, 14 Nov 2024 14:23:48 +0100 Subject: [PATCH] githubplugin: fix error handling, add pull repo feature --- githubplugin/__init__.py | 35 +++++++++++++++++++++++-- githubplugin/webif/__init__.py | 19 ++++++++++++++ githubplugin/webif/templates/index.html | 35 ++++++++++++++++++++++--- 3 files changed, 84 insertions(+), 5 deletions(-) diff --git a/githubplugin/__init__.py b/githubplugin/__init__.py index a5f61aefa..c1bc93bab 100644 --- a/githubplugin/__init__.py +++ b/githubplugin/__init__.py @@ -504,8 +504,7 @@ def create_repo(self, name, owner, plugin, branch=None) -> bool: except FileExistsError: self.loggerr(f'plugin link {repo["link"]} was created by someone else while we were setting up repo. Not overwriting, check link file manually') - self.repos[name] = self.init_repos[name] - del self.init_repos[name] + self.repos[name] = repo return True @@ -607,6 +606,38 @@ def is_repo_clean(self, name: str, exc=False) -> bool: self.repos[name]['clean'] = clean return clean + def pull_repo(self, name: str) -> bool: + """ pull repo if clean """ + if not name or name not in self.repos: + self.loggerr(f'repo {name} invalid or not found') + return False + + try: + res = self.is_repo_clean(name) + if not res: + raise GPError('worktree not clean') + except Exception as e: + self.loggerr(f'error checking repo {name}: {e}') + return False + + repo = self.repos[name]['repo'] + org = None + try: + org = repo.remotes.origin + except Exception: + if len(repo.remotes) > 0: + org = repo.remotes.get('origin') + + if org is None: + self.loggerr(f'remote "origin" not found in remotes {repo.remotes}') + return False + + try: + org.pull() + return True + except Exception as e: + self.loggerr(f'error while pulling: {e}') + def setup_github(self) -> bool: """ login to github and set repo """ try: diff --git a/githubplugin/webif/__init__.py b/githubplugin/webif/__init__.py index 9fb1c33b5..8a8500369 100644 --- a/githubplugin/webif/__init__.py +++ b/githubplugin/webif/__init__.py @@ -152,6 +152,25 @@ def isRepoClean(self): cherrypy.response.status = ERR_CODE return {"error": str(e)} + @cherrypy.expose + @cherrypy.tools.json_in() + @cherrypy.tools.json_out() + def pullRepo(self): + try: + json = cherrypy.request.json + name = json.get('name') + + if not name or name not in self.plugin.repos: + raise Exception(f'repo {name} invalid or not found') + + if self.plugin.pull_repo(name): + return {"operation": "request", "result": "success"} + else: + raise Exception(f'pull for repo {name} failed') + except Exception as e: + cherrypy.response.status = ERR_CODE + return {"error": str(e)} + @cherrypy.expose @cherrypy.tools.json_in() @cherrypy.tools.json_out() diff --git a/githubplugin/webif/templates/index.html b/githubplugin/webif/templates/index.html index 5ae84ccb6..d724bede2 100644 --- a/githubplugin/webif/templates/index.html +++ b/githubplugin/webif/templates/index.html @@ -44,6 +44,14 @@ + +
× @@ -166,6 +174,12 @@ $('#alert').show(); } + function successMsg(msg) { + // show message + document.getElementById('successmsg').textContent = msg; + $('#success').show(); + } + function clearSelect(sel) { // empty HTML select value list except for first "empty" entry var i, L = sel.options.length - 1; @@ -457,6 +471,17 @@ } } + function pullRepo(name) { + // try to pull from origin for given repo + if (name != '') { + sendData("pullRepo", {'name': name}, + function(response) {}, + function(response) { + successMsg('Plugin ' + name + ' erfolgreich aktualisiert'); + } + ); + } + } function removePlugin(owner, branch, plugin, name) { // check if plugin can be removed, show confirmation if (name != '') { @@ -469,7 +494,7 @@ if (clean) { showModal('remove', owner, branch, plugin, name); } else { - alert('Plugin ' + name + ' kann nicht entfernt werden, Repo ist nicht sauber (lose Dateien im Arbeitsverzeichnis, Änderungen am Index, commits nicht gepushed).') + alertMsg('Plugin ' + name + ' kann nicht entfernt werden, Repo ist nicht sauber (lose Dateien im Arbeitsverzeichnis, Änderungen am Index, commits nicht gepushed).') } } ); @@ -550,8 +575,9 @@ $(document).ready( function () { - // hide alert popup + // hide alert popups $('#alert').hide(); + $('#success').hide(); // enable datatable table $('#plugintable').DataTable( { @@ -591,7 +617,10 @@ {{ repos[plugin].owner }} {{ repos[plugin].branch }} {{ repos[plugin].full_wt_path }} - {% if not repos[plugin].clean %}Änderungen vorhanden{% else %}{% endif %} + {% if not repos[plugin].clean %}Änderungen vorhanden{% else %} + + + {% endif %} {% endfor %}