Skip to content

Commit

Permalink
fix: Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
lakshmiravalir committed Aug 26, 2021
1 parent 749a316 commit a262359
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 109 deletions.
115 changes: 59 additions & 56 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,63 +31,66 @@ jobs:
outputs:
changeLog: ${{ steps.updateFolder.outputs.changeLog }}
steps:
- run: mkdir -p ~/oai_definitions/json
- run: cd ~/oai_definitions
- run: pwd
- run: ls
- uses: actions/checkout@v2
with:
repository: 'LakshmiRavali/twilio-oai'
token: ${{ secrets.OAI_TOKEN }}
- run: |
cp -R spec/json/. ~/oai_definitions/json/
cp -R CHANGES.md ~/oai_definitions/CHANGES.md
# - run: mkdir -p ~/oai_definitions/json
# - run: cd ~/oai_definitions
# - run: pwd
# - run: ls
# - uses: actions/checkout@v2
# with:
# repository: 'LakshmiRavali/twilio-oai'
# token: ${{ secrets.OAI_TOKEN }}
# - run: |
# cp -R spec/json/. ~/oai_definitions/json/
# cp -R CHANGES.md ~/oai_definitions/CHANGES.md
- uses: actions/checkout@v2
- name: Update OAI folder
id: updateFolder
run: |
cp -R ~/oai_definitions/CHANGES.md OAI_CHANGES.md
source scripts/updateApiDefinitions.sh
echo "::set-output name=changeLog::$changeLog"
release:
runs-on: ubuntu-latest
needs: [updateAPIDefinitions]
steps:
- uses: actions/checkout@v2
- run: git pull
- run: git branch
- run: git log -3
- run: npm install
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: semanticRelease
run: npm install --save-dev @semantic-release/changelog @semantic-release/git
- run: npm ci && npx semantic-release -t \${version}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
updateRelease:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x]
needs: [updateAPIDefinitions, release]
steps:
- uses: actions/checkout@v2
- run: git fetch --tags
- name: Getting tag
id: test
run: |
echo "::set-output name=TAG_NAME::$(git describe --tags $(git rev-list --tags --max-count=1))"
- run: echo "${{steps.test.outputs.TAG_NAME}}"
- name: update release
id: update_release
uses: tubone24/update_release@v1.2.0
env:
GITHUB_TOKEN: ${{ github.token }}
TAG_NAME: ${{steps.test.outputs.TAG_NAME}}
with:
is_append_body: true
body: ${{needs.updateAPIDefinitions.outputs.changeLog}}
echo "::set-output name=changeLog::$(git branch --show-current)"
- run: echo ${{ steps.updateFolder.outputs.changeLog}}
# run: |
# cp -R ~/oai_definitions/CHANGES.md OAI_CHANGES.md
# source scripts/updateApiDefinitions.sh
# echo "::set-output name=changeLog::$changeLog"
# release:
# runs-on: ubuntu-latest
# needs: [updateAPIDefinitions]
# steps:
# - uses: actions/checkout@v2
# - run: git pull
# - run: git branch
# - run: git log -3
# - run: npm install
# - name: Use Node.js ${{ matrix.node-version }}
# uses: actions/setup-node@v2
# with:
# node-version: ${{ matrix.node-version }}
# cache: 'npm'
# - name: semanticRelease
# run: npm install --save-dev @semantic-release/changelog @semantic-release/git
# - run: npm ci && npx semantic-release -t \${version}
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# updateRelease:
# runs-on: ubuntu-latest
# strategy:
# matrix:
# node-version: [10.x]
# needs: [updateAPIDefinitions, release]
# steps:
# - uses: actions/checkout@v2
# - run: git fetch --tags
# - name: Getting tag
# id: test
# run: |
# echo "::set-output name=TAG_NAME::$(git describe --tags $(git rev-list --tags --max-count=1))"
# - run: echo "${{steps.test.outputs.TAG_NAME}}"
# - name: update release
# id: update_release
# uses: tubone24/update_release@v1.2.0
# env:
# GITHUB_TOKEN: ${{ github.token }}
# TAG_NAME: ${{steps.test.outputs.TAG_NAME}}
# with:
# is_append_body: true
# body: ${{needs.updateAPIDefinitions.outputs.changeLog}}
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
},
"devDependencies": {
"@oclif/test": "^1.2.6",
"@semantic-release/changelog": "^5.0.1",
"@semantic-release/git": "^9.0.0",
"@twilio/cli-test": "^2.1.0",
"chai": "^4.2.0",
"eslint": "^7.5.0",
Expand Down Expand Up @@ -88,7 +90,10 @@
[
"@semantic-release/git",
{
"assets": ["CHANGES.md", "package.json"],
"assets": [
"CHANGES.md",
"package.json"
],
"message": "chore(release): set `package.json` to ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
}
]
Expand Down
91 changes: 51 additions & 40 deletions scripts/change-log-helper.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,73 @@
/* eslint-disable no-console */
const fs = require('fs');
const readline = require('readline');

const { logger } = require('../src/services/messaging/logging');

const defaultVersionRegex = /(\d+)\.(\d+)\.(\d+)/;
const defaultDateRegex = /\d{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/;

class ChangeLogHelper {
constructor(versionRegex, dateRegex, cliCoreChangelogFilename, oaiChangelogFilename) {
constructor(
cliCoreChangelogFilename,
oaiChangelogFilename,
versionRegex = defaultVersionRegex,
dateRegex = defaultDateRegex,
) {
this.versionRegex = versionRegex;
this.dateRegex = dateRegex;
this.cliCoreChangelogFilename = cliCoreChangelogFilename;
this.oaiChangelogFilename = oaiChangelogFilename;
this.logger = logger;
}

async getFirstTwoVersions() {
async getAllReleaseVersionsFromGivenDate(date) {
this.logger.debug(`Started detecting the versions from the date: ${date}`);
const versions = [];
const rl = await this.getReadLiner(this.oaiChangelogFilename);
for await (const line of rl) {
const version = this.versionRegex.exec(line);
if (version) {
versions.push(version[0]);
if (versions.length === 2) break;
const readLine = await this.getReadLiner(this.oaiChangelogFilename);
for await (const line of readLine) {
const currentDate = this.dateRegex.exec(line);
if (currentDate) {
const version = this.versionRegex.exec(line);
if (version) {
versions.push(version[0]);
}
if (currentDate[0] <= date) {
break;
}
}
}
/*
* if (versions.length === 2) {
* console.log(versions);
* } else {
* console.log('Invalid versions');
* }
*/
this.logger.debug(`Detected Versions: ${versions}`);
return versions;
}

async getLatestChangelogGeneratedDate() {
this.logger.debug('Started detecting the latest date in cli core changelog');
let latestDate;
const rl = await this.getReadLiner(this.cliCoreChangelogFilename);
for await (const line of rl) {
const readLine = await this.getReadLiner(this.cliCoreChangelogFilename);
for await (const line of readLine) {
latestDate = this.dateRegex.exec(line);
if (latestDate) {
latestDate = latestDate[0];
this.logger.debug(`Detected the latest Date: ${latestDate}`);
break;
}
}

/*
* if (latestDate) {
* console.log(`Detected latest date: ${latestDate}`);
* } else {
* console.log('Did not detect any dates');
* }
*/

return latestDate;
}

async getChangesAfterGivenDate(date) {
this.logger.debug(`Started getting the changelog from given date: ${date}`);
let readLines = false;
let fileData = '';
const rl = await this.getReadLiner(this.oaiChangelogFilename);
for await (const line of rl) {
const readLine = await this.getReadLiner(this.oaiChangelogFilename);
for await (const line of readLine) {
const currentDate = this.dateRegex.exec(line);
if (currentDate) {
if (currentDate[0] > date) {
this.logger.debug('Reading the lines');
readLines = true;
} else {
this.logger.debug(`Changes from OpenAPI specs: ${fileData}`);
break;
}
} else if (readLines) {
Expand All @@ -71,19 +77,25 @@ class ChangeLogHelper {
return fileData;
}

async appendAndGetChangesToChangelog() {
async getAndAppendChangesToChangelog() {
this.logger.debug('Started getAndAppendChangesToChangelog');
const latestDate = await this.getLatestChangelogGeneratedDate(); // changes.md
if (latestDate) {
const changeLog = await this.getChangesAfterGivenDate(latestDate); // oai_changes.md
if (changeLog) {
const data = fs.readFileSync(this.cliCoreChangelogFilename);
const fd = fs.openSync(this.cliCoreChangelogFilename, 'w+');
const insert = Buffer.from(changeLog);
fs.writeSync(fd, insert, 0, insert.length, 0);
fs.writeSync(fd, data, 0, data.length, insert.length);
fs.close(fd, (err) => {
if (err) throw err;
});
try {
this.logger.debug('Updating the CHANGES.md');
const data = fs.readFileSync(this.cliCoreChangelogFilename);
const fd = fs.openSync(this.cliCoreChangelogFilename, 'w+');
const insert = Buffer.from(changeLog);
fs.writeSync(fd, insert, 0, insert.length, 0);
fs.writeSync(fd, data, 0, data.length, insert.length);
fs.close(fd, (err) => {
if (err) throw err;
});
} catch (error) {
this.logger.error(`Error while updating the changelog: ${error}`);
}
return changeLog;
}
}
Expand All @@ -92,10 +104,9 @@ class ChangeLogHelper {

async getReadLiner(filename) {
const fileStream = fs.createReadStream(filename);
const rl = readline.createInterface({
return readline.createInterface({
input: fileStream,
});
return rl;
}
}
module.exports = {
Expand Down
11 changes: 5 additions & 6 deletions scripts/get-version-type.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
/* eslint-disable no-console */
const { ChangeLogHelper } = require('./change-log-helper');

const versionRegex = /(\d+)\.(\d+)\.(\d+)/;
const dateRegex = /\d{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/;
const ch = new ChangeLogHelper(versionRegex, dateRegex, 'CHANGES.md', 'OAI_CHANGES.md');
const ch = new ChangeLogHelper('CHANGES.md', 'OAI_CHANGES.md');

const getVersionType = async () => {
const versions = await ch.getFirstTwoVersions();
if (versions.length === 2) {
const latestDate = await ch.getLatestChangelogGeneratedDate();
const versions = await ch.getAllReleaseVersionsFromGivenDate(latestDate);
if (versions.length >= 2) {
const version1 = versions[0].split('.');
const version2 = versions[1].split('.');
const version2 = versions[versions.length - 1].split('.');
for (let i = 0; i < 3; i++) {
if (version1[i] !== version2[i]) return i;
}
Expand Down
7 changes: 2 additions & 5 deletions scripts/update-api-definitions.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
/* eslint-disable no-console */
const { ChangeLogHelper } = require('./change-log-helper');

const versionRegex = /(\d+)\.(\d+)\.(\d+)/;
const dateRegex = /\d{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/;
const ch = new ChangeLogHelper(versionRegex, dateRegex, 'CHANGES.md', 'OAI_CHANGES.md');
const ch = new ChangeLogHelper('CHANGES.md', 'OAI_CHANGES.md');

const getAndUpdateChangeLog = async () => {
const changeLog = await ch.appendAndGetChangesToChangelog();
return changeLog;
return ch.getAndAppendChangesToChangelog();
};
(async () => {
console.log(await getAndUpdateChangeLog());
Expand Down
3 changes: 2 additions & 1 deletion scripts/updateApiDefinitions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ if [ "$changeLog" != '' ]; then
changeLog="${changeLog//$'\n'/'%0A'}"
changeLog="${changeLog//$'\r'/'%0D'}"
fi
echo "Changelog: $changeLog"
versionType=$(node scripts/get-version-type.js)
echo "Version type: $versionType"
rm -rf OAI_CHANGES.md
Expand All @@ -34,5 +35,5 @@ if [ -n "$(git status --porcelain)" ]; then
git commit -m "$commitMessage"
git push origin "$branch"
else
echo "No OpenAPI changes to commit";
echo "No changes to commit";
fi

0 comments on commit a262359

Please sign in to comment.