Skip to content

Commit

Permalink
githubplugin: implemented plugin selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Morg42 committed Nov 6, 2024
1 parent e92d3cf commit bfe4d10
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 60 deletions.
77 changes: 47 additions & 30 deletions githubplugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,35 +134,18 @@ def set_repo(self) -> bool:
self.git_repo = self.get_repo('smarthomeNG', self.repo)
return True

def get_pull(self, number):
def get_pulls(self, fetch=False) -> bool:
if not self._github:
self.logger.error('error: Github object not initialized')
return False

if not self.git_repo:
self.set_repo()

try:
pull = self.git_repo.get_pull(number=number)
except Exception:
return

return {
'title': pull.title,
'pull': pull,
'git_repo': pull.head.repo,
'owner': pull.head.repo.owner.login,
'repo': pull.head.repo.name,
'branch': pull.head.ref
}

def get_pulls(self) -> bool:
if not self._github:
self.logger.error('error: Github object not initialized')
return False

if not self.git_repo:
self.set_repo()
# succeed if cached pulls present and fetch not requested
if not fetch:
if self.pulls != {}:
return True

self.pulls = {}
for pull in self.git_repo.get_pulls():
Expand All @@ -177,21 +160,26 @@ def get_pulls(self) -> bool:

return True

def get_forks(self) -> bool:
def get_forks(self, fetch=False) -> bool:
if not self._github:
self.logger.error('error: Github object not initialized')
return False

if not self.git_repo:
self.set_repo()

# succeed if cached forks present and fetch not requested
if not fetch:
if self.forks != {}:
return True

self.forks = {}
for fork in self.git_repo.get_forks():
self.forks[fork.full_name.split('/')[0]] = {'repo': fork}

return True

def get_branches_from(self, fork=None, owner='') -> dict:
def get_branches_from(self, fork=None, owner='', fetch=False) -> dict:

if fork is None and owner:
try:
Expand All @@ -201,6 +189,17 @@ def get_branches_from(self, fork=None, owner='') -> dict:
if not fork:
return {}

# if not specifically told to fetch from github -
if not fetch:
# try to get cached branches
try:
b_list = self.forks[fork.owner.login]['branches']
except KeyError:
pass
else:
return b_list

# fetch from github
branches = fork.get_branches()
b_list = {}
for branch in branches:
Expand All @@ -209,7 +208,7 @@ def get_branches_from(self, fork=None, owner='') -> dict:
self.forks[fork.owner.login]['branches'] = b_list
return b_list

def get_plugins_from(self, fork=None, owner='', branch='') -> list:
def get_plugins_from(self, fork=None, owner='', branch='', fetch=False) -> list:

if not branch:
return []
Expand All @@ -223,9 +222,26 @@ def get_plugins_from(self, fork=None, owner='', branch='') -> list:
if not fork:
return []

# if not specifically told to fetch from github, -
if not fetch:
# try to get cached plugin list
try:
plugins = self.forks[fork.owner.login]['branches'][branch]['plugins']
except KeyError:
pass
else:
return plugins

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

try:
# add plugins to branch entry, if present
b = self.forks[fork.owner.login]['branches'][branch]
b['plugins'] = plugins
except KeyError:
pass
return sorted(plugins)

def get_all_branches(self) -> bool:
Expand Down Expand Up @@ -551,17 +567,18 @@ def fetch_github_pulls(self) -> bool:
""" fetch PRs from github API """
return self.gh.get_pulls()

def fetch_github_branches_from(self, fork=None, owner='') -> dict:
def fetch_github_branches_from(self, fork=None, owner='', fetch=False) -> dict:
"""
fetch branches for given fork from github API
if fork is given as fork object, use this
if owner is given and present in self.forks, use their fork object
"""
self.logger.error(f'fetch github branches for {owner} or {fork}')
res = self.gh.get_branches_from(fork=fork, owner=owner)
self.logger.error(f'got {res}')
return res
return self.gh.get_branches_from(fork=fork, owner=owner, fetch=fetch)

def fetch_github_plugins_from(self, fork=None, owner='', branch='', fetch=False) -> list:
""" fetch plugin names for selected fork/branch """
return self.gh.get_plugins_from(fork=fork, owner=owner, branch=branch, fetch=fetch)

def get_github_forks(self, owner=None) -> dict:
""" return forks or single fork for given owner """
Expand Down
26 changes: 15 additions & 11 deletions githubplugin/webif/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,25 @@ def index(self, reload=None, action=None, prnum=None, owner=None, branch=None, p
@cherrypy.tools.json_out()
@cherrypy.tools.json_in()
def updateBranches(self):
# cl = cherrypy.request.headers['Content-Length']
# if not cl:
# return
# try:
# rawbody = cherrypy.request.body.read(int(cl))
# data = json.loads(rawbody)
# except Exception:
# return
json = cherrypy.request.json
fork = json.get("fork")
if fork is not None and fork != '':
branches = self.plugin.fetch_github_branches_from(owner=fork)
owner = json.get("owner")
if owner is not None and owner != '':
branches = self.plugin.fetch_github_branches_from(owner=owner)
if branches != {}:
return {"operation": "request", "result": "success", "data": list(branches.keys())}

@cherrypy.expose
@cherrypy.tools.json_out()
@cherrypy.tools.json_in()
def updatePlugins(self):
json = cherrypy.request.json
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)
if plugins != {}:
return {"operation": "request", "result": "success", "data": plugins}


