Skip to content

Commit

Permalink
chore: structural changes & more fields on Slack message (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
volkanto authored Aug 25, 2021
1 parent 4afcfd3 commit 96d79b8
Show file tree
Hide file tree
Showing 11 changed files with 395 additions and 104 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ enable `yaba` CLI to access your repos.
Now, your personal access token is generated. Copy that token and define that one as an environment variable:

```shell
export GITHUB_ACCESS_TOKEN=generated_personal_access_token
export YABA_GITHUB_ACCESS_TOKEN=generated_personal_access_token
```

You can define that env variable into `~/.bashrc`, `~/.bash_profile` or `~/.zshrc` file, choose which one is suitable
Expand All @@ -52,7 +52,7 @@ If the repository owner is another GitHub account or organisation, you can defin
owner to the command in every run.

```shell
export GITHUB_REPO_OWNER=repository_owner
export YABA_GITHUB_REPO_OWNER=repository_owner
```

Always `-o` or `--owner` has precedence over authenticated user. Presendence is
Expand All @@ -64,13 +64,13 @@ If you want to announce your release/changelog to the specific Slack channel, yo
variable with the appropriate value.

```shell
export SLACK_HOOK_URL=your_slack_hook_url
export YABA_SLACK_HOOK_URL=your_slack_hook_url
```

Also, multiple hook URLs allowed to be defined like below:

```shell
export SLACK_HOOK_URL=your_slack_hook_url_1,your_slack_hook_url_2,...
export YABA_SLACK_HOOK_URL=your_slack_hook_url_1,your_slack_hook_url_2,...
```

If the above variable is set and the `-p` command given while running the command, an announcement will be post to the
Expand Down
33 changes: 33 additions & 0 deletions bin/assets/slack-post-template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"text": ":warning: Release has been created for {repo}!",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "<!here> will release `{repo}` soon!"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": ":rocket: *{repo} Changelog*"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "```{changelog}```"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Visit release page: <{releaseUrl}|{releaseName}>"
}
}
]
}
2 changes: 2 additions & 0 deletions bin/assets/yaba-update-message-template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
New version of Yaba available! {localVersion} -> {lastVersion}
Please run {updateCommand} to update!
103 changes: 65 additions & 38 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -1,78 +1,105 @@
#!/usr/bin/env node

// third party lib definitions
const kleur = require("kleur");

// local variables
const helper = require('./utils/helper.js');
const tool = require('./utils/tool.js');
const flow = require('./utils/flow.js');
const options = require('./utils/command.js').options;

