Skip to content

Commit

Permalink
feat: log additional data and add DRY_RUN env var
Browse files Browse the repository at this point in the history
  • Loading branch information
dessant committed Jun 27, 2018
1 parent c048628 commit cf70fac
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 30 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"dependencies": {
"joi": "^13.4.0",
"probot": "^6.2.0",
"probot-config": "dessant/probot-config"
"probot-config": "dessant/probot-config",
"uuid": "^3.3.0"
},
"devDependencies": {
"nodemon": "1.17.5",
Expand Down
51 changes: 23 additions & 28 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
const uuidV4 = require('uuid/v4');
const getMergedConfig = require('probot-config');

const schema = require('./schema');

module.exports = robot => {
module.exports = async robot => {
const github = await robot.auth();
const appName = (await github.apps.get({})).data.name;

robot.on(
['project_card.created', 'project_card.converted', 'project_card.moved'],
async context => {
await processCard(context);
const logger = context.log.child({appName, session: uuidV4()});
await processCard(context, logger);
}
);

Expand All @@ -25,7 +30,7 @@ module.exports = robot => {
return storedCards.includes(id);
}

async function processCard(context) {
async function processCard(context, log) {
const {payload, github} = context;

const cardId = payload.project_card.id;
Expand All @@ -38,66 +43,56 @@ module.exports = robot => {
if (!match) {
return;
}
const [owner, repo, issue] = match.slice(1);
const config = await getConfig(context, {owner, repo});
const [owner, repo, number] = match.slice(1);
const issue = {owner, repo, number};
const config = await getConfig(context, log, {owner, repo});
if (!config) {
return;
}
const {openIssueColumns, closedIssueColumns, perform} = config;

const {data: columnData} = await github.projects.getProjectColumn({
column_id: payload.project_card.column_id
});
const columnName = columnData.name;
let newState;
if (config.openIssueColumns.includes(columnName)) {
if (openIssueColumns.includes(columnName)) {
newState = 'open';
}
if (config.closedIssueColumns.includes(columnName)) {
if (closedIssueColumns.includes(columnName)) {
newState = 'closed';
}
if (!newState) {
return;
}

const {data: issueData} = await github.issues.get({
owner,
repo,
number: issue
});
const {data: issueData} = await github.issues.get(issue);
if (issueData.state === newState || issueData.pull_request) {
return;
}

robot.log.info(
{owner, repo, issue},
log.info(
{issue, cardId, perform},
newState === 'open' ? 'Opening issue' : 'Closing issue'
);
await github.issues.edit({
owner,
repo,
number: issue,
state: newState
});
if (perform) {
await github.issues.edit({...issue, state: newState});
}

storeCard(cardId);
}

async function getConfig(context, repo) {
async function getConfig(context, log, repo, file = 'issue-states.yml') {
let config;
try {
const repoConfig = await getMergedConfig(
context,
'issue-states.yml',
{},
repo
);
const repoConfig = await getMergedConfig(context, file, {}, repo);
const {error, value} = schema.validate(repoConfig || {});
if (error) {
throw error;
}
config = value;
} catch (err) {
robot.log.warn({err: new Error(err), ...repo}, 'Invalid config');
log.warn({err: new Error(err), repo, file}, 'Invalid config');
}

return config;
Expand Down
4 changes: 3 additions & 1 deletion src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ const schema = Joi.object().keys({
'Close issues that are moved to these project columns. Set to `[]` to disable'
),

_extends: Joi.string().description('Repository to extend settings from')
_extends: Joi.string().description('Repository to extend settings from'),

perform: Joi.boolean().default(!process.env.DRY_RUN)
});

module.exports = schema;
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4236,6 +4236,10 @@ uuid@^3.1.0:
version "3.2.1"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.2.1.tgz#12c528bb9d58d0b9265d9a2f6f0fe8be17ff1f14"

uuid@^3.3.0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.0.tgz#b237147804881d7b86f40a7ff8f590f15c37de32"

validate-npm-package-license@^3.0.1:
version "3.0.3"
resolved "https://registry.yarnpkg.com/validate-npm-package-license/-/validate-npm-package-license-3.0.3.tgz#81643bcbef1bdfecd4623793dc4648948ba98338"
Expand Down

0 comments on commit cf70fac

Please sign in to comment.