Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(publisher-bitbucket): initial publish publisher-bitbucket #571

Merged
merged 2 commits into from
Sep 12, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/publisher/bitbucket/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@electron-forge/publisher-bitbucket",
"version": "6.0.0-beta.28",
"description": "Github publisher for Electron Forge",
"repository": "https://github.com/electron-userland/electron-forge",
"author": "Samuel Attard",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I chuck my name here? XD

"license": "MIT",
"main": "dist/PublisherBitbucket.js",
"typings": "dist/PublisherBitbucket.d.ts",
"scripts": {
"test": "mocha --require ts-node/register test/**/*_spec.ts test/**/**/*_spec.ts --opts ../../../mocha.opts"
},
"devDependencies": {
"chai": "^4.0.0",
"mocha": "^5.0.0"
},
"engines": {
"node": ">= 6.0"
},
"dependencies": {
"@electron-forge/async-ora": "6.0.0-beta.28",
"@electron-forge/publisher-base": "6.0.0-beta.28",
"@electron-forge/shared-types": "6.0.0-beta.28",
lukebatchelor marked this conversation as resolved.
Show resolved Hide resolved
"fs-extra": "^7.0.0"
}
}
41 changes: 41 additions & 0 deletions packages/publisher/bitbucket/src/Config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

lukebatchelor marked this conversation as resolved.
Show resolved Hide resolved
export interface BitbucketRepository {
/**
* The name of your repository
*/
name: string;
/**
* The owner of your repository, this is either your username or the name of
* the organization that owns the repository.
*/
owner: string;
}

export interface BitbucketAuth {
/**
* The username to use when uploading.
*
* You can set the BITBUCKET_USERNAME environment variable if you don't want to hard
* code this into your config.
*/
username?: string;
/**
* An authorization token with permission to upload downloads to this
* repository.
*
* You can set the BITBUCKET_APP_PASSWORD environment variable if you don't want to hard
* code this into your config.
*/
appPassword?: string;
}

export interface PublisherBitbucketConfig {
/**
* Details that identify your repository (name and owner)
*/
repository: BitbucketRepository;
/**
* User details for uploading releases
*/
auth: BitbucketAuth;
}
66 changes: 66 additions & 0 deletions packages/publisher/bitbucket/src/PublisherBitbucket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import PublisherBase, { PublisherOptions } from '@electron-forge/publisher-base';
import { asyncOra } from '@electron-forge/async-ora';

import fetch from 'node-fetch';
lukebatchelor marked this conversation as resolved.
Show resolved Hide resolved
import FormData from 'form-data';
lukebatchelor marked this conversation as resolved.
Show resolved Hide resolved
import fs from 'fs-extra';

import { PublisherBitbucketConfig } from './Config';

export default class PublisherGithub extends PublisherBase<PublisherBitbucketConfig> {
name = 'bitbucket';

async publish({ makeResults }: PublisherOptions) {
const { config } = this;
const hasRepositoryConfig = config.repository && typeof config.repository;
const hasAuthConfig = config.auth && typeof config.auth === 'object';
let appPassword = process.env.BITBUCKET_APP_PASSWORD;
let bbUsername = process.env.BITBUCKET_USERNAME;

if (!(hasRepositoryConfig && config.repository.owner && config.repository.name)) {
throw 'In order to publish to Bitbucket you must set the "repository.owner" and "repository.name" properties in your forge config. See the docs for more info'; // eslint-disable-line
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally prefer to specify which eslint rule I'm ignoring here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly couldn't tell you which it was, I copied it from the GitHub one, probably max-len.

I'll update both tomorrow.

}

if (!(config.auth && typeof config.auth === 'object') && !appPassword && !bbUsername) {
lukebatchelor marked this conversation as resolved.
Show resolved Hide resolved
throw 'In order to publish to Bitbucket you must set the "auth" object in your forge config or use BITBUCKET_APP_PASSWORD and BITBUCKET_USERNAME environment variables';
}

appPassword = config.auth.appPassword || process.env.BITBUCKET_APP_PASSWORD;
bbUsername = config.auth.username || process.env.BITBUCKET_USERNAME;

if (!appPassword || !bbUsername) {
throw 'In order to publish to Bitbucket you must set the "auth.appPassword" and "auth.username" properties in your forge config.';
}

for (const [index, makeResult] of makeResults.entries()) {
const data = new FormData();

let i = 0;
for (const artifactPath of makeResult.artifacts) {
data.append('files', fs.createReadStream(artifactPath));
i += 1;
}

// TODO: Consider checking if the files already exist at the current version and abort if so?
lukebatchelor marked this conversation as resolved.
Show resolved Hide resolved

await asyncOra(`Uploading result (${index + 1}/${makeResults.length})`, async () => {
// TODO: See if we can use this same API for bitbucket server, we could take in a `host` config if so
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

const apiUrl = `https://api.bitbucket.org/2.0/repositories/${config.repository.owner}/${config.repository.name}/downloads`;
const encodedUserAndPass = Buffer.from(`${bbUsername}:${appPassword}`).toString('base64');

const response = await fetch(apiUrl, {
headers: {
Authorization: `Basic ${encodedUserAndPass}`,
},
method: 'POST',
body: data,
});

// We will get a 200 on the inital upload and a 201 if publishing over the same version
if (response.status !== 200 && response.status !== 201) {
throw `Unexpected response code from Bitbucket: ${response.status} ${response.statusText}\n\nBody:\n${await response.text()}`;
}
});
}
}
}