Skip to content

Commit 956b966

Browse files
hkaltaiclaude
andcommitted
fix: the sync a cleanup failure names removes the leftover, failed syncs print cleanup warnings, an attach-only failure no longer says it saved anything, and tests ignore an exported GitHub host
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2072595 commit 956b966

3 files changed

Lines changed: 47 additions & 6 deletions

File tree

‎packages/opencode/src/altimate/integrations/core.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,7 @@ export interface UpsertResult {
929929

930930
export const NOT_LINKED_LABEL = "Not linked to a workspace: run altimate link"
931931
export const ATTACH_FAILED_PREFIX = "Saved the connection but could not attach it to the workspace: "
932+
export const ATTACH_EXISTING_FAILED_PREFIX = "Could not attach the connection already in your account to the workspace: "
932933

933934
export const NOT_SIGNED_IN = "Not signed in to Altimate, kept local."
934935

@@ -1114,9 +1115,9 @@ async function syncReal(
11141115
steps.push({ label: l, ok: true })
11151116
onStep?.(l, true)
11161117
}
1117-
const reportWorkspace = (w: NonNullable<UpsertResult["workspace"]>, sharedConnection = false, rewritten = sharedConnection): SyncOutcome => {
1118+
const reportWorkspace = (w: NonNullable<UpsertResult["workspace"]>, sharedConnection = false, rewritten = sharedConnection, uploaded = true): SyncOutcome => {
11181119
// A shared connection that was rewritten stays reported as shared even when the attach after it fails.
1119-
if ("error" in w) return { id, result: "failed", reason: ATTACH_FAILED_PREFIX + w.error + (rewritten ? ` ${SHARED_REWRITE_NOTE}` : ""), steps, ...(sharedConnection ? { shared: true as const } : {}) }
1120+
if ("error" in w) return { id, result: "failed", reason: (uploaded ? ATTACH_FAILED_PREFIX : ATTACH_EXISTING_FAILED_PREFIX) + w.error + (rewritten ? ` ${SHARED_REWRITE_NOTE}` : ""), steps, ...(sharedConnection ? { shared: true as const } : {}) }
11201121
if ("attached" in w) step(`${w.kept ? `Kept the workspace's existing connection in ${workspaceLabel(w.attached, w.workspaceId)}` : `Attached to ${workspaceLabel(w.attached, w.workspaceId)}`}${w.shared ? SHARED_AUDIENCE : ""}`)
11211122
else {
11221123
steps.push({ label: NOT_LINKED_LABEL, ok: false })
@@ -1144,7 +1145,16 @@ async function syncReal(
11441145
if (!found) return { id, result: "skipped", reason: known == null ? collected.reason : REMOVED_NOTE, steps: [] }
11451146
if (found.is_valid === false) return { id, result: "skipped", reason: INVALID_IN_WORKSPACE, steps: [] }
11461147
step("Connection already in the workspace")
1147-
return reportWorkspace(await attachToBoundWorkspace(cfg, id, found.id, visible.map((c) => c.id), share), found.privacy !== "private", false)
1148+
// A synced record keeps no secret here; one left by an earlier cleanup that failed is removed now, which is what that failure told the user to run sync for.
1149+
if (known != null) {
1150+
try {
1151+
await purgeCredentialSecrets(id, store.providers[id], false)
1152+
await deleteSecret(id)
1153+
} catch (e) {
1154+
steps.push({ label: `${LOCAL_CLEANUP_FAILED_PREFIX}${e instanceof Error ? e.message : String(e)}. Run altimate integrations sync ${id} to remove it.`, ok: false })
1155+
}
1156+
}
1157+
return reportWorkspace(await attachToBoundWorkspace(cfg, id, found.id, visible.map((c) => c.id), share), found.privacy !== "private", false, false)
11481158
})
11491159
} catch (e) {
11501160
return { id, result: "failed", reason: e instanceof Error ? e.message : String(e), steps }

‎packages/opencode/src/cli/cmd/integrations.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,17 @@ export const syncedTo = (o: Pick<core.SyncOutcome, "shared">) => `synced to work
103103

104104
function renderSync(o: core.SyncOutcome) {
105105
const name = core.PROVIDER_NAMES[o.id]
106+
const warnings = () => {
107+
for (const st of o.steps) if (!st.ok && st.label.startsWith(core.LOCAL_CLEANUP_FAILED_PREFIX)) out(` ${S.TEXT_WARNING}!${S.TEXT_NORMAL} ${clean(st.label)}`)
108+
}
106109
if (o.result === "kept_local") return out(` ${S.TEXT_WARNING}⊘${S.TEXT_NORMAL} ${name}: kept local (${clean(o.reason)})`)
107-
if (o.result === "failed") return out(` ${S.TEXT_DANGER}✗${S.TEXT_NORMAL} ${name}: sync failed (${clean(o.reason)})`)
110+
if (o.result === "failed") {
111+
out(` ${S.TEXT_DANGER}✗${S.TEXT_NORMAL} ${name}: sync failed (${clean(o.reason)})`)
112+
return warnings()
113+
}
108114
if (o.result === "skipped") return out(` ${S.TEXT_DIM}- ${name}: skipped (${clean(o.reason)})${S.TEXT_NORMAL}`)
109115
out(` ${S.TEXT_SUCCESS}✓${S.TEXT_NORMAL} ${name} ${syncedTo(o)}`)
110-
for (const st of o.steps) if (!st.ok && st.label.startsWith(core.LOCAL_CLEANUP_FAILED_PREFIX)) out(` ${S.TEXT_WARNING}!${S.TEXT_NORMAL} ${clean(st.label)}`)
116+
warnings()
111117
}
112118

113119
async function runSync(ids: ProviderId[], asJson: boolean, skipSynced = false, share = false) {

‎packages/opencode/test/altimate/integrations.test.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ afterAll(() => {
3434
beforeEach(() => {
3535
for (const f of fs.readdirSync(tmp)) fs.rmSync(path.join(tmp, f), { recursive: true, force: true })
3636
delete process.env.ALTIMATE_INTEGRATIONS_DEMO
37+
// A GitHub host exported on this machine would block GitHub tokens in every test.
38+
for (const k of ["GITHUB_HOST", "GH_HOST", "GITHUB_API_URL", "GITHUB_SERVER_URL", "GITHUB_ENTERPRISE_URL"]) delete process.env[k]
3739
})
3840

3941
describe("sameRecord", () => {
@@ -658,7 +660,7 @@ describe("workspace connections", () => {
658660
const update = spyOn(backend, "updateDatamateIntegrations").mockResolvedValue(undefined as any)
659661
try {
660662
const refused = await core.syncProvider("github", undefined, 0, true)
661-
expect([refused.result, refused.reason]).toEqual(["failed", core.ATTACH_FAILED_PREFIX + core.SHARED_WORKSPACE_REFUSAL("WS", "github")])
663+
expect([refused.result, refused.reason]).toEqual(["failed", core.ATTACH_EXISTING_FAILED_PREFIX + core.SHARED_WORKSPACE_REFUSAL("WS", "github")])
662664
expect(update).not.toHaveBeenCalled()
663665
const shared = await core.syncProvider("github", undefined, 0, true, true)
664666
expect([shared.result, shared.shared]).toEqual(["synced", true])
@@ -847,6 +849,23 @@ describe("workspace connections", () => {
847849
spy.mockRestore()
848850
}
849851
expect(core.isSynced("github")).toBe(true)
852+
// The sync that the warning names has nothing left to upload, and removes the leftover while it attaches.
853+
expect(await core.readSecret("github")).toBe("leftover_token")
854+
delete process.env.GITHUB_TOKEN
855+
// No gh either, so the retry has nothing to upload and takes the attach-only path.
856+
const pathBefore = process.env.PATH
857+
process.env.PATH = fs.mkdtempSync(path.join(tmp, "nobin-"))
858+
const update = spyOn(backend, "updateConnection")
859+
let retry: core.SyncOutcome
860+
try {
861+
retry = await core.syncProvider("github")
862+
expect(update).not.toHaveBeenCalled()
863+
} finally {
864+
process.env.PATH = pathBefore
865+
update.mockRestore()
866+
}
867+
expect([retry.result, retry.steps.some((st) => st.label.startsWith(core.LOCAL_CLEANUP_FAILED_PREFIX))]).toEqual(["synced", false])
868+
expect(await core.readSecret("github")).toBeUndefined()
850869
})
851870

852871
test("a record that cannot be written after the workspace accepted the upload is reported, and a retry updates the same connection", async () => {
@@ -2868,6 +2887,12 @@ describe("the CLI passes --share through and reports shared", () => {
28682887
await (SyncCommand.handler as any)({ provider: "github", share: true, json: true })
28692888
expect(sync.mock.calls[0][4]).toBe(true)
28702889
expect(JSON.parse(logs[logs.length - 1]).results[0].shared).toBe(true)
2890+
// Text output shows a local cleanup warning on a failed sync too, as --json carries it.
2891+
const warning = `${core.LOCAL_CLEANUP_FAILED_PREFIX}disk full. Run altimate integrations sync github to remove it.`
2892+
sync.mockResolvedValue({ id: "github", result: "failed", reason: "boom", steps: [{ label: warning, ok: false }] })
2893+
logs.length = 0
2894+
await (SyncCommand.handler as any)({ provider: "github", json: false })
2895+
expect(logs.some((l) => clean(l).includes(warning))).toBe(true)
28712896
} finally {
28722897
for (const sp of [log, connect, sync, info]) sp.mockRestore()
28732898
process.exitCode = 0

0 commit comments

Comments
 (0)