Skip to content

Commit

Permalink
githubplugin: fix error handling, add pull repo feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Morg42 committed Nov 14, 2024
1 parent 77750ab commit db31a91
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 5 deletions.
35 changes: 33 additions & 2 deletions githubplugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
19 changes: 19 additions & 0 deletions githubplugin/webif/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
35 changes: 32 additions & 3 deletions githubplugin/webif/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@
</button>
</div>

<div id="success" class="mb-2 alert alert-info alert-dismissible show" role="alert">
<strong>Ausführung erfolgreich</strong><br/>
<span id="successmsg"></span>
<button type="button" class="close" data-hide="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>

<div id="install_ConfirmModal" class="or-modal">
<div class="or-modal-content">
<span class="or-close" onclick="document.getElementById('install_ConfirmModal').style.display = 'none';">&times;</span>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 != '') {
Expand All @@ -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).')
}
}
);
Expand Down Expand Up @@ -550,8 +575,9 @@

$(document).ready( function () {

// hide alert popup
// hide alert popups
$('#alert').hide();
$('#success').hide();

// enable datatable table
$('#plugintable').DataTable( {
Expand Down Expand Up @@ -591,7 +617,10 @@
<td id="{{ plugin }}_owner">{{ repos[plugin].owner }}</td>
<td id="{{ plugin }}_branch">{{ repos[plugin].branch }}</td>
<td id="{{ plugin }}_wtpath">{{ repos[plugin].full_wt_path }}</td>
<td id="{{ plugin }}_action">{% if not repos[plugin].clean %}Änderungen vorhanden{% else %}<button type="button" class="btn btn-danger btn-sm" onclick="javascript:removePlugin('{{ repos[plugin].owner }}', '{{ repos[plugin].branch }}', '{{ repos[plugin].plugin }}', '{{ plugin }}');"><i class="fas fa-times"></i></button>{% endif %}</td>
<td id="{{ plugin }}_action">{% if not repos[plugin].clean %}Änderungen vorhanden{% else %}
<button type="button" class="btn btn-danger btn-sm" onclick="javascript:removePlugin('{{ repos[plugin].owner }}', '{{ repos[plugin].branch }}', '{{ repos[plugin].plugin }}', '{{ plugin }}');"><i class="fas fa-times"></i></button>
<button type="button" class="btn btn-shng btn-sm" onclick="javascript:pullRepo('{{ plugin }}');"><i class="fas fa-download"></i></button>
{% endif %}</td>
</tr>
{% endfor %}
</tbody>
Expand Down

0 comments on commit db31a91

Please sign in to comment.