Skip to content

Commit

Permalink
fix(publish): Bitbucket publish can have username different from owner (
Browse files Browse the repository at this point in the history
  • Loading branch information
sr258 authored Oct 1, 2021
1 parent b7e4c38 commit 8ebfc96
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/young-roses-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"app-builder-lib": patch
---

Introduced env var to allow custom username for Bitbucket publish. This allows you to user a username different from the owner. No changes to interfaces or signatures that require changes in consumers.
16 changes: 12 additions & 4 deletions packages/app-builder-lib/src/publish/BitbucketPublisher.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Arch, InvalidConfigurationError, isEmptyOrSpaces } from "builder-util"
import { Arch, InvalidConfigurationError, isEmptyOrSpaces, log } from "builder-util"
import { httpExecutor } from "builder-util/out/nodeHttpExecutor"
import { ClientRequest, RequestOptions } from "http"
import { HttpPublisher, PublishContext } from "electron-publish"
import { BitbucketOptions } from "builder-util-runtime/out/publishOptions"
import { configureRequestOptions, HttpExecutor } from "builder-util-runtime"
import * as FormData from "form-data"
import { readFile } from "fs/promises"

export class BitbucketPublisher extends HttpPublisher {
readonly providerName = "bitbucket"
readonly hostname = "api.bitbucket.org"
Expand All @@ -18,11 +19,18 @@ export class BitbucketPublisher extends HttpPublisher {
super(context)

const token = process.env.BITBUCKET_TOKEN
const username = process.env.BITBUCKET_USERNAME

if (isEmptyOrSpaces(token)) {
throw new InvalidConfigurationError(`Bitbucket token is not set using env "BITBUCKET_TOKEN" (see https://www.electron.build/configuration/publish#BitbucketOptions)`)
}

if (isEmptyOrSpaces(username)) {
log.warn('No Bitbucket username provided via "BITBUCKET_USERNAME". Defaulting to use repo owner.')
}

this.info = info
this.auth = BitbucketPublisher.convertAppPassword(this.info.owner, token)
this.auth = BitbucketPublisher.convertAppPassword(username ?? this.info.owner, token)
this.basePath = `/2.0/repositories/${this.info.owner}/${this.info.slug}/downloads`
}

Expand Down Expand Up @@ -60,8 +68,8 @@ export class BitbucketPublisher extends HttpPublisher {
return `Bitbucket (owner: ${owner}, slug: ${slug}, channel: ${channel})`
}

static convertAppPassword(owner: string, token: string) {
const base64encodedData = Buffer.from(`${owner}:${token.trim()}`).toString("base64")
static convertAppPassword(username: string, token: string) {
const base64encodedData = Buffer.from(`${username}:${token.trim()}`).toString("base64")
return `Basic ${base64encodedData}`
}
}

0 comments on commit 8ebfc96

Please sign in to comment.