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 @@ +