Skip to content

Commit

Permalink
fix: Merger main into release feature branch
Browse files Browse the repository at this point in the history
  • Loading branch information
sr010 committed Oct 27, 2021
2 parents fbdc36b + a26d6ee commit fbcd3e8
Show file tree
Hide file tree
Showing 58 changed files with 5,225 additions and 282 deletions.
123 changes: 123 additions & 0 deletions .github/scripts/change-log-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
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])/;
const cliCoreChangelogFile = 'CHANGES.md';
const oaiChangelogFile = 'OAI_CHANGES.md';

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

async getAllReleaseVersionsFromGivenDate(date) {
this.logger.info(`Started detecting the versions from the date: ${date}`);
const versions = [];
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;
}
}
}
this.logger.info(`Detected Versions: ${versions}`);
return versions;
}

async getLatestChangelogGeneratedDate() {
this.logger.info('Started detecting the latest date in cli core changelog');
let latestDate;
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.info(`Detected the latest Date: ${latestDate}`);
break;
}
}
return latestDate;
}

async getChangesAfterGivenDate(date) {
this.logger.info(`Started getting the changelog from given date: ${date}`);
let readLines = false;
let fileData = '';
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.info('Reading the lines');
readLines = true;
} else {
this.logger.info(`Changes from OpenAPI specs: ${fileData}`);
break;
}
} else if (readLines) {
fileData += `${line}\n`;
}
}
return fileData;
}

async appendChangesToChangelog() {
this.logger.info('Started appendChangesToChangelog');
try {
const latestDate = await this.getLatestChangelogGeneratedDate(); // changes.md
if (latestDate) {
const changeLog = await this.getChangesAfterGivenDate(latestDate); // oai_changes.md
if (changeLog) {
this.logger.info('Updating the CHANGES.md');
const data = fs.readFileSync(this.cliCoreChangelogFilename);
if (data.toString().includes(changeLog)) {
this.logger.info(`Provided changes are already in cli core changeLog: ${changeLog}`);
return;
}
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;
});
fs.writeFileSync('changeLog.md', changeLog);
}
}
} catch (error) {
this.logger.error(`Error while updating the changelog: ${error.message}`);
throw new Error(error);
}
}

async getReadLiner(filename) {
if (!fs.existsSync(filename)) {
throw new Error(`File not found: ${filename}`);
}
const fileStream = fs.createReadStream(filename);
return readline.createInterface({
input: fileStream,
});
}
}
module.exports = {
ChangeLogHelper,
};
23 changes: 23 additions & 0 deletions .github/scripts/get-version-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* eslint-disable no-console */
const { ChangeLogHelper } = require('./change-log-helper');

const ch = new ChangeLogHelper();

const getVersionType = async () => {
const latestDate = await ch.getLatestChangelogGeneratedDate();
const versions = await ch.getAllReleaseVersionsFromGivenDate(latestDate);
if (versions.length >= 2) {
const version1 = versions[0].split('.');
const version2 = versions[versions.length - 1].split('.');
for (let i = 0; i < 3; i++) {
if (version1[i] !== version2[i]) return i;
}
}
return -1;
};
(async () => {
console.log(await getVersionType());
})();
module.exports = {
getVersionType,
};
47 changes: 47 additions & 0 deletions .github/scripts/trigger-workflow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const core = require('@actions/core');
const { Octokit } = require('@octokit/rest');

/**
* Functionality from benc-uk/workflow-dispatch.
* Link: https://github.com/benc-uk/workflow-dispatch
*/
const triggerWorkflow = async () => {
try {
const octokit = new Octokit({
auth: process.env.REPO_ACCESS_TOKEN,
});
const workflowRef = process.env.WORKFLOW_NAME;
const ref = process.env.BRANCH_NAME;
const [owner, repo] = process.env.REPO_NAME ? process.env.REPO_NAME.split('/') : [null, null];

// Decode inputs, this MUST be a valid JSON string
let inputs = {};
const inputsJson = process.env.INPUTS;
if (inputsJson) {
inputs = JSON.parse(inputsJson);
}

const workflow = await octokit.rest.actions.getWorkflow({
owner,
repo,
workflow_id: workflowRef,
});

core.info(`Workflow id is: ${workflow.data.id}`);

const dispatchResp = await octokit.rest.actions.createWorkflowDispatch({
owner,
repo,
workflow_id: workflow.data.id,
ref,
inputs,
});
core.info(`API response status: ${dispatchResp.status}.`);
} catch (error) {
core.setFailed(error.message);
}
};

