Skip to content

Commit

Permalink
feat(911): Add method to get changed files. BREAKING CHANGE: requires…
Browse files Browse the repository at this point in the history
… getChangedFiles
  • Loading branch information
tkyi authored and d2lam committed Mar 27, 2018
1 parent 0dc32eb commit 77b0350
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 76 deletions.
52 changes: 2 additions & 50 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,3 @@
# Contributing
# Contributing to Screwdriver

Thank you for considering contributing! There are many ways you can help.

## Issues

File an issue if you think you've found a bug. Be sure to describe

1. How can it be reproduced?
2. What did you expect?
3. What actually occurred?
4. Version, platform, etc. if possibly relevant.

## Docs

Documentation, READMEs, and examples are extremely important. Please help improve them and if you find a typo or notice a problem, please send a fix or say something.

## Submitting Patches

Patches for fixes, features, and improvements are accepted through pull requests.

* Write good commit messages, in the present tense! (Add X, not Added X). Short title, blank line, bullet points if needed. Capitalize the first letter of the title or bullet item. No punctuation in the title.
* Code must pass lint and style checks.
* All external methods must be documented.
* Include tests to improve coverage and prevent regressions.
* Squash changes into a single commit per feature/fix. Ask if you're unsure how to discretize your work.

Please ask before embarking on a large improvement so you're not disappointed if it does not align with the goals of the project or owner(s).

## Commit message format

We use [semantic-release](https://www.npmjs.com/package/semantic-release), which requires commit messages to be in this specific format: `<type>(<scope>): <subject>`

* Types:
* feat (feature)
* fix (bug fix)
* docs (documentation)
* style (formatting, missing semi colons, …)
* refactor
* test (when adding missing tests)
* chore (maintain)
* Scope: anything that specifies the scope of the commit. Can be blank or `*`
* Subject: description of the commit. For **breaking changes** that require major version bump, add `BREAKING CHANGE` to the commit message.

**Examples commit messages:**
* Bug fix: `fix: Remove extra space`
* Breaking change: `feat: Add addWebhook method. BREAKING CHANGE: scm plugin need to implement addWebhook method now`

## Feature Requests

Make the case for a feature via an issue with a good title. The feature should be discussed and given a target inclusion milestone or closed.
Have a look at our guidelines, as well as pointers on where to start making changes, in our official [documentation](http://docs.screwdriver.cd/about/contributing).
35 changes: 29 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,45 @@ Required parameters:
A key-map of data related to the received payload in the form of:
```js
{
type: 'pr', // can be 'pr' or 'repo'
hookId: '81e6bd80-9a2c-11e6-939d-beaa5d9adaf3', // webhook event uuid
action: 'opened', // can be 'opened', 'reopened', 'closed', or 'synchronized' for type 'pr'; 'push' for type 'repo'
username: 'robin', // should be the actor/creator of the webhook event (not necessarily the author)
checkoutUrl: 'https://[email protected]/batman/test.git',
branch: 'mynewbranch',
sha: '9ff49b2d1437567cad2b5fed7a0706472131e927',
checkoutUrl: 'https://[email protected]/batman/test.git',
hookId: '81e6bd80-9a2c-11e6-939d-beaa5d9adaf3', // webhook event uuid
lastCommitMessage: 'This is the last commit message', // get a message of the last one from commits object
prNum: 3,
prRef: 'pull/3/merge'
prRef: 'pull/3/merge',
prSource: 'fork', // If type is 'pr', prSource is 'fork' or 'branch'
scmContext: 'github:github.com',
sha: '9ff49b2d1437567cad2b5fed7a0706472131e927',
type: 'pr', // can be 'pr' or 'repo'
username: 'robin' // should be the actor/creator of the webhook event (not necessarily the author)
}
```

#### Expected Promise response
1. Resolve with a parsed hook object
2. Reject if not able to parse hook

### getChangedFiles
Required parameters:

| Parameter | Type | Description |
| :------------- | :---- | :-------------|
| config | Object | Yes | Configuration Object |
| config.type | String | The type of action from Git (can be 'pr' or 'repo') |
| config.payload | Object | The webhook payload received from the SCM service |
| config.token | String | Access token for scm |

#### Expected Outcome
An array of file paths that were changed:
```js
['README.md', 'folder/screwdriver.yaml'] // array of changed files
```

#### Expected Promise response
1. Resolve with an array of files
2. Reject if not able to parse hook

### getCheckoutCommand
Required parameters:

Expand Down Expand Up @@ -374,6 +396,7 @@ To make use of the validation functions, the functions to override are:
1. `_addWebhook`
1. `_parseUrl`
1. `_parseHook`
1. `_getChangedFiles`
1. `_getCheckoutCommand`
1. `_decorateUrl`
1. `_decorateCommit`
Expand Down
23 changes: 22 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,34 @@ class ScmBase {
* @return {Promise}
*/
parseHook(headers, payload) {
return this._parseHook(headers, payload);
return this._parseHook(headers, payload)
.then(hook => validate(hook, dataSchema.plugins.scm.parseHookOutput));
}

_parseHook() {
return Promise.reject(new Error('Not implemented'));
}

/**
* Parse the webhook to get the changed files
* @method getChangedFiles
* @param {Object} config
* @param {String} config.type The type of action from Git (can be 'pr' or 'repo')
* @param {Object} config.payload The webhook payload received from the SCM service
* @param {String} config.token The token used to authenticate to the SCM
* @return {Promise} Returns an array of changed files
*/
getChangedFiles(config) {
return validate(config, dataSchema.plugins.scm.getChangedFilesInput)
.then(validInput => this._getChangedFiles(validInput))
.then(changedFiles => validate(changedFiles,
dataSchema.plugins.scm.getChangedFilesOutput));
}

_getChangedFiles() {
return Promise.reject(new Error('Not implemented'));
}

/**
* Checkout the source code from a repository; resolves as an object with checkout commands
* @method getCheckoutCommand
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@
},
"dependencies": {
"joi": "^13.0.0",
"screwdriver-data-schema": "^18.0.0"
"screwdriver-data-schema": "^18.13.0"
}
}
13 changes: 13 additions & 0 deletions test/data/parseHookOutput.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"action": "opened",
"branch": "mynewbranch",
"checkoutUrl": "https://[email protected]/batman/test.git",
"hookId": "81e6bd80-9a2c-11e6-939d-beaa5d9adaf3",
"lastCommitMessage": "This is the last commit message",
"prRef": "pull/3/merge",
"prSource": "fork",
"scmContext": "github:github.com",
"sha": "9ff49b2d1437567cad2b5fed7a0706472131e927",
"type": "pr",
"username": "robin"
}
80 changes: 62 additions & 18 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

/* eslint-disable no-underscore-dangle */
const assert = require('chai').assert;
const token = 'token';
const testParseHook = require('./data/parseHookOutput.json');

describe('index test', () => {
let instance;
Expand Down Expand Up @@ -79,19 +81,15 @@ describe('index test', () => {
stuff: 'foo'
};
const payload = {
moreStuff: 'bar'
type: 'pr'
};

it('returns data from underlying method', () => {
instance._parseHook = () => Promise.resolve({
type: 'pr'
});
instance._parseHook = () => Promise.resolve(testParseHook);

return instance.parseHook()
.then((output) => {
assert.deepEqual(output, {
type: 'pr'
});
assert.deepEqual(output, testParseHook);
});
});

Expand All @@ -105,6 +103,52 @@ describe('index test', () => {
);
});

describe('getChangedFiles', () => {
const type = 'pr';
const payload = {
type
};

it('returns data from underlying method', () => {
instance.getChangedFiles = () => Promise.resolve([
'README.md',
'folder/screwdriver.yaml'
]);

return instance.getChangedFiles()
.then((output) => {
assert.deepEqual(output, [
'README.md',
'folder/screwdriver.yaml'
]);
});
});

it('returns error when invalid output', () => {
instance._getChangedFiles = () => Promise.resolve({
invalid: 'object'
});

return instance.getChangedFiles({ type, payload, token })
.then(() => {
assert.fail('you will never get dis');
})
.catch((err) => {
assert.instanceOf(err, Error);
assert.equal(err.name, 'ValidationError');
});
});

it('returns not implemented', () =>
instance.getChangedFiles({ type, payload, token })
.then(() => {
assert.fail('This should not fail the test');
}, (err) => {
assert.equal(err.message, 'Not implemented');
})
);
});

describe('getCheckoutCommand', () => {
const config = {
branch: 'branch',
Expand Down Expand Up @@ -215,7 +259,7 @@ describe('index test', () => {
describe('decorateUrl', () => {
const config = {
scmUri: 'github.com:repoId:branch',
token: 'token',
token,
scmContext: 'github:github.com'
};

Expand Down Expand Up @@ -260,7 +304,7 @@ describe('index test', () => {
const config = {
sha: '0264b13de9aa293b7abc8cf36793b6458c07af38',
scmUri: 'github.com:repoId:branch',
token: 'token',
token,
scmContext: 'github:github.com'
};

Expand Down Expand Up @@ -304,7 +348,7 @@ describe('index test', () => {
describe('decorateAuthor', () => {
const config = {
username: 'd2lam',
token: 'token',
token,
scmContext: 'github:github.com'
};

Expand Down Expand Up @@ -348,7 +392,7 @@ describe('index test', () => {
describe('getPermissons', () => {
const config = {
scmUri: 'github.com:repoId:branch',
token: 'token',
token,
scmContext: 'github:github.com'
};

Expand Down Expand Up @@ -392,7 +436,7 @@ describe('index test', () => {
describe('getCommitSha', () => {
const config = {
scmUri: 'github.com:repoId:branch',
token: 'token',
token,
scmContext: 'github:github.com'
};

Expand Down Expand Up @@ -438,7 +482,7 @@ describe('index test', () => {
scmUri: 'github.com:repoId:branch',
sha: '0264b13de9aa293b7abc8cf36793b6458c07af38',
buildStatus: 'SUCCESS',
token: 'token',
token,
url: 'https://foo.bar',
pipelineId: 123,
scmContext: 'github:github.com'
Expand Down Expand Up @@ -491,7 +535,7 @@ describe('index test', () => {
const config = {
scmUri: 'github.com:repoId:branch',
path: 'testFile',
token: 'token',
token,
scmContext: 'github:github.com'
};

Expand Down Expand Up @@ -535,7 +579,7 @@ describe('index test', () => {
describe('getOpenedPRs', () => {
const config = {
scmUri: 'github.com:repoId:branch',
token: 'token',
token,
scmContext: 'github:github.com'
};

Expand Down Expand Up @@ -603,7 +647,7 @@ describe('index test', () => {
describe('addWebhook', () => {
const config = {
scmUri: 'github.com:20161206:branch',
token: 'token',
token,
webhookUrl: 'https://bob.by/ford',
scmContext: 'github:github.com'
};
Expand All @@ -615,7 +659,7 @@ describe('index test', () => {

return instance.addWebhook({
scmUri: 'github.com:20161206:branch',
token: 'token',
token,
webhookUrl: 'https://bob.by/ford',
scmContext: 'github:github.com'
}).then((result) => {
Expand All @@ -642,7 +686,7 @@ describe('index test', () => {
describe('getPrInfo', () => {
const config = {
scmUri: 'github.com:repoId:branch',
token: 'token',
token,
prNum: 123,
scmContext: 'github:github.com'
};
Expand Down

0 comments on commit 77b0350

Please sign in to comment.