Files
orca/src/shared/bitbucket-credentials.ts
T
Neilanddevatnull 63271a5933 feat(bitbucket): connect Bitbucket from Settings and create pull requests (#5832)
* feat(bitbucket): connect Bitbucket from Settings with encrypted credential storage

Bitbucket Cloud was the only review provider with no in-app auth: GitHub and
GitLab delegate to the gh/glab CLIs, but Bitbucket has no comparable
first-party CLI, so the only option was ORCA_BITBUCKET_* env vars plus a
restart (discussion #5364).

Adds a Connect/Edit/Disconnect flow on the Bitbucket integration card,
modeled on Linear and Jira:

- Credentials are verified against /user before they are persisted, so a
  dead token is rejected inline instead of silently stored.
- The secret is encrypted with safeStorage (0600 plaintext fallback when no
  OS keyring); non-secret metadata lives in a separate plaintext file so
  status reads render the connected account without decrypting. Opening
  Settings therefore never triggers a keychain prompt.
- Env vars keep precedence over stored credentials, so existing headless and
  SSH setups are unaffected. Env-managed connections hide Disconnect.
- connect/disconnect reset the preflight cache, so no relaunch is needed.

The Bitbucket card moves to its own file to stay under the tsx max-lines cap.

* feat(bitbucket): support creating pull requests from Orca

Bitbucket was the only configured provider whose Create button reported
"This repository provider does not support creating a pull request from
Orca" — supportsReviewCreation was false and the forge provider had no
createReview, so even a correctly authenticated setup was blocked.

Adds createBitbucketPullRequest against POST /repositories/{ws}/{repo}/
pullrequests, using the same env-first / stored-credential resolution as PR
lookups (extracted into resolve-auth.ts so both share one path).

Bitbucket Cloud has no draft pull requests, so a draft request is rejected
with a clear message rather than silently publishing a live PR.

* fix(bitbucket): hide the draft toggle where drafts do not exist, plus review fixes

Bitbucket Cloud has no draft pull requests, so the composer no longer offers
the toggle for it and forces the flag off at submit — better than failing
after the user has filled the form in.

Review fixes:
- writeFileSync's `mode` only applies when it creates the file, so rewriting
  a credential kept whatever permissions it already had. chmod after every
  write, for the secret and the metadata.
- An explicit ORCA_BITBUCKET_API_BASE_URL now wins over a stored base URL.
  Env precedence is per-setting, not all-or-nothing.
- Enter in the credentials dialog only submits from a text field, so it no
  longer hijacks Cancel and the docs link.
- Replace the chmod-based delete-failure test with a mocked unlinkSync: file
  modes are not portable to Windows and elevated runners unlink anyway.

* fix(bitbucket): stop a merged pull request from blocking the branch's next one

Reported on #5832: with a merged PR on a branch, Create reported "Pull
request already exists" and offered no way forward.

The branch lookup queries every PR state and returns the most recently
updated one, so a merged PR came back as the branch's current review and
eligibility blocked on it. Bitbucket only discarded such a match on the repo
default branch (#9171), while GitHub already drops any merged PR it matched
by branch alone — "a merged PR without an explicit link is just a historical
branch match, not implicit review context".

Applies that rule to Bitbucket. An explicitly linked review still resolves
through the linked-number fallback, so merging a PR Orca knows about keeps
showing it.

* fix(bitbucket): add bitbucket to the shared review-creation provider list

Reported on #5832: on a Bitbucket repo with no existing PR, Create still
said "This repository provider does not support creating a pull request
from Orca", even after the forge provider gained createReview.

There are two capability lists. Enabling supportsReviewCreation on the forge
provider was necessary but not sufficient — the blocker and the whole
renderer read the separate shared list, which never included bitbucket.

Adds it, gives Bitbucket its own provider name so review copy stops saying
"GitHub", and asserts the two lists agree so they cannot drift apart again.

* fix(bitbucket): persist pull request links after creation

* fix(bitbucket): fetch linked pull requests by number first

* fix(i18n): use generated Bitbucket integration keys

* test(bitbucket): cover forge creation delegation

* fix(bitbucket): fall back when linked pull request is stale

* docs(bitbucket): explain notFoundIsNull and fix a garbled permissions comment

notFoundIsNull arrived without the rationale its sibling flag carries, and
reads as a bare `true` at the only call site that opts in.

* fix(bitbucket): address review findings before merge

Two of these made the feature unusable in real setups:

- Create PR checked GitHub authentication for Bitbucket. isProviderAuthenticated
  fell through to isGitHubAuthenticated, which was unreachable while Bitbucket
  could not create reviews at all. Anyone with Bitbucket connected but no
  `gh auth login` got auth_required with no way forward.
- The draft flag was only gated in ChecksPanel, not the two SourceControl call
  sites. With "create as draft" saved as a default, the composer hides the
  toggle for Bitbucket, so the flag could not be cleared and creation failed
  every time. Bitbucket now ignores draft instead of rejecting it.

Also:
- Blocked-create copy said "GitHub is not authenticated. Run gh auth login" on
  Bitbucket repos, in both the main-process and renderer paths.
- A decryption failure resolved to an anonymous config and queried anyway; a
  private repo answers 404, which reads as "no pull request" and offers Create
  for a branch that already has one. Requests now fail closed.
- Hiding non-open implicit branch matches was too broad: a declined PR became
  permanently invisible off the default branch. Scoped to merged, restoring the
  default-branch rule (#9171) for the rest.
- A failed disconnect rejected unhandled and the card silently re-rendered as
  connected; a partial delete left the secret live in memory for the session.
- The credentials dialog refused to open on a remote runtime, so a local repo
  could never store a credential. Now only the storage note changes, matching
  the Jira dialog.

---------

Co-authored-by: devatnull <59279509+devatnull@users.noreply.github.com>
2026-08-11 15:32:13 -07:00

29 lines
868 B
TypeScript

export type BitbucketAuthMode = 'token' | 'basic'
// Where the active credential comes from. Drives whether the UI offers
// Disconnect, which is only meaningful for in-app `stored` credentials.
export type BitbucketCredentialSource = 'environment' | 'stored' | 'none'
export type BitbucketConnectArgs = {
authMode: BitbucketAuthMode
accessToken?: string | null
email?: string | null
apiToken?: string | null
baseUrl?: string | null
}
// Deliberately excludes the secret: it never crosses the IPC boundary back to
// the renderer.
export type BitbucketConnectionStatus = {
configured: boolean
source: BitbucketCredentialSource
account: string | null
authMode: BitbucketAuthMode | null
email: string | null
baseUrl: string | null
}
export type BitbucketConnectResult =
| { ok: true; account: string | null }
| { ok: false; error: string }