module.exports = {
triggerWorkflow,
};
45 changes: 45 additions & 0 deletions .github/scripts/update-api-spec-with-changelog.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/sh
echo "Copying api-definitions"
cp -R ~/oai_definitions/json/. src/services/twilio-api/
echo "Running update changelog script"
node .github/scripts/update-change-log.js
changeLog=''
versionType=-1
if [ -f changeLog.md ]; then
changeLog=$(cat changeLog.md)
rm -rf changeLog.md
if [ "$changeLog" != '' ]; then
changeLog="${changeLog//'%'/'%25'}"
changeLog="${changeLog//$'\n'/'%0A'}"
changeLog="${changeLog//$'\r'/'%0D'}"
versionType=$(node .github/scripts/get-version-type.js | tail -n -1)
fi
fi
echo "Changelog: $changeLog"
echo "Version type: $versionType"
rm -rf OAI_CHANGES.md
echo "Git configurations"
git config --global user.email "team_interfaces+github@twilio.com"
git config --global user.name "twilio-dx"
branch=$(git branch --show-current)
echo "Current branch: $branch"
git add -A
if [ -n "$(git status --porcelain)" ]; then
echo "There are changes to commit.";
commitMessage=''
if [ "$versionType" == 0 ] || [ "$versionType" == 1 ]
then
commitMessage='oaiFeat: Updated api definitions'
elif [ "$versionType" == 2 ]
then
commitMessage='oaiFix: Updated api definitions'
else
echo "Invalid versionType: $versionType";
exit
fi
echo "Commit message:$commitMessage"
git commit -m "$commitMessage"
git push origin "$branch"
else
echo "No changes to commit";
fi
10 changes: 10 additions & 0 deletions .github/scripts/update-change-log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { ChangeLogHelper } = require('./change-log-helper');

const ch = new ChangeLogHelper();

const updateChangeLog = async () => {
return ch.appendChangesToChangelog();
};
(async () => {
await updateChangeLog();
})();
70 changes: 70 additions & 0 deletions .github/scripts/update-release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const core = require('@actions/core');
const { GitHub } = require('@actions/github');

/**
* Functionality from tubone24/update_release.
* Link: https://github.com/tubone24/update_release
*/
const updateRelease = async () => {
try {
const github = new GitHub(process.env.REPO_ACCESS_TOKEN);
const [owner, repo] = process.env.REPO_NAME ? process.env.REPO_NAME.split('/') : [null, null];
const tag = process.env.TAG_NAME;
const getReleaseResponse = await github.repos.getReleaseByTag({
owner,
repo,
tag,
});

const {
data: {
id: oldReleaseId,
html_url: oldHtmlUrl,
upload_url: oldUploadUrl,
body: oldBody,
draft: oldDraft,
name: oldName,
prerelease: oldPrerelease,
},
} = getReleaseResponse;

core.info(`Got release info: '${oldReleaseId}', ${oldName}, '${oldHtmlUrl}', '${oldUploadUrl},'`);
core.info(`Body: ${oldBody}`);
core.info(`Draft: ${oldDraft}, Prerelease: ${oldPrerelease}`);

const newBody = process.env.RELEASE_BODY;
const newPrerelease = process.env.PRE_RELEASE;

let body;
if (newBody === '') {
body = oldBody;
} else {
body = `${oldBody}\n${newBody}`;
}

let prerelease;
if (newPrerelease !== '' && Boolean(newPrerelease)) {
prerelease = newPrerelease === 'true';
} else {
prerelease = oldPrerelease;
}

await github.repos.updateRelease({
owner,
release_id: oldReleaseId,
repo,
body,
name: oldName,
draft: oldDraft,
prerelease,
});

core.info(`Updated release with body: ${body}`);
} catch (error) {
core.setFailed(error.message);
}
};

module.exports = {
updateRelease,
};
55 changes: 55 additions & 0 deletions .github/workflows/cli-core-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Cli Core Tests
on:
push:
branches: [ main ]
pull_request:

jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version: [16.x, 14.x, 10.x]
steps:
- name: Checkout cli core repo
uses: actions/checkout@v2
- run: npm install
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: Run tests
run: npm test
sonarcloud:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
# Disabling shallow clone is recommended for improving relevancy of reporting
fetch-depth: 0
- name: SonarCloud Scan
uses: sonarsource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
notify-complete-fail:
if: ${{ failure() || cancelled() }}
needs: [ test, sonarcloud ]
name: Notify Test Failed
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Slack Notification
uses: rtCamp/action-slack-notify@v2
env:
SLACK_WEBHOOK: ${{ secrets.ALERT_SLACK_WEB_HOOK }}
SLACK_COLOR: ${{ job.status }}
SLACK_USERNAME: CLI Github Actions
SLACK_MSG_AUTHOR: twilio-dx
SLACK_ICON_EMOJI: ':github:'
SLACK_TITLE: "Twilio Cli Core"
SLACK_MESSAGE: 'Cli core tests failed'
MSG_MINIMAL: actions url
SLACK_FOOTER: Posted automatically using GitHub Actions
Loading

0 comments on commit fbcd3e8

Please sign in to comment.