321 lines
16 KiB
Go
321 lines
16 KiB
Go
// Code generated by templ - DO NOT EDIT.
|
|
|
|
// templ: version: v0.3.819
|
|
package component
|
|
|
|
//lint:file-ignore SA4006 This context is only used if a nested component is present.
|
|
|
|
import "github.com/a-h/templ"
|
|
import templruntime "github.com/a-h/templ/runtime"
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"forge.cadoles.com/wpetit/clearcase/internal/core/model"
|
|
"forge.cadoles.com/wpetit/clearcase/internal/http/form"
|
|
common "forge.cadoles.com/wpetit/clearcase/internal/http/handler/webui/common/component"
|
|
"github.com/pkg/errors"
|
|
"github.com/yuin/goldmark"
|
|
"log/slog"
|
|
"strings"
|
|
)
|
|
|
|
type PullRequestPageVModel struct {
|
|
PullRequestURL string
|
|
SummaryForm *form.Form
|
|
PullRequestForm *form.Form
|
|
PullRequestTips string
|
|
Projects []*model.Project
|
|
PullRequests []*model.PullRequest
|
|
SelectedProjectID string
|
|
SelectedPullRequestID string
|
|
}
|
|
|
|
const summaryPlaceholder = `
|
|
Décrivez rapidement les modifications apportées par la PR, ClearCase utilisera le modèle de PR présent dans le dépôt (ou un modèle par défaut) afin de générer une version mise en forme et complétée.
|
|
|
|
Afin de fournir plus d'information de contexte au LLM, vous pouvez faire référence à d'autres tickets du dépôt via un ou plusieurs '#<pr_id>' et/ou des chemins vers des fichiers présents dans celui ci.
|
|
`
|
|
|
|
const bodyPlaceholder = `
|
|
Une fois votre PR générée, vous pourrez l'éditer puis la créer directement en cliquant sur le bouton 'Mettre à jour' ci-dessous.
|
|
`
|
|
|
|
const prTemplatePlaceholder = `
|
|
Vous pouvez surcharger le modèle de PR fourni par le projet en remplissant ce champ.
|
|
`
|
|
|
|
func NewPullRequestSummaryForm() *form.Form {
|
|
return form.New(
|
|
form.NewField(
|
|
"project",
|
|
form.Attrs{},
|
|
form.NonEmpty("Ce champ ne doit pas être vide."),
|
|
),
|
|
form.NewField(
|
|
"pullrequest",
|
|
form.Attrs{},
|
|
form.NonEmpty("Ce champ ne doit pas être vide."),
|
|
),
|
|
form.NewField(
|
|
"summary",
|
|
form.Attrs{
|
|
"type": "textarea",
|
|
"rows": "20",
|
|
"placeholder": strings.TrimSpace(summaryPlaceholder),
|
|
},
|
|
form.NonEmpty("Ce champ ne doit pas être vide."),
|
|
),
|
|
form.NewField(
|
|
"template",
|
|
form.Attrs{
|
|
"type": "textarea",
|
|
"rows": "20",
|
|
"placeholder": strings.TrimSpace(prTemplatePlaceholder),
|
|
},
|
|
),
|
|
)
|
|
}
|
|
|
|
func NewPullRequestForm() *form.Form {
|
|
return form.New(
|
|
form.NewField(
|
|
"title",
|
|
form.Attrs{
|
|
"type": "text",
|
|
"placeholder": "Écrivez le résumé de votre demande et cliquez sur 'Générer' pour remplir automatiquement ces champs.",
|
|
},
|
|
form.NonEmpty("Ce champ ne doit pas être vide."),
|
|
),
|
|
form.NewField(
|
|
"body",
|
|
form.Attrs{
|
|
"type": "textarea",
|
|
"rows": "20",
|
|
"placeholder": strings.TrimSpace(bodyPlaceholder),
|
|
},
|
|
form.NonEmpty("Ce champ ne doit pas être vide."),
|
|
),
|
|
)
|
|
}
|
|
|
|
func PullRequestPage(vmodel PullRequestPageVModel) templ.Component {
|
|
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
|
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
|
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
|
|
return templ_7745c5c3_CtxErr
|
|
}
|
|
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
|
if !templ_7745c5c3_IsBuffer {
|
|
defer func() {
|
|
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
|
if templ_7745c5c3_Err == nil {
|
|
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
|
}
|
|
}()
|
|
}
|
|
ctx = templ.InitializeContext(ctx)
|
|
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
|
|
if templ_7745c5c3_Var1 == nil {
|
|
templ_7745c5c3_Var1 = templ.NopComponent
|
|
}
|
|
ctx = templ.ClearChildren(ctx)
|
|
templ_7745c5c3_Var2 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
|
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
|
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
|
if !templ_7745c5c3_IsBuffer {
|
|
defer func() {
|
|
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
|
if templ_7745c5c3_Err == nil {
|
|
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
|
}
|
|
}()
|
|
}
|
|
ctx = templ.InitializeContext(ctx)
|
|
if vmodel.PullRequestURL != "" {
|
|
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<article class=\"message is-primary\"><div class=\"message-header\"><p>Pull Request modifiée !</p><button class=\"delete\" aria-label=\"delete\" hx-on:click=\"onCloseMessage(this)\"></button></div><div class=\"message-body\">Votre PR a été mise à jour et est disponible à l'adresse suivante: <a href=\"")
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
var templ_7745c5c3_Var3 templ.SafeURL = templ.SafeURL(vmodel.PullRequestURL)
|
|
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(string(templ_7745c5c3_Var3)))
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "\" target=\"_blank\"><code>")
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
var templ_7745c5c3_Var4 string
|
|
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(vmodel.PullRequestURL)
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/http/handler/webui/pullrequest/component/pullrequest_page.templ`, Line: 106, Col: 99}
|
|
}
|
|
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "</code></a>.</div></article><script type=\"text/javascript\">\n\t\t\t\t\t\tfunction clearSummary(projectId) {\n\t\t\t\t\t\t\tsessionStorage.removeItem(`pr-summary-${projectId}`)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tfunction openPR(prUrl) {\n\t\t\t\t\t\t\twindow.open(prUrl, \"_blank\");\n\t\t\t\t\t\t}\n\t\t\t\t\t</script> ")
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
templ_7745c5c3_Err = templ.JSFuncCall("clearSummary", vmodel.SelectedProjectID).Render(ctx, templ_7745c5c3_Buffer)
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, " ")
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
templ_7745c5c3_Err = templ.JSFuncCall("openPR", vmodel.PullRequestURL).Render(ctx, templ_7745c5c3_Buffer)
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
}
|
|
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, " <div class=\"columns\"><div class=\"column is-4\"><form id=\"summary-form\" action=\"")
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
var templ_7745c5c3_Var5 templ.SafeURL = common.CurrentURL(ctx)
|
|
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(string(templ_7745c5c3_Var5)))
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "\" method=\"put\" hx-disabled-elt=\"textarea, input, select, button\" hx-on:htmx:before-send=\"savePreferred()\" hx-indicator=\"#generation-progress\"><h2 class=\"title is-size-3\">Résumé de la PR</h2>")
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
templ_7745c5c3_Err = common.FormSelect(
|
|
vmodel.SummaryForm, "pr-project", "project", "Projet",
|
|
common.WithOptions(projectsToOptions(vmodel.Projects)...),
|
|
common.WithAttrs(
|
|
"hx-get", string(common.CurrentURL(ctx, common.WithoutValues("project", "*"))),
|
|
"hx-target", "body",
|
|
"hx-push-url", "true",
|
|
),
|
|
).Render(ctx, templ_7745c5c3_Buffer)
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
templ_7745c5c3_Err = common.FormSelect(
|
|
vmodel.SummaryForm, "pr-pullrequest", "pullrequest", "PR",
|
|
common.WithOptions(pullRequestsToOptions(vmodel.PullRequests)...),
|
|
common.WithAttrs(
|
|
"hx-get", string(common.CurrentURL(ctx, common.WithoutValues("pullrequest", "*"))),
|
|
"hx-target", "body",
|
|
"hx-push-url", "true",
|
|
),
|
|
).Render(ctx, templ_7745c5c3_Buffer)
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
templ_7745c5c3_Err = common.FormTextarea(
|
|
vmodel.SummaryForm, "pr-summary", "summary", "Résumé",
|
|
common.WithTextareaAttrs(
|
|
"hx-on:change", "onSummaryChange(event)",
|
|
),
|
|
).Render(ctx, templ_7745c5c3_Buffer)
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "<details class=\"my-3\"><summary class=\"is-clickable\">Paramètres avancés</summary>")
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
templ_7745c5c3_Err = common.FormTextarea(
|
|
vmodel.SummaryForm, "pr-template", "template", "Surcharger le modèle de demande",
|
|
common.WithTextareaAttrs(
|
|
"hx-on:change", "onPullRequestTemplateChange(event)",
|
|
),
|
|
).Render(ctx, templ_7745c5c3_Buffer)
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, "</details><div class=\"buttons is-right\"><button type=\"submit\" class=\"button is-info is-large\"><span class=\"icon\"><i class=\"fa fa-robot\"></i></span> <span>Générer</span></button></div></form></div><div class=\"column\"><h2 class=\"title is-size-3\">Votre PR</h2><form action=\"")
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
var templ_7745c5c3_Var6 templ.SafeURL = common.CurrentURL(ctx)
|
|
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(string(templ_7745c5c3_Var6)))
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "\" method=\"post\" hx-disabled-elt=\"textarea, input, select, button\" hx-indicator=\"#generation-progress\">")
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
templ_7745c5c3_Err = common.FormField(vmodel.PullRequestForm, "pr-title", "title", "Titre").Render(ctx, templ_7745c5c3_Buffer)
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
templ_7745c5c3_Err = common.FormTextarea(vmodel.PullRequestForm, "pr-body", "body", "Corps").Render(ctx, templ_7745c5c3_Buffer)
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, "<div class=\"buttons is-right\"><button type=\"submit\" class=\"button is-info is-large\"><span class=\"icon\"><i class=\"fa fa-rocket\"></i></span> <span>Mettre à jour</span></button></div></form></div></div><progress id=\"generation-progress\" class=\"htmx-indicator progress\"></progress> ")
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
if vmodel.PullRequestTips != "" {
|
|
html := markdownToHTML(ctx, vmodel.PullRequestTips)
|
|
if html != "" {
|
|
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 11, "<article class=\"message is-info mt-5\"><div class=\"message-header\"><p><span class=\"icon\"><i class=\"fa fa-lightbulb\"></i></span>Questionnements</p><button class=\"delete\" aria-label=\"delete\" hx-on:click=\"onCloseMessage(this)\"></button></div><div class=\"message-body\"><div class=\"content\"><p>Utilisez ces quelques questions pour réfléchir aux éléments d'informations nécessaire à la bonne rédaction de votre PR:</p>")
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
templ_7745c5c3_Err = templ.Raw(html).Render(ctx, templ_7745c5c3_Buffer)
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 12, "</div></div></article>")
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
}
|
|
}
|
|
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 13, " <script type=\"text/javascript\">\n\t\tfunction onCloseMessage(closeElement) {\n\t\t\tcloseElement.closest('.message').style.display = 'none';\n\t\t}\n\n\t\tfunction onSummaryChange(evt) {\n\t\t\tconst summary = evt.currentTarget.value;\n\t\t\tconst projectId = document.getElementById(\"pr-project\").value;\n\t\t\tsessionStorage.setItem(`pr-summary-${projectId}`, summary);\n\t\t}\n\n\t\tfunction onPullRequestTemplateChange(evt) {\n\t\t\tconst prTemplate = evt.currentTarget.value;\n\t\t\tconst pullRequestId = document.getElementById(\"pr-pullrequest\").value;\n\t\t\tlocalStorage.setItem(`pr-template-${pullRequestId}`, prTemplate);\n\t\t}\n\n\t\tfunction savePreferred() {\n\t\t\tsavePreferredProject()\n\t\t\tsavePreferredPullRequest()\n\t\t}\n\n\t\tfunction savePreferredProject() {\n\t\t\tconst projectId = document.getElementById(\"pr-project\").value;\n\t\t\tlocalStorage.setItem(`preferred-project`, projectId);\n\t\t}\n\n\t\tfunction restorePreferredProject() {\n\t\t\tconst preferredProject = localStorage.getItem(`preferred-project`);\n\t\t\tif (!preferredProject) return;\n\t\t\tconst projectElement = document.getElementById(\"pr-project\");\n\t\t\tif (!projectElement) return;\n\t\t\tif (preferredProject === projectElement.value) return;\n\t\t\tprojectElement.value = preferredProject;\n\t\t\tprojectElement.dispatchEvent(new Event('change'));\n\t\t}\n\n\t\tfunction savePreferredPullRequest() {\n\t\t\tconst pullRequestId = document.getElementById(\"pr-pullrequest\").value;\n\t\t\tlocalStorage.setItem(`preferred-pullrequest`, pullRequestId);\n\t\t}\n\n\t\tfunction restorePreferredPullRequest() {\n\t\t\tconst preferredPullRequest = localStorage.getItem(`preferred-pullrequest`);\n\t\t\tif (!preferredPullRequest) return;\n\t\t\tconst pullRequestElement = document.getElementById(\"pr-pullrequest\");\n\t\t\tif (!pullRequestElement) return;\n\t\t\tif (preferredPullRequest === pullRequestElement.value) return;\n\t\t\tpullRequestElement.value = preferredPullRequest;\n\t\t\tpullRequestElement.dispatchEvent(new Event('change'));\n\t\t}\n\n\t\tfunction restoreLastSummary() {\n\t\t\tconst summaryTextarea = document.getElementById(\"pr-summary\");\n\t\t\tif (!summaryTextarea) return;\n const summary = summaryTextarea.value;\n\t\t\tif (summary !== \"\") return;\n\t\t\tconst projectId = document.getElementById(\"pr-project\").value;\n\t\t\tif (!projectId) return;\n\t\t\tconst savedSummary = sessionStorage.getItem(`pr-summary-${projectId}`);\n\t\t\tif (!savedSummary) return;\n\t\t\tsummaryTextarea.value = savedSummary;\n\t\t}\n\n\t\tfunction restorePullRequestTemplate() {\n\t\t\tconst prTemplateTextarea = document.getElementById(\"pr-template\");\n\t\t\tif (!prTemplateTextarea) return;\n const prTemplate = prTemplateTextarea.value;\n\t\t\tif (prTemplate !== \"\") return;\n\t\t\tconst projectId = document.getElementById(\"pr-project\").value;\n\t\t\tif (!projectId) return;\n\t\t\tconst savedprTemplate = localStorage.getItem(`pr-template-${projectId}`);\n\t\t\tif (!savedprTemplate) return;\n\t\t\tprTemplateTextarea.value = savedprTemplate;\n\t\t}\n\n\t\thtmx.onLoad(function(){\n\t\t\trestoreLastSummary();\n\t\t\trestorePreferredProject();\n\t\t\trestorePreferredPullRequest();\n\t\t\trestorePullRequestTemplate();\n })\n\t\t</script>")
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
return nil
|
|
})
|
|
templ_7745c5c3_Err = common.AppPage(common.WithPageOptions(
|
|
common.WithTitle("Éditer une PR"),
|
|
)).Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer)
|
|
if templ_7745c5c3_Err != nil {
|
|
return templ_7745c5c3_Err
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func projectsToOptions(projects []*model.Project) []string {
|
|
options := make([]string, 0, len(projects))
|
|
options = append(options, "", "")
|
|
for _, p := range projects {
|
|
options = append(options, p.Name, p.ID)
|
|
}
|
|
return options
|
|
}
|
|
|
|
func pullRequestsToOptions(pullRequests []*model.PullRequest) []string {
|
|
options := make([]string, 0, len(pullRequests))
|
|
options = append(options, "", "")
|
|
for _, pr := range pullRequests {
|
|
options = append(options, pr.Title, pr.ID)
|
|
}
|
|
return options
|
|
}
|
|
|
|
func markdownToHTML(ctx context.Context, text string) string {
|
|
var buff bytes.Buffer
|
|
if err := goldmark.Convert([]byte(text), &buff); err != nil {
|
|
slog.ErrorContext(ctx, "could not convert markdown to html", slog.Any("error", errors.WithStack(err)))
|
|
return ""
|
|
}
|
|
|
|
return buff.String()
|
|
}
|
|
|
|
var _ = templruntime.GeneratedTemplate
|