Skip to content

Commit

Permalink
githubplugin: enable updating forks/branches/PRs from github
Browse files Browse the repository at this point in the history
  • Loading branch information
Morg42 committed Nov 7, 2024
1 parent 6cde447 commit 41f8373
Show file tree
Hide file tree
Showing 3 changed files with 218 additions and 81 deletions.
14 changes: 10 additions & 4 deletions githubplugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ def get_pulls(self, fetch=False) -> bool:
# succeed if cached pulls present and fetch not requested
if not fetch:
if self.pulls != {}:
self.logger.debug(f'using cached pulls: {self.pulls.keys()}')
return True

self.logger.debug('fetching pulls from github')
self.pulls = {}
for pull in self.git_repo.get_pulls():
self.pulls[pull.number] = {
Expand Down Expand Up @@ -198,9 +200,11 @@ def get_branches_from(self, fork=None, owner='', fetch=False) -> dict:
except KeyError:
pass
else:
self.logger.debug(f'returning cached {b_list}')
return b_list

# fetch from github
self.logger.debug('refreshing branches from github')
branches = fork.get_branches()
b_list = {}
for branch in branches:
Expand Down Expand Up @@ -231,9 +235,11 @@ def get_plugins_from(self, fork=None, owner='', branch='', fetch=False) -> list:
except KeyError:
pass
else:
self.logger.debug(f'returning cached plugins {plugins}')
return plugins

# plugins not yet cached, fetch from github
self.logger.debug('fetching plugins from github')
contents = fork.get_contents("", ref=branch)
plugins = [item.path for item in contents if item.type == 'dir' and not item.path.startswith('.')]

Expand Down Expand Up @@ -634,17 +640,17 @@ def setup_github(self) -> bool:

return self.gh.set_repo()

def fetch_github_forks(self) -> bool:
def fetch_github_forks(self, fetch=False) -> bool:
""" fetch forks from github API """
if self.gh:
return self.gh.get_forks()
return self.gh.get_forks(fetch=fetch)
else:
return False

def fetch_github_pulls(self) -> bool:
def fetch_github_pulls(self, fetch=False) -> bool:
""" fetch PRs from github API """
if self.gh:
return self.gh.get_pulls()
return self.gh.get_pulls(fetch=fetch)
else:
return False

Expand Down
79 changes: 66 additions & 13 deletions githubplugin/webif/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,15 @@
#
#########################################################################

import os
import json

from lib.item import Items
from lib.model.smartplugin import SmartPluginWebIf

import cherrypy

# ------------------------------------------
# Webinterface of the plugin
# ------------------------------------------

import cherrypy
import csv

class WebInterface(SmartPluginWebIf):

Expand Down Expand Up @@ -83,13 +80,17 @@ def index(self, action=None):
# collect only PRs which (branches) are not already installed as a worktree
pulls = {}
for pr in self.plugin.gh.pulls:
skip = False
if self.plugin.gh.pulls[pr]['owner'] in reposbyowner:
if self.plugin.gh.pulls[pr]['branch'] not in reposbyowner[self.plugin.gh.pulls[pr]['owner']]:
pulls[pr] = {
'title': self.plugin.gh.pulls[pr]['title'],
'owner': self.plugin.gh.pulls[pr]['owner'],
'branch': self.plugin.gh.pulls[pr]['branch']
}
if self.plugin.gh.pulls[pr]['branch'] in reposbyowner[self.plugin.gh.pulls[pr]['owner']]:
skip = True

if not skip:
pulls[pr] = {
'title': self.plugin.gh.pulls[pr]['title'],
'owner': self.plugin.gh.pulls[pr]['owner'],
'branch': self.plugin.gh.pulls[pr]['branch']
}

return tmpl.render(p=self.plugin,
webif_pagelength=pagelength,
Expand All @@ -100,14 +101,48 @@ def index(self, action=None):
pulls=pulls,
language=self.plugin.get_sh().get_defaultlanguage())

@cherrypy.expose
@cherrypy.tools.json_out()
def updateForks(self):
if self.plugin.fetch_github_forks(fetch=True):
return {"operation": "request", "result": "success", "data": sorted(self.plugin.gh.forks.keys())}

@cherrypy.expose
@cherrypy.tools.json_in()
@cherrypy.tools.json_out()
def getPull(self):
json = cherrypy.request.json
try:
pr = int(json.get("pull", 0))
except Exception:
self.logger.error(f'invalid value for pr in {json}')
return
if pr > 0:
pull = self.plugin.get_github_pulls(number=pr)
b = pull.get('branch')
o = pull.get('owner')
if b and o:
return {"operation": "request", "result": "success", "owner": o, "branch": b}
else:
self.logger.error(f'invalid data on processing PR {pr}')

@cherrypy.expose
@cherrypy.tools.json_out()
def updatePulls(self):
if self.plugin.fetch_github_pulls(fetch=True):
prn = list(self.plugin.get_github_pulls().keys())
prt = [v['title'] for pr, v in self.plugin.get_github_pulls().items()]
return {"operation": "request", "result": "success", "prn": prn, "prt": prt}

@cherrypy.expose
@cherrypy.tools.json_out()
@cherrypy.tools.json_in()
def updateBranches(self):
json = cherrypy.request.json
owner = json.get("owner")
force = json.get("force", False)
if owner is not None and owner != '':
branches = self.plugin.fetch_github_branches_from(owner=owner)
branches = self.plugin.fetch_github_branches_from(owner=owner, fetch=force)
if branches != {}:
return {"operation": "request", "result": "success", "data": list(branches.keys())}

Expand All @@ -116,10 +151,11 @@ def updateBranches(self):
@cherrypy.tools.json_in()
def updatePlugins(self):
json = cherrypy.request.json
force = json.get("force", False)
owner = json.get("owner")
branch = json.get("branch")
if owner is not None and owner != '' and branch is not None and branch != '':
plugins = self.plugin.fetch_github_plugins_from(owner=owner, branch=branch)
plugins = self.plugin.fetch_github_plugins_from(owner=owner, branch=branch, fetch=force)
if plugins != {}:
return {"operation": "request", "result": "success", "data": plugins}

Expand All @@ -137,6 +173,23 @@ def removePlugin(self):
if self.plugin.remove_plugin(name):
return {"operation": "request", "result": "success"}

@cherrypy.expose
@cherrypy.tools.json_out()
@cherrypy.tools.json_in()
def removeInitPlugin(self):
json = cherrypy.request.json
name = json.get("name")
if name is None or name == '' or name not in self.plugin.init_repos:
msg = f'Plugin {name} nicht vorhanden.'
self.logger.error(msg)
return {"operation": "request", "result": "error", "data": msg}

try:
del self.plugin.init_repos[name]
except Exception:
pass
return {"operation": "request", "result": "success"}

@cherrypy.expose
@cherrypy.tools.json_out()
@cherrypy.tools.json_in()
Expand Down
Loading

0 comments on commit 41f8373

Please sign in to comment.