Skip to content

Commit

Permalink
Merge pull request screwdriver-cd#5 from screwdriver-cd/decorated
Browse files Browse the repository at this point in the history
Add decorated functions
  • Loading branch information
d2lam authored Oct 18, 2016
2 parents 1971564 + 3d11683 commit 4d3549e
Show file tree
Hide file tree
Showing 4 changed files with 543 additions and 93 deletions.
78 changes: 77 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Required parameters:
| config.token | String | Access token for scm |

#### Output: Promise
1. Resolves to an scm uri for the repository. Ex: `bitbucket.org:{1234}:branchName`, where `{1234}` is repository's uuid.
1. Resolves to an scm uri for the repository. Ex: `bitbucket.org:batman/{1234}:branchName`, where `batman` is the repository's owner and `{1234}` is repository's uuid.
2. Rejects if not able to parse url

### parseHook
Expand All @@ -45,6 +45,82 @@ Required parameters:
```
2. Rejects if not able to parse webhook payload

### decorateUrl
Required parameters:

| Parameter | Type | Description |
| :------------- | :---- | :-------------|
| config | Object | Configuration Object |
| config.scmUri | String | Scm uri (ex: `bitbucket.org:batman/{1234}:branchName`) |
| config.token | String | Access token for scm |

#### Expected Outcome
Decorated url in the form of:
```js
{
url: 'https://bitbucket.org/batman/test.git',
name: 'batman/test',
branch: 'mybranch'
}
```

#### Expected Promise response
1. Resolve with a decorated url object for the repository
2. Reject if not able to get decorate url

### decorateCommit
Required parameters:

| Parameter | Type | Description |
| :------------- | :---- | :-------------|
| config | Object | Configuration Object |
| config.sha | String | Commit sha to decorate |
| config.scmUri | String | Scm uri (ex: `bitbucket.org:1234:branchName`) |
| config.token | String | Access token for scm |

#### Expected Outcome
Decorated commit in the form of:
```js
{
url: 'https://bitbucket.org/screwdriver-cd/scm-base/commit/5c3b2cc64ee4bdab73e44c394ad1f92208441411',
message: 'Use screwdriver to publish',
author: {
url: 'https://bitbucket.org/d2lam',
name: 'Dao Lam',
username: 'd2lam',
avatar: 'https://bitbucket.org/account/d2lam/avatar/32/'
}
}
```

#### Expected Promise response
1. Resolve with a decorate commit object for the repository
2. Reject if not able to decorate commit

### decorateAuthor
Required parameters:

| Parameter | Type | Description |
| :------------- | :---- | :-------------|
| config | Object | Configuration Object |
| config.username | String | Author to decorate |
| config.token | String | Access token for scm |

#### Expected Outcome
Decorated author in the form of:
```js
{
url: 'https://bitbucket.org/d2lam',
name: 'Dao Lam',
username: 'd2lam',
avatar: 'https://bitbucket.org/account/d2lam/avatar/32/'
}
```

#### Expected Promise response
1. Resolve with a decorate author object for the repository
2. Reject if not able to decorate author

## Testing

```bash
Expand Down
125 changes: 119 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,31 @@ function getRepoInfo(checkoutUrl) {
};
}

/**
* @method getScmUriParts
* @param {String} scmUri
* @return {Object}
*/
function getScmUriParts(scmUri) {
const scm = {};

[scm.hostname, scm.repoId, scm.branch] = scmUri.split(':');

return scm;
}

