def _unauthorized(req): if req.status_code == 401: raise GiteaAPIError('Unauthorized') elif req.status_code == 404: raise GiteaAPIError('Not found') elif req.status_code == 500: raise GiteaAPIError('Gitea server error')
def remove(): path = request.args.get('path') user = get_user(user_id=session.get('user_id')) error = False if request.method == 'POST': try: req = GiteaAPI(user['gitea_token']).repo_get_contents( path, {'ref': 'master'}) if isinstance(req.json(), list): raise GiteaAPIError('Cannot delete a folder, must be a file.') GiteaAPI(user['gitea_token']).repo_delete_contents( path, data=json_dumps({'sha': req.json()['sha']})) except (requests.exceptions.RequestException, GiteaAPIError) as e: flash(gettext(u'Error file does not exist.'), 'fail') error = True current_app.logger.error(f"{e}") else: flash(gettext(u'File deleted.'), 'success') return redirect(url_for('configuration_files.index')) else: try: req = GiteaAPI(user['gitea_token']).repo_get_contents( path, {'ref': 'master'}) if isinstance(req.json(), list): raise GiteaAPIError('Cannot delete a folder, must be a file') except (requests.exceptions.RequestException, GiteaAPIError) as e: flash(gettext(u'Error file does not exist.'), 'fail') current_app.logger.error(f"{e}") return redirect(url_for('configuration_files.index')) return render_template('configuration_files/remove.html', path=path, error=error, isLogged=True)
def save(): path = request.form.get('path', '') data = request.form.get('data') user = get_user(user_id=session.get('user_id')) if not data: flash(gettext(u'No data to update.'), 'fail') return redirect(url_for('configuration_files.index')) if path: try: req = GiteaAPI(user['gitea_token']).repo_get_contents(path) if isinstance(req.json(), list): raise GiteaAPIError('Cannot save a folder, must be a file.') GiteaAPI(user['gitea_token']).repo_put_contents( path, data=json_dumps({ 'content': str(b64encode(data.encode('utf-8')), 'utf-8'), 'sha': req.json()['sha'] })) flash(gettext(u'File contents updated.'), 'success') except (requests.exceptions.RequestException, GiteaAPIError) as e: flash(gettext(u'Error file does not exist.'), 'fail') current_app.logger.error(f"{e}") return redirect(url_for('configuration_files.index')) else: path = '' flash(gettext(u'Error file path is undefined.'), 'fail') return redirect(url_for('configuration_files.index', path=path))
def confirm_save(): path = request.form.get('path') new_data = request.form.get('data') old_data = None diffs = None user = get_user(user_id=session.get('user_id')) try: req = GiteaAPI(user['gitea_token']).repo_get_contents( path, {'ref': 'master'}) if isinstance(req.json(), list): raise GiteaAPIError('Cannot save a folder, must be a file.') except (requests.exceptions.RequestException, GiteaAPIError) as e: flash(gettext(u'Error file does not exist.'), 'fail') current_app.logger.error(f"{e}") return redirect(url_for('configuration_files.index')) else: old_data = b64decode(req.json()['content']).decode('utf-8') diffs = list( unified_diff(old_data.splitlines(keepends=True), new_data.splitlines(keepends=True))) if not diffs: flash(gettext(u'No data to update.'), 'fail') return redirect(url_for('configuration_files.index', path=path)) return render_template('configuration_files/confirm_save.html', path=path, new_data=new_data, old_data=old_data, diffs=diffs, isLogged=True)
def __get(self, path, params=None, headers=None): try: req = requests.get(self.__url(path), params=params, headers=self.__headers(headers)) self.__class__._unauthorized(req) return req except requests.exceptions.RequestException as e: raise GiteaAPIError(e)
def __delete(self, path, data=None, headers=None): try: req = requests.delete(self.__url(path), headers=self.__headers(headers), data=data) self.__class__._unauthorized(req) return req except requests.exceptions.RequestException as e: raise GiteaAPIError(e)