# @cherrypy.expose
# @cherrypy.tools.json_out()
Expand Down
92 changes: 73 additions & 19 deletions githubplugin/webif/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,41 @@
}
}

function addOption(sel, val) {
function addOption(sel, val, def) {
var option = document.createElement('option');
option.text = val;
if (def) {
option.selected = true;
}
sel.add(option);
}

function setPR(selObj) {
var PR = document.getElementById('pr').value;
if (PR > 0) {
document.getElementById('fork').value = pulls[PR]['owner'];
document.getElementById('owner').value = pulls[PR]['owner'];
var branch = document.getElementById('branch');
clearSelect(branch);
addOption(branch, pulls[PR]['branch']);
branch.value = pulls[PR]['branch'];
document.getElementById('branch').disabled = false;
document.getElementById('btn-branch').disabled = false;
document.getElementById('btn-plugin').disabled = true;
}
}

function updateBranches(selObj) {
var fork = document.getElementById('fork').value;
var owner = document.getElementById('owner').value;

if (fork != '') {
// alert(fork);
if (owner != '') {
$.ajax({
type: "POST",
url: "updateBranches",
data: JSON.stringify({'fork': fork}),
data: JSON.stringify({'owner': owner}),
contentType: 'application/json',
dataType: 'json',
error: function(response) {
alert("Fehler beim Übermitteln der Daten. Bitte shng-Log prüfen!");
// document.getElementById('orphanModal').style.display = 'none';
},
success: function(response) {
var item = document.getElementById('branch');
Expand All @@ -73,13 +76,57 @@
if (branch == 'master' || branch == 'main') {
continue;
}
addOption(item, branch);
addOption(item, branch, false);
}
}
})
}
}

function selectedBranch(selObj) {
if (document.getElementById('branch').value != '') {
document.getElementById('btn-branch').disabled = false;
} else {
document.getElementById('btn-branch').disabled = true;
document.getElementById('btn-plugin').disabled = true;
}
}

function updatePlugins(selObj) {
var owner = document.getElementById('owner').value;
var branch = document.getElementById('branch').value;

if (owner != '' && branch != '') {
$.ajax({
type: "POST",
url: "updatePlugins",
data: JSON.stringify({'owner': owner, 'branch': branch}),
contentType: 'application/json',
dataType: 'json',
error: function(response) {
alert("Fehler beim Übermitteln der Daten. Bitte shng-Log prüfen!");
},
success: function(response) {
var item = document.getElementById('plugin');

// enable branch options
item.disabled = false;

// clear options
clearSelect(item);

// add all branches except master and main
for (const plugin of response['data']) {
addOption(item, plugin, plugin==branch);
}

document.getElementById('btn-plugin').disabled = false;
}
})
}
}


</script>

{%- endblock pluginscripts %}
Expand Down Expand Up @@ -153,31 +200,38 @@
<td>&nbsp;</td>
</tr>
<tr>
<td>Fork:</td>
<td>Repo von:</td>
<td>
<select id='fork' onchange="javascript:updateBranches(this);">
<option value=''>(Fork auswählen)</option>
{% for fork in forklist %}
<option>{{ fork }}</option>
<select id='owner' onchange="javascript:updateBranches(this);">
<option value=''>(Repo auswählen)</option>
{% for owner in forklist %}
<option>{{ owner }}</option>
{% endfor %}
</select>
</td>
<td style='padding-left: 15px;'>Branch:</td>
<td>
<select id='branch' disabled>
<select id='branch' disabled onchange="javascript:selectedBranch(this)">
<option value=''>(Branch auswählen)</option>
</select>
</td>
<td style='padding-left: 15px;'>
<button type="button" class="btn btn-shng btn-sm" onclick="javascript:fetchFork(this);">Auswählen</button>
<button id='btn-branch' disabled type="button" class="btn btn-shng btn-sm" onclick="javascript:updatePlugins(this);">Auswählen</button>
</td>
</tr>
<tr>
<td>Plugin:</td>
<td colspan=3>
<select id='plugin' disabled>
<option value=''>(Plugin auswählen)</option>
</select>
</td>
<td style='padding-left: 15px;'>
<button id='btn-plugin' disabled type="button" class="btn btn-shng btn-sm" onclick="javascript:selectPlugin(this);">Auswählen</button>
</td>
</tr>
</table>
</div>

<select id='plugin' disabled>
<option value=''>(select plugin)</option>
</select>
</form>

{% endblock bodytab1 %}
Expand Down

0 comments on commit bfe4d10

Please sign in to comment.