From 675cacd8733a2121764ea5f59965d4b25400b85b Mon Sep 17 00:00:00 2001 From: Technote Date: Tue, 25 Feb 2020 00:58:21 +0900 Subject: [PATCH 1/5] chore: tweaks --- .gitignore | 1 - src/config.ts | 17 ++++++++--------- src/env.ts | 1 - 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index a128c8b..2d9c420 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,3 @@ /coverage /lib .eslintcache -.work diff --git a/src/config.ts b/src/config.ts index 1c32ab7..d23db94 100644 --- a/src/config.ts +++ b/src/config.ts @@ -12,10 +12,9 @@ export const normalizeConfigKeys = (config: { [key: string]: string }): { [key: }))); /* istanbul ignore next */ -const getActionSettingFile = (): string => - existsSync(resolve(__dirname, '../../release-github-actions/action.yml')) ? - resolve(__dirname, '../../release-github-actions/action.yml') : - resolve(__dirname, '../node_modules/@technote-space/release-github-actions/action.yml'); +const getActionSettingFile = (): string => existsSync(resolve(__dirname, '../../release-github-actions/action.yml')) ? + resolve(__dirname, '../../release-github-actions/action.yml') : + resolve(__dirname, '../node_modules/@technote-space/release-github-actions/action.yml'); export const getActionDefaultInputs = (): { [key: string]: string } => { const actionSetting = yaml.safeLoad(readFileSync(getActionSettingFile(), 'utf8')); @@ -35,6 +34,11 @@ export const getConfig = (dir: string): Config | never => { delete config.OWNER; } + if ('REPO' in config) { + config.repo = config.REPO; + delete config.REPO; + } + if (!('owner' in config)) { config.owner = owner; } @@ -42,11 +46,6 @@ export const getConfig = (dir: string): Config | never => { if (!('repo' in config)) { config.repo = repo; } - - if ('REPO' in config) { - config.repo = config.REPO; - delete config.REPO; - } } return {...config, inputs: {...getActionDefaultInputs(), ...normalizeConfigKeys(config.inputs ?? config.INPUTS ?? {})}}; diff --git a/src/env.ts b/src/env.ts index b176f04..47d6464 100644 --- a/src/env.ts +++ b/src/env.ts @@ -4,7 +4,6 @@ import { Config } from './types'; export const setEnv = (config: Config, token: string, workspace: string): void => { process.env.INPUT_GITHUB_TOKEN = token; process.env.GITHUB_ACTOR = config.owner; - // eslint-disable-next-line no-magic-numbers process.env.GITHUB_WORKSPACE = resolve(process.cwd(), workspace); if (config.inputs) { From 5d082a1324f2a8dcff3554d6efe0cdbaaa20bd33 Mon Sep 17 00:00:00 2001 From: Technote Date: Tue, 25 Feb 2020 01:01:18 +0900 Subject: [PATCH 2/5] feat: use local source for build (#25) --- __tests__/index.test.ts | 20 ++++++------------ src/index.ts | 2 +- src/misc.ts | 7 +++--- src/types.ts | 2 +- src/wrapper.ts | 47 +++++++++++++++++++++++++++++++++++++++-- 5 files changed, 58 insertions(+), 20 deletions(-) diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index c61b08f..7b5c854 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -70,10 +70,7 @@ describe('execute', () => { 'git checkout -b gh-actions origin/gh-actions || :', 'git init \'.\'', 'git checkout --orphan gh-actions', - 'git init \'.\'', - 'git remote add origin \'https://test-owner:token@github.com/test-owner/test-repo.git\' > /dev/null 2>&1 || :', - 'git fetch --no-tags origin \'refs/heads/master:refs/remotes/origin/master\' || :', - 'git checkout -qf FETCH_HEAD', + `rsync -ac -C '--filter=:- .gitignore' --exclude '.git' --exclude '.work' --exclude '.github' --delete './' '${cwd}/__tests__/tmp/.work/build'`, 'rm -rdf node_modules', 'npm install --production', `mv -f '${cwd}/__tests__/tmp/.work/build/action.yaml' '${cwd}/__tests__/tmp/.work/push/action.yml' > /dev/null 2>&1 || :`, @@ -127,13 +124,8 @@ describe('execute', () => { '[command]git checkout --orphan gh-actions', ' >> stdout', '::endgroup::', - '::group::Cloning the remote repo for build...', - '[command]git init \'.\'', - ' >> stdout', - '[command]git remote add origin', - '[command]git fetch --no-tags origin \'refs/heads/master:refs/remotes/origin/master\'', - ' >> stdout', - '[command]git checkout -qf FETCH_HEAD', + '::group::Copying current code to build directory...', + '[command]rsync -ac -C \'--filter=:- .gitignore\' --exclude \'.git\' --exclude \'.work\' --exclude \'.github\' --delete \'./\' \'\'', ' >> stdout', '::endgroup::', '::group::Running build for release...', @@ -206,6 +198,8 @@ describe('execute', () => { 'token', '-t', 'test/v1.2.3', + '-b', + 'release/v1.2.3', '-p', '__tests__/fixtures/test11', '-w', @@ -224,7 +218,7 @@ describe('execute', () => { 'git checkout --orphan gh-actions', 'git init \'.\'', 'git remote add origin \'https://test-owner:token@github.com/test-owner/test-repo.git\' > /dev/null 2>&1 || :', - 'git fetch --no-tags origin \'refs/heads/master:refs/remotes/origin/master\' || :', + 'git fetch --no-tags origin \'refs/heads/release/v1.2.3:refs/remotes/origin/release/v1.2.3\' || :', 'git checkout -qf FETCH_HEAD', 'rm -rdf node_modules', 'npm install --production', @@ -270,7 +264,7 @@ describe('execute', () => { '[command]git init \'.\'', ' >> stdout', '[command]git remote add origin', - '[command]git fetch --no-tags origin \'refs/heads/master:refs/remotes/origin/master\'', + '[command]git fetch --no-tags origin \'refs/heads/release/v1.2.3:refs/remotes/origin/release/v1.2.3\'', ' >> stdout', '[command]git checkout -qf FETCH_HEAD', ' >> stdout', diff --git a/src/index.ts b/src/index.ts index fd46423..d63f36e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,7 +8,7 @@ export const execute = async(): Promise => { commander .requiredOption('--token ', 'token') .requiredOption('-t, --tag ', 'tag name') - .option('-b, --branch [branch]', 'branch name', 'master') + .option('-b, --branch [branch]', 'branch name') .option('-w, --workspace [workspace]', 'working directory name', '.') .option('-p, --package [package]', 'package file directory name', process.cwd()) .option('-n, --dry-run', 'show what would have been pushed') diff --git a/src/misc.ts b/src/misc.ts index 9d9f4e9..64726d7 100644 --- a/src/misc.ts +++ b/src/misc.ts @@ -1,5 +1,6 @@ import { readFileSync } from 'fs'; import { resolve } from 'path'; +import { Context } from '@actions/github/lib/context'; import { GitHelper, Logger } from '@technote-space/github-action-helper'; import { Config, ContextArgs } from './types'; @@ -21,9 +22,9 @@ export const getRepository = (dir: string): { owner: string; repo: string } | ne }; }; -export const getContextArgs = (tagName: string, branch: string, config: Config): ContextArgs => ({...config, tagName, branch}); +export const getContextArgs = (tagName: string, branch: string | undefined, config: Config): ContextArgs => ({...config, tagName, branch}); -export const getContext = (args: ContextArgs): object => ({ +export const getContext = (args: ContextArgs): Context => ({ payload: { action: 'published', release: { @@ -31,7 +32,7 @@ export const getContext = (args: ContextArgs): object => ({ }, }, eventName: 'release', - ref: `refs/heads/${args.branch}`, + ref: `refs/heads/${args.branch || 'master'}`, sha: 'FETCH_HEAD', workflow: '', action: '', diff --git a/src/types.ts b/src/types.ts index 18828e5..b196653 100644 --- a/src/types.ts +++ b/src/types.ts @@ -2,7 +2,7 @@ export type ContextArgs = Readonly<{ owner: string; repo: string; tagName: string; - branch: string; + branch?: string; }>; export type Config = Readonly<{ diff --git a/src/wrapper.ts b/src/wrapper.ts index c3f2525..efd3a6a 100644 --- a/src/wrapper.ts +++ b/src/wrapper.ts @@ -1,4 +1,6 @@ -import { GitHelper } from '@technote-space/github-action-helper'; +import { mkdirSync } from 'fs'; +import { Context } from '@actions/github/lib/context'; +import { GitHelper, Logger, Command } from '@technote-space/github-action-helper'; import * as command from '@technote-space/release-github-actions/lib/utils/command'; import * as misc from '@technote-space/release-github-actions/lib/utils/misc'; import { getContext } from './misc'; @@ -6,7 +8,48 @@ import { ContextArgs } from './types'; export const isValidContext = (args: ContextArgs): boolean => misc.isValidContext(getContext(args)); -export const prepare = async(helper: GitHelper, args: ContextArgs): Promise => command.prepareCommit(helper, getContext(args)); +export const prepareFiles = async(helper: GitHelper, args: ContextArgs, context: Context): Promise => { + const {buildDir, pushDir} = misc.getParams(); + const logger = new Logger(command.replaceDirectory); + mkdirSync(buildDir, {recursive: true}); + + if (args.branch) { + logger.startProcess('Cloning the remote repo for build...'); + await helper.checkout(buildDir, context); + } else { + logger.startProcess('Copying current source to build directory...'); + const com = new Command(logger); + await com.execAsync({ + command: 'rsync', + args: [ + '-ac', + '-C', + '--filter=:- .gitignore', + '--exclude', + '.git', + '--exclude', + '.work', + '--exclude', + '.github', + '--delete', + './', + buildDir, + ], + }); + } + + logger.startProcess('Running build for release...'); + await helper.runCommand(buildDir, misc.getBuildCommands(buildDir, pushDir)); +}; + +export const prepare = async(helper: GitHelper, args: ContextArgs): Promise => { + const context = getContext(args); + await command.clone(helper, context); + await command.checkBranch(await helper.getCurrentBranchName(misc.getParams().pushDir), helper); + await prepareFiles(helper, args, context); + await command.createBuildInfoFile(context); + await command.copyFiles(); +}; export const commit = async(helper: GitHelper): Promise => { await command.config(helper); From 36d9542ad09c9ca63f6a2fb4ba1ae6395664c7f3 Mon Sep 17 00:00:00 2001 From: Technote Date: Tue, 25 Feb 2020 01:51:45 +0900 Subject: [PATCH 3/5] feat: remove working directory --- __tests__/index.test.ts | 8 +++++++- src/wrapper.ts | 13 ++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 7b5c854..91d1c5d 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -64,6 +64,7 @@ describe('execute', () => { await execute(); execCalledWith(mockExec, [ + `rm -rdf ${cwd}/__tests__/tmp/.work/build ${cwd}/__tests__/tmp/.work/push`, 'git init \'.\'', 'git remote add origin \'https://test-owner:token@github.com/test-owner/test-repo.git\' > /dev/null 2>&1 || :', 'git fetch --no-tags origin \'refs/heads/gh-actions:refs/remotes/origin/gh-actions\' || :', @@ -105,6 +106,8 @@ describe('execute', () => { 'git push --tags \'https://test-owner:token@github.com/test-owner/test-repo.git\' \'gh-actions:refs/heads/gh-actions\' > /dev/null 2>&1 || :', ]); stdoutCalledWith(mockStdout, [ + '[command]rm -rdf ', + ' >> stdout', '::group::Fetching...', '[command]git init \'.\'', ' >> stdout', @@ -124,7 +127,7 @@ describe('execute', () => { '[command]git checkout --orphan gh-actions', ' >> stdout', '::endgroup::', - '::group::Copying current code to build directory...', + '::group::Copying current source to build directory...', '[command]rsync -ac -C \'--filter=:- .gitignore\' --exclude \'.git\' --exclude \'.work\' --exclude \'.github\' --delete \'./\' \'\'', ' >> stdout', '::endgroup::', @@ -210,6 +213,7 @@ describe('execute', () => { await execute(); execCalledWith(mockExec, [ + `rm -rdf ${cwd}/__tests__/tmp/.work/build ${cwd}/__tests__/tmp/.work/push`, 'git init \'.\'', 'git remote add origin \'https://test-owner:token@github.com/test-owner/test-repo.git\' > /dev/null 2>&1 || :', 'git fetch --no-tags origin \'refs/heads/gh-actions:refs/remotes/origin/gh-actions\' || :', @@ -241,6 +245,8 @@ describe('execute', () => { 'git show \'--stat-count=10\' HEAD', ]); stdoutCalledWith(mockStdout, [ + '[command]rm -rdf ', + ' >> stdout', '::group::Fetching...', '[command]git init \'.\'', ' >> stdout', diff --git a/src/wrapper.ts b/src/wrapper.ts index efd3a6a..07fbe00 100644 --- a/src/wrapper.ts +++ b/src/wrapper.ts @@ -8,9 +8,8 @@ import { ContextArgs } from './types'; export const isValidContext = (args: ContextArgs): boolean => misc.isValidContext(getContext(args)); -export const prepareFiles = async(helper: GitHelper, args: ContextArgs, context: Context): Promise => { +export const prepareFiles = async(logger: Logger, com: Command, helper: GitHelper, args: ContextArgs, context: Context): Promise => { const {buildDir, pushDir} = misc.getParams(); - const logger = new Logger(command.replaceDirectory); mkdirSync(buildDir, {recursive: true}); if (args.branch) { @@ -18,7 +17,6 @@ export const prepareFiles = async(helper: GitHelper, args: ContextArgs, context: await helper.checkout(buildDir, context); } else { logger.startProcess('Copying current source to build directory...'); - const com = new Command(logger); await com.execAsync({ command: 'rsync', args: [ @@ -43,10 +41,15 @@ export const prepareFiles = async(helper: GitHelper, args: ContextArgs, context: }; export const prepare = async(helper: GitHelper, args: ContextArgs): Promise => { - const context = getContext(args); + const context = getContext(args); + const {buildDir, pushDir} = misc.getParams(); + const logger = new Logger(command.replaceDirectory); + const com = new Command(logger); + + await com.execAsync({command: `rm -rdf ${buildDir} ${pushDir}`}); await command.clone(helper, context); await command.checkBranch(await helper.getCurrentBranchName(misc.getParams().pushDir), helper); - await prepareFiles(helper, args, context); + await prepareFiles(logger, com, helper, args, context); await command.createBuildInfoFile(context); await command.copyFiles(); }; From b7021942a6a2da417be95819429cd7852b5aba90 Mon Sep 17 00:00:00 2001 From: Technote Date: Tue, 25 Feb 2020 02:01:05 +0900 Subject: [PATCH 4/5] feat: Update package version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2388f2c..8e5d3aa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@technote-space/release-github-actions-cli", - "version": "1.0.1", + "version": "1.1.0", "description": "Release GitHub Actions Cli", "author": { "name": "Technote", From bf301f2893c80b2f61888b0ea8d4bf77bce0e511 Mon Sep 17 00:00:00 2001 From: Technote Date: Tue, 25 Feb 2020 02:06:46 +0900 Subject: [PATCH 5/5] chore: update description of branch name args --- README.ja.md | 3 ++- README.md | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.ja.md b/README.ja.md index fae0308..94008ed 100644 --- a/README.ja.md +++ b/README.ja.md @@ -88,7 +88,8 @@ ### オプション #### branch -ビルドに使用するブランチ名 +ビルドに使用するブランチ名 +このオプションが指定されていない場合、現在のソースコードが使用されます。 例: ``` diff --git a/README.md b/README.md index e008117..be31f01 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,7 @@ e.g. ### Option #### branch Branch name to use for build. +If this option is not specified, the current source code will be used. e.g. ```