Skip to content

Commit

Permalink
fix(travis-bot): Fix conversations, move CLI to TypeScript (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
ffflorian committed Mar 20, 2018
1 parent 3c7f3e2 commit 702217a
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 124 deletions.
124 changes: 1 addition & 123 deletions packages/travis-bot/bin/wire-travis-bot.js
Expand Up @@ -19,126 +19,4 @@
*
*/

// @ts-check

const {TravisBot} = require('../');
const {version} = require('../package.json');
const logdown = require('logdown');

const logger = logdown('@wireapp/travis-bot/cli', {
logger: console,
markdown: false,
});

const scriptName = require('path').basename(process.argv[1]);

const requiredEnvVars = ['WIRE_WEBAPP_BOT_EMAIL', 'WIRE_WEBAPP_BOT_PASSWORD'];
const travisEnvVars = ['TRAVIS_BRANCH', 'TRAVIS_BUILD_NUMBER', 'TRAVIS_COMMIT', 'TRAVIS_REPO_SLUG'];

const setBold = text => `\x1b[1m${text}\x1b[0m`;

const usage = () => {
console.info(`${setBold('Usage:')} ${scriptName} <conversation id(s)>\n`);
console.info(
`${setBold('Example:')} ${scriptName} "e4302e84-75fd-4dc7-8a16-67018bd94ce7,44be7db8-7b7c-4acf-887d-86fbb9a5508f"`
);
};
const envVarUsage = () => console.info(setBold('Required environment variables:'), requiredEnvVars.join(', '));

const start = async () => {
const {WIRE_WEBAPP_BOT_EMAIL, WIRE_WEBAPP_BOT_PASSWORD, WIRE_WEBAPP_BOT_CONVERSATION_IDS} = process.env;
const {TRAVIS_BRANCH, TRAVIS_BUILD_NUMBER, TRAVIS_COMMIT, TRAVIS_REPO_SLUG, TRAVIS_TAG} = process.env;

const commitAuthor = await TravisBot.runCommand(`git log | grep Author: | cut -d' ' -f2- | uniq | head -n1`);
let commitSummary = await TravisBot.runCommand('git log -1 --pretty=%s');

if (TRAVIS_TAG) {
const MAXIMUM_CHANGELOG_CHARS = 800;

const previousGitTag = await TravisBot.runCommand(`git describe --abbrev=0 --tags ${TRAVIS_TAG}^`);
const changelog = await TravisBot.generateChangelog(
TRAVIS_REPO_SLUG,
`${previousGitTag}..${TRAVIS_TAG}`,
MAXIMUM_CHANGELOG_CHARS
);

commitSummary += '\n\n' + changelog;
}

const loginData = {
email: WIRE_WEBAPP_BOT_EMAIL,
password: WIRE_WEBAPP_BOT_PASSWORD,
persist: false,
};

const messageData = {
build: {
number: TRAVIS_BUILD_NUMBER,
repositoryName: TRAVIS_REPO_SLUG,
url: '',
},
commit: {
author: commitAuthor,
branch: TRAVIS_BRANCH,
hash: TRAVIS_COMMIT,
message: commitSummary,
},
};

if (WIRE_WEBAPP_BOT_CONVERSATION_IDS) {
messageData.commit.conversationIds = WIRE_WEBAPP_BOT_CONVERSATION_IDS.replace(' ', '').split(',');
}

logger.info('Booting up ...');

const bot = new TravisBot(loginData, messageData);
await bot.start();

return bot;
};

logger.info(setBold(`wire-travis-bot v${version}`) + '\n');

const SECOND_ARGUMENT = 2;

switch (process.argv[SECOND_ARGUMENT]) {
case '-help':
case '--help':
case '-h':
case '--h': {
usage();
envVarUsage();
process.exit(0);
}
default: {
process.env.WIRE_WEBAPP_BOT_CONVERSATION_IDS = process.argv[SECOND_ARGUMENT];
}
}

travisEnvVars.forEach(envVar => {
if (!process.env[envVar]) {
console.error(
`${setBold('Error:')} Travis environment variable "${envVar}" is not set.\n` +
'Read more: https://docs.travis-ci.com/user/environment-variables/#Default-Environment-Variables'
);
process.exit(1);
}
});

requiredEnvVars.forEach(envVar => {
if (!process.env[envVar]) {
console.error(`Error: Environment variable "${envVar}" is not set.`);
envVarUsage();
process.exit(1);
}
});

(async () => {
try {
await start();
process.exit(0);
} catch (error) {
console.error(error);
process.exit(1);
}
})();
require('../dist/cli');
142 changes: 142 additions & 0 deletions packages/travis-bot/src/main/cli.ts
@@ -0,0 +1,142 @@
/*
* Wire
* Copyright (C) 2018 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*/

import {TravisBot, MessageData} from './index';
import {LoginData} from '@wireapp/api-client/dist/commonjs/auth/';

const logdown = require('logdown');
const {version}: {version: string} = require('../package.json');

const logger = logdown('@wireapp/travis-bot/cli', {
logger: console,
markdown: false,
});

const scriptName = require('path').basename(process.argv[1]);

const requiredEnvVars = ['WIRE_WEBAPP_BOT_EMAIL', 'WIRE_WEBAPP_BOT_PASSWORD'];
const travisEnvVars = ['TRAVIS_BRANCH', 'TRAVIS_BUILD_NUMBER', 'TRAVIS_COMMIT', 'TRAVIS_REPO_SLUG'];

const setBold = (text: string): string => `\x1b[1m${text}\x1b[0m`;

const usage = (): void => {
console.info(`${setBold('Usage:')} ${scriptName} <conversation id(s)>\n`);
console.info(
`${setBold('Example:')} ${scriptName} "e4302e84-75fd-4dc7-8a16-67018bd94ce7,44be7db8-7b7c-4acf-887d-86fbb9a5508f"`
);
};
const envVarUsage = (): void => console.info(setBold('Required environment variables:'), requiredEnvVars.join(', '));

const start = async (): Promise<TravisBot> => {
const {WIRE_WEBAPP_BOT_EMAIL, WIRE_WEBAPP_BOT_PASSWORD, WIRE_WEBAPP_BOT_CONVERSATION_IDS} = process.env;
const {TRAVIS_BRANCH, TRAVIS_BUILD_NUMBER, TRAVIS_COMMIT, TRAVIS_REPO_SLUG, TRAVIS_TAG} = process.env;

const commitAuthor = await TravisBot.runCommand(`git log | grep Author: | cut -d' ' -f2- | uniq | head -n1`);
let commitSummary = await TravisBot.runCommand('git log -1 --pretty=%s');

if (TRAVIS_TAG) {
const MAXIMUM_CHANGELOG_CHARS = 800;

const previousGitTag = await TravisBot.runCommand(`git describe --abbrev=0 --tags ${TRAVIS_TAG}^`);
const changelog = await TravisBot.generateChangelog(
String(TRAVIS_REPO_SLUG),
`${previousGitTag}..${TRAVIS_TAG}`,
MAXIMUM_CHANGELOG_CHARS
);

commitSummary += '\n\n' + changelog;
}

const loginData: LoginData = {
email: WIRE_WEBAPP_BOT_EMAIL,
password: WIRE_WEBAPP_BOT_PASSWORD,
persist: false,
};

const messageData: MessageData = {
build: {
number: String(TRAVIS_BUILD_NUMBER),
repositoryName: String(TRAVIS_REPO_SLUG),
url: '',
},
commit: {
author: commitAuthor,
branch: String(TRAVIS_BRANCH),
hash: String(TRAVIS_COMMIT),
message: commitSummary,
},
};

if (WIRE_WEBAPP_BOT_CONVERSATION_IDS) {
messageData.conversationIds = WIRE_WEBAPP_BOT_CONVERSATION_IDS.replace(' ', '').split(',');
}

logger.info('Booting up ...');

const bot = new TravisBot(loginData, messageData);
await bot.start();

return bot;
};

logger.info(setBold(`wire-travis-bot v${version}`) + '\n');

const SECOND_ARGUMENT = 2;

switch (process.argv[SECOND_ARGUMENT]) {
case '-help':
case '--help':
case '-h':
case '--h': {
usage();
envVarUsage();
process.exit(0);
}
default: {
process.env.WIRE_WEBAPP_BOT_CONVERSATION_IDS = process.argv[SECOND_ARGUMENT];
}
}

travisEnvVars.forEach(envVar => {
if (!process.env[envVar]) {
console.error(
`${setBold('Error:')} Travis environment variable "${envVar}" is not set.\n` +
'Read more: https://docs.travis-ci.com/user/environment-variables/#Default-Environment-Variables'
);
process.exit(1);
}
});

requiredEnvVars.forEach(envVar => {
if (!process.env[envVar]) {
console.error(`Error: Environment variable "${envVar}" is not set.`);
envVarUsage();
process.exit(1);
}
});

(async () => {
try {
await start();
process.exit(0);
} catch (error) {
console.error(error);
process.exit(1);
}
})();
2 changes: 1 addition & 1 deletion packages/travis-bot/src/main/index.ts
Expand Up @@ -57,7 +57,7 @@ class TravisBot {
const {commit: {branch, author, hash, message}} = this.messageData;

return (
`**${repositoryName}: Travis build '${buildNumber}' deployed on '${branch}' environment.** ᕦ( ̄ ³ ̄)ᕤ\n` +
`**${repositoryName}: Travis build '${buildNumber}' finished on '${branch}' branch.** ᕦ( ̄ ³ ̄)ᕤ\n` +
`- Last commit from: ${author}\n` +
`- Last commit message: ${message}\n` +
`- https://github.com/${repositoryName}/commit/${hash}`
Expand Down

0 comments on commit 702217a

Please sign in to comment.