class BitbucketScm extends Scm {
/**
* Constructor for Scm
* @method constructor
* @param {Object} config Configuration
* @param {Object} config Configuration
* @param {String} [config.fusebox] Options for the circuit breaker
* @return {ScmBase}
*/
constructor(config) {
super(config);

this.breaker = new Fusebox(request);
this.breaker = new Fusebox(request, config.fusebox);
}

/**
Expand All @@ -54,12 +68,10 @@ class BitbucketScm extends Scm {
_parseUrl(config) {
const repoInfo = getRepoInfo(config.checkoutUrl);
const getBranchUrl = `${API_URL}/repositories/${repoInfo.username}/${repoInfo.repo}` +
`/refs/branches/${repoInfo.branch}`;
`/refs/branches/${repoInfo.branch}?access_key=${config.token}`;
const options = {
url: getBranchUrl,
method: 'GET',
login_type: 'oauth2',
oauth_access_token: config.token
method: 'GET'
};

return this.breaker.runCommand(options)
Expand Down Expand Up @@ -130,6 +142,107 @@ class BitbucketScm extends Scm {
}
}

/**
* Decorate the author based on the Bitbucket
* @method _decorateAuthor
* @param {Object} config Configuration object
* @param {Object} config.token Access token to authenticate with Bitbucket
* @param {Object} config.username Username to query more information for
* @return {Promise}
*/
_decorateAuthor(config) {
const options = {
url: `${API_URL}/users/${config.username}?access_key=${config.token}`,
method: 'GET'
};

return this.breaker.runCommand(options)
.then((response) => {
const body = response.body;

if (response.statusCode !== 200) {
throw new Error(`STATUS CODE ${response.statusCode}: ${body}`);
}

return {
url: body.links.html.href,
name: body.display_name,
username: body.username,
avatar: body.links.avatar.href
};
});
}

/**
* Decorate a given SCM URI with additional data to better display
* related information. If a branch suffix is not provided, it will default
* to the master branch
* @method decorateUrl
* @param {Config} config Configuration object
* @param {String} config.scmUri The SCM URI the commit belongs to
* @param {String} config.token Service token to authenticate with Github
* @return {Object}
*/
_decorateUrl(config) {
const scm = getScmUriParts(config.scmUri);
const options = {
url: `${API_URL}/repositories/${scm.repoId}?access_key=${config.token}`,
method: 'GET'
};

return this.breaker.runCommand(options)
.then((response) => {
const body = response.body;

if (response.statusCode !== 200) {
throw new Error(`STATUS CODE ${response.statusCode}: ${body}`);
}

return {
url: body.links.html.href,
name: body.full_name,
branch: scm.branch
};
});
}

/**
* Decorate the commit based on the repository
* @method _decorateCommit
* @param {Object} config Configuration object
* @param {Object} config.sha Commit sha to decorate
* @param {Object} config.scmUri SCM URI the commit belongs to
* @param {Object} config.token Service token to authenticate with Github
* @return {Promise}
*/
_decorateCommit(config) {
const scm = getScmUriParts(config.scmUri);
const options = {
url: `${API_URL}/repositories/${scm.repoId}` +
`/commit/${config.sha}?access_key=${config.token}`,
method: 'GET'
};

return this.breaker.runCommand(options)
.then((response) => {
const body = response.body;

if (response.statusCode !== 200) {
throw new Error(`STATUS CODE ${response.statusCode}: ${body}`);
}

// eslint-disable-next-line
return this._decorateAuthor({
username: body.author.user.username,
token: config.token
}).then(author => ({
url: body.links.html.href,
message: body.message,
author
}));
});
}

/**
* Retreive stats for the scm
* @method stats
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
],
"devDependencies": {
"chai": "^3.5.0",
"circuit-fuses": "^2.0.3",
"eslint": "^3.2.2",
"eslint-config-screwdriver": "^2.0.0",
"eslint-plugin-import": "^1.12.0",
Expand All @@ -40,6 +39,7 @@
"sinon-as-promised": "^4.0.2"
},
"dependencies": {
"circuit-fuses": "^2.1.0",
"hoek": "^4.1.0",
"request": "^2.75.0",
"screwdriver-data-schema": "^14.0.1",
Expand Down
Loading

0 comments on commit 4d3549e

Please sign in to comment.