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 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 gitea_connect(): user = get_user(user_id=session.get('user_id')) gitea_token = request.form.get('gitea_token') error = None if user['gitea_token'] is not None: error = gettext(u'Your account is already linked with Gitea.') elif not gitea_token: error = gettext(u'Token is required.') if error is None: # test if API works try: GiteaAPI(gitea_token).user_get_current() except (requests.exceptions.RequestException, GiteaAPIError) as e: error = gettext(u'Error account not linked.') current_app.logger.error(f"{e}") else: update_token(session.get('user_id'), gitea_token) flash(gettext(u'Your account is now linked with Gitea.'), 'success') if error: flash(error, 'fail') return redirect(url_for('settings.index'))
def wrapped_view(**kwargs): user = get_user(user_id=session.get('user_id')) if user['gitea_token'] is None: flash(gettext(u'Your account is not linked with a gitea account.'), 'fail') return redirect(url_for('settings.index')) try: GiteaAPI(user['gitea_token']).repo_get_commits({'sha': 'master'}).json() except (JSONDecodeError, GiteaAPIError): flash(gettext(u'Error can\'t get configuration files from Gitea.'), 'fail') return redirect(url_for('settings.index')) return view(**kwargs)
def index(): user = get_user(user_id=session.get('user_id')) gitea_user = None error = False if user['gitea_token']: try: gitea_user = GiteaAPI(user['gitea_token']).user_get_current().json() except (requests.exceptions.RequestException, GiteaAPIError, JSONDecodeError) as e: error = True flash(gettext(u'Error something went wrong.'), 'fail') current_app.logger.error(f"{e}") return render_template('settings/index.html', gitea_user=gitea_user, gitea=get_gitea(), api_key=user['api_key'], error=error, isLogged=True)
def wrapped_view(**kwargs): api_key = request.headers.get("X-Api-Key") if api_key is None: user = get_user(user_id=session.get('user_id')) else: user = get_user(api_key=api_key) if user is None or user['gitea_token'] is None: return {"message": gettext(u'ERROR: Gitea Token not configured')}, 503 try: GiteaAPI(user['gitea_token']).repo_get_commits({'sha': 'master'}).json() except (JSONDecodeError, GiteaAPIError): return {"message": gettext(u'ERROR: Can\'t connect to Gitea server')}, 503 return view(**kwargs)
def index(): ref = request.args.get('ref', 'master') path = request.args.get('path', '') user = get_user(user_id=session.get('user_id')) gitea = get_gitea() repo_contents = None commits = None error = False folder = False try: commits = GiteaAPI(user['gitea_token']).repo_get_commits().json() if not path: req = GiteaAPI(user['gitea_token']).repo_get_contents_list( {'ref': ref}) else: req = GiteaAPI(user['gitea_token']).repo_get_contents( path, {'ref': ref}) except (requests.exceptions.RequestException, GiteaAPIError) as e: flash(gettext(u'Error cannot get configuration files from Gitea.'), 'fail') error = True current_app.logger.error(f"{e}") return redirect(url_for( 'docs.index')) if ref == 'master' and path == '' else redirect( url_for('configuration_files.index')) else: repo_contents = req.json() folder = isinstance(repo_contents, list) if not folder: try: repo_contents['content'] = b64decode( repo_contents['content']).decode('utf-8') except UnicodeDecodeError: flash(gettext(u'Cannot read this type of file.'), 'fail') return redirect(url_for('configuration_files.index')) return render_template('configuration_files/index.html', folder=folder, gitea=gitea, repo_contents=repo_contents, commits=commits, ref=ref, error=error, isLogged=True)