oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package equipo
2
3import (
4 "context"
5 "errors"
6 "fmt"
7 "log/slog"
8 "net/http"
9 "strconv"
10 "strings"
11
12 "amadan.net/oficina/equipo/internal/equipo/github"
13 "amadan.net/rastrillo/rastrillo"
14 numbers "amadan.net/rastrillo/rastrillo/number"
15)
16
17type githubPageView struct {
18 Rows []githubRowView
19 Manifest, ManifestURL string
20 Notice string
21}
22type githubRowView struct {
23 github.View
24 Lines, Keys []string
25 Check, Confirm, Disconnect, Cleanup, Reconnect bool
26 RepositoriesURL string
27}
28
29var (
30 errGitHubUnknownState = errors.New("github: unknown connection state")
31 errGitHubHiddenState = errors.New("github: connection state is never rendered")
32)
33
34// githubStateKeys maps each typed state to its exact brief keys. There is no
35// default: an unknown state is an error, never a silent blank row.
36func githubStateKeys(v github.View) ([]string, error) {
37 switch v.State {
38 case github.StateManifest, github.StateConverting:
39 return []string{"github.create_hint"}, nil
40 case github.StatePending:
41 keys := []string{"github.pending", "github.pending_hint"}
42 if v.AccountType == "Organization" {
43 keys = append(keys, "github.pending_approval")
44 }
45 return keys, nil
46 case github.StateReady:
47 return []string{"github.confirm_account", "github.repository_count"}, nil
48 case github.StateLive:
49 return []string{"github.connected", "github.disconnect_hint"}, nil
50 case github.StateAlreadyConnected:
51 return []string{"github.already_connected", "github.connected", "github.disconnect_hint"}, nil
52 case github.StateManifestExpired, github.StateFailed:
53 return []string{"github.setup_failed", "github.app_remains"}, nil
54 case github.StateInstallationExpired:
55 return []string{"github.redirect_expired"}, nil
56 case github.StateExpired:
57 return []string{"github.pending_expired", "github.app_remains"}, nil
58 case github.StateSuperseded:
59 return []string{"github.app_remains"}, nil
60 case github.StateAbandoned:
61 // No App was created; List never returns these rows.
62 return nil, errGitHubHiddenState
63 case github.StateDisconnected, github.StateUninstalledExpired:
64 return []string{"github.not_connected", "github.app_remains"}, nil
65 case github.StateUninstalled:
66 return []string{"github.not_connected"}, nil
67 case github.StateReconnect:
68 return []string{"github.reconnect_hint", "github.app_remains"}, nil
69 case github.StateCheckFailed:
70 return []string{"github.check_failed"}, nil
71 case github.StateSuspended:
72 return []string{"github.suspended"}, nil
73 case github.StateTooMany:
74 return []string{"github.too_many"}, nil
75 }
76 return nil, errGitHubUnknownState
77}
78
79// githubNotice maps the clean redirect's failed parameter to its notice key.
80func githubNotice(failed string) (string, bool) {
81 switch failed {
82 case "1":
83 return "github.setup_failed", true
84 case "check":
85 return "github.check_failed", true
86 }
87 return "", false
88}
89
90// githubFailure is the clean redirect for a lifecycle error that has no row
91// state of its own: retryable work says so, anything else is a setup failure.
92func githubFailure(err error) string {
93 var refused *github.Refusal
94 switch {
95 case err == nil || errors.As(err, &refused):
96 return ""
97 case github.Retryable(err):
98 return "?failed=check"
99 }
100 return "?failed=1"
101}
102
103func githubRow(r *http.Request, v github.View) (githubRowView, error) {
104 row := githubRowView{View: v}
105 keys, err := githubStateKeys(v)
106 if err != nil {
107 return row, err
108 }
109 for _, key := range keys {
110 text := copyText(key)
111 switch key {
112 case "github.confirm_account", "github.already_connected":
113 text = fmt.Sprintf(text, v.Login)
114 case "github.repository_count":
115 if !v.CountKnown {
116 continue
117 }
118 text = fmt.Sprintf(text, v.Count)
119 case "github.too_many":
120 n, _ := numbers.Format(rastrillo.FormatLocale(r), numbers.Decimal(strconv.Itoa(github.MaxRepositories)), numbers.Options{})
121 text = fmt.Sprintf(text, n)
122 case "github.app_remains":
123 row.Cleanup = true
124 }
125 row.Keys = append(row.Keys, key)
126 row.Lines = append(row.Lines, text)
127 }
128 switch v.State {
129 case github.StatePending, github.StateInstallationExpired, github.StateCheckFailed, github.StateUninstalled, github.StateSuspended, github.StateTooMany, github.StateReady:
130 row.Check = true
131 row.Disconnect = true
132 case github.StateLive, github.StateAlreadyConnected:
133 row.Disconnect = true
134 case github.StateExpired, github.StateUninstalledExpired, github.StateDisconnected, github.StateReconnect:
135 row.Reconnect = true
136 }
137 row.Confirm = v.State == github.StateReady && v.CountKnown && v.Count <= github.MaxRepositories
138 if v.State == github.StateTooMany && v.InstallationID > 0 {
139 base := "https://github.com/settings/installations/"
140 if v.AccountType == "Organization" {
141 base = "https://github.com/organizations/" + v.Login + "/settings/installations/"
142 }
143 row.RepositoriesURL = base + strconv.FormatInt(v.InstallationID, 10)
144 }
145 for _, action := range []struct {
146 on bool
147 key string
148 }{{row.Cleanup, "github.app_settings"}, {row.RepositoriesURL != "", "github.choose_repositories"}, {row.Check, "github.check_again"}, {row.Confirm, "github.confirm"}, {row.Disconnect, "github.disconnect"}, {row.Reconnect, "github.reconnect"}} {
149 if action.on {
150 row.Keys = append(row.Keys, action.key)
151 }
152 }
153 return row, nil
154}
155
156// logGitHubSweep records a sweep failure with its type only: no error text,
157// which could carry an upstream value.
158func logGitHubSweep(logger *slog.Logger, err error) {
159 kind := "other"
160 switch {
161 case errors.Is(err, context.DeadlineExceeded), errors.Is(err, context.Canceled):
162 kind = "timeout"
163 case errors.Is(err, github.ErrAccess):
164 kind = "access"
165 case errors.Is(err, github.ErrBusy):
166 kind = "busy"
167 case errors.Is(err, github.ErrUnavailable), errors.Is(err, github.ErrRateLimit):
168 kind = "unavailable"
169 }
170 logger.Error(copyGitHubSweep, "err_type", fmt.Sprintf("%T", err), "kind", kind)
171}
172
173// Only the manifest document may submit outside the instance. Preserve the
174// framework's other directives, including frame-ancestors and base-uri.
175func githubManifestCSP(w http.ResponseWriter) {
176 policy := w.Header().Get("Content-Security-Policy")
177 parts := strings.Split(policy, ";")
178 found := false
179 for i, part := range parts {
180 if strings.HasPrefix(strings.TrimSpace(part), "form-action ") {
181 parts[i] = " form-action 'self' https://github.com"
182 found = true
183 }
184 }
185 if !found {
186 parts = append(parts, "form-action 'self' https://github.com")
187 }
188 w.Header().Set("Content-Security-Policy", strings.Join(parts, ";"))
189}
190