From 03c4ecb2b69d48084fb469bb7c89f80be4d542ff Mon Sep 17 00:00:00 2001 From: Toan Nguyen Date: Tue, 23 Mar 2021 08:23:59 +0700 Subject: [PATCH] feat: check new commits before re-generate models (GHORG-38) * feat: check new commits before re-generate models * fix: use workflow's github token to authorize github api * style: rename hasNewCommitsSinceYesterday to hasNewCommits * refactor: require dotenv before generate models * chore: add GITHUB_TOKEN into env example * refactor: add hour frequency as an environment variable * fix: just set hours number as parameters * fix: change to look back hour --- .env.example | 17 ++++++++++++----- gulpfile.ts | 49 +++++++++++++++++++++++++++++++++++-------------- package.json | 1 + 3 files changed, 48 insertions(+), 19 deletions(-) diff --git a/.env.example b/.env.example index c455f084..5ea25b54 100644 --- a/.env.example +++ b/.env.example @@ -1,14 +1,21 @@ -## get from AWS +# get from AWS AWS_ACCESS_KEY_ID=AKIA... AWS_SECRET_ACCESS_KEY=... -## need to be created +# need to be created ROLE_ARN= -## get from Seller Central / Developer Central account +# get from Seller Central / Developer Central account CLIENT_ID= CLIENT_SECRET= -## self authorize +# self authorize REFRESH_TOKEN= -## set to your preference +# set to your preference API_REGION=eu-west-1 + +# authorize github api when check api model commits +GITHUB_TOKEN= + +# api model's generation lookback. +# Default: 1 +# LOOKBACK_HOURS= diff --git a/gulpfile.ts b/gulpfile.ts index 4e7b89ff..3e77d149 100644 --- a/gulpfile.ts +++ b/gulpfile.ts @@ -1,6 +1,7 @@ import SwaggerParser from '@apidevtools/swagger-parser' import { Octokit } from '@octokit/rest' import { exec, ExecException } from 'child_process' +import env from 'env-var' import fs, { promises as fsPromises } from 'fs' import { task } from 'gulp' import { camelCase, has, isEmpty, upperFirst } from 'lodash' @@ -22,14 +23,32 @@ interface APIModel extends GithubObject { outputPath: string } +const GITHUB_TOKEN: string = env.get('GITHUB_TOKEN').required().asString() +const LOOKBACK_HOURS: number = env.get('LOOKBACK_HOURS').default(1).asIntPositive() + // Using authentication to increases Github API rate limit. -const octokit = new Octokit() +const octokit = new Octokit({ auth: GITHUB_TOKEN }) const OWNER = 'amzn' const REPO = 'selling-partner-api-models' const REDUNDANT_FILES: string[] = ['.gitignore', '.openapi-generator-ignore', 'git_push.sh'] const REDUNDANT_DIRECTORIES: string[] = ['.openapi-generator'] const EXCLUDE_EXPORTED_OBJECTS = new Set(['ErrorList', 'Error']) +async function hasNewCommits(repoPath = 'models'): Promise { + const date = new Date() + date.setHours(date.getHours() - LOOKBACK_HOURS) + + const { data } = await octokit.repos.listCommits({ + owner: OWNER, + repo: REPO, + path: repoPath, + since: date.toISOString(), + per_page: 1, + }) + + return data.length > 0 +} + async function fetchContentsByPath(repoPath = 'models'): Promise { return octokit.repos .getContent({ @@ -147,22 +166,24 @@ function writeStatementsToFile(statements: string[]): void { } async function generateModels() { - const githubDirectories = await fetchContentsByPath() - const githubFilePromises = githubDirectories.map((directory) => - fetchContentsByPath(directory.path), - ) + if (await hasNewCommits()) { + const githubDirectories = await fetchContentsByPath() + const githubFilePromises = githubDirectories.map((directory) => + fetchContentsByPath(directory.path), + ) - const githubFiles = await Promise.all(githubFilePromises) + const githubFiles = await Promise.all(githubFilePromises) - const apiModelGeneratorPromises = githubFiles - .flat() - .map(generateAPIModel) - .map(executeGeneratorCLI) + const apiModelGeneratorPromises = githubFiles + .flat() + .map(generateAPIModel) + .map(executeGeneratorCLI) - const apiModels = await Promise.all(apiModelGeneratorPromises) - await Promise.all(apiModels.map(removeRedundantObjects)) - const statements: string[] = await Promise.all(apiModels.map(generateExportStatement)) - writeStatementsToFile(statements) + const apiModels = await Promise.all(apiModelGeneratorPromises) + await Promise.all(apiModels.map(removeRedundantObjects)) + const statements: string[] = await Promise.all(apiModels.map(generateExportStatement)) + writeStatementsToFile(statements) + } } task(generateModels) diff --git a/package.json b/package.json index b4cf679a..bfb86ce3 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "build": "tsc --build tsconfig.build.json", "clean": "rimraf lib/*", "dev": "ts-node-dev --respawn --transpileOnly src", + "generate-models": "gulp --require dotenv/config generateModels", "lint": "eslint --ext ts,js src/ test/", "lint:fix": "npm run lint -- --fix", "semantic-release": "npx @scaleleap/semantic-release-config",