Skip to content
This repository has been archived by the owner on Jan 29, 2021. It is now read-only.

Commit

Permalink
Add "only" query parameter (#137)
Browse files Browse the repository at this point in the history
* add only option, add tests for query options and fix tests

* fix linting error
  • Loading branch information
bluzi authored Nov 11, 2018
1 parent 561eb75 commit e554b1f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 21 deletions.
7 changes: 7 additions & 0 deletions pipes/create-comment.pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ const createComment = async context => {
token: context.meta.githubToken,
});

const onlyOption = context.query.only;
if (onlyOption && onlyOption.split(',').includes(context.state) === false) {
logger.log(`Skipping comment because only option forbids it`);
context.onlyOptionSkippedComment = true;
return context;
}

const issues = gh.getIssues(context.owner, context.repo);

const commentResult = await issues.createIssueComment(
Expand Down
1 change: 1 addition & 0 deletions pipes/finish.pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const finish = context => {
commentId: context.commentId,
buildNumber: context.buildNumber,
payload: JSON.stringify(context.payload, null, 4),
onlyOptionSkippedComment: !!context.onlyOptionSkippedComment,

...context.meta,
},
Expand Down
50 changes: 30 additions & 20 deletions pipes/parse-travis-payload.pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ const createJobObject = (job, index, owner, repoName) => ({
});

const getAllComments = async (githubToken, owner, repo, pullRequestNumber) => {
if (!githubToken) {
logger.warn('No Github token, cannot fetch comments');
return [];
}

const gh = new GitHub({
token: githubToken,
});
Expand Down Expand Up @@ -51,26 +56,31 @@ const getPullRequestAuthor = async (
pullRequestNumber,
) =>
new Promise((resolve, reject) => {
const gh = new GitHub({
token: githubToken,
});

gh
.getRepo(owner, repo)
.getPullRequest(pullRequestNumber, (err, pullRequest) => {
if (err) return reject(err);
return resolve(pullRequest.user.login);
})
.catch(() =>
logger.warn(
`Could not find author in: ${owner}/${repo} #${pullRequestNumber}`,
{
owner,
repo,
pullRequestNumber,
},
),
);
if (!githubToken) {
logger.warn('No GitHub token, unable to fetch PR owner');
resolve('Unknown PR author');
} else {
const gh = new GitHub({
token: githubToken,
});

gh
.getRepo(owner, repo)
.getPullRequest(pullRequestNumber, (err, pullRequest) => {
if (err) return reject(err);
return resolve(pullRequest.user.login);
})
.catch(() =>
logger.warn(
`Could not find author in: ${owner}/${repo} #${pullRequestNumber}`,
{
owner,
repo,
pullRequestNumber,
},
),
);
}
});

const parseTravisPayload = async ({ payload, meta, ...restOfContext }) => ({
Expand Down
5 changes: 5 additions & 0 deletions pipes/star.pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ const logger = require('../utils/logger');
const GitHub = require('better-github-api');

const star = async context => {
if (!context.meta.githubToken) {
logger.warn('No GitHub token, unable to star repo');
return context;
}

const gh = new GitHub({
token: context.meta.githubToken,
});
Expand Down
5 changes: 4 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('api', () => {
describe('POST /', () => {
it('should return HTTP status 201 and ok message', done => {
request(app)
.post('/')
.post('/?someOption=someValue')
.send({ payload: JSON.stringify(samplePayload) })
.expect(201)
.end((err, { body: { ok, context } }) => {
Expand All @@ -23,6 +23,9 @@ describe('api', () => {
return done(`Weird message:\n${context.message}`);
if (!context.message.includes('TravisBuddy Request Identifier'))
return done('No request identifier');
if (context.query.someOption !== 'someValue') {
return done('Query configuration does not work');
}

done();
});
Expand Down

0 comments on commit e554b1f

Please sign in to comment.