| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "net/url" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/equipo/github" |
| 9 | "amadan.net/oficina/equipo/internal/equipo/identity" |
| 10 | "github.com/go-chi/chi/v5" |
| 11 | ) |
| 12 | |
| 13 | func (a *application) githubRoutes(r chi.Router) { |
| 14 | r.Get("/conversation/settings/github", a.githubSettings) |
| 15 | r.Post("/github/connect/start", a.githubStart) |
| 16 | r.Post("/github/connect/check", a.githubAction("check")) |
| 17 | r.Post("/github/connect/confirm", a.githubAction("confirm")) |
| 18 | r.Post("/github/connect/disconnect", a.githubAction("disconnect")) |
| 19 | } |
| 20 | func (a *application) githubPerson(w http.ResponseWriter, r *http.Request) (github.Principal, bool) { |
| 21 | w.Header().Set("Cache-Control", "no-store") |
| 22 | w.Header().Set("Referrer-Policy", "no-referrer") |
| 23 | if a.store.forge == nil || !a.store.forge.Enabled { |
| 24 | a.failure(w, r, ErrNotFound) |
| 25 | return github.Principal{}, false |
| 26 | } |
| 27 | _, person, ok := sessionContext(r) |
| 28 | cookie, err := r.Cookie(a.auth.SessionCookie()) |
| 29 | if !ok || err != nil || cookie.Value == "" { |
| 30 | a.failure(w, r, ErrForbidden) |
| 31 | return github.Principal{}, false |
| 32 | } |
| 33 | p := github.Principal{Address: person, Session: cookie.Value} |
| 34 | // The lifecycle repeats this on the primary inside each write transaction. |
| 35 | if _, err = a.store.forge.List(r.Context(), p); err != nil { |
| 36 | a.failure(w, r, ErrForbidden) |
| 37 | return github.Principal{}, false |
| 38 | } |
| 39 | return p, true |
| 40 | } |
| 41 | func (a *application) githubRender(w http.ResponseWriter, r *http.Request, view githubPageView) { |
| 42 | data, err := a.settingsPage(r, "github", false) |
| 43 | if err != nil { |
| 44 | a.failure(w, r, err) |
| 45 | return |
| 46 | } |
| 47 | data.Title = "github.title" |
| 48 | data.GitHub = view |
| 49 | render(w, "github", data) |
| 50 | } |
| 51 | func (a *application) githubSettings(w http.ResponseWriter, r *http.Request) { |
| 52 | p, ok := a.githubPerson(w, r) |
| 53 | if !ok { |
| 54 | return |
| 55 | } |
| 56 | if r.URL.Query().Get("install") == "1" { |
| 57 | target, err := a.store.forge.InstallationURL(r.Context(), p, r.URL.Query().Get("id")) |
| 58 | if err == nil { |
| 59 | http.Redirect(w, r, target, http.StatusSeeOther) |
| 60 | return |
| 61 | } |
| 62 | } |
| 63 | rows, err := a.store.forge.List(r.Context(), p) |
| 64 | if err != nil { |
| 65 | a.failure(w, r, ErrUnavailable) |
| 66 | return |
| 67 | } |
| 68 | view := githubPageView{} |
| 69 | if key, ok := githubNotice(r.URL.Query().Get("failed")); ok { |
| 70 | view.Notice = copyText(key) |
| 71 | } |
| 72 | for _, v := range rows { |
| 73 | row, err := githubRow(r, v) |
| 74 | if err != nil { |
| 75 | a.failure(w, r, ErrUnavailable) |
| 76 | return |
| 77 | } |
| 78 | view.Rows = append(view.Rows, row) |
| 79 | } |
| 80 | a.githubRender(w, r, view) |
| 81 | } |
| 82 | func (a *application) githubStart(w http.ResponseWriter, r *http.Request) { |
| 83 | p, ok := a.githubPerson(w, r) |
| 84 | if !ok { |
| 85 | return |
| 86 | } |
| 87 | r.Body = http.MaxBytesReader(w, r.Body, 4096) |
| 88 | if r.ParseForm() != nil { |
| 89 | a.failure(w, r, ErrInvalid) |
| 90 | return |
| 91 | } |
| 92 | ctx, _, _ := sessionContext(r) |
| 93 | started, err := a.store.forge.Start(ctx, p, r.PostForm.Get("account"), r.PostForm.Get("account_type")) |
| 94 | if err != nil { |
| 95 | a.failure(w, r, ErrInvalid) |
| 96 | return |
| 97 | } |
| 98 | view := githubPageView{} |
| 99 | if started.Superseded != nil { |
| 100 | old := *started.Superseded |
| 101 | old.State = github.StateSuperseded |
| 102 | row, err := githubRow(r, old) |
| 103 | if err != nil { |
| 104 | a.failure(w, r, ErrUnavailable) |
| 105 | return |
| 106 | } |
| 107 | view.Rows = append(view.Rows, row) |
| 108 | } |
| 109 | if started.State == "" { |
| 110 | row, err := githubRow(r, started.View) |
| 111 | if err != nil { |
| 112 | a.failure(w, r, ErrUnavailable) |
| 113 | return |
| 114 | } |
| 115 | view.Rows = append(view.Rows, row) |
| 116 | a.githubRender(w, r, view) |
| 117 | return |
| 118 | } |
| 119 | manifest := map[string]any{ |
| 120 | "name": started.Name, "url": a.origin, "public": false, |
| 121 | "redirect_url": a.origin + "/github/connect/manifest", |
| 122 | "setup_url": a.origin + "/github/connect/installation", |
| 123 | "hook_attributes": map[string]any{"url": a.origin + "/github/events", "active": false}, |
| 124 | "default_events": []string{}, "request_oauth_on_install": false, |
| 125 | "default_permissions": map[string]string{"contents": "write", "pull_requests": "write", "checks": "read", "statuses": "read", "actions": "read", "metadata": "read"}, |
| 126 | } |
| 127 | raw, err := json.Marshal(manifest) |
| 128 | if err != nil { |
| 129 | a.failure(w, r, ErrUnavailable) |
| 130 | return |
| 131 | } |
| 132 | view.Manifest = string(raw) |
| 133 | view.ManifestURL = "https://github.com/settings/apps/new" |
| 134 | if started.View.AccountType == "Organization" { |
| 135 | view.ManifestURL = "https://github.com/organizations/" + started.View.Login + "/settings/apps/new" |
| 136 | } |
| 137 | view.ManifestURL += "?state=" + url.QueryEscape(started.State) |
| 138 | githubManifestCSP(w) |
| 139 | a.githubRender(w, r, view) |
| 140 | } |
| 141 | func (a *application) githubCallback(w http.ResponseWriter, r *http.Request, manifest bool) { |
| 142 | w.Header().Set("Cache-Control", "no-store") |
| 143 | w.Header().Set("Referrer-Policy", "no-referrer") |
| 144 | id, ok := a.sessionFrom(r) |
| 145 | cookie, cookieErr := r.Cookie(a.auth.SessionCookie()) |
| 146 | if !ok || cookieErr != nil || !a.store.forge.Enabled { |
| 147 | http.Redirect(w, r, "/conversation/settings/github?failed=1", http.StatusSeeOther) |
| 148 | return |
| 149 | } |
| 150 | r = identity.With(r, id) |
| 151 | ctx, _, _ := sessionContext(r) |
| 152 | p := github.Principal{Address: normalizeAddress(id.Address), Session: cookie.Value} |
| 153 | var view github.View |
| 154 | var err error |
| 155 | if manifest { |
| 156 | view, err = a.store.forge.Convert(ctx, p, r.URL.Query().Get("state"), r.URL.Query().Get("code")) |
| 157 | } else { |
| 158 | view, err = a.store.forge.Install(ctx, p, r.URL.Query().Get("state")) |
| 159 | } |
| 160 | // No code, state, upstream body or raw error survives into the clean URL. |
| 161 | target := "/conversation/settings/github" + githubFailure(err) |
| 162 | if manifest && err == nil { |
| 163 | target += "?id=" + url.QueryEscape(view.ID) + "&install=1" |
| 164 | } |
| 165 | http.Redirect(w, r, target, http.StatusSeeOther) |
| 166 | } |
| 167 | func (a *application) githubManifest(w http.ResponseWriter, r *http.Request) { |
| 168 | a.githubCallback(w, r, true) |
| 169 | } |
| 170 | func (a *application) githubInstallation(w http.ResponseWriter, r *http.Request) { |
| 171 | a.githubCallback(w, r, false) |
| 172 | } |
| 173 | func (a *application) githubAction(action string) http.HandlerFunc { |
| 174 | return func(w http.ResponseWriter, r *http.Request) { |
| 175 | p, ok := a.githubPerson(w, r) |
| 176 | if !ok { |
| 177 | return |
| 178 | } |
| 179 | r.Body = http.MaxBytesReader(w, r.Body, 4096) |
| 180 | if r.ParseForm() != nil { |
| 181 | a.failure(w, r, ErrInvalid) |
| 182 | return |
| 183 | } |
| 184 | id := r.PostForm.Get("id") |
| 185 | ctx, _, _ := sessionContext(r) |
| 186 | var err error |
| 187 | switch action { |
| 188 | case "check": |
| 189 | _, err = a.store.forge.Check(ctx, p, id) |
| 190 | case "confirm": |
| 191 | _, err = a.store.forge.Connect(ctx, p, id) |
| 192 | case "disconnect": |
| 193 | _, err = a.store.forge.Disconnect(ctx, p, id) |
| 194 | } |
| 195 | http.Redirect(w, r, "/conversation/settings/github"+githubFailure(err), http.StatusSeeOther) |
| 196 | } |
| 197 | } |
| 198 | |