diff --git a/__tests__/utils/command.test.ts b/__tests__/utils/command.test.ts index b4837be..8d9c691 100644 --- a/__tests__/utils/command.test.ts +++ b/__tests__/utils/command.test.ts @@ -158,6 +158,8 @@ describe('checkBranch', () => { }))).toBe(false); execCalledWith(mockExec, [ + 'git rev-parse --abbrev-ref HEAD || :', + 'git checkout hello-world/test-branch || :', 'git rev-parse --abbrev-ref HEAD || :', 'git remote add origin \'https://octocat:test-token@github.com/hello/world.git\' > /dev/null 2>&1 || :', 'git fetch --no-tags origin \'refs/heads/feature/new-feature:refs/remotes/origin/feature/new-feature\' || :', diff --git a/__tests__/utils/process1.test.ts b/__tests__/utils/process1.test.ts index 86d00cd..89a40aa 100644 --- a/__tests__/utils/process1.test.ts +++ b/__tests__/utils/process1.test.ts @@ -202,6 +202,9 @@ describe('execute', () => { '[command]git checkout -b Hello-World/test-21031067 origin/Hello-World/test-21031067', '[command]git rev-parse --abbrev-ref HEAD', ' >> test', + '[command]git checkout Hello-World/test-21031067', + '[command]git rev-parse --abbrev-ref HEAD', + ' >> test', '> remote branch [Hello-World/test-21031067] not found.', '> now branch: test', '::endgroup::', @@ -284,6 +287,10 @@ describe('execute', () => { ' >> stdout', '[command]git rev-parse --abbrev-ref HEAD', ' >> stdout', + '[command]git checkout Hello-World/test-21031067', + ' >> stdout', + '[command]git rev-parse --abbrev-ref HEAD', + ' >> stdout', '> remote branch [Hello-World/test-21031067] not found.', '> now branch: stdout', '::endgroup::', @@ -628,6 +635,10 @@ describe('execute', () => { ' >> stdout', '[command]git rev-parse --abbrev-ref HEAD', ' >> stdout', + '[command]git checkout Hello-World/test-21031067', + ' >> stdout', + '[command]git rev-parse --abbrev-ref HEAD', + ' >> stdout', '> remote branch [Hello-World/test-21031067] not found.', '> now branch: stdout', '::endgroup::', diff --git a/__tests__/utils/process2.test.ts b/__tests__/utils/process2.test.ts index b60c76b..e1f24ba 100644 --- a/__tests__/utils/process2.test.ts +++ b/__tests__/utils/process2.test.ts @@ -122,6 +122,9 @@ describe('execute', () => { '[command]git checkout -b Hello-World/test-21031067 origin/Hello-World/test-21031067', '[command]git rev-parse --abbrev-ref HEAD', ' >> test', + '[command]git checkout Hello-World/test-21031067', + '[command]git rev-parse --abbrev-ref HEAD', + ' >> test', '> remote branch [Hello-World/test-21031067] not found.', '> now branch: test', '::endgroup::', @@ -204,6 +207,9 @@ describe('execute', () => { '[command]git checkout -b Hello-World/test-21031067 origin/Hello-World/test-21031067', '[command]git rev-parse --abbrev-ref HEAD', ' >> test', + '[command]git checkout Hello-World/test-21031067', + '[command]git rev-parse --abbrev-ref HEAD', + ' >> test', '> remote branch [Hello-World/test-21031067] not found.', '> now branch: test', '::endgroup::', @@ -283,6 +289,9 @@ describe('execute', () => { '[command]git checkout -b test/test-1 origin/test/test-1', '[command]git rev-parse --abbrev-ref HEAD', ' >> test', + '[command]git checkout test/test-1', + '[command]git rev-parse --abbrev-ref HEAD', + ' >> test', '> remote branch [test/test-1] not found.', '> now branch: test', '> Cloning [change/new-topic1] from the remote repo...', @@ -309,6 +318,9 @@ describe('execute', () => { '[command]git checkout -b test/test-2 origin/test/test-2', '[command]git rev-parse --abbrev-ref HEAD', ' >> test', + '[command]git checkout test/test-2', + '[command]git rev-parse --abbrev-ref HEAD', + ' >> test', '> remote branch [test/test-2] not found.', '> now branch: test', '> Cloning [change/new-topic2] from the remote repo...', @@ -383,6 +395,9 @@ describe('execute', () => { '[command]git checkout -b test/test-branch origin/test/test-branch', '[command]git rev-parse --abbrev-ref HEAD', ' >> test', + '[command]git checkout test/test-branch', + '[command]git rev-parse --abbrev-ref HEAD', + ' >> test', '> remote branch [test/test-branch] not found.', '> now branch: test', '> Cloning [change/new-topic1] from the remote repo...', @@ -791,6 +806,9 @@ describe('execute', () => { '[command]git checkout -b Hello-World/test-branch origin/Hello-World/test-branch', '[command]git rev-parse --abbrev-ref HEAD', ' >> test', + '[command]git checkout Hello-World/test-branch', + '[command]git rev-parse --abbrev-ref HEAD', + ' >> test', '> remote branch [Hello-World/test-branch] not found.', '> now branch: test', '> Cloning [feature/new-topic3] from the remote repo...', @@ -1050,6 +1068,9 @@ describe('execute', () => { '[command]git checkout -b master origin/master', '[command]git rev-parse --abbrev-ref HEAD', ' >> test', + '[command]git checkout master', + '[command]git rev-parse --abbrev-ref HEAD', + ' >> test', '> remote branch [master] not found.', '> now branch: test', '> Cloning [master] from the remote repo...', @@ -1127,6 +1148,9 @@ describe('execute', () => { '[command]git checkout -b Hello-World/test-branch origin/Hello-World/test-branch', '[command]git rev-parse --abbrev-ref HEAD', ' >> test', + '[command]git checkout Hello-World/test-branch', + '[command]git rev-parse --abbrev-ref HEAD', + ' >> test', '> remote branch [Hello-World/test-branch] not found.', '> now branch: test', '> Cloning [feature/new-topic3] from the remote repo...', @@ -1231,6 +1255,9 @@ describe('execute', () => { '[command]git checkout -b change/test-0 origin/change/test-0', '[command]git rev-parse --abbrev-ref HEAD', ' >> change/new-topic1', + '[command]git checkout change/test-0', + '[command]git rev-parse --abbrev-ref HEAD', + ' >> change/new-topic1', '> remote branch [change/test-0] not found.', '> now branch: change/new-topic1', '> Cloning [master] from the remote repo...', @@ -1304,6 +1331,9 @@ describe('execute', () => { '[command]git checkout -b Hello-World/test-21031067 origin/Hello-World/test-21031067', '[command]git rev-parse --abbrev-ref HEAD', ' >> test', + '[command]git checkout Hello-World/test-21031067', + '[command]git rev-parse --abbrev-ref HEAD', + ' >> test', '> remote branch [Hello-World/test-21031067] not found.', '> now branch: test', '::endgroup::', @@ -1362,6 +1392,7 @@ describe('execute', () => { '::endgroup::', '::group::Switching branch to [test/change]...', '[command]git checkout -b test/change origin/test/change', + '[command]git checkout test/change', ]); }); @@ -1525,6 +1556,10 @@ describe('execute', () => { ' >> stdout', '[command]git rev-parse --abbrev-ref HEAD', ' >> stdout', + '[command]git checkout Hello-World/test-21031067', + ' >> stdout', + '[command]git rev-parse --abbrev-ref HEAD', + ' >> stdout', '> remote branch [Hello-World/test-21031067] not found.', '> now branch: stdout', '::endgroup::', diff --git a/package.json b/package.json index 076ee1f..8b901ca 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@technote-space/github-action-pr-helper", - "version": "2.0.1", + "version": "2.0.2", "description": "PullRequest Helper for GitHub Actions.", "keywords": [ "github", @@ -46,13 +46,13 @@ "@technote-space/github-action-test-helper": "^0.6.4", "@types/jest": "^26.0.15", "@types/node": "^14.14.9", - "@typescript-eslint/eslint-plugin": "^4.8.1", - "@typescript-eslint/parser": "^4.8.1", + "@typescript-eslint/eslint-plugin": "^4.8.2", + "@typescript-eslint/parser": "^4.8.2", "eslint": "^7.14.0", "husky": "^4.3.0", "jest": "^26.6.3", "jest-circus": "^26.6.3", - "lint-staged": "^10.5.1", + "lint-staged": "^10.5.2", "nock": "^13.0.5", "ts-jest": "^26.4.4", "typescript": "^4.1.2" diff --git a/src/utils/command.ts b/src/utils/command.ts index 3b312ac..7b56f99 100644 --- a/src/utils/command.ts +++ b/src/utils/command.ts @@ -50,8 +50,18 @@ export const clone = async(helper: GitHelper, logger: Logger, octokit: Octokit, }; export const checkBranch = async(helper: GitHelper, logger: Logger, octokit: Octokit, context: ActionContext): Promise => { - const clonedBranch = await helper.getCurrentBranchName(getWorkspace()); - const branchName = await getPrBranchName(helper, octokit, context); + let clonedBranch = await helper.getCurrentBranchName(getWorkspace()); + const branchName = await getPrBranchName(helper, octokit, context); + if (branchName !== clonedBranch) { + await helper.runCommand(getWorkspace(), { + command: 'git checkout', + args: [branchName], + suppressError: true, + stderrToStdout: true, + }); + clonedBranch = await helper.getCurrentBranchName(getWorkspace()); + } + if (branchName === clonedBranch) { await helper.runCommand(getWorkspace(), { diff --git a/yarn.lock b/yarn.lock index 3ff537f..ea47076 100644 --- a/yarn.lock +++ b/yarn.lock @@ -32,9 +32,9 @@ "@babel/highlight" "^7.10.4" "@babel/core@^7.1.0", "@babel/core@^7.7.5": - version "7.12.7" - resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.12.7.tgz#bf55363c08c8352a37691f7216ec30090bf7e3bf" - integrity sha512-tRKx9B53kJe8NCGGIxEQb2Bkr0riUIEuN7Sc1fxhs5H8lKlCWUvQCSNMVIB0Meva7hcbCRJ76de15KoLltdoqw== + version "7.12.8" + resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.12.8.tgz#8ad76c1a7d2a6a3beecc4395fa4f7b4cb88390e6" + integrity sha512-ra28JXL+5z73r1IC/t+FT1ApXU5LsulFDnTDntNfLQaScJUJmcHL5Qxm/IWanCToQk3bPWQo5bflbplU5r15pg== dependencies: "@babel/code-frame" "^7.10.4" "@babel/generator" "^7.12.5" @@ -42,7 +42,7 @@ "@babel/helpers" "^7.12.5" "@babel/parser" "^7.12.7" "@babel/template" "^7.12.7" - "@babel/traverse" "^7.12.7" + "@babel/traverse" "^7.12.8" "@babel/types" "^7.12.7" convert-source-map "^1.7.0" debug "^4.1.0" @@ -271,10 +271,10 @@ "@babel/parser" "^7.12.7" "@babel/types" "^7.12.7" -"@babel/traverse@^7.1.0", "@babel/traverse@^7.12.1", "@babel/traverse@^7.12.5", "@babel/traverse@^7.12.7": - version "7.12.7" - resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.12.7.tgz#572a722408681cef17d6b0bef69ef2e728ca69f1" - integrity sha512-nMWaqsQEeSvMNypswUDzjqQ+0rR6pqCtoQpsqGJC4/Khm9cISwPTSpai57F6/jDaOoEGz8yE/WxcO3PV6tKSmQ== +"@babel/traverse@^7.1.0", "@babel/traverse@^7.12.1", "@babel/traverse@^7.12.5", "@babel/traverse@^7.12.8": + version "7.12.8" + resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.12.8.tgz#c1c2983bf9ba0f4f0eaa11dff7e77fa63307b2a4" + integrity sha512-EIRQXPTwFEGRZyu6gXbjfpNORN1oZvwuzJbxcXjAgWV0iqXYDszN1Hx3FVm6YgZfu1ZQbCVAk3l+nIw95Xll9Q== dependencies: "@babel/code-frame" "^7.10.4" "@babel/generator" "^7.12.5" @@ -916,61 +916,61 @@ dependencies: "@types/yargs-parser" "*" -"@typescript-eslint/eslint-plugin@^4.8.1": - version "4.8.1" - resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-4.8.1.tgz#b362abe0ee478a6c6d06c14552a6497f0b480769" - integrity sha512-d7LeQ7dbUrIv5YVFNzGgaW3IQKMmnmKFneRWagRlGYOSfLJVaRbj/FrBNOBC1a3tVO+TgNq1GbHvRtg1kwL0FQ== +"@typescript-eslint/eslint-plugin@^4.8.2": + version "4.8.2" + resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-4.8.2.tgz#cf9102ec800391caa574f589ffe0623cca1d9308" + integrity sha512-gQ06QLV5l1DtvYtqOyFLXD9PdcILYqlrJj2l+CGDlPtmgLUzc1GpqciJFIRvyfvgLALpnxYINFuw+n9AZhPBKQ== dependencies: - "@typescript-eslint/experimental-utils" "4.8.1" - "@typescript-eslint/scope-manager" "4.8.1" + "@typescript-eslint/experimental-utils" "4.8.2" + "@typescript-eslint/scope-manager" "4.8.2" debug "^4.1.1" functional-red-black-tree "^1.0.1" regexpp "^3.0.0" semver "^7.3.2" tsutils "^3.17.1" -"@typescript-eslint/experimental-utils@4.8.1": - version "4.8.1" - resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-4.8.1.tgz#27275c20fa4336df99ebcf6195f7d7aa7aa9f22d" - integrity sha512-WigyLn144R3+lGATXW4nNcDJ9JlTkG8YdBWHkDlN0lC3gUGtDi7Pe3h5GPvFKMcRz8KbZpm9FJV9NTW8CpRHpg== +"@typescript-eslint/experimental-utils@4.8.2": + version "4.8.2" + resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-4.8.2.tgz#8909a5732f19329cf5ef0c39766170476bff5e50" + integrity sha512-hpTw6o6IhBZEsQsjuw/4RWmceRyESfAiEzAEnXHKG1X7S5DXFaZ4IO1JO7CW1aQ604leQBzjZmuMI9QBCAJX8Q== dependencies: "@types/json-schema" "^7.0.3" - "@typescript-eslint/scope-manager" "4.8.1" - "@typescript-eslint/types" "4.8.1" - "@typescript-eslint/typescript-estree" "4.8.1" + "@typescript-eslint/scope-manager" "4.8.2" + "@typescript-eslint/types" "4.8.2" + "@typescript-eslint/typescript-estree" "4.8.2" eslint-scope "^5.0.0" eslint-utils "^2.0.0" -"@typescript-eslint/parser@^4.8.1": - version "4.8.1" - resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-4.8.1.tgz#4fe2fbdbb67485bafc4320b3ae91e34efe1219d1" - integrity sha512-QND8XSVetATHK9y2Ltc/XBl5Ro7Y62YuZKnPEwnNPB8E379fDsvzJ1dMJ46fg/VOmk0hXhatc+GXs5MaXuL5Uw== +"@typescript-eslint/parser@^4.8.2": + version "4.8.2" + resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-4.8.2.tgz#78dccbe5124de2b8dea2d4c363dee9f769151ca8" + integrity sha512-u0leyJqmclYr3KcXOqd2fmx6SDGBO0MUNHHAjr0JS4Crbb3C3d8dwAdlazy133PLCcPn+aOUFiHn72wcuc5wYw== dependencies: - "@typescript-eslint/scope-manager" "4.8.1" - "@typescript-eslint/types" "4.8.1" - "@typescript-eslint/typescript-estree" "4.8.1" + "@typescript-eslint/scope-manager" "4.8.2" + "@typescript-eslint/types" "4.8.2" + "@typescript-eslint/typescript-estree" "4.8.2" debug "^4.1.1" -"@typescript-eslint/scope-manager@4.8.1": - version "4.8.1" - resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-4.8.1.tgz#e343c475f8f1d15801b546cb17d7f309b768fdce" - integrity sha512-r0iUOc41KFFbZdPAdCS4K1mXivnSZqXS5D9oW+iykQsRlTbQRfuFRSW20xKDdYiaCoH+SkSLeIF484g3kWzwOQ== +"@typescript-eslint/scope-manager@4.8.2": + version "4.8.2" + resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-4.8.2.tgz#a18388c63ae9c17adde519384f539392f2c4f0d9" + integrity sha512-qHQ8ODi7mMin4Sq2eh/6eu03uVzsf5TX+J43xRmiq8ujng7ViQSHNPLOHGw/Wr5dFEoxq/ubKhzClIIdQy5q3g== dependencies: - "@typescript-eslint/types" "4.8.1" - "@typescript-eslint/visitor-keys" "4.8.1" + "@typescript-eslint/types" "4.8.2" + "@typescript-eslint/visitor-keys" "4.8.2" -"@typescript-eslint/types@4.8.1": - version "4.8.1" - resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.8.1.tgz#23829c73c5fc6f4fcd5346a7780b274f72fee222" - integrity sha512-ave2a18x2Y25q5K05K/U3JQIe2Av4+TNi/2YuzyaXLAsDx6UZkz1boZ7nR/N6Wwae2PpudTZmHFXqu7faXfHmA== +"@typescript-eslint/types@4.8.2": + version "4.8.2" + resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.8.2.tgz#c862dd0e569d9478eb82d6aee662ea53f5661a36" + integrity sha512-z1/AVcVF8ju5ObaHe2fOpZYEQrwHyZ7PTOlmjd3EoFeX9sv7UekQhfrCmgUO7PruLNfSHrJGQvrW3Q7xQ8EoAw== -"@typescript-eslint/typescript-estree@4.8.1": - version "4.8.1" - resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-4.8.1.tgz#7307e3f2c9e95df7daa8dc0a34b8c43b7ec0dd32" - integrity sha512-bJ6Fn/6tW2g7WIkCWh3QRlaSU7CdUUK52shx36/J7T5oTQzANvi6raoTsbwGM11+7eBbeem8hCCKbyvAc0X3sQ== +"@typescript-eslint/typescript-estree@4.8.2": + version "4.8.2" + resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-4.8.2.tgz#eeec34707d8577600fb21661b5287226cc8b3bed" + integrity sha512-HToGNwI6fekH0dOw3XEVESUm71Onfam0AKin6f26S2FtUmO7o3cLlWgrIaT1q3vjB3wCTdww3Dx2iGq5wtUOCg== dependencies: - "@typescript-eslint/types" "4.8.1" - "@typescript-eslint/visitor-keys" "4.8.1" + "@typescript-eslint/types" "4.8.2" + "@typescript-eslint/visitor-keys" "4.8.2" debug "^4.1.1" globby "^11.0.1" is-glob "^4.0.1" @@ -978,12 +978,12 @@ semver "^7.3.2" tsutils "^3.17.1" -"@typescript-eslint/visitor-keys@4.8.1": - version "4.8.1" - resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-4.8.1.tgz#794f68ee292d1b2e3aa9690ebedfcb3a8c90e3c3" - integrity sha512-3nrwXFdEYALQh/zW8rFwP4QltqsanCDz4CwWMPiIZmwlk9GlvBeueEIbq05SEq4ganqM0g9nh02xXgv5XI3PeQ== +"@typescript-eslint/visitor-keys@4.8.2": + version "4.8.2" + resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-4.8.2.tgz#62cd3fbbbf65f8eccfbe6f159eb1b84a243a3f77" + integrity sha512-Vg+/SJTMZJEKKGHW7YC21QxgKJrSbxoYYd3MEUGtW7zuytHuEcksewq0DUmo4eh/CTNrVJGSdIY9AtRb6riWFw== dependencies: - "@typescript-eslint/types" "4.8.1" + "@typescript-eslint/types" "4.8.2" eslint-visitor-keys "^2.0.0" JSONStream@^1.0.4: @@ -3362,10 +3362,10 @@ lines-and-columns@^1.1.6: resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.1.6.tgz#1c00c743b433cd0a4e80758f7b64a57440d9ff00" integrity sha1-HADHQ7QzzQpOgHWPe2SldEDZ/wA= -lint-staged@^10.5.1: - version "10.5.1" - resolved "https://registry.yarnpkg.com/lint-staged/-/lint-staged-10.5.1.tgz#901e915c2360072dded0e7d752a0d9a49e079daa" - integrity sha512-fTkTGFtwFIJJzn/PbUO3RXyEBHIhbfYBE7+rJyLcOXabViaO/h6OslgeK6zpeUtzkDrzkgyAYDTLAwx6JzDTHw== +lint-staged@^10.5.2: + version "10.5.2" + resolved "https://registry.yarnpkg.com/lint-staged/-/lint-staged-10.5.2.tgz#acfaa0093af3262aee3130b2e22438941530bdd1" + integrity sha512-e8AYR1TDlzwB8VVd38Xu2lXDZf6BcshVqKVuBQThDJRaJLobqKnpbm4dkwJ2puypQNbLr9KF/9mfA649mAGvjA== dependencies: chalk "^4.1.0" cli-truncate "^2.1.0"