Skip to content

Commit

Permalink
feaT: back to plain response
Browse files Browse the repository at this point in the history
  • Loading branch information
arlac77 committed Aug 13, 2018
1 parent 88f3266 commit 7f1ed37
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 105 deletions.
25 changes: 5 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
"main": "dist/bitbucket-provider.js",
"module": "src/bitbucket-provider.js",
"description": "repository provider for bitbucket",
"keywords": [
"git",
"bitbucket",
"repository-provider"
],
"keywords": ["git", "bitbucket", "repository-provider"],
"contributors": [
{
"name": "Markus Felten",
Expand All @@ -32,8 +28,6 @@
"travis-deploy-once": "travis-deploy-once"
},
"dependencies": {
"btoa": "^1.2.1",
"node-fetch": "^2.2.0",
"repository-provider": "^2.24.0",
"request": "^2.88.0",
"request-promise": "^4.2.2"
Expand Down Expand Up @@ -66,21 +60,12 @@
"homepage": "https://github.com/arlac77/bitbucket-repository-provider#readme",
"release": {},
"ava": {
"files": [
"build/*-test.js"
],
"require": [
"babel-register"
]
"files": ["build/*-test.js"],
"require": ["babel-register"]
},
"nyc": {
"include": [
"build/*-test.js",
"src/**/*.js"
],
"reporter": [
"lcov"
],
"include": ["build/*-test.js", "src/**/*.js"],
"reporter": ["lcov"],
"report-dir": "./build/coverage"
},
"xo": {
Expand Down
63 changes: 4 additions & 59 deletions src/bitbucket-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { BitbucketBranch } from './bitbucket-branch';
import { BitbucketRepository } from './bitbucket-repository';
import { BitbucketProject } from './bitbucket-project';
import { URL } from 'url';
import fetch from 'node-fetch';
import btoa from 'btoa';

const request = require('request-promise');

Expand Down Expand Up @@ -207,64 +205,11 @@ export class BitbucketProvider extends Provider {
return r;
}

/**
* @param {URL} url
* @param {Object} options
* @return {Promise} fetch result
*/
async fetch(url, options = {}) {
options = Object.assign({}, options, {
headers: Object.assign({}, options.headers)
});

let response;

for (let n = 1; n <= 2; n++) {
response = await fetch(url, options);

if (response.status < 200 || response.status >= 300) {
switch (response.status) {
case 401:
const credentials = await this.provideCredentials(
context,
parseAuthenticate(response.headers.get('WWW-Authenticate'), {
url
})
);
if (credentials !== undefined) {
this.addAuthorizationHeader(options.headers, credentials);
break;
}
default:
throw new Error(response);
}
} else {
return response;
}
}
return response;
}

/**
* inserts the authorization data into the reguest header
* @param {Object} headers http credentials will be inserted into
* @param {Object} credentials
*
* @return {boolean} true if auth info has been written into headers
*/
addAuthorizationHeader(headers, credentials) {
if (credentials !== undefined) {
if (
credentials.user !== undefined &&
credentials.password !== undefined
) {
headers.authorization =
'Basic ' + btoa(credentials.user + ':' + credentials.password);
return true;
}
}
async project(name) {
const username = 'xxxx';
const response = await this.get(`teams/${username}/projects/`); // 2.0

return false;
console.log(response);
}

get(path) {
Expand Down
26 changes: 0 additions & 26 deletions tests/bitbucket-provider-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,32 +186,6 @@ test('provider repo with branch name', async t => {
//t.is(branch.user, 'arlac77');
});

test('create branch', async t => {
const provider = new BitbucketProvider(config);
const repository = await provider.repository(REPOSITORY_NAME);
const newName = `test-${new Date().getTime()}`;
const branch = await repository.createBranch(newName);

t.is(branch.name, newName);

//await branch.delete();
});

test.skip('delete branch', async t => {
const provider = new BitbucketProvider(config);
const repository = await provider.repository(REPOSITORY_NAME);

const branches = await repository.branches();

for (const [name, branch] of branches.entries()) {
if (name.match(/^test-/)) {
await repository.deleteBranch(name);
t.is(await repository.branch(name), undefined);
}
//console.log(`${name}: ${branch}`);
}
});

test('list', async t => {
const provider = new BitbucketProvider(config);
const branch = await provider.branch(REPOSITORY_NAME);
Expand Down
35 changes: 35 additions & 0 deletions tests/branch-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import test from 'ava';
import { BitbucketProvider } from '../src/bitbucket-provider';
import { BitbucketProject } from '../src/bitbucket-project';

const REPOSITORY_URL =
'https://arlac77@bitbucket.org/arlac77/sync-test-repository.git';
const REPOSITORY_NAME = 'arlac77/sync-test-repository';

const config = BitbucketProvider.optionsFromEnvironment(process.env);

test.skip('create branch', async t => {
const provider = new BitbucketProvider(config);
const repository = await provider.repository(REPOSITORY_NAME);
const newName = `test-${new Date().getTime()}`;
const branch = await repository.createBranch(newName);

t.is(branch.name, newName);

//await branch.delete();
});

test.skip('delete branch', async t => {
const provider = new BitbucketProvider(config);
const repository = await provider.repository(REPOSITORY_NAME);

const branches = await repository.branches();

for (const [name, branch] of branches.entries()) {
if (name.match(/^test-/)) {
await repository.deleteBranch(name);
t.is(await repository.branch(name), undefined);
}
//console.log(`${name}: ${branch}`);
}
});
16 changes: 16 additions & 0 deletions tests/project-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import test from 'ava';
import { BitbucketProvider } from '../src/bitbucket-provider';
import { BitbucketProject } from '../src/bitbucket-project';

const REPOSITORY_URL =
'https://arlac77@bitbucket.org/arlac77/sync-test-repository.git';
const REPOSITORY_NAME = 'arlac77/sync-test-repository';

const config = BitbucketProvider.optionsFromEnvironment(process.env);

test.skip('project by name', async t => {
const provider = new BitbucketProvider(config);
const project = await provider.project('decission-table');

t.is(project.name, 'decission-table');
});

0 comments on commit 7f1ed37

Please sign in to comment.