diff --git a/desktop/internal/i18n/shared.generated.go b/desktop/internal/i18n/shared.generated.go index 841faad5f7..6d4306cd78 100644 --- a/desktop/internal/i18n/shared.generated.go +++ b/desktop/internal/i18n/shared.generated.go @@ -702,7 +702,6 @@ var sharedCatalogs = map[string]map[string]string{ "desktop.github.noChecks": "No checks", "desktop.github.noPullRequests": "No pull requests match this filter.", "desktop.github.noReviewThreads": "No review threads", - "desktop.github.notConfigured": "GitHub sign-in is not configured for this build.", "desktop.github.openFailed": "Could not open the browser: {reason}", "desktop.github.openInBrowser": "Open in Browser", "desktop.github.prBody": "Description (Markdown)", @@ -3432,7 +3431,6 @@ var sharedCatalogs = map[string]map[string]string{ "desktop.github.noChecks": "没有检查", "desktop.github.noPullRequests": "没有符合此筛选的 Pull Request。", "desktop.github.noReviewThreads": "没有评审线程", - "desktop.github.notConfigured": "此构建未配置 GitHub 登录。", "desktop.github.openFailed": "无法打开浏览器:{reason}", "desktop.github.openInBrowser": "在浏览器中打开", "desktop.github.prBody": "描述(Markdown)", diff --git a/desktop/internal/settingswindow/github_account_view.go b/desktop/internal/settingswindow/github_account_view.go index ea950a8e7e..053947fbe1 100644 --- a/desktop/internal/settingswindow/github_account_view.go +++ b/desktop/internal/settingswindow/github_account_view.go @@ -17,9 +17,10 @@ type GitHubAccount struct { SignOut func() } -// GitHubState is what the card shows: whether sign-in is configured, whether an account is +// GitHubState is what the card shows: whether this build can reach GitHub, whether an account is // connected and as whom, and the device authorization in progress with its user code. type GitHubState struct { + // Available is whether the kernel plans GitHub requests; without it the card offers no sign-in. Available bool Connected bool Login string @@ -59,7 +60,7 @@ func (w *window) gitHubCard(c ui.C, language i18n.Language, account *GitHubAccou ui.Text(c, i18n.Shared(language, "desktop.github.working")) default: if !state.Available { - ui.Text(c, i18n.Shared(language, "desktop.github.notConfigured")).Color(w.pal.muted) + ui.Text(c, i18n.Shared(language, "desktop.github.unavailable")).Color(w.pal.muted) } else if ui.Button(c, i18n.Shared(language, "desktop.github.signIn")).Outline().Clicked() && account.SignIn != nil { account.SignIn() } diff --git a/desktop/internal/settingswindow/version_control_view.go b/desktop/internal/settingswindow/version_control_view.go index 23ab3bd1f3..f9a35e7d98 100644 --- a/desktop/internal/settingswindow/version_control_view.go +++ b/desktop/internal/settingswindow/version_control_view.go @@ -5,14 +5,11 @@ import ( "github.com/vivaui/viva/ui" ) -// versionControlPage is the Version Control page: the Git switches of the editor, the Commit -// window and the Git console. Opening the page writes nothing; a switch hands its change to +// versionControlPage is the Version Control page: the GitHub account, then the Git switches of +// the editor, the Commit window and the Git console. Opening the page writes nothing; a switch hands its change to // the settings owner at once. func (w *window) versionControlPage(c ui.C) { language := w.marketLanguage() - if w.ctrl.opts.GitHub != nil { - w.gitHubCard(c, language, w.ctrl.opts.GitHub) - } gutterTitle := i18n.Shared(language, "desktop.git.settings.gutter") blameTitle := i18n.Shared(language, "desktop.git.settings.blame") gutter := !w.sess.values.HideGitGutter @@ -22,6 +19,11 @@ func (w *window) versionControlPage(c ui.C) { changeListsTitle := i18n.Shared(language, "desktop.git.settings.changeLists") parentsTitle := i18n.Shared(language, "desktop.git.settings.parentReferences") w.pageScroll(c, pageVersionControl, func() { + // The account card is the page's first card, inside the scroll so it lines up and scrolls + // with the others. + if w.ctrl.opts.GitHub != nil { + w.gitHubCard(c, language, w.ctrl.opts.GitHub) + } w.card(c, gutterTitle, i18n.Shared(language, "desktop.git.settings.gutterHelp"), func() { if ui.Switch(c.Part("gutter"), &gutter, gutterTitle).Changed() { w.setHideGitGutter(!gutter, false) diff --git a/desktop/internal/settingswindow/visual_capture_test.go b/desktop/internal/settingswindow/visual_capture_test.go index f0bfc0bffb..00b0be27d9 100644 --- a/desktop/internal/settingswindow/visual_capture_test.go +++ b/desktop/internal/settingswindow/visual_capture_test.go @@ -75,6 +75,8 @@ func TestVisualSettings(t *testing.T) { Marketplace: func(context.Context) (marketplace.Snapshot, error) { return marketplace.Snapshot{}, nil }, UpdateStatus: func() UpdateStatus { return UpdateStatus{Configured: true} }, Toolchains: &scopedToolchainLoader{fakeToolchainLoader{snapshot: globalVisualToolchains()}}, + // Every build can sign in to GitHub; no account is connected yet. + GitHub: &GitHubAccount{Snapshot: func() GitHubState { return GitHubState{Available: true} }}, } w := newWindow(NewController(opts), Values{Theme: theme, Language: language.Code(), TerminalScrollback: 2000}) w.sess.page = scenario.page diff --git a/desktop/internal/workbench/github_account.go b/desktop/internal/workbench/github_account.go index 810cc1429c..554e5f7e70 100644 --- a/desktop/internal/workbench/github_account.go +++ b/desktop/internal/workbench/github_account.go @@ -20,8 +20,8 @@ import ( // github-device-authorization): it shows the user code, opens GitHub's verification page, polls // until the user answers, stores the token in the platform's credential store and validates it // with currentUser. The token never leaves this file's state and the store; the settings page -// shows the login and the phase only. The client ID is the product's public OAuth client, read -// from LITHE_GITHUB_CLIENT_ID; without one sign-in is unavailable. +// shows the login and the phase only. The client ID is the product's public OAuth client, +// built in; LITHE_GITHUB_CLIENT_ID replaces it for a fork's or a development OAuth app. const ( // githubCredentialPlugin and githubCredentialKey name the stored token in the credential store. @@ -29,6 +29,10 @@ const ( githubCredentialKey = "access-token" // githubAccountTimeout bounds one validation or one start request. githubAccountTimeout = 30 * time.Second + // githubProductClientID is Lithe's OAuth app on GitHub, the one the macOS client shipped in its + // Info.plist. The device flow is a public-client flow with no client secret, so the ID is not + // a credential and every build carries it. + githubProductClientID = "Ov23li60nlOOHwDY3MO6" ) // githubPhase is where the sign-in stands. @@ -62,9 +66,13 @@ type githubAccount struct { // errGitHubUnavailable reports that the kernel cannot plan GitHub requests. var errGitHubUnavailable = errors.New("GitHub is unavailable") -// githubClientID is the OAuth client of the product, or empty when none is configured. +// githubClientID is the OAuth client sign-in uses: LITHE_GITHUB_CLIENT_ID when it is set, else the +// product's. func githubClientID() string { - return strings.TrimSpace(os.Getenv("LITHE_GITHUB_CLIENT_ID")) + if id := strings.TrimSpace(os.Getenv("LITHE_GITHUB_CLIENT_ID")); id != "" { + return id + } + return githubProductClientID } // githubAPI is the client that plans and sends GitHub requests, made once with the kernel. @@ -124,10 +132,7 @@ func (a *App) startGitHubSignIn() { clientID := githubClientID() g := &a.github g.mu.Lock() - if client == nil || clientID == "" || g.phase != githubIdle { - if clientID == "" { - g.err = i18n.Shared(a.language(), "desktop.github.notConfigured") - } + if client == nil || g.phase != githubIdle { g.mu.Unlock() return } @@ -252,13 +257,15 @@ type githubSnapshot struct { err string } -// githubAccountSnapshot copies the account for a paint. +// githubAccountSnapshot copies the account for a paint. Sign-in is available when the kernel plans +// GitHub requests; githubAPI takes the account's lock itself, so it is asked first. func (a *App) githubAccountSnapshot() githubSnapshot { + available := a.githubAPI() != nil g := &a.github g.mu.Lock() defer g.mu.Unlock() return githubSnapshot{ - available: githubClientID() != "", + available: available, connected: g.connected, login: g.login, phase: g.phase, diff --git a/desktop/internal/workbench/github_account_test.go b/desktop/internal/workbench/github_account_test.go new file mode 100644 index 0000000000..01e6fc3d46 --- /dev/null +++ b/desktop/internal/workbench/github_account_test.go @@ -0,0 +1,29 @@ +package workbench + +import ( + "testing" + + "github.com/1lck/Lithe-IDEA/desktop/internal/core" +) + +// A packaged build is launched without LITHE_GITHUB_CLIENT_ID, so sign-in must not depend on it: +// the Viva client used to read the OAuth client only from that variable and showed every user +// "GitHub sign-in is not configured for this build" on the Version Control page. +func TestGitHubSignInIsAvailableWithoutClientIDVariable(t *testing.T) { + t.Setenv("LITHE_GITHUB_CLIENT_ID", "") + if got := githubClientID(); got == "" { + t.Fatal("githubClientID() is empty without LITHE_GITHUB_CLIENT_ID; the product's OAuth client must be built in") + } + h := newHarness(t, &testKernel{Client: core.NewClient()}) + if snapshot := h.app.githubAccountSnapshot(); !snapshot.available { + t.Fatalf("the Version Control page reports GitHub sign-in unavailable with a kernel that plans GitHub requests: %+v", snapshot) + } +} + +// LITHE_GITHUB_CLIENT_ID still replaces the built-in client, for a fork's or a development OAuth app. +func TestGitHubClientIDVariableReplacesBuiltInClient(t *testing.T) { + t.Setenv("LITHE_GITHUB_CLIENT_ID", " dev-client ") + if got := githubClientID(); got != "dev-client" { + t.Fatalf("githubClientID() = %q, want the trimmed variable %q", got, "dev-client") + } +} diff --git a/shared/i18n/locales/en-US.json b/shared/i18n/locales/en-US.json index bfc010b1cc..6b9af70732 100644 --- a/shared/i18n/locales/en-US.json +++ b/shared/i18n/locales/en-US.json @@ -10636,7 +10636,6 @@ "desktop.github.enterCode": "Enter this code on GitHub:", "desktop.github.cancelSignIn": "Cancel", "desktop.github.working": "Connecting to GitHub…", - "desktop.github.notConfigured": "GitHub sign-in is not configured for this build.", "desktop.github.expired": "The code expired before it was entered on GitHub. Try again.", "desktop.github.denied": "The authorization was denied on GitHub.", "desktop.github.failed": "GitHub sign-in failed: {reason}", diff --git a/shared/i18n/locales/zh-CN.json b/shared/i18n/locales/zh-CN.json index f3106796ef..d1e0b196b3 100644 --- a/shared/i18n/locales/zh-CN.json +++ b/shared/i18n/locales/zh-CN.json @@ -10636,7 +10636,6 @@ "desktop.github.enterCode": "在 GitHub 上输入此代码:", "desktop.github.cancelSignIn": "取消", "desktop.github.working": "正在连接 GitHub…", - "desktop.github.notConfigured": "此构建未配置 GitHub 登录。", "desktop.github.expired": "代码在 GitHub 上输入前已过期,请重试。", "desktop.github.denied": "GitHub 上的授权被拒绝。", "desktop.github.failed": "GitHub 登录失败:{reason}", diff --git a/shared/i18n/messages.json b/shared/i18n/messages.json index 33455be2d6..c4b4fe7f0e 100644 --- a/shared/i18n/messages.json +++ b/shared/i18n/messages.json @@ -11007,13 +11007,6 @@ "desktop" ] }, - { - "id": "desktop.github.notConfigured", - "description": "Shown when this build has no GitHub OAuth client", - "platforms": [ - "desktop" - ] - }, { "id": "desktop.github.openFailed", "description": "Notice when the pull request page could not be opened in the browser", diff --git a/shared/platform-feature-matrix/features/github-device-authorization.json b/shared/platform-feature-matrix/features/github-device-authorization.json index d6b8ed3765..ba9c809e42 100644 --- a/shared/platform-feature-matrix/features/github-device-authorization.json +++ b/shared/platform-feature-matrix/features/github-device-authorization.json @@ -9,6 +9,7 @@ "evidence": [ "docs/viva-migration.md", "desktop/internal/workbench/github_account.go", + "desktop/internal/workbench/github_account_test.go", "desktop/internal/githubapi/device.go", "desktop/internal/core/github.go", "desktop/internal/settingswindow/github_account_view.go" @@ -20,6 +21,7 @@ "evidence": [ "docs/viva-migration.md", "desktop/internal/workbench/github_account.go", + "desktop/internal/workbench/github_account_test.go", "desktop/internal/githubapi/device.go", "desktop/internal/core/github.go", "desktop/internal/settingswindow/github_account_view.go" @@ -27,7 +29,7 @@ }, "owner": "GitHub", "verification": "使用可控时钟验证待授权、退避、过期和成功授权;实际 GitHub OAuth 与钥匙串流程仍需在 macOS 上验收。", - "notes": "Viva:GitHub 设备码登录:显示用户码并打开验证页,按服务器给的间隔轮询,处理 slow_down 退避与过期(githubapi/device.go),令牌存平台凭据库并用 currentUser 校验,设置页只显示登录名和阶段。OAuth client ID 从 LITHE_GITHUB_CLIENT_ID 读取,未配置时登录不可用。没有专门的自动化测试。", + "notes": "Viva:GitHub 设备码登录:显示用户码并打开验证页,按服务器给的间隔轮询,处理 slow_down 退避与过期(githubapi/device.go),令牌存平台凭据库并用 currentUser 校验,设置页只显示登录名和阶段。OAuth client ID 内置为产品的 OAuth App(与旧 macOS 客户端 Info.plist 相同),LITHE_GITHUB_CLIENT_ID 只用于换成 fork 或开发用的 OAuth App;设置页是否可登录只取决于内核能否规划 GitHub 请求,github_account_test.go 覆盖未设该变量时仍可登录。设备码流程本身没有专门的自动化测试。", "legacy": { "revision": "5a54c41aa37a4e6e6b68918a4b05765b9dd07a36", "macos": {