async function run() {
runYaba();

async function runYaba() {

try {

// https://www.npmjs.com/package/tiny-updater OR https://www.npmjs.com/package/update-notifier
// can be used instead below method.
await tool.checkUpdate(); // check if the yaba cli has newer version

await tool.checkUpdate(); // check if the yaba cli has newer version
// check required ENV variables
flow.checkRequiredEnvVariables();

flow.checkRequiredEnvVariables();
// check if the current directory is git repo
if (options.repo == undefined && !helper.isGitRepo()) {
console.log(`The directory '${helper.retrieveCurrentDirectory()}' is not a Git repo.`);
return;
}

checkDirectory()
// check internet connection

await flow.checkInternetConnection();
// prepare username, repoOwner and releaseRepo

const username = await flow.retrieveUsername();
const repoOwner = helper.retrieveOwner(options.owner, username);
const releaseRepo = helper.retrieveReleaseRepo(options.repo);

// fetch latest release
const latestRelease = await flow.fetchLatestRelease(repoOwner, releaseRepo);

// fetch head branch
const headBranch = await flow.fetchHeadBranch(repoOwner, releaseRepo);

if (headBranch == null) {
console.log(kleur.red("Head branch can not be found! The release has been interrupted!"));
return;
}
const headBranch = await checkHeadBranch(repoOwner, releaseRepo);

// preparing the changeLog from the main/master branch if there is no previous release
let changeLog = latestRelease == null
? await flow.listCommits(repoOwner, releaseRepo, headBranch)
: await flow.prepareChangelog(repoOwner, releaseRepo, latestRelease.tag_name, headBranch);
let changeLog = await flow.prepareChangeLog(repoOwner, releaseRepo, headBranch);

// show only changelog
if (changeLog.length != 0 && options.changelog) {
console.log('\n\n' + kleur.green().underline(`${releaseRepo} changelog for upcoming release:`) + `\n\n${helper.prepareChangeLog(options.body, changeLog)}\n`);
if (canShowChangelog(changeLog)) {
printChangelog(releaseRepo, changeLog);
}

// create the release
if (changeLog.length != 0 && !options.changelog) {
const isPermitted = await helper.releaseCreatePermit(options.interactive);
if (isPermitted) {
let preparedChangeLog = helper.prepareChangeLog(options.body, changeLog);
await flow.createRelease(repoOwner, releaseRepo, options.draft, options.releaseName, preparedChangeLog, options.tag);
helper.playSound(options.sound);

// publishes the changelog on slack
flow.publishToSlack(options.publish, releaseRepo, preparedChangeLog);

} else {
console.log('Release was not prepared!');
}
if (canCreateRelease(changeLog)) {
await prepareRelease(changeLog, repoOwner, releaseRepo);
}

// release completed, to prevent hanging forcing to exit
process.exit(1);

} catch (error) {
console.log(error);
}
}

run();
async function prepareRelease(changeLog, repoOwner, releaseRepo) {

const hasReleaseCreatePermission = await helper.releaseCreatePermit(options.interactive);
if (hasReleaseCreatePermission) {
let preparedChangeLog = helper.prepareChangeLog(options.body, changeLog);
const releaseUrl = await flow.createRelease(repoOwner, releaseRepo, options.draft, options.releaseName,
preparedChangeLog, options.tag);

// play yaba sound if the release successfully created
helper.playSound(options.sound);

// publishes the changelog on slack
await flow.publishToSlack(options.publish, releaseRepo, preparedChangeLog, releaseUrl, options.releaseName);

} else {
console.log('Release was not prepared!');
}
}

async function checkHeadBranch(repoOwner, releaseRepo) {
const headBranch = await flow.fetchHeadBranch(repoOwner, releaseRepo);
if (headBranch == null) {
console.log(kleur.red("Head branch can not be found! The release has been interrupted!"));
process.exit();
}
return headBranch;
}

function checkDirectory() {
// check if the current directory is git repo
if (options.repo == undefined && !helper.isGitRepo()) {
console.log(`The directory '${helper.retrieveCurrentDirectory()}' is not a Git repo.`);
process.exit();
}
}

function printChangelog(repoName, changeLog) {
console.log('\n\n' + kleur.green().underline(`${repoName} changelog for upcoming release:`) + `\n\n${helper.prepareChangeLog(options.body, changeLog)}\n`);
}

function canCreateRelease(changeLog) {
return changeLog.length != 0 && !options.changelog;
}

function canShowChangelog(changeLog) {
return changeLog.length != 0 && options.changelog;
}

8 changes: 4 additions & 4 deletions bin/utils/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const package = require('../../package.json');
const commands = yargs
.usage("Usage: yaba -o <owner> -r <repository> -t <tag> -n <release-name> -b <body> -d <draft> " +
"-c <changelog> -i <interactive> -s <sound> -p <publish>")
.option("o", { alias: "owner", describe: "The repository owner.", type: "string" })
.option("r", { alias: "repo", describe: "The repository name.", type: "string" })
.option("t", { alias: "tag", describe: "The name of the tag.", type: "string" })
.option("n", { alias: "release-name", describe: "The name of the release.", type: "string" })
.option("o", {alias: "owner", describe: "The repository owner.", type: "string"})
.option("r", {alias: "repo", describe: "The repository name.", type: "string"})
.option("t", {alias: "tag", describe: "The name of the tag.", type: "string"})
.option("n", {alias: "release-name", describe: "The name of the release.", type: "string"})
.option("b", {
alias: "body",
describe: "Text describing the contents of the tag. If not provided, the default changelog " +
Expand Down
8 changes: 8 additions & 0 deletions bin/utils/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = Object.freeze({
SOUND_PATH: '../assets/yaba.mp3',
SLACK_POST_TEMPLATE: '../assets/slack-post-template.json',
RELEASE_DATE_FORMAT: 'yyyy-MM-dd',
TAG_DATE_FORMAT: 'yyyyMMdd',
UPDATE_COMMAND: 'npm update -g yaba-release-cli',
UPDATE_MESSAGE_TEMPLATE: '../assets/yaba-update-message-template.txt'
});
Loading

0 comments on commit 96d79b8

Please sign in to comment.