-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(travis-bot): Fix conversations, move CLI to TypeScript (#387)
- Loading branch information
Showing
3 changed files
with
144 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters