Skip to content

Commit

Permalink
feat: check new commits before re-generate models (GHORG-38)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nguyentoanit authored Mar 23, 2021
1 parent 78d007e commit 03c4ecb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
17 changes: 12 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
@@ -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=
49 changes: 35 additions & 14 deletions gulpfile.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<boolean> {
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<GithubObject[]> {
return octokit.repos
.getContent({
Expand Down Expand Up @@ -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<APIModel>(apiModelGeneratorPromises)
await Promise.all(apiModels.map(removeRedundantObjects))
const statements: string[] = await Promise.all(apiModels.map(generateExportStatement))
writeStatementsToFile(statements)
const apiModels = await Promise.all<APIModel>(apiModelGeneratorPromises)
await Promise.all(apiModels.map(removeRedundantObjects))
const statements: string[] = await Promise.all(apiModels.map(generateExportStatement))
writeStatementsToFile(statements)
}
}

task(generateModels)
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 03c4ecb

Please sign in to comment.