# creation.py import re import base64 import streamlit as st from .core import charger_fiches_et_labels, construire_corps_ticket_markdown, creer_ticket_gitea, get_labels_existants, nettoyer_labels from config import ENV import requests def formulaire_creation_ticket_dynamique(fiche_selectionnee): with st.expander("Créer un nouveau ticket lié à cette fiche", expanded=False): contenu_modele = charger_modele_ticket() if not contenu_modele: st.error("Impossible de charger le modèle de ticket.") return # Découpe le modèle en sections sections, reponses = {}, {} lignes, titre_courant, contenu = contenu_modele.splitlines(), None, [] for ligne in lignes: if ligne.startswith("## "): if titre_courant: sections[titre_courant] = "\n".join(contenu).strip() titre_courant, contenu = ligne[3:].strip(), [] elif titre_courant: contenu.append(ligne) if titre_courant: sections[titre_courant] = "\n".join(contenu).strip() # Labels prédéfinis selon la fiche labels, selected_ops = [], [] correspondances = charger_fiches_et_labels() cible = correspondances.get(fiche_selectionnee) if cible: if len(cible["operations"]) == 1: labels.append(cible["operations"][0]) elif len(cible["operations"]) > 1: selected_ops = st.multiselect("Labels opération à associer", cible["operations"], default=cible["operations"]) # Génération des champs for section, aide in sections.items(): if "Type de contribution" in section: options = sorted(set(re.findall(r"- \[.\] (.+)", aide))) if "Autre" not in options: options.append("Autre") choix = st.radio("Type de contribution", options) reponses[section] = st.text_input("Précisez", "") if choix == "Autre" else choix elif "Fiche concernée" in section: url_fiche = f"https://fabnum-git.peccini.fr/FabNum/Fiches/src/branch/{ENV}/Documents/{fiche_selectionnee.replace(' ', '%20')}" reponses[section] = url_fiche st.text_input("Fiche concernée", value=url_fiche, disabled=True) elif "Sujet de la proposition" in section: reponses[section] = st.text_input(section, help=aide) else: reponses[section] = st.text_area(section, help=aide) col1, col2 = st.columns(2) if col1.button("Prévisualiser le ticket"): st.session_state.previsualiser = True if col2.button("Annuler"): st.session_state.previsualiser = False st.rerun() if st.session_state.get("previsualiser", False): st.subheader("Prévisualisation du ticket") for section, texte in reponses.items(): st.markdown(f"#### {section}") st.code(texte, language="markdown") titre_ticket = reponses.get("Sujet de la proposition", "").strip() or "Ticket FabNum" final_labels = nettoyer_labels(labels + selected_ops + ([cible["item"]] if cible else [])) st.markdown(f"**Résumé :**\n- **Titre** : `{titre_ticket}`\n- **Labels** : `{', '.join(final_labels)}`") if st.button("Confirmer la création du ticket"): labels_existants = get_labels_existants() labels_ids = [labels_existants[l] for l in final_labels if l in labels_existants] if "Backlog" in labels_existants: labels_ids.append(labels_existants["Backlog"]) corps = construire_corps_ticket_markdown(reponses) creer_ticket_gitea(titre_ticket, corps, labels_ids) st.session_state.previsualiser = False st.success("Ticket créé et formulaire vidé.") def charger_modele_ticket(): from config import GITEA_URL, GITEA_TOKEN, ORGANISATION, DEPOT_FICHES headers = {"Authorization": f"token {GITEA_TOKEN}"} url = f"{GITEA_URL}/repos/{ORGANISATION}/{DEPOT_FICHES}/contents/.gitea/ISSUE_TEMPLATE/Contenu.md" try: r = requests.get(url, headers=headers, timeout=10) r.raise_for_status() return base64.b64decode(r.json().get("content", "")).decode("utf-8") except Exception as e: st.error(f"Erreur chargement modèle : {e}") return ""