| 1 | package github |
| 2 | |
| 3 | import "time" |
| 4 | |
| 5 | // Connection is a team forge connection, never a person's model connection. |
| 6 | // Only View is exposed to a browser or a device. |
| 7 | type Connection struct { |
| 8 | ID string `gorm:"primaryKey"` |
| 9 | Generation int64 |
| 10 | State State |
| 11 | Login string |
| 12 | AccountType string |
| 13 | AccountID int64 |
| 14 | AppID int64 |
| 15 | Slug string |
| 16 | AppName string |
| 17 | InstallationID int64 |
| 18 | Selection string |
| 19 | Confirmed bool |
| 20 | ActiveAccount *int64 `gorm:"uniqueIndex:forge_active_account"` |
| 21 | SealedKey string `json:"-"` |
| 22 | Owner string `json:"-"` |
| 23 | Session string `json:"-"` |
| 24 | Origin string |
| 25 | ManifestHash string `json:"-"` |
| 26 | InstallationHash string `json:"-"` |
| 27 | StartedAt time.Time |
| 28 | RedirectUntil time.Time |
| 29 | PendingUntil time.Time |
| 30 | UninstalledAt time.Time |
| 31 | Count int |
| 32 | CountKnown bool |
| 33 | Claim string `json:"-"` |
| 34 | ClaimUntil time.Time |
| 35 | UpdatedAt time.Time |
| 36 | } |
| 37 | |
| 38 | func (Connection) TableName() string { return "forge_connections" } |
| 39 | |
| 40 | // Pending has one row per team instance. Start replaces its connection atomically. |
| 41 | type Pending struct { |
| 42 | ID int64 `gorm:"primaryKey;autoIncrement:false"` |
| 43 | ConnectionID string |
| 44 | } |
| 45 | |
| 46 | func (Pending) TableName() string { return "forge_pending" } |
| 47 | |
| 48 | var Models = []any{&Connection{}, &Pending{}} |
| 49 | |
| 50 | type Principal struct{ Address, Session string } |
| 51 | type View struct { |
| 52 | State State |
| 53 | ID, Login, AccountType, Slug, SettingsURL string |
| 54 | Generation, AppID, AccountID, InstallationID int64 |
| 55 | Count int |
| 56 | CountKnown, Confirmed bool |
| 57 | } |
| 58 | type Started struct { |
| 59 | View View |
| 60 | State, Name string |
| 61 | Superseded *View |
| 62 | } |
| 63 | |
| 64 | // State is a connection's lifecycle state. Every value is listed in States and |
| 65 | // has an explicit rendering; there is no default. |
| 66 | type State string |
| 67 | |
| 68 | const ( |
| 69 | StateManifest State = "manifest" |
| 70 | StateConverting State = "converting" |
| 71 | StatePending State = "pending" |
| 72 | StateReady State = "ready" |
| 73 | StateLive State = "live" |
| 74 | StateAlreadyConnected State = "already_connected" |
| 75 | StateManifestExpired State = "manifest_expired" |
| 76 | StateFailed State = "failed" |
| 77 | StateInstallationExpired State = "installation_expired" |
| 78 | StateExpired State = "expired" |
| 79 | StateSuperseded State = "superseded" |
| 80 | StateAbandoned State = "abandoned" |
| 81 | StateDisconnected State = "disconnected" |
| 82 | StateUninstalled State = "uninstalled" |
| 83 | StateUninstalledExpired State = "uninstalled_expired" |
| 84 | StateReconnect State = "reconnect" |
| 85 | StateCheckFailed State = "check_failed" |
| 86 | StateSuspended State = "suspended" |
| 87 | StateTooMany State = "too_many" |
| 88 | ) |
| 89 | |
| 90 | // Dead states hold no key and no authority. Only the latest per account is |
| 91 | // listed, and only when GitHub has created its App. |
| 92 | func (s State) Dead() bool { |
| 93 | switch s { |
| 94 | case StateSuperseded, StateAbandoned, StateFailed, StateManifestExpired, StateExpired, StateDisconnected, StateUninstalledExpired, StateReconnect: |
| 95 | return true |
| 96 | } |
| 97 | return false |
| 98 | } |
| 99 | |
| 100 | // calledBack reports that GitHub redirected with a manifest code for this |
| 101 | // attempt, so its App exists on GitHub even if conversion never finished. |
| 102 | func calledBack(c Connection) bool { return c.State != StateManifest && c.State != StateAbandoned } |
| 103 | |
| 104 | // States lists every lifecycle state, for exhaustive rendering tests. |
| 105 | var States = []State{StateManifest, StateConverting, StatePending, StateReady, StateLive, StateAlreadyConnected, StateManifestExpired, StateFailed, StateInstallationExpired, StateExpired, StateSuperseded, StateAbandoned, StateDisconnected, StateUninstalled, StateUninstalledExpired, StateReconnect, StateCheckFailed, StateSuspended, StateTooMany} |
| 106 | |