Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b7b9abb6ed |
@@ -1,3 +0,0 @@
|
|||||||
dist/
|
|
||||||
lib/
|
|
||||||
node_modules/
|
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
{
|
|
||||||
"plugins": ["jest", "@typescript-eslint"],
|
|
||||||
"extends": ["plugin:github/es6"],
|
|
||||||
"parser": "@typescript-eslint/parser",
|
|
||||||
"parserOptions": {
|
|
||||||
"ecmaVersion": 9,
|
|
||||||
"sourceType": "module",
|
|
||||||
"project": "./tsconfig.json"
|
|
||||||
},
|
|
||||||
"rules": {
|
|
||||||
"eslint-comments/no-use": "off",
|
|
||||||
"import/no-namespace": "off",
|
|
||||||
"no-unused-vars": "off",
|
|
||||||
"@typescript-eslint/no-unused-vars": "error",
|
|
||||||
"@typescript-eslint/explicit-member-accessibility": ["error", {"accessibility": "no-public"}],
|
|
||||||
"@typescript-eslint/no-require-imports": "error",
|
|
||||||
"@typescript-eslint/array-type": "error",
|
|
||||||
"@typescript-eslint/await-thenable": "error",
|
|
||||||
"@typescript-eslint/ban-ts-ignore": "error",
|
|
||||||
"camelcase": "off",
|
|
||||||
"@typescript-eslint/camelcase": "error",
|
|
||||||
"@typescript-eslint/class-name-casing": "error",
|
|
||||||
"@typescript-eslint/explicit-function-return-type": ["error", {"allowExpressions": true}],
|
|
||||||
"@typescript-eslint/func-call-spacing": ["error", "never"],
|
|
||||||
"@typescript-eslint/generic-type-naming": ["error", "^[A-Z][A-Za-z]*$"],
|
|
||||||
"@typescript-eslint/no-array-constructor": "error",
|
|
||||||
"@typescript-eslint/no-empty-interface": "error",
|
|
||||||
"@typescript-eslint/no-explicit-any": "error",
|
|
||||||
"@typescript-eslint/no-extraneous-class": "error",
|
|
||||||
"@typescript-eslint/no-for-in-array": "error",
|
|
||||||
"@typescript-eslint/no-inferrable-types": "error",
|
|
||||||
"@typescript-eslint/no-misused-new": "error",
|
|
||||||
"@typescript-eslint/no-namespace": "error",
|
|
||||||
"@typescript-eslint/no-non-null-assertion": "warn",
|
|
||||||
"@typescript-eslint/no-object-literal-type-assertion": "error",
|
|
||||||
"@typescript-eslint/no-unnecessary-qualifier": "error",
|
|
||||||
"@typescript-eslint/no-unnecessary-type-assertion": "error",
|
|
||||||
"@typescript-eslint/no-useless-constructor": "error",
|
|
||||||
"@typescript-eslint/no-var-requires": "error",
|
|
||||||
"@typescript-eslint/prefer-for-of": "warn",
|
|
||||||
"@typescript-eslint/prefer-function-type": "warn",
|
|
||||||
"@typescript-eslint/prefer-includes": "error",
|
|
||||||
"@typescript-eslint/prefer-interface": "error",
|
|
||||||
"@typescript-eslint/prefer-string-starts-ends-with": "error",
|
|
||||||
"@typescript-eslint/promise-function-async": "error",
|
|
||||||
"@typescript-eslint/require-array-sort-compare": "error",
|
|
||||||
"@typescript-eslint/restrict-plus-operands": "error",
|
|
||||||
"semi": "off",
|
|
||||||
"@typescript-eslint/semi": ["error", "never"],
|
|
||||||
"@typescript-eslint/type-annotation-spacing": "error",
|
|
||||||
"@typescript-eslint/unbound-method": "error"
|
|
||||||
},
|
|
||||||
"env": {
|
|
||||||
"node": true,
|
|
||||||
"es6": true,
|
|
||||||
"jest/globals": true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,207 +0,0 @@
|
|||||||
name: Build and Test
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- releases/*
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/setup-node@v1
|
|
||||||
with:
|
|
||||||
node-version: 12.x
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
- run: npm ci
|
|
||||||
- run: npm run build
|
|
||||||
- run: npm run format-check
|
|
||||||
- run: npm run lint
|
|
||||||
- run: npm test
|
|
||||||
- name: Verify no unstaged changes
|
|
||||||
run: __test__/verify-no-unstaged-changes.sh
|
|
||||||
|
|
||||||
test:
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
runs-on: [ubuntu-latest, macos-latest, windows-latest]
|
|
||||||
runs-on: ${{ matrix.runs-on }}
|
|
||||||
|
|
||||||
steps:
|
|
||||||
# Clone this repo
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
|
|
||||||
# Basic checkout
|
|
||||||
- name: Checkout basic
|
|
||||||
uses: ./
|
|
||||||
with:
|
|
||||||
ref: test-data/v2/basic
|
|
||||||
path: basic
|
|
||||||
- name: Verify basic
|
|
||||||
shell: bash
|
|
||||||
run: __test__/verify-basic.sh
|
|
||||||
|
|
||||||
# Clean
|
|
||||||
- name: Modify work tree
|
|
||||||
shell: bash
|
|
||||||
run: __test__/modify-work-tree.sh
|
|
||||||
- name: Checkout clean
|
|
||||||
uses: ./
|
|
||||||
with:
|
|
||||||
ref: test-data/v2/basic
|
|
||||||
path: basic
|
|
||||||
- name: Verify clean
|
|
||||||
shell: bash
|
|
||||||
run: __test__/verify-clean.sh
|
|
||||||
|
|
||||||
# Side by side
|
|
||||||
- name: Checkout side by side 1
|
|
||||||
uses: ./
|
|
||||||
with:
|
|
||||||
ref: test-data/v2/side-by-side-1
|
|
||||||
path: side-by-side-1
|
|
||||||
- name: Checkout side by side 2
|
|
||||||
uses: ./
|
|
||||||
with:
|
|
||||||
ref: test-data/v2/side-by-side-2
|
|
||||||
path: side-by-side-2
|
|
||||||
- name: Verify side by side
|
|
||||||
shell: bash
|
|
||||||
run: __test__/verify-side-by-side.sh
|
|
||||||
|
|
||||||
# LFS
|
|
||||||
- name: Checkout LFS
|
|
||||||
uses: ./
|
|
||||||
with:
|
|
||||||
repository: actions/checkout # hardcoded, otherwise doesn't work from a fork
|
|
||||||
ref: test-data/v2/lfs
|
|
||||||
path: lfs
|
|
||||||
lfs: true
|
|
||||||
- name: Verify LFS
|
|
||||||
shell: bash
|
|
||||||
run: __test__/verify-lfs.sh
|
|
||||||
|
|
||||||
# Submodules false
|
|
||||||
- name: Checkout submodules false
|
|
||||||
uses: ./
|
|
||||||
with:
|
|
||||||
ref: test-data/v2/submodule-ssh-url
|
|
||||||
path: submodules-false
|
|
||||||
- name: Verify submodules false
|
|
||||||
run: __test__/verify-submodules-false.sh
|
|
||||||
|
|
||||||
# Submodules one level
|
|
||||||
- name: Checkout submodules true
|
|
||||||
uses: ./
|
|
||||||
with:
|
|
||||||
ref: test-data/v2/submodule-ssh-url
|
|
||||||
path: submodules-true
|
|
||||||
submodules: true
|
|
||||||
- name: Verify submodules true
|
|
||||||
run: __test__/verify-submodules-true.sh
|
|
||||||
|
|
||||||
# Submodules recursive
|
|
||||||
- name: Checkout submodules recursive
|
|
||||||
uses: ./
|
|
||||||
with:
|
|
||||||
ref: test-data/v2/submodule-ssh-url
|
|
||||||
path: submodules-recursive
|
|
||||||
submodules: recursive
|
|
||||||
- name: Verify submodules recursive
|
|
||||||
run: __test__/verify-submodules-recursive.sh
|
|
||||||
|
|
||||||
# Basic checkout using REST API
|
|
||||||
- name: Remove basic
|
|
||||||
if: runner.os != 'windows'
|
|
||||||
run: rm -rf basic
|
|
||||||
- name: Remove basic (Windows)
|
|
||||||
if: runner.os == 'windows'
|
|
||||||
shell: cmd
|
|
||||||
run: rmdir /s /q basic
|
|
||||||
- name: Override git version
|
|
||||||
if: runner.os != 'windows'
|
|
||||||
run: __test__/override-git-version.sh
|
|
||||||
- name: Override git version (Windows)
|
|
||||||
if: runner.os == 'windows'
|
|
||||||
run: __test__\\override-git-version.cmd
|
|
||||||
- name: Checkout basic using REST API
|
|
||||||
uses: ./
|
|
||||||
with:
|
|
||||||
ref: test-data/v2/basic
|
|
||||||
path: basic
|
|
||||||
- name: Verify basic
|
|
||||||
run: __test__/verify-basic.sh --archive
|
|
||||||
|
|
||||||
test-proxy:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
container:
|
|
||||||
image: alpine/git:latest
|
|
||||||
options: --dns 127.0.0.1
|
|
||||||
services:
|
|
||||||
squid-proxy:
|
|
||||||
image: datadog/squid:latest
|
|
||||||
ports:
|
|
||||||
- 3128:3128
|
|
||||||
env:
|
|
||||||
https_proxy: http://squid-proxy:3128
|
|
||||||
steps:
|
|
||||||
# Clone this repo
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
|
|
||||||
# Basic checkout using git
|
|
||||||
- name: Checkout basic
|
|
||||||
uses: ./
|
|
||||||
with:
|
|
||||||
ref: test-data/v2/basic
|
|
||||||
path: basic
|
|
||||||
- name: Verify basic
|
|
||||||
run: __test__/verify-basic.sh
|
|
||||||
|
|
||||||
# Basic checkout using REST API
|
|
||||||
- name: Remove basic
|
|
||||||
run: rm -rf basic
|
|
||||||
- name: Override git version
|
|
||||||
run: __test__/override-git-version.sh
|
|
||||||
- name: Basic checkout using REST API
|
|
||||||
uses: ./
|
|
||||||
with:
|
|
||||||
ref: test-data/v2/basic
|
|
||||||
path: basic
|
|
||||||
- name: Verify basic
|
|
||||||
run: __test__/verify-basic.sh --archive
|
|
||||||
|
|
||||||
test-bypass-proxy:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
env:
|
|
||||||
https_proxy: http://no-such-proxy:3128
|
|
||||||
no_proxy: api.github.com,github.com
|
|
||||||
steps:
|
|
||||||
# Clone this repo
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
|
|
||||||
# Basic checkout using git
|
|
||||||
- name: Checkout basic
|
|
||||||
uses: ./
|
|
||||||
with:
|
|
||||||
ref: test-data/v2/basic
|
|
||||||
path: basic
|
|
||||||
- name: Verify basic
|
|
||||||
run: __test__/verify-basic.sh
|
|
||||||
- name: Remove basic
|
|
||||||
run: rm -rf basic
|
|
||||||
|
|
||||||
# Basic checkout using REST API
|
|
||||||
- name: Override git version
|
|
||||||
run: __test__/override-git-version.sh
|
|
||||||
- name: Checkout basic using REST API
|
|
||||||
uses: ./
|
|
||||||
with:
|
|
||||||
ref: test-data/v2/basic
|
|
||||||
path: basic
|
|
||||||
- name: Verify basic
|
|
||||||
run: __test__/verify-basic.sh --archive
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
__test__/_temp
|
|
||||||
lib/
|
|
||||||
node_modules/
|
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
[submodule "submodule-level-1"]
|
||||||
|
path = submodule-level-1
|
||||||
|
url = ./
|
||||||
|
branch = test-data/v2/submodule-level-1
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
dist/
|
|
||||||
lib/
|
|
||||||
node_modules/
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
{
|
|
||||||
"printWidth": 80,
|
|
||||||
"tabWidth": 2,
|
|
||||||
"useTabs": false,
|
|
||||||
"semi": false,
|
|
||||||
"singleQuote": true,
|
|
||||||
"trailingComma": "none",
|
|
||||||
"bracketSpacing": false,
|
|
||||||
"arrowParens": "avoid",
|
|
||||||
"parser": "typescript"
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
# Changelog
|
|
||||||
|
|
||||||
## v2 (beta)
|
|
||||||
|
|
||||||
- Improved fetch performance
|
|
||||||
- The default behavior now fetches only the SHA being checked-out
|
|
||||||
- Script authenticated git commands
|
|
||||||
- Persists `with.token` in the local git config
|
|
||||||
- Enables your scripts to run authenticated git commands
|
|
||||||
- Post-job cleanup removes the token
|
|
||||||
- Coming soon: Opt out by setting `with.persist-credentials` to `false`
|
|
||||||
- Creates a local branch
|
|
||||||
- No longer detached HEAD when checking out a branch
|
|
||||||
- A local branch is created with the corresponding upstream branch set
|
|
||||||
- Improved layout
|
|
||||||
- `with.path` is always relative to `github.workspace`
|
|
||||||
- Aligns better with container actions, where `github.workspace` gets mapped in
|
|
||||||
- Removed input `submodules`
|
|
||||||
|
|
||||||
|
|
||||||
## v1
|
|
||||||
|
|
||||||
Refer [here](https://github.com/actions/checkout/blob/v1/CHANGELOG.md) for the V1 changelog
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
|
|
||||||
The MIT License (MIT)
|
|
||||||
|
|
||||||
Copyright (c) 2018 GitHub, Inc. and contributors
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in
|
|
||||||
all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
THE SOFTWARE.
|
|
||||||
@@ -1,250 +0,0 @@
|
|||||||
<p align="center">
|
|
||||||
<a href="https://github.com/actions/checkout"><img alt="GitHub Actions status" src="https://github.com/actions/checkout/workflows/test-local/badge.svg"></a>
|
|
||||||
</p>
|
|
||||||
|
|
||||||
# Checkout V2
|
|
||||||
|
|
||||||
This action checks-out your repository under `$GITHUB_WORKSPACE`, so your workflow can access it.
|
|
||||||
|
|
||||||
Only a single commit is fetched by default, for the ref/SHA that triggered the workflow. Set `fetch-depth` to fetch more history. Refer [here](https://help.github.com/en/articles/events-that-trigger-workflows) to learn which commit `$GITHUB_SHA` points to for different events.
|
|
||||||
|
|
||||||
The auth token is persisted in the local git config. This enables your scripts to run authenticated git commands. The token is removed during post-job cleanup. Set `persist-credentials: false` to opt-out.
|
|
||||||
|
|
||||||
When Git 2.18 or higher is not in your PATH, falls back to the REST API to download the files.
|
|
||||||
|
|
||||||
# What's new
|
|
||||||
|
|
||||||
- Improved performance
|
|
||||||
- Fetches only a single commit by default
|
|
||||||
- Script authenticated git commands
|
|
||||||
- Auth token persisted in the local git config
|
|
||||||
- Creates a local branch
|
|
||||||
- No longer detached HEAD when checking out a branch
|
|
||||||
- Improved layout
|
|
||||||
- The input `path` is always relative to $GITHUB_WORKSPACE
|
|
||||||
- Aligns better with container actions, where $GITHUB_WORKSPACE gets mapped in
|
|
||||||
- Fallback to REST API download
|
|
||||||
- When Git 2.18 or higher is not in the PATH, the REST API will be used to download the files
|
|
||||||
- When using a job container, the container's PATH is used
|
|
||||||
- Removed input `submodules`
|
|
||||||
|
|
||||||
Refer [here](https://github.com/actions/checkout/blob/v1/README.md) for previous versions.
|
|
||||||
|
|
||||||
# Usage
|
|
||||||
|
|
||||||
<!-- start usage -->
|
|
||||||
```yaml
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
with:
|
|
||||||
# Repository name with owner. For example, actions/checkout
|
|
||||||
# Default: ${{ github.repository }}
|
|
||||||
repository: ''
|
|
||||||
|
|
||||||
# The branch, tag or SHA to checkout. When checking out the repository that
|
|
||||||
# triggered a workflow, this defaults to the reference or SHA for that event.
|
|
||||||
# Otherwise, defaults to `master`.
|
|
||||||
ref: ''
|
|
||||||
|
|
||||||
# Personal access token (PAT) used to fetch the repository. The PAT is configured
|
|
||||||
# with the local git config, which enables your scripts to run authenticated git
|
|
||||||
# commands. The post-job step removes the PAT.
|
|
||||||
#
|
|
||||||
# We recommend using a service account with the least permissions necessary. Also
|
|
||||||
# when generating a new PAT, select the least scopes necessary.
|
|
||||||
#
|
|
||||||
# [Learn more about creating and using encrypted secrets](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets)
|
|
||||||
#
|
|
||||||
# Default: ${{ github.token }}
|
|
||||||
token: ''
|
|
||||||
|
|
||||||
# SSH key used to fetch the repository. The SSH key is configured with the local
|
|
||||||
# git config, which enables your scripts to run authenticated git commands. The
|
|
||||||
# post-job step removes the SSH key.
|
|
||||||
#
|
|
||||||
# We recommend using a service account with the least permissions necessary.
|
|
||||||
#
|
|
||||||
# [Learn more about creating and using encrypted secrets](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets)
|
|
||||||
ssh-key: ''
|
|
||||||
|
|
||||||
# Known hosts in addition to the user and global host key database. The public SSH
|
|
||||||
# keys for a host may be obtained using the utility `ssh-keyscan`. For example,
|
|
||||||
# `ssh-keyscan github.com`. The public key for github.com is always implicitly
|
|
||||||
# added.
|
|
||||||
ssh-known-hosts: ''
|
|
||||||
|
|
||||||
# Whether to perform strict host key checking. When true, adds the options
|
|
||||||
# `StrictHostKeyChecking=yes` and `CheckHostIP=no` to the SSH command line. Use
|
|
||||||
# the input `ssh-known-hosts` to configure additional hosts.
|
|
||||||
# Default: true
|
|
||||||
ssh-strict: ''
|
|
||||||
|
|
||||||
# Whether to configure the token or SSH key with the local git config
|
|
||||||
# Default: true
|
|
||||||
persist-credentials: ''
|
|
||||||
|
|
||||||
# Relative path under $GITHUB_WORKSPACE to place the repository
|
|
||||||
path: ''
|
|
||||||
|
|
||||||
# Whether to execute `git clean -ffdx && git reset --hard HEAD` before fetching
|
|
||||||
# Default: true
|
|
||||||
clean: ''
|
|
||||||
|
|
||||||
# Number of commits to fetch. 0 indicates all history.
|
|
||||||
# Default: 1
|
|
||||||
fetch-depth: ''
|
|
||||||
|
|
||||||
# Whether to download Git-LFS files
|
|
||||||
# Default: false
|
|
||||||
lfs: ''
|
|
||||||
|
|
||||||
# Whether to checkout submodules: `true` to checkout submodules or `recursive` to
|
|
||||||
# recursively checkout submodules.
|
|
||||||
#
|
|
||||||
# When the `ssh-key` input is not provided, SSH URLs beginning with
|
|
||||||
# `git@github.com:` are converted to HTTPS.
|
|
||||||
#
|
|
||||||
# Default: false
|
|
||||||
submodules: ''
|
|
||||||
```
|
|
||||||
<!-- end usage -->
|
|
||||||
|
|
||||||
# Scenarios
|
|
||||||
|
|
||||||
- [Checkout a different branch](#Checkout-a-different-branch)
|
|
||||||
- [Checkout HEAD^](#Checkout-HEAD)
|
|
||||||
- [Checkout multiple repos (side by side)](#Checkout-multiple-repos-side-by-side)
|
|
||||||
- [Checkout multiple repos (nested)](#Checkout-multiple-repos-nested)
|
|
||||||
- [Checkout multiple repos (private)](#Checkout-multiple-repos-private)
|
|
||||||
- [Checkout pull request HEAD commit instead of merge commit](#Checkout-pull-request-HEAD-commit-instead-of-merge-commit)
|
|
||||||
- [Checkout pull request on closed event](#Checkout-pull-request-on-closed-event)
|
|
||||||
- [Checkout submodules](#Checkout-submodules)
|
|
||||||
- [Fetch all tags](#Fetch-all-tags)
|
|
||||||
- [Fetch all branches](#Fetch-all-branches)
|
|
||||||
- [Fetch all history for all tags and branches](#Fetch-all-history-for-all-tags-and-branches)
|
|
||||||
|
|
||||||
## Checkout a different branch
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
with:
|
|
||||||
ref: my-branch
|
|
||||||
```
|
|
||||||
|
|
||||||
## Checkout HEAD^
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
with:
|
|
||||||
fetch-depth: 2
|
|
||||||
- run: git checkout HEAD^
|
|
||||||
```
|
|
||||||
|
|
||||||
## Checkout multiple repos (side by side)
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
with:
|
|
||||||
path: main
|
|
||||||
|
|
||||||
- name: Checkout tools repo
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
with:
|
|
||||||
repository: my-org/my-tools
|
|
||||||
path: my-tools
|
|
||||||
```
|
|
||||||
|
|
||||||
## Checkout multiple repos (nested)
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
|
|
||||||
- name: Checkout tools repo
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
with:
|
|
||||||
repository: my-org/my-tools
|
|
||||||
path: my-tools
|
|
||||||
```
|
|
||||||
|
|
||||||
## Checkout multiple repos (private)
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
with:
|
|
||||||
path: main
|
|
||||||
|
|
||||||
- name: Checkout private tools
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
with:
|
|
||||||
repository: my-org/my-private-tools
|
|
||||||
token: ${{ secrets.GitHub_PAT }} # `GitHub_PAT` is a secret that contains your PAT
|
|
||||||
path: my-tools
|
|
||||||
```
|
|
||||||
|
|
||||||
> - `${{ github.token }}` is scoped to the current repository, so if you want to checkout a different repository that is private you will need to provide your own [PAT](https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line).
|
|
||||||
|
|
||||||
|
|
||||||
## Checkout pull request HEAD commit instead of merge commit
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
with:
|
|
||||||
ref: ${{ github.event.pull_request.head.sha }}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Checkout pull request on closed event
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
branches: [master]
|
|
||||||
types: [opened, synchronize, closed]
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
```
|
|
||||||
|
|
||||||
## Checkout submodules
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
- name: Checkout submodules
|
|
||||||
shell: bash
|
|
||||||
run: |
|
|
||||||
# If your submodules are configured to use SSH instead of HTTPS please uncomment the following line
|
|
||||||
# git config --global url."https://github.com/".insteadOf "git@github.com:"
|
|
||||||
auth_header="$(git config --local --get http.https://github.com/.extraheader)"
|
|
||||||
git submodule sync --recursive
|
|
||||||
git -c "http.extraheader=$auth_header" -c protocol.version=2 submodule update --init --force --recursive --depth=1
|
|
||||||
```
|
|
||||||
|
|
||||||
## Fetch all tags
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
- run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*
|
|
||||||
```
|
|
||||||
|
|
||||||
## Fetch all branches
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
- run: |
|
|
||||||
git fetch --no-tags --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/*
|
|
||||||
```
|
|
||||||
|
|
||||||
## Fetch all history for all tags and branches
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
- run: |
|
|
||||||
git fetch --prune --unshallow
|
|
||||||
```
|
|
||||||
|
|
||||||
# License
|
|
||||||
|
|
||||||
The scripts and documentation in this project are released under the [MIT License](LICENSE)
|
|
||||||
@@ -1,801 +0,0 @@
|
|||||||
import * as core from '@actions/core'
|
|
||||||
import * as fs from 'fs'
|
|
||||||
import * as gitAuthHelper from '../lib/git-auth-helper'
|
|
||||||
import * as io from '@actions/io'
|
|
||||||
import * as os from 'os'
|
|
||||||
import * as path from 'path'
|
|
||||||
import * as stateHelper from '../lib/state-helper'
|
|
||||||
import {IGitCommandManager} from '../lib/git-command-manager'
|
|
||||||
import {IGitSourceSettings} from '../lib/git-source-settings'
|
|
||||||
|
|
||||||
const isWindows = process.platform === 'win32'
|
|
||||||
const testWorkspace = path.join(__dirname, '_temp', 'git-auth-helper')
|
|
||||||
const originalRunnerTemp = process.env['RUNNER_TEMP']
|
|
||||||
const originalHome = process.env['HOME']
|
|
||||||
let workspace: string
|
|
||||||
let localGitConfigPath: string
|
|
||||||
let globalGitConfigPath: string
|
|
||||||
let runnerTemp: string
|
|
||||||
let tempHomedir: string
|
|
||||||
let git: IGitCommandManager & {env: {[key: string]: string}}
|
|
||||||
let settings: IGitSourceSettings
|
|
||||||
let sshPath: string
|
|
||||||
|
|
||||||
describe('git-auth-helper tests', () => {
|
|
||||||
beforeAll(async () => {
|
|
||||||
// SSH
|
|
||||||
sshPath = await io.which('ssh')
|
|
||||||
|
|
||||||
// Clear test workspace
|
|
||||||
await io.rmRF(testWorkspace)
|
|
||||||
})
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
// Mock setSecret
|
|
||||||
jest.spyOn(core, 'setSecret').mockImplementation((secret: string) => {})
|
|
||||||
|
|
||||||
// Mock error/warning/info/debug
|
|
||||||
jest.spyOn(core, 'error').mockImplementation(jest.fn())
|
|
||||||
jest.spyOn(core, 'warning').mockImplementation(jest.fn())
|
|
||||||
jest.spyOn(core, 'info').mockImplementation(jest.fn())
|
|
||||||
jest.spyOn(core, 'debug').mockImplementation(jest.fn())
|
|
||||||
|
|
||||||
// Mock state helper
|
|
||||||
jest.spyOn(stateHelper, 'setSshKeyPath').mockImplementation(jest.fn())
|
|
||||||
jest
|
|
||||||
.spyOn(stateHelper, 'setSshKnownHostsPath')
|
|
||||||
.mockImplementation(jest.fn())
|
|
||||||
})
|
|
||||||
|
|
||||||
afterEach(() => {
|
|
||||||
// Unregister mocks
|
|
||||||
jest.restoreAllMocks()
|
|
||||||
|
|
||||||
// Restore HOME
|
|
||||||
if (originalHome) {
|
|
||||||
process.env['HOME'] = originalHome
|
|
||||||
} else {
|
|
||||||
delete process.env['HOME']
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
afterAll(() => {
|
|
||||||
// Restore RUNNER_TEMP
|
|
||||||
delete process.env['RUNNER_TEMP']
|
|
||||||
if (originalRunnerTemp) {
|
|
||||||
process.env['RUNNER_TEMP'] = originalRunnerTemp
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
const configureAuth_configuresAuthHeader =
|
|
||||||
'configureAuth configures auth header'
|
|
||||||
it(configureAuth_configuresAuthHeader, async () => {
|
|
||||||
// Arrange
|
|
||||||
await setup(configureAuth_configuresAuthHeader)
|
|
||||||
expect(settings.authToken).toBeTruthy() // sanity check
|
|
||||||
const authHelper = gitAuthHelper.createAuthHelper(git, settings)
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await authHelper.configureAuth()
|
|
||||||
|
|
||||||
// Assert config
|
|
||||||
const configContent = (
|
|
||||||
await fs.promises.readFile(localGitConfigPath)
|
|
||||||
).toString()
|
|
||||||
const basicCredential = Buffer.from(
|
|
||||||
`x-access-token:${settings.authToken}`,
|
|
||||||
'utf8'
|
|
||||||
).toString('base64')
|
|
||||||
expect(
|
|
||||||
configContent.indexOf(
|
|
||||||
`http.https://github.com/.extraheader AUTHORIZATION: basic ${basicCredential}`
|
|
||||||
)
|
|
||||||
).toBeGreaterThanOrEqual(0)
|
|
||||||
})
|
|
||||||
|
|
||||||
const configureAuth_configuresAuthHeaderEvenWhenPersistCredentialsFalse =
|
|
||||||
'configureAuth configures auth header even when persist credentials false'
|
|
||||||
it(
|
|
||||||
configureAuth_configuresAuthHeaderEvenWhenPersistCredentialsFalse,
|
|
||||||
async () => {
|
|
||||||
// Arrange
|
|
||||||
await setup(
|
|
||||||
configureAuth_configuresAuthHeaderEvenWhenPersistCredentialsFalse
|
|
||||||
)
|
|
||||||
expect(settings.authToken).toBeTruthy() // sanity check
|
|
||||||
settings.persistCredentials = false
|
|
||||||
const authHelper = gitAuthHelper.createAuthHelper(git, settings)
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await authHelper.configureAuth()
|
|
||||||
|
|
||||||
// Assert config
|
|
||||||
const configContent = (
|
|
||||||
await fs.promises.readFile(localGitConfigPath)
|
|
||||||
).toString()
|
|
||||||
expect(
|
|
||||||
configContent.indexOf(
|
|
||||||
`http.https://github.com/.extraheader AUTHORIZATION`
|
|
||||||
)
|
|
||||||
).toBeGreaterThanOrEqual(0)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
const configureAuth_copiesUserKnownHosts =
|
|
||||||
'configureAuth copies user known hosts'
|
|
||||||
it(configureAuth_copiesUserKnownHosts, async () => {
|
|
||||||
if (!sshPath) {
|
|
||||||
process.stdout.write(
|
|
||||||
`Skipped test "${configureAuth_copiesUserKnownHosts}". Executable 'ssh' not found in the PATH.\n`
|
|
||||||
)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Arange
|
|
||||||
await setup(configureAuth_copiesUserKnownHosts)
|
|
||||||
expect(settings.sshKey).toBeTruthy() // sanity check
|
|
||||||
|
|
||||||
// Mock fs.promises.readFile
|
|
||||||
const realReadFile = fs.promises.readFile
|
|
||||||
jest.spyOn(fs.promises, 'readFile').mockImplementation(
|
|
||||||
async (file: any, options: any): Promise<Buffer> => {
|
|
||||||
const userKnownHostsPath = path.join(
|
|
||||||
os.homedir(),
|
|
||||||
'.ssh',
|
|
||||||
'known_hosts'
|
|
||||||
)
|
|
||||||
if (file === userKnownHostsPath) {
|
|
||||||
return Buffer.from('some-domain.com ssh-rsa ABCDEF')
|
|
||||||
}
|
|
||||||
|
|
||||||
return await realReadFile(file, options)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
// Act
|
|
||||||
const authHelper = gitAuthHelper.createAuthHelper(git, settings)
|
|
||||||
await authHelper.configureAuth()
|
|
||||||
|
|
||||||
// Assert known hosts
|
|
||||||
const actualSshKnownHostsPath = await getActualSshKnownHostsPath()
|
|
||||||
const actualSshKnownHostsContent = (
|
|
||||||
await fs.promises.readFile(actualSshKnownHostsPath)
|
|
||||||
).toString()
|
|
||||||
expect(actualSshKnownHostsContent).toMatch(
|
|
||||||
/some-domain\.com ssh-rsa ABCDEF/
|
|
||||||
)
|
|
||||||
expect(actualSshKnownHostsContent).toMatch(/github\.com ssh-rsa AAAAB3N/)
|
|
||||||
})
|
|
||||||
|
|
||||||
const configureAuth_registersBasicCredentialAsSecret =
|
|
||||||
'configureAuth registers basic credential as secret'
|
|
||||||
it(configureAuth_registersBasicCredentialAsSecret, async () => {
|
|
||||||
// Arrange
|
|
||||||
await setup(configureAuth_registersBasicCredentialAsSecret)
|
|
||||||
expect(settings.authToken).toBeTruthy() // sanity check
|
|
||||||
const authHelper = gitAuthHelper.createAuthHelper(git, settings)
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await authHelper.configureAuth()
|
|
||||||
|
|
||||||
// Assert secret
|
|
||||||
const setSecretSpy = core.setSecret as jest.Mock<any, any>
|
|
||||||
expect(setSecretSpy).toHaveBeenCalledTimes(1)
|
|
||||||
const expectedSecret = Buffer.from(
|
|
||||||
`x-access-token:${settings.authToken}`,
|
|
||||||
'utf8'
|
|
||||||
).toString('base64')
|
|
||||||
expect(setSecretSpy).toHaveBeenCalledWith(expectedSecret)
|
|
||||||
})
|
|
||||||
|
|
||||||
const setsSshCommandEnvVarWhenPersistCredentialsFalse =
|
|
||||||
'sets SSH command env var when persist-credentials false'
|
|
||||||
it(setsSshCommandEnvVarWhenPersistCredentialsFalse, async () => {
|
|
||||||
if (!sshPath) {
|
|
||||||
process.stdout.write(
|
|
||||||
`Skipped test "${setsSshCommandEnvVarWhenPersistCredentialsFalse}". Executable 'ssh' not found in the PATH.\n`
|
|
||||||
)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Arrange
|
|
||||||
await setup(setsSshCommandEnvVarWhenPersistCredentialsFalse)
|
|
||||||
settings.persistCredentials = false
|
|
||||||
const authHelper = gitAuthHelper.createAuthHelper(git, settings)
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await authHelper.configureAuth()
|
|
||||||
|
|
||||||
// Assert git env var
|
|
||||||
const actualKeyPath = await getActualSshKeyPath()
|
|
||||||
const actualKnownHostsPath = await getActualSshKnownHostsPath()
|
|
||||||
const expectedSshCommand = `"${sshPath}" -i "$RUNNER_TEMP/${path.basename(
|
|
||||||
actualKeyPath
|
|
||||||
)}" -o StrictHostKeyChecking=yes -o CheckHostIP=no -o "UserKnownHostsFile=$RUNNER_TEMP/${path.basename(
|
|
||||||
actualKnownHostsPath
|
|
||||||
)}"`
|
|
||||||
expect(git.setEnvironmentVariable).toHaveBeenCalledWith(
|
|
||||||
'GIT_SSH_COMMAND',
|
|
||||||
expectedSshCommand
|
|
||||||
)
|
|
||||||
|
|
||||||
// Asserty git config
|
|
||||||
const gitConfigLines = (await fs.promises.readFile(localGitConfigPath))
|
|
||||||
.toString()
|
|
||||||
.split('\n')
|
|
||||||
.filter(x => x)
|
|
||||||
expect(gitConfigLines).toHaveLength(1)
|
|
||||||
expect(gitConfigLines[0]).toMatch(/^http\./)
|
|
||||||
})
|
|
||||||
|
|
||||||
const configureAuth_setsSshCommandWhenPersistCredentialsTrue =
|
|
||||||
'sets SSH command when persist-credentials true'
|
|
||||||
it(configureAuth_setsSshCommandWhenPersistCredentialsTrue, async () => {
|
|
||||||
if (!sshPath) {
|
|
||||||
process.stdout.write(
|
|
||||||
`Skipped test "${configureAuth_setsSshCommandWhenPersistCredentialsTrue}". Executable 'ssh' not found in the PATH.\n`
|
|
||||||
)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Arrange
|
|
||||||
await setup(configureAuth_setsSshCommandWhenPersistCredentialsTrue)
|
|
||||||
const authHelper = gitAuthHelper.createAuthHelper(git, settings)
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await authHelper.configureAuth()
|
|
||||||
|
|
||||||
// Assert git env var
|
|
||||||
const actualKeyPath = await getActualSshKeyPath()
|
|
||||||
const actualKnownHostsPath = await getActualSshKnownHostsPath()
|
|
||||||
const expectedSshCommand = `"${sshPath}" -i "$RUNNER_TEMP/${path.basename(
|
|
||||||
actualKeyPath
|
|
||||||
)}" -o StrictHostKeyChecking=yes -o CheckHostIP=no -o "UserKnownHostsFile=$RUNNER_TEMP/${path.basename(
|
|
||||||
actualKnownHostsPath
|
|
||||||
)}"`
|
|
||||||
expect(git.setEnvironmentVariable).toHaveBeenCalledWith(
|
|
||||||
'GIT_SSH_COMMAND',
|
|
||||||
expectedSshCommand
|
|
||||||
)
|
|
||||||
|
|
||||||
// Asserty git config
|
|
||||||
expect(git.config).toHaveBeenCalledWith(
|
|
||||||
'core.sshCommand',
|
|
||||||
expectedSshCommand
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
const configureAuth_writesExplicitKnownHosts = 'writes explicit known hosts'
|
|
||||||
it(configureAuth_writesExplicitKnownHosts, async () => {
|
|
||||||
if (!sshPath) {
|
|
||||||
process.stdout.write(
|
|
||||||
`Skipped test "${configureAuth_writesExplicitKnownHosts}". Executable 'ssh' not found in the PATH.\n`
|
|
||||||
)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Arrange
|
|
||||||
await setup(configureAuth_writesExplicitKnownHosts)
|
|
||||||
expect(settings.sshKey).toBeTruthy() // sanity check
|
|
||||||
settings.sshKnownHosts = 'my-custom-host.com ssh-rsa ABC123'
|
|
||||||
const authHelper = gitAuthHelper.createAuthHelper(git, settings)
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await authHelper.configureAuth()
|
|
||||||
|
|
||||||
// Assert known hosts
|
|
||||||
const actualSshKnownHostsPath = await getActualSshKnownHostsPath()
|
|
||||||
const actualSshKnownHostsContent = (
|
|
||||||
await fs.promises.readFile(actualSshKnownHostsPath)
|
|
||||||
).toString()
|
|
||||||
expect(actualSshKnownHostsContent).toMatch(
|
|
||||||
/my-custom-host\.com ssh-rsa ABC123/
|
|
||||||
)
|
|
||||||
expect(actualSshKnownHostsContent).toMatch(/github\.com ssh-rsa AAAAB3N/)
|
|
||||||
})
|
|
||||||
|
|
||||||
const configureAuth_writesSshKeyAndImplicitKnownHosts =
|
|
||||||
'writes SSH key and implicit known hosts'
|
|
||||||
it(configureAuth_writesSshKeyAndImplicitKnownHosts, async () => {
|
|
||||||
if (!sshPath) {
|
|
||||||
process.stdout.write(
|
|
||||||
`Skipped test "${configureAuth_writesSshKeyAndImplicitKnownHosts}". Executable 'ssh' not found in the PATH.\n`
|
|
||||||
)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Arrange
|
|
||||||
await setup(configureAuth_writesSshKeyAndImplicitKnownHosts)
|
|
||||||
expect(settings.sshKey).toBeTruthy() // sanity check
|
|
||||||
const authHelper = gitAuthHelper.createAuthHelper(git, settings)
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await authHelper.configureAuth()
|
|
||||||
|
|
||||||
// Assert SSH key
|
|
||||||
const actualSshKeyPath = await getActualSshKeyPath()
|
|
||||||
expect(actualSshKeyPath).toBeTruthy()
|
|
||||||
const actualSshKeyContent = (
|
|
||||||
await fs.promises.readFile(actualSshKeyPath)
|
|
||||||
).toString()
|
|
||||||
expect(actualSshKeyContent).toBe(settings.sshKey + '\n')
|
|
||||||
if (!isWindows) {
|
|
||||||
// Assert read/write for user, not group or others.
|
|
||||||
// Otherwise SSH client will error.
|
|
||||||
expect((await fs.promises.stat(actualSshKeyPath)).mode & 0o777).toBe(
|
|
||||||
0o600
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Assert known hosts
|
|
||||||
const actualSshKnownHostsPath = await getActualSshKnownHostsPath()
|
|
||||||
const actualSshKnownHostsContent = (
|
|
||||||
await fs.promises.readFile(actualSshKnownHostsPath)
|
|
||||||
).toString()
|
|
||||||
expect(actualSshKnownHostsContent).toMatch(/github\.com ssh-rsa AAAAB3N/)
|
|
||||||
})
|
|
||||||
|
|
||||||
const configureGlobalAuth_configuresUrlInsteadOfWhenSshKeyNotSet =
|
|
||||||
'configureGlobalAuth configures URL insteadOf when SSH key not set'
|
|
||||||
it(configureGlobalAuth_configuresUrlInsteadOfWhenSshKeyNotSet, async () => {
|
|
||||||
// Arrange
|
|
||||||
await setup(configureGlobalAuth_configuresUrlInsteadOfWhenSshKeyNotSet)
|
|
||||||
settings.sshKey = ''
|
|
||||||
const authHelper = gitAuthHelper.createAuthHelper(git, settings)
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await authHelper.configureAuth()
|
|
||||||
await authHelper.configureGlobalAuth()
|
|
||||||
|
|
||||||
// Assert temporary global config
|
|
||||||
expect(git.env['HOME']).toBeTruthy()
|
|
||||||
const configContent = (
|
|
||||||
await fs.promises.readFile(path.join(git.env['HOME'], '.gitconfig'))
|
|
||||||
).toString()
|
|
||||||
expect(
|
|
||||||
configContent.indexOf(`url.https://github.com/.insteadOf git@github.com`)
|
|
||||||
).toBeGreaterThanOrEqual(0)
|
|
||||||
})
|
|
||||||
|
|
||||||
const configureGlobalAuth_copiesGlobalGitConfig =
|
|
||||||
'configureGlobalAuth copies global git config'
|
|
||||||
it(configureGlobalAuth_copiesGlobalGitConfig, async () => {
|
|
||||||
// Arrange
|
|
||||||
await setup(configureGlobalAuth_copiesGlobalGitConfig)
|
|
||||||
await fs.promises.writeFile(globalGitConfigPath, 'value-from-global-config')
|
|
||||||
const authHelper = gitAuthHelper.createAuthHelper(git, settings)
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await authHelper.configureAuth()
|
|
||||||
await authHelper.configureGlobalAuth()
|
|
||||||
|
|
||||||
// Assert original global config not altered
|
|
||||||
let configContent = (
|
|
||||||
await fs.promises.readFile(globalGitConfigPath)
|
|
||||||
).toString()
|
|
||||||
expect(configContent).toBe('value-from-global-config')
|
|
||||||
|
|
||||||
// Assert temporary global config
|
|
||||||
expect(git.env['HOME']).toBeTruthy()
|
|
||||||
const basicCredential = Buffer.from(
|
|
||||||
`x-access-token:${settings.authToken}`,
|
|
||||||
'utf8'
|
|
||||||
).toString('base64')
|
|
||||||
configContent = (
|
|
||||||
await fs.promises.readFile(path.join(git.env['HOME'], '.gitconfig'))
|
|
||||||
).toString()
|
|
||||||
expect(
|
|
||||||
configContent.indexOf('value-from-global-config')
|
|
||||||
).toBeGreaterThanOrEqual(0)
|
|
||||||
expect(
|
|
||||||
configContent.indexOf(
|
|
||||||
`http.https://github.com/.extraheader AUTHORIZATION: basic ${basicCredential}`
|
|
||||||
)
|
|
||||||
).toBeGreaterThanOrEqual(0)
|
|
||||||
})
|
|
||||||
|
|
||||||
const configureGlobalAuth_createsNewGlobalGitConfigWhenGlobalDoesNotExist =
|
|
||||||
'configureGlobalAuth creates new git config when global does not exist'
|
|
||||||
it(
|
|
||||||
configureGlobalAuth_createsNewGlobalGitConfigWhenGlobalDoesNotExist,
|
|
||||||
async () => {
|
|
||||||
// Arrange
|
|
||||||
await setup(
|
|
||||||
configureGlobalAuth_createsNewGlobalGitConfigWhenGlobalDoesNotExist
|
|
||||||
)
|
|
||||||
await io.rmRF(globalGitConfigPath)
|
|
||||||
const authHelper = gitAuthHelper.createAuthHelper(git, settings)
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await authHelper.configureAuth()
|
|
||||||
await authHelper.configureGlobalAuth()
|
|
||||||
|
|
||||||
// Assert original global config not recreated
|
|
||||||
try {
|
|
||||||
await fs.promises.stat(globalGitConfigPath)
|
|
||||||
throw new Error(
|
|
||||||
`Did not expect file to exist: '${globalGitConfigPath}'`
|
|
||||||
)
|
|
||||||
} catch (err) {
|
|
||||||
if (err.code !== 'ENOENT') {
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Assert temporary global config
|
|
||||||
expect(git.env['HOME']).toBeTruthy()
|
|
||||||
const basicCredential = Buffer.from(
|
|
||||||
`x-access-token:${settings.authToken}`,
|
|
||||||
'utf8'
|
|
||||||
).toString('base64')
|
|
||||||
const configContent = (
|
|
||||||
await fs.promises.readFile(path.join(git.env['HOME'], '.gitconfig'))
|
|
||||||
).toString()
|
|
||||||
expect(
|
|
||||||
configContent.indexOf(
|
|
||||||
`http.https://github.com/.extraheader AUTHORIZATION: basic ${basicCredential}`
|
|
||||||
)
|
|
||||||
).toBeGreaterThanOrEqual(0)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
const configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsFalseAndSshKeyNotSet =
|
|
||||||
'configureSubmoduleAuth configures submodules when persist credentials false and SSH key not set'
|
|
||||||
it(
|
|
||||||
configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsFalseAndSshKeyNotSet,
|
|
||||||
async () => {
|
|
||||||
// Arrange
|
|
||||||
await setup(
|
|
||||||
configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsFalseAndSshKeyNotSet
|
|
||||||
)
|
|
||||||
settings.persistCredentials = false
|
|
||||||
settings.sshKey = ''
|
|
||||||
const authHelper = gitAuthHelper.createAuthHelper(git, settings)
|
|
||||||
await authHelper.configureAuth()
|
|
||||||
const mockSubmoduleForeach = git.submoduleForeach as jest.Mock<any, any>
|
|
||||||
mockSubmoduleForeach.mockClear() // reset calls
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await authHelper.configureSubmoduleAuth()
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(mockSubmoduleForeach).toBeCalledTimes(1)
|
|
||||||
expect(mockSubmoduleForeach.mock.calls[0][0] as string).toMatch(
|
|
||||||
/unset-all.*insteadOf/
|
|
||||||
)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
const configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsFalseAndSshKeySet =
|
|
||||||
'configureSubmoduleAuth configures submodules when persist credentials false and SSH key set'
|
|
||||||
it(
|
|
||||||
configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsFalseAndSshKeySet,
|
|
||||||
async () => {
|
|
||||||
if (!sshPath) {
|
|
||||||
process.stdout.write(
|
|
||||||
`Skipped test "${configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsFalseAndSshKeySet}". Executable 'ssh' not found in the PATH.\n`
|
|
||||||
)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Arrange
|
|
||||||
await setup(
|
|
||||||
configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsFalseAndSshKeySet
|
|
||||||
)
|
|
||||||
settings.persistCredentials = false
|
|
||||||
const authHelper = gitAuthHelper.createAuthHelper(git, settings)
|
|
||||||
await authHelper.configureAuth()
|
|
||||||
const mockSubmoduleForeach = git.submoduleForeach as jest.Mock<any, any>
|
|
||||||
mockSubmoduleForeach.mockClear() // reset calls
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await authHelper.configureSubmoduleAuth()
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(mockSubmoduleForeach).toHaveBeenCalledTimes(1)
|
|
||||||
expect(mockSubmoduleForeach.mock.calls[0][0]).toMatch(
|
|
||||||
/unset-all.*insteadOf/
|
|
||||||
)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
const configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsTrueAndSshKeyNotSet =
|
|
||||||
'configureSubmoduleAuth configures submodules when persist credentials true and SSH key not set'
|
|
||||||
it(
|
|
||||||
configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsTrueAndSshKeyNotSet,
|
|
||||||
async () => {
|
|
||||||
// Arrange
|
|
||||||
await setup(
|
|
||||||
configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsTrueAndSshKeyNotSet
|
|
||||||
)
|
|
||||||
settings.sshKey = ''
|
|
||||||
const authHelper = gitAuthHelper.createAuthHelper(git, settings)
|
|
||||||
await authHelper.configureAuth()
|
|
||||||
const mockSubmoduleForeach = git.submoduleForeach as jest.Mock<any, any>
|
|
||||||
mockSubmoduleForeach.mockClear() // reset calls
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await authHelper.configureSubmoduleAuth()
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(mockSubmoduleForeach).toHaveBeenCalledTimes(3)
|
|
||||||
expect(mockSubmoduleForeach.mock.calls[0][0]).toMatch(
|
|
||||||
/unset-all.*insteadOf/
|
|
||||||
)
|
|
||||||
expect(mockSubmoduleForeach.mock.calls[1][0]).toMatch(/http.*extraheader/)
|
|
||||||
expect(mockSubmoduleForeach.mock.calls[2][0]).toMatch(/url.*insteadOf/)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
const configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsTrueAndSshKeySet =
|
|
||||||
'configureSubmoduleAuth configures submodules when persist credentials true and SSH key set'
|
|
||||||
it(
|
|
||||||
configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsTrueAndSshKeySet,
|
|
||||||
async () => {
|
|
||||||
if (!sshPath) {
|
|
||||||
process.stdout.write(
|
|
||||||
`Skipped test "${configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsTrueAndSshKeySet}". Executable 'ssh' not found in the PATH.\n`
|
|
||||||
)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Arrange
|
|
||||||
await setup(
|
|
||||||
configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsTrueAndSshKeySet
|
|
||||||
)
|
|
||||||
const authHelper = gitAuthHelper.createAuthHelper(git, settings)
|
|
||||||
await authHelper.configureAuth()
|
|
||||||
const mockSubmoduleForeach = git.submoduleForeach as jest.Mock<any, any>
|
|
||||||
mockSubmoduleForeach.mockClear() // reset calls
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await authHelper.configureSubmoduleAuth()
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(mockSubmoduleForeach).toHaveBeenCalledTimes(3)
|
|
||||||
expect(mockSubmoduleForeach.mock.calls[0][0]).toMatch(
|
|
||||||
/unset-all.*insteadOf/
|
|
||||||
)
|
|
||||||
expect(mockSubmoduleForeach.mock.calls[1][0]).toMatch(/http.*extraheader/)
|
|
||||||
expect(mockSubmoduleForeach.mock.calls[2][0]).toMatch(/core\.sshCommand/)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
const removeAuth_removesSshCommand = 'removeAuth removes SSH command'
|
|
||||||
it(removeAuth_removesSshCommand, async () => {
|
|
||||||
if (!sshPath) {
|
|
||||||
process.stdout.write(
|
|
||||||
`Skipped test "${removeAuth_removesSshCommand}". Executable 'ssh' not found in the PATH.\n`
|
|
||||||
)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Arrange
|
|
||||||
await setup(removeAuth_removesSshCommand)
|
|
||||||
const authHelper = gitAuthHelper.createAuthHelper(git, settings)
|
|
||||||
await authHelper.configureAuth()
|
|
||||||
let gitConfigContent = (
|
|
||||||
await fs.promises.readFile(localGitConfigPath)
|
|
||||||
).toString()
|
|
||||||
expect(gitConfigContent.indexOf('core.sshCommand')).toBeGreaterThanOrEqual(
|
|
||||||
0
|
|
||||||
) // sanity check
|
|
||||||
const actualKeyPath = await getActualSshKeyPath()
|
|
||||||
expect(actualKeyPath).toBeTruthy()
|
|
||||||
await fs.promises.stat(actualKeyPath)
|
|
||||||
const actualKnownHostsPath = await getActualSshKnownHostsPath()
|
|
||||||
expect(actualKnownHostsPath).toBeTruthy()
|
|
||||||
await fs.promises.stat(actualKnownHostsPath)
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await authHelper.removeAuth()
|
|
||||||
|
|
||||||
// Assert git config
|
|
||||||
gitConfigContent = (
|
|
||||||
await fs.promises.readFile(localGitConfigPath)
|
|
||||||
).toString()
|
|
||||||
expect(gitConfigContent.indexOf('core.sshCommand')).toBeLessThan(0)
|
|
||||||
|
|
||||||
// Assert SSH key file
|
|
||||||
try {
|
|
||||||
await fs.promises.stat(actualKeyPath)
|
|
||||||
throw new Error('SSH key should have been deleted')
|
|
||||||
} catch (err) {
|
|
||||||
if (err.code !== 'ENOENT') {
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Assert known hosts file
|
|
||||||
try {
|
|
||||||
await fs.promises.stat(actualKnownHostsPath)
|
|
||||||
throw new Error('SSH known hosts should have been deleted')
|
|
||||||
} catch (err) {
|
|
||||||
if (err.code !== 'ENOENT') {
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
const removeAuth_removesToken = 'removeAuth removes token'
|
|
||||||
it(removeAuth_removesToken, async () => {
|
|
||||||
// Arrange
|
|
||||||
await setup(removeAuth_removesToken)
|
|
||||||
const authHelper = gitAuthHelper.createAuthHelper(git, settings)
|
|
||||||
await authHelper.configureAuth()
|
|
||||||
let gitConfigContent = (
|
|
||||||
await fs.promises.readFile(localGitConfigPath)
|
|
||||||
).toString()
|
|
||||||
expect(gitConfigContent.indexOf('http.')).toBeGreaterThanOrEqual(0) // sanity check
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await authHelper.removeAuth()
|
|
||||||
|
|
||||||
// Assert git config
|
|
||||||
gitConfigContent = (
|
|
||||||
await fs.promises.readFile(localGitConfigPath)
|
|
||||||
).toString()
|
|
||||||
expect(gitConfigContent.indexOf('http.')).toBeLessThan(0)
|
|
||||||
})
|
|
||||||
|
|
||||||
const removeGlobalAuth_removesOverride = 'removeGlobalAuth removes override'
|
|
||||||
it(removeGlobalAuth_removesOverride, async () => {
|
|
||||||
// Arrange
|
|
||||||
await setup(removeGlobalAuth_removesOverride)
|
|
||||||
const authHelper = gitAuthHelper.createAuthHelper(git, settings)
|
|
||||||
await authHelper.configureAuth()
|
|
||||||
await authHelper.configureGlobalAuth()
|
|
||||||
const homeOverride = git.env['HOME'] // Sanity check
|
|
||||||
expect(homeOverride).toBeTruthy()
|
|
||||||
await fs.promises.stat(path.join(git.env['HOME'], '.gitconfig'))
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await authHelper.removeGlobalAuth()
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
expect(git.env['HOME']).toBeUndefined()
|
|
||||||
try {
|
|
||||||
await fs.promises.stat(homeOverride)
|
|
||||||
throw new Error(`Should have been deleted '${homeOverride}'`)
|
|
||||||
} catch (err) {
|
|
||||||
if (err.code !== 'ENOENT') {
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
async function setup(testName: string): Promise<void> {
|
|
||||||
testName = testName.replace(/[^a-zA-Z0-9_]+/g, '-')
|
|
||||||
|
|
||||||
// Directories
|
|
||||||
workspace = path.join(testWorkspace, testName, 'workspace')
|
|
||||||
runnerTemp = path.join(testWorkspace, testName, 'runner-temp')
|
|
||||||
tempHomedir = path.join(testWorkspace, testName, 'home-dir')
|
|
||||||
await fs.promises.mkdir(workspace, {recursive: true})
|
|
||||||
await fs.promises.mkdir(runnerTemp, {recursive: true})
|
|
||||||
await fs.promises.mkdir(tempHomedir, {recursive: true})
|
|
||||||
process.env['RUNNER_TEMP'] = runnerTemp
|
|
||||||
process.env['HOME'] = tempHomedir
|
|
||||||
|
|
||||||
// Create git config
|
|
||||||
globalGitConfigPath = path.join(tempHomedir, '.gitconfig')
|
|
||||||
await fs.promises.writeFile(globalGitConfigPath, '')
|
|
||||||
localGitConfigPath = path.join(workspace, '.git', 'config')
|
|
||||||
await fs.promises.mkdir(path.dirname(localGitConfigPath), {recursive: true})
|
|
||||||
await fs.promises.writeFile(localGitConfigPath, '')
|
|
||||||
|
|
||||||
git = {
|
|
||||||
branchDelete: jest.fn(),
|
|
||||||
branchExists: jest.fn(),
|
|
||||||
branchList: jest.fn(),
|
|
||||||
checkout: jest.fn(),
|
|
||||||
checkoutDetach: jest.fn(),
|
|
||||||
config: jest.fn(
|
|
||||||
async (key: string, value: string, globalConfig?: boolean) => {
|
|
||||||
const configPath = globalConfig
|
|
||||||
? path.join(git.env['HOME'] || tempHomedir, '.gitconfig')
|
|
||||||
: localGitConfigPath
|
|
||||||
await fs.promises.appendFile(configPath, `\n${key} ${value}`)
|
|
||||||
}
|
|
||||||
),
|
|
||||||
configExists: jest.fn(
|
|
||||||
async (key: string, globalConfig?: boolean): Promise<boolean> => {
|
|
||||||
const configPath = globalConfig
|
|
||||||
? path.join(git.env['HOME'] || tempHomedir, '.gitconfig')
|
|
||||||
: localGitConfigPath
|
|
||||||
const content = await fs.promises.readFile(configPath)
|
|
||||||
const lines = content
|
|
||||||
.toString()
|
|
||||||
.split('\n')
|
|
||||||
.filter(x => x)
|
|
||||||
return lines.some(x => x.startsWith(key))
|
|
||||||
}
|
|
||||||
),
|
|
||||||
env: {},
|
|
||||||
fetch: jest.fn(),
|
|
||||||
getWorkingDirectory: jest.fn(() => workspace),
|
|
||||||
init: jest.fn(),
|
|
||||||
isDetached: jest.fn(),
|
|
||||||
lfsFetch: jest.fn(),
|
|
||||||
lfsInstall: jest.fn(),
|
|
||||||
log1: jest.fn(),
|
|
||||||
remoteAdd: jest.fn(),
|
|
||||||
removeEnvironmentVariable: jest.fn((name: string) => delete git.env[name]),
|
|
||||||
setEnvironmentVariable: jest.fn((name: string, value: string) => {
|
|
||||||
git.env[name] = value
|
|
||||||
}),
|
|
||||||
setRemoteUrl: jest.fn(),
|
|
||||||
submoduleForeach: jest.fn(async () => {
|
|
||||||
return ''
|
|
||||||
}),
|
|
||||||
submoduleSync: jest.fn(),
|
|
||||||
submoduleUpdate: jest.fn(),
|
|
||||||
tagExists: jest.fn(),
|
|
||||||
tryClean: jest.fn(),
|
|
||||||
tryConfigUnset: jest.fn(
|
|
||||||
async (key: string, globalConfig?: boolean): Promise<boolean> => {
|
|
||||||
const configPath = globalConfig
|
|
||||||
? path.join(git.env['HOME'] || tempHomedir, '.gitconfig')
|
|
||||||
: localGitConfigPath
|
|
||||||
let content = await fs.promises.readFile(configPath)
|
|
||||||
let lines = content
|
|
||||||
.toString()
|
|
||||||
.split('\n')
|
|
||||||
.filter(x => x)
|
|
||||||
.filter(x => !x.startsWith(key))
|
|
||||||
await fs.promises.writeFile(configPath, lines.join('\n'))
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
),
|
|
||||||
tryDisableAutomaticGarbageCollection: jest.fn(),
|
|
||||||
tryGetRemoteUrl: jest.fn(),
|
|
||||||
tryReset: jest.fn()
|
|
||||||
}
|
|
||||||
|
|
||||||
settings = {
|
|
||||||
authToken: 'some auth token',
|
|
||||||
clean: true,
|
|
||||||
commit: '',
|
|
||||||
fetchDepth: 1,
|
|
||||||
isWorkflowRepository: true,
|
|
||||||
lfs: false,
|
|
||||||
submodules: false,
|
|
||||||
nestedSubmodules: false,
|
|
||||||
persistCredentials: true,
|
|
||||||
ref: 'refs/heads/master',
|
|
||||||
repositoryName: 'my-repo',
|
|
||||||
repositoryOwner: 'my-org',
|
|
||||||
repositoryPath: '',
|
|
||||||
sshKey: sshPath ? 'some ssh private key' : '',
|
|
||||||
sshKnownHosts: '',
|
|
||||||
sshStrict: true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getActualSshKeyPath(): Promise<string> {
|
|
||||||
let actualTempFiles = (await fs.promises.readdir(runnerTemp))
|
|
||||||
.sort()
|
|
||||||
.map(x => path.join(runnerTemp, x))
|
|
||||||
if (actualTempFiles.length === 0) {
|
|
||||||
return ''
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(actualTempFiles).toHaveLength(2)
|
|
||||||
expect(actualTempFiles[0].endsWith('_known_hosts')).toBeFalsy()
|
|
||||||
return actualTempFiles[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getActualSshKnownHostsPath(): Promise<string> {
|
|
||||||
let actualTempFiles = (await fs.promises.readdir(runnerTemp))
|
|
||||||
.sort()
|
|
||||||
.map(x => path.join(runnerTemp, x))
|
|
||||||
if (actualTempFiles.length === 0) {
|
|
||||||
return ''
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(actualTempFiles).toHaveLength(2)
|
|
||||||
expect(actualTempFiles[1].endsWith('_known_hosts')).toBeTruthy()
|
|
||||||
expect(actualTempFiles[1].startsWith(actualTempFiles[0])).toBeTruthy()
|
|
||||||
return actualTempFiles[1]
|
|
||||||
}
|
|
||||||
@@ -1,425 +0,0 @@
|
|||||||
import * as core from '@actions/core'
|
|
||||||
import * as fs from 'fs'
|
|
||||||
import * as gitDirectoryHelper from '../lib/git-directory-helper'
|
|
||||||
import * as io from '@actions/io'
|
|
||||||
import * as path from 'path'
|
|
||||||
import {IGitCommandManager} from '../lib/git-command-manager'
|
|
||||||
|
|
||||||
const testWorkspace = path.join(__dirname, '_temp', 'git-directory-helper')
|
|
||||||
let repositoryPath: string
|
|
||||||
let httpsUrl: string
|
|
||||||
let sshUrl: string
|
|
||||||
let clean: boolean
|
|
||||||
let git: IGitCommandManager
|
|
||||||
|
|
||||||
describe('git-directory-helper tests', () => {
|
|
||||||
beforeAll(async () => {
|
|
||||||
// Clear test workspace
|
|
||||||
await io.rmRF(testWorkspace)
|
|
||||||
})
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
// Mock error/warning/info/debug
|
|
||||||
jest.spyOn(core, 'error').mockImplementation(jest.fn())
|
|
||||||
jest.spyOn(core, 'warning').mockImplementation(jest.fn())
|
|
||||||
jest.spyOn(core, 'info').mockImplementation(jest.fn())
|
|
||||||
jest.spyOn(core, 'debug').mockImplementation(jest.fn())
|
|
||||||
})
|
|
||||||
|
|
||||||
afterEach(() => {
|
|
||||||
// Unregister mocks
|
|
||||||
jest.restoreAllMocks()
|
|
||||||
})
|
|
||||||
|
|
||||||
const cleansWhenCleanTrue = 'cleans when clean true'
|
|
||||||
it(cleansWhenCleanTrue, async () => {
|
|
||||||
// Arrange
|
|
||||||
await setup(cleansWhenCleanTrue)
|
|
||||||
await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await gitDirectoryHelper.prepareExistingDirectory(
|
|
||||||
git,
|
|
||||||
repositoryPath,
|
|
||||||
httpsUrl,
|
|
||||||
[httpsUrl, sshUrl],
|
|
||||||
clean
|
|
||||||
)
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
const files = await fs.promises.readdir(repositoryPath)
|
|
||||||
expect(files.sort()).toEqual(['.git', 'my-file'])
|
|
||||||
expect(git.tryClean).toHaveBeenCalled()
|
|
||||||
expect(git.tryReset).toHaveBeenCalled()
|
|
||||||
expect(core.warning).not.toHaveBeenCalled()
|
|
||||||
})
|
|
||||||
|
|
||||||
const checkoutDetachWhenNotDetached = 'checkout detach when not detached'
|
|
||||||
it(checkoutDetachWhenNotDetached, async () => {
|
|
||||||
// Arrange
|
|
||||||
await setup(checkoutDetachWhenNotDetached)
|
|
||||||
await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await gitDirectoryHelper.prepareExistingDirectory(
|
|
||||||
git,
|
|
||||||
repositoryPath,
|
|
||||||
httpsUrl,
|
|
||||||
[httpsUrl, sshUrl],
|
|
||||||
clean
|
|
||||||
)
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
const files = await fs.promises.readdir(repositoryPath)
|
|
||||||
expect(files.sort()).toEqual(['.git', 'my-file'])
|
|
||||||
expect(git.checkoutDetach).toHaveBeenCalled()
|
|
||||||
})
|
|
||||||
|
|
||||||
const doesNotCheckoutDetachWhenNotAlreadyDetached =
|
|
||||||
'does not checkout detach when already detached'
|
|
||||||
it(doesNotCheckoutDetachWhenNotAlreadyDetached, async () => {
|
|
||||||
// Arrange
|
|
||||||
await setup(doesNotCheckoutDetachWhenNotAlreadyDetached)
|
|
||||||
await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
|
|
||||||
const mockIsDetached = git.isDetached as jest.Mock<any, any>
|
|
||||||
mockIsDetached.mockImplementation(async () => {
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await gitDirectoryHelper.prepareExistingDirectory(
|
|
||||||
git,
|
|
||||||
repositoryPath,
|
|
||||||
httpsUrl,
|
|
||||||
[httpsUrl, sshUrl],
|
|
||||||
clean
|
|
||||||
)
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
const files = await fs.promises.readdir(repositoryPath)
|
|
||||||
expect(files.sort()).toEqual(['.git', 'my-file'])
|
|
||||||
expect(git.checkoutDetach).not.toHaveBeenCalled()
|
|
||||||
})
|
|
||||||
|
|
||||||
const doesNotCleanWhenCleanFalse = 'does not clean when clean false'
|
|
||||||
it(doesNotCleanWhenCleanFalse, async () => {
|
|
||||||
// Arrange
|
|
||||||
await setup(doesNotCleanWhenCleanFalse)
|
|
||||||
clean = false
|
|
||||||
await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await gitDirectoryHelper.prepareExistingDirectory(
|
|
||||||
git,
|
|
||||||
repositoryPath,
|
|
||||||
httpsUrl,
|
|
||||||
[httpsUrl, sshUrl],
|
|
||||||
clean
|
|
||||||
)
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
const files = await fs.promises.readdir(repositoryPath)
|
|
||||||
expect(files.sort()).toEqual(['.git', 'my-file'])
|
|
||||||
expect(git.isDetached).toHaveBeenCalled()
|
|
||||||
expect(git.branchList).toHaveBeenCalled()
|
|
||||||
expect(core.warning).not.toHaveBeenCalled()
|
|
||||||
expect(git.tryClean).not.toHaveBeenCalled()
|
|
||||||
expect(git.tryReset).not.toHaveBeenCalled()
|
|
||||||
})
|
|
||||||
|
|
||||||
const removesContentsWhenCleanFails = 'removes contents when clean fails'
|
|
||||||
it(removesContentsWhenCleanFails, async () => {
|
|
||||||
// Arrange
|
|
||||||
await setup(removesContentsWhenCleanFails)
|
|
||||||
await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
|
|
||||||
let mockTryClean = git.tryClean as jest.Mock<any, any>
|
|
||||||
mockTryClean.mockImplementation(async () => {
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await gitDirectoryHelper.prepareExistingDirectory(
|
|
||||||
git,
|
|
||||||
repositoryPath,
|
|
||||||
httpsUrl,
|
|
||||||
[httpsUrl, sshUrl],
|
|
||||||
clean
|
|
||||||
)
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
const files = await fs.promises.readdir(repositoryPath)
|
|
||||||
expect(files).toHaveLength(0)
|
|
||||||
expect(git.tryClean).toHaveBeenCalled()
|
|
||||||
expect(core.warning).toHaveBeenCalled()
|
|
||||||
expect(git.tryReset).not.toHaveBeenCalled()
|
|
||||||
})
|
|
||||||
|
|
||||||
const removesContentsWhenDifferentRepositoryUrl =
|
|
||||||
'removes contents when different repository url'
|
|
||||||
it(removesContentsWhenDifferentRepositoryUrl, async () => {
|
|
||||||
// Arrange
|
|
||||||
await setup(removesContentsWhenDifferentRepositoryUrl)
|
|
||||||
clean = false
|
|
||||||
await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
|
|
||||||
const differentRemoteUrl =
|
|
||||||
'https://github.com/my-different-org/my-different-repo'
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await gitDirectoryHelper.prepareExistingDirectory(
|
|
||||||
git,
|
|
||||||
repositoryPath,
|
|
||||||
differentRemoteUrl,
|
|
||||||
[differentRemoteUrl],
|
|
||||||
clean
|
|
||||||
)
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
const files = await fs.promises.readdir(repositoryPath)
|
|
||||||
expect(files).toHaveLength(0)
|
|
||||||
expect(core.warning).not.toHaveBeenCalled()
|
|
||||||
expect(git.isDetached).not.toHaveBeenCalled()
|
|
||||||
})
|
|
||||||
|
|
||||||
const removesContentsWhenNoGitDirectory =
|
|
||||||
'removes contents when no git directory'
|
|
||||||
it(removesContentsWhenNoGitDirectory, async () => {
|
|
||||||
// Arrange
|
|
||||||
await setup(removesContentsWhenNoGitDirectory)
|
|
||||||
clean = false
|
|
||||||
await io.rmRF(path.join(repositoryPath, '.git'))
|
|
||||||
await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await gitDirectoryHelper.prepareExistingDirectory(
|
|
||||||
git,
|
|
||||||
repositoryPath,
|
|
||||||
httpsUrl,
|
|
||||||
[httpsUrl, sshUrl],
|
|
||||||
clean
|
|
||||||
)
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
const files = await fs.promises.readdir(repositoryPath)
|
|
||||||
expect(files).toHaveLength(0)
|
|
||||||
expect(core.warning).not.toHaveBeenCalled()
|
|
||||||
expect(git.isDetached).not.toHaveBeenCalled()
|
|
||||||
})
|
|
||||||
|
|
||||||
const removesContentsWhenResetFails = 'removes contents when reset fails'
|
|
||||||
it(removesContentsWhenResetFails, async () => {
|
|
||||||
// Arrange
|
|
||||||
await setup(removesContentsWhenResetFails)
|
|
||||||
await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
|
|
||||||
let mockTryReset = git.tryReset as jest.Mock<any, any>
|
|
||||||
mockTryReset.mockImplementation(async () => {
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await gitDirectoryHelper.prepareExistingDirectory(
|
|
||||||
git,
|
|
||||||
repositoryPath,
|
|
||||||
httpsUrl,
|
|
||||||
[httpsUrl, sshUrl],
|
|
||||||
clean
|
|
||||||
)
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
const files = await fs.promises.readdir(repositoryPath)
|
|
||||||
expect(files).toHaveLength(0)
|
|
||||||
expect(git.tryClean).toHaveBeenCalled()
|
|
||||||
expect(git.tryReset).toHaveBeenCalled()
|
|
||||||
expect(core.warning).toHaveBeenCalled()
|
|
||||||
})
|
|
||||||
|
|
||||||
const removesContentsWhenUndefinedGitCommandManager =
|
|
||||||
'removes contents when undefined git command manager'
|
|
||||||
it(removesContentsWhenUndefinedGitCommandManager, async () => {
|
|
||||||
// Arrange
|
|
||||||
await setup(removesContentsWhenUndefinedGitCommandManager)
|
|
||||||
clean = false
|
|
||||||
await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await gitDirectoryHelper.prepareExistingDirectory(
|
|
||||||
undefined,
|
|
||||||
repositoryPath,
|
|
||||||
httpsUrl,
|
|
||||||
[httpsUrl, sshUrl],
|
|
||||||
clean
|
|
||||||
)
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
const files = await fs.promises.readdir(repositoryPath)
|
|
||||||
expect(files).toHaveLength(0)
|
|
||||||
expect(core.warning).not.toHaveBeenCalled()
|
|
||||||
})
|
|
||||||
|
|
||||||
const removesLocalBranches = 'removes local branches'
|
|
||||||
it(removesLocalBranches, async () => {
|
|
||||||
// Arrange
|
|
||||||
await setup(removesLocalBranches)
|
|
||||||
await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
|
|
||||||
const mockBranchList = git.branchList as jest.Mock<any, any>
|
|
||||||
mockBranchList.mockImplementation(async (remote: boolean) => {
|
|
||||||
return remote ? [] : ['local-branch-1', 'local-branch-2']
|
|
||||||
})
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await gitDirectoryHelper.prepareExistingDirectory(
|
|
||||||
git,
|
|
||||||
repositoryPath,
|
|
||||||
httpsUrl,
|
|
||||||
[httpsUrl, sshUrl],
|
|
||||||
clean
|
|
||||||
)
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
const files = await fs.promises.readdir(repositoryPath)
|
|
||||||
expect(files.sort()).toEqual(['.git', 'my-file'])
|
|
||||||
expect(git.branchDelete).toHaveBeenCalledWith(false, 'local-branch-1')
|
|
||||||
expect(git.branchDelete).toHaveBeenCalledWith(false, 'local-branch-2')
|
|
||||||
})
|
|
||||||
|
|
||||||
const removesLockFiles = 'removes lock files'
|
|
||||||
it(removesLockFiles, async () => {
|
|
||||||
// Arrange
|
|
||||||
await setup(removesLockFiles)
|
|
||||||
clean = false
|
|
||||||
await fs.promises.writeFile(
|
|
||||||
path.join(repositoryPath, '.git', 'index.lock'),
|
|
||||||
''
|
|
||||||
)
|
|
||||||
await fs.promises.writeFile(
|
|
||||||
path.join(repositoryPath, '.git', 'shallow.lock'),
|
|
||||||
''
|
|
||||||
)
|
|
||||||
await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await gitDirectoryHelper.prepareExistingDirectory(
|
|
||||||
git,
|
|
||||||
repositoryPath,
|
|
||||||
httpsUrl,
|
|
||||||
[httpsUrl, sshUrl],
|
|
||||||
clean
|
|
||||||
)
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
let files = await fs.promises.readdir(path.join(repositoryPath, '.git'))
|
|
||||||
expect(files).toHaveLength(0)
|
|
||||||
files = await fs.promises.readdir(repositoryPath)
|
|
||||||
expect(files.sort()).toEqual(['.git', 'my-file'])
|
|
||||||
expect(git.isDetached).toHaveBeenCalled()
|
|
||||||
expect(git.branchList).toHaveBeenCalled()
|
|
||||||
expect(core.warning).not.toHaveBeenCalled()
|
|
||||||
expect(git.tryClean).not.toHaveBeenCalled()
|
|
||||||
expect(git.tryReset).not.toHaveBeenCalled()
|
|
||||||
})
|
|
||||||
|
|
||||||
const removesRemoteBranches = 'removes local branches'
|
|
||||||
it(removesRemoteBranches, async () => {
|
|
||||||
// Arrange
|
|
||||||
await setup(removesRemoteBranches)
|
|
||||||
await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
|
|
||||||
const mockBranchList = git.branchList as jest.Mock<any, any>
|
|
||||||
mockBranchList.mockImplementation(async (remote: boolean) => {
|
|
||||||
return remote ? ['remote-branch-1', 'remote-branch-2'] : []
|
|
||||||
})
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await gitDirectoryHelper.prepareExistingDirectory(
|
|
||||||
git,
|
|
||||||
repositoryPath,
|
|
||||||
httpsUrl,
|
|
||||||
[httpsUrl, sshUrl],
|
|
||||||
clean
|
|
||||||
)
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
const files = await fs.promises.readdir(repositoryPath)
|
|
||||||
expect(files.sort()).toEqual(['.git', 'my-file'])
|
|
||||||
expect(git.branchDelete).toHaveBeenCalledWith(true, 'remote-branch-1')
|
|
||||||
expect(git.branchDelete).toHaveBeenCalledWith(true, 'remote-branch-2')
|
|
||||||
})
|
|
||||||
|
|
||||||
const updatesRemoteUrl = 'updates remote URL'
|
|
||||||
it(updatesRemoteUrl, async () => {
|
|
||||||
// Arrange
|
|
||||||
await setup(updatesRemoteUrl)
|
|
||||||
await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await gitDirectoryHelper.prepareExistingDirectory(
|
|
||||||
git,
|
|
||||||
repositoryPath,
|
|
||||||
sshUrl,
|
|
||||||
[sshUrl, httpsUrl],
|
|
||||||
clean
|
|
||||||
)
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
const files = await fs.promises.readdir(repositoryPath)
|
|
||||||
expect(files.sort()).toEqual(['.git', 'my-file'])
|
|
||||||
expect(git.isDetached).toHaveBeenCalled()
|
|
||||||
expect(git.branchList).toHaveBeenCalled()
|
|
||||||
expect(core.warning).not.toHaveBeenCalled()
|
|
||||||
expect(git.setRemoteUrl).toHaveBeenCalledWith(sshUrl)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
async function setup(testName: string): Promise<void> {
|
|
||||||
testName = testName.replace(/[^a-zA-Z0-9_]+/g, '-')
|
|
||||||
|
|
||||||
// Repository directory
|
|
||||||
repositoryPath = path.join(testWorkspace, testName)
|
|
||||||
await fs.promises.mkdir(path.join(repositoryPath, '.git'), {recursive: true})
|
|
||||||
|
|
||||||
// Remote URLs
|
|
||||||
httpsUrl = 'https://github.com/my-org/my-repo'
|
|
||||||
sshUrl = 'git@github.com:my-org/my-repo'
|
|
||||||
|
|
||||||
// Clean
|
|
||||||
clean = true
|
|
||||||
|
|
||||||
// Git command manager
|
|
||||||
git = {
|
|
||||||
branchDelete: jest.fn(),
|
|
||||||
branchExists: jest.fn(),
|
|
||||||
branchList: jest.fn(async () => {
|
|
||||||
return []
|
|
||||||
}),
|
|
||||||
checkout: jest.fn(),
|
|
||||||
checkoutDetach: jest.fn(),
|
|
||||||
config: jest.fn(),
|
|
||||||
configExists: jest.fn(),
|
|
||||||
fetch: jest.fn(),
|
|
||||||
getWorkingDirectory: jest.fn(() => repositoryPath),
|
|
||||||
init: jest.fn(),
|
|
||||||
isDetached: jest.fn(),
|
|
||||||
lfsFetch: jest.fn(),
|
|
||||||
lfsInstall: jest.fn(),
|
|
||||||
log1: jest.fn(),
|
|
||||||
remoteAdd: jest.fn(),
|
|
||||||
removeEnvironmentVariable: jest.fn(),
|
|
||||||
setEnvironmentVariable: jest.fn(),
|
|
||||||
setRemoteUrl: jest.fn(),
|
|
||||||
submoduleForeach: jest.fn(),
|
|
||||||
submoduleSync: jest.fn(),
|
|
||||||
submoduleUpdate: jest.fn(),
|
|
||||||
tagExists: jest.fn(),
|
|
||||||
tryClean: jest.fn(async () => {
|
|
||||||
return true
|
|
||||||
}),
|
|
||||||
tryConfigUnset: jest.fn(),
|
|
||||||
tryDisableAutomaticGarbageCollection: jest.fn(),
|
|
||||||
tryGetRemoteUrl: jest.fn(async () => {
|
|
||||||
// Sanity check - this function shouldn't be called when the .git directory doesn't exist
|
|
||||||
await fs.promises.stat(path.join(repositoryPath, '.git'))
|
|
||||||
return httpsUrl
|
|
||||||
}),
|
|
||||||
tryReset: jest.fn(async () => {
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
import {GitVersion} from '../lib/git-version'
|
|
||||||
|
|
||||||
describe('git-version tests', () => {
|
|
||||||
it('basics', async () => {
|
|
||||||
let version = new GitVersion('')
|
|
||||||
expect(version.isValid()).toBeFalsy()
|
|
||||||
|
|
||||||
version = new GitVersion('asdf')
|
|
||||||
expect(version.isValid()).toBeFalsy()
|
|
||||||
|
|
||||||
version = new GitVersion('1.2')
|
|
||||||
expect(version.isValid()).toBeTruthy()
|
|
||||||
expect(version.toString()).toBe('1.2')
|
|
||||||
|
|
||||||
version = new GitVersion('1.2.3')
|
|
||||||
expect(version.isValid()).toBeTruthy()
|
|
||||||
expect(version.toString()).toBe('1.2.3')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('check minimum', async () => {
|
|
||||||
let version = new GitVersion('4.5')
|
|
||||||
expect(version.checkMinimum(new GitVersion('3.6'))).toBeTruthy()
|
|
||||||
expect(version.checkMinimum(new GitVersion('3.6.7'))).toBeTruthy()
|
|
||||||
expect(version.checkMinimum(new GitVersion('4.4'))).toBeTruthy()
|
|
||||||
expect(version.checkMinimum(new GitVersion('4.5'))).toBeTruthy()
|
|
||||||
expect(version.checkMinimum(new GitVersion('4.5.0'))).toBeTruthy()
|
|
||||||
expect(version.checkMinimum(new GitVersion('4.6'))).toBeFalsy()
|
|
||||||
expect(version.checkMinimum(new GitVersion('4.6.0'))).toBeFalsy()
|
|
||||||
expect(version.checkMinimum(new GitVersion('5.1'))).toBeFalsy()
|
|
||||||
expect(version.checkMinimum(new GitVersion('5.1.2'))).toBeFalsy()
|
|
||||||
|
|
||||||
version = new GitVersion('4.5.6')
|
|
||||||
expect(version.checkMinimum(new GitVersion('3.6'))).toBeTruthy()
|
|
||||||
expect(version.checkMinimum(new GitVersion('3.6.7'))).toBeTruthy()
|
|
||||||
expect(version.checkMinimum(new GitVersion('4.4'))).toBeTruthy()
|
|
||||||
expect(version.checkMinimum(new GitVersion('4.5'))).toBeTruthy()
|
|
||||||
expect(version.checkMinimum(new GitVersion('4.5.5'))).toBeTruthy()
|
|
||||||
expect(version.checkMinimum(new GitVersion('4.5.6'))).toBeTruthy()
|
|
||||||
expect(version.checkMinimum(new GitVersion('4.5.7'))).toBeFalsy()
|
|
||||||
expect(version.checkMinimum(new GitVersion('4.6'))).toBeFalsy()
|
|
||||||
expect(version.checkMinimum(new GitVersion('4.6.0'))).toBeFalsy()
|
|
||||||
expect(version.checkMinimum(new GitVersion('5.1'))).toBeFalsy()
|
|
||||||
expect(version.checkMinimum(new GitVersion('5.1.2'))).toBeFalsy()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -1,133 +0,0 @@
|
|||||||
import * as assert from 'assert'
|
|
||||||
import * as core from '@actions/core'
|
|
||||||
import * as fsHelper from '../lib/fs-helper'
|
|
||||||
import * as github from '@actions/github'
|
|
||||||
import * as inputHelper from '../lib/input-helper'
|
|
||||||
import * as path from 'path'
|
|
||||||
import {IGitSourceSettings} from '../lib/git-source-settings'
|
|
||||||
|
|
||||||
const originalGitHubWorkspace = process.env['GITHUB_WORKSPACE']
|
|
||||||
const gitHubWorkspace = path.resolve('/checkout-tests/workspace')
|
|
||||||
|
|
||||||
// Inputs for mock @actions/core
|
|
||||||
let inputs = {} as any
|
|
||||||
|
|
||||||
// Shallow clone original @actions/github context
|
|
||||||
let originalContext = {...github.context}
|
|
||||||
|
|
||||||
describe('input-helper tests', () => {
|
|
||||||
beforeAll(() => {
|
|
||||||
// Mock getInput
|
|
||||||
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
|
|
||||||
return inputs[name]
|
|
||||||
})
|
|
||||||
|
|
||||||
// Mock error/warning/info/debug
|
|
||||||
jest.spyOn(core, 'error').mockImplementation(jest.fn())
|
|
||||||
jest.spyOn(core, 'warning').mockImplementation(jest.fn())
|
|
||||||
jest.spyOn(core, 'info').mockImplementation(jest.fn())
|
|
||||||
jest.spyOn(core, 'debug').mockImplementation(jest.fn())
|
|
||||||
|
|
||||||
// Mock github context
|
|
||||||
jest.spyOn(github.context, 'repo', 'get').mockImplementation(() => {
|
|
||||||
return {
|
|
||||||
owner: 'some-owner',
|
|
||||||
repo: 'some-repo'
|
|
||||||
}
|
|
||||||
})
|
|
||||||
github.context.ref = 'refs/heads/some-ref'
|
|
||||||
github.context.sha = '1234567890123456789012345678901234567890'
|
|
||||||
|
|
||||||
// Mock ./fs-helper directoryExistsSync()
|
|
||||||
jest
|
|
||||||
.spyOn(fsHelper, 'directoryExistsSync')
|
|
||||||
.mockImplementation((path: string) => path == gitHubWorkspace)
|
|
||||||
|
|
||||||
// GitHub workspace
|
|
||||||
process.env['GITHUB_WORKSPACE'] = gitHubWorkspace
|
|
||||||
})
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
// Reset inputs
|
|
||||||
inputs = {}
|
|
||||||
})
|
|
||||||
|
|
||||||
afterAll(() => {
|
|
||||||
// Restore GitHub workspace
|
|
||||||
delete process.env['GITHUB_WORKSPACE']
|
|
||||||
if (originalGitHubWorkspace) {
|
|
||||||
process.env['GITHUB_WORKSPACE'] = originalGitHubWorkspace
|
|
||||||
}
|
|
||||||
|
|
||||||
// Restore @actions/github context
|
|
||||||
github.context.ref = originalContext.ref
|
|
||||||
github.context.sha = originalContext.sha
|
|
||||||
|
|
||||||
// Restore
|
|
||||||
jest.restoreAllMocks()
|
|
||||||
})
|
|
||||||
|
|
||||||
it('sets defaults', () => {
|
|
||||||
const settings: IGitSourceSettings = inputHelper.getInputs()
|
|
||||||
expect(settings).toBeTruthy()
|
|
||||||
expect(settings.authToken).toBeFalsy()
|
|
||||||
expect(settings.clean).toBe(true)
|
|
||||||
expect(settings.commit).toBeTruthy()
|
|
||||||
expect(settings.commit).toBe('1234567890123456789012345678901234567890')
|
|
||||||
expect(settings.fetchDepth).toBe(1)
|
|
||||||
expect(settings.lfs).toBe(false)
|
|
||||||
expect(settings.ref).toBe('refs/heads/some-ref')
|
|
||||||
expect(settings.repositoryName).toBe('some-repo')
|
|
||||||
expect(settings.repositoryOwner).toBe('some-owner')
|
|
||||||
expect(settings.repositoryPath).toBe(gitHubWorkspace)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('qualifies ref', () => {
|
|
||||||
let originalRef = github.context.ref
|
|
||||||
try {
|
|
||||||
github.context.ref = 'some-unqualified-ref'
|
|
||||||
const settings: IGitSourceSettings = inputHelper.getInputs()
|
|
||||||
expect(settings).toBeTruthy()
|
|
||||||
expect(settings.commit).toBe('1234567890123456789012345678901234567890')
|
|
||||||
expect(settings.ref).toBe('refs/heads/some-unqualified-ref')
|
|
||||||
} finally {
|
|
||||||
github.context.ref = originalRef
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('requires qualified repo', () => {
|
|
||||||
inputs.repository = 'some-unqualified-repo'
|
|
||||||
assert.throws(() => {
|
|
||||||
inputHelper.getInputs()
|
|
||||||
}, /Invalid repository 'some-unqualified-repo'/)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('roots path', () => {
|
|
||||||
inputs.path = 'some-directory/some-subdirectory'
|
|
||||||
const settings: IGitSourceSettings = inputHelper.getInputs()
|
|
||||||
expect(settings.repositoryPath).toBe(
|
|
||||||
path.join(gitHubWorkspace, 'some-directory', 'some-subdirectory')
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('sets correct default ref/sha for other repo', () => {
|
|
||||||
inputs.repository = 'some-owner/some-other-repo'
|
|
||||||
const settings: IGitSourceSettings = inputHelper.getInputs()
|
|
||||||
expect(settings.ref).toBe('refs/heads/master')
|
|
||||||
expect(settings.commit).toBeFalsy()
|
|
||||||
})
|
|
||||||
|
|
||||||
it('sets ref to empty when explicit sha', () => {
|
|
||||||
inputs.ref = '1111111111222222222233333333334444444444'
|
|
||||||
const settings: IGitSourceSettings = inputHelper.getInputs()
|
|
||||||
expect(settings.ref).toBeFalsy()
|
|
||||||
expect(settings.commit).toBe('1111111111222222222233333333334444444444')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('sets sha to empty when explicit ref', () => {
|
|
||||||
inputs.ref = 'refs/heads/some-other-ref'
|
|
||||||
const settings: IGitSourceSettings = inputHelper.getInputs()
|
|
||||||
expect(settings.ref).toBe('refs/heads/some-other-ref')
|
|
||||||
expect(settings.commit).toBeFalsy()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
if [ ! -f "./basic/basic-file.txt" ]; then
|
|
||||||
echo "Expected basic file does not exist"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo hello >> ./basic/basic-file.txt
|
|
||||||
echo hello >> ./basic/new-file.txt
|
|
||||||
git -C ./basic status
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
|
|
||||||
mkdir override-git-version
|
|
||||||
cd override-git-version
|
|
||||||
echo @echo override git version 1.2.3 > git.cmd
|
|
||||||
echo ::add-path::%CD%
|
|
||||||
cd ..
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
mkdir override-git-version
|
|
||||||
cd override-git-version
|
|
||||||
echo "#!/bin/sh" > git
|
|
||||||
echo "echo override git version 1.2.3" >> git
|
|
||||||
chmod +x git
|
|
||||||
echo "::add-path::$(pwd)"
|
|
||||||
cd ..
|
|
||||||
@@ -1,168 +0,0 @@
|
|||||||
import * as assert from 'assert'
|
|
||||||
import * as refHelper from '../lib/ref-helper'
|
|
||||||
import {IGitCommandManager} from '../lib/git-command-manager'
|
|
||||||
|
|
||||||
const commit = '1234567890123456789012345678901234567890'
|
|
||||||
let git: IGitCommandManager
|
|
||||||
|
|
||||||
describe('ref-helper tests', () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
git = ({} as unknown) as IGitCommandManager
|
|
||||||
})
|
|
||||||
|
|
||||||
it('getCheckoutInfo requires git', async () => {
|
|
||||||
const git = (null as unknown) as IGitCommandManager
|
|
||||||
try {
|
|
||||||
await refHelper.getCheckoutInfo(git, 'refs/heads/my/branch', commit)
|
|
||||||
throw new Error('Should not reach here')
|
|
||||||
} catch (err) {
|
|
||||||
expect(err.message).toBe('Arg git cannot be empty')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('getCheckoutInfo requires ref or commit', async () => {
|
|
||||||
try {
|
|
||||||
await refHelper.getCheckoutInfo(git, '', '')
|
|
||||||
throw new Error('Should not reach here')
|
|
||||||
} catch (err) {
|
|
||||||
expect(err.message).toBe('Args ref and commit cannot both be empty')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('getCheckoutInfo sha only', async () => {
|
|
||||||
const checkoutInfo = await refHelper.getCheckoutInfo(git, '', commit)
|
|
||||||
expect(checkoutInfo.ref).toBe(commit)
|
|
||||||
expect(checkoutInfo.startPoint).toBeFalsy()
|
|
||||||
})
|
|
||||||
|
|
||||||
it('getCheckoutInfo refs/heads/', async () => {
|
|
||||||
const checkoutInfo = await refHelper.getCheckoutInfo(
|
|
||||||
git,
|
|
||||||
'refs/heads/my/branch',
|
|
||||||
commit
|
|
||||||
)
|
|
||||||
expect(checkoutInfo.ref).toBe('my/branch')
|
|
||||||
expect(checkoutInfo.startPoint).toBe('refs/remotes/origin/my/branch')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('getCheckoutInfo refs/pull/', async () => {
|
|
||||||
const checkoutInfo = await refHelper.getCheckoutInfo(
|
|
||||||
git,
|
|
||||||
'refs/pull/123/merge',
|
|
||||||
commit
|
|
||||||
)
|
|
||||||
expect(checkoutInfo.ref).toBe('refs/remotes/pull/123/merge')
|
|
||||||
expect(checkoutInfo.startPoint).toBeFalsy()
|
|
||||||
})
|
|
||||||
|
|
||||||
it('getCheckoutInfo refs/tags/', async () => {
|
|
||||||
const checkoutInfo = await refHelper.getCheckoutInfo(
|
|
||||||
git,
|
|
||||||
'refs/tags/my-tag',
|
|
||||||
commit
|
|
||||||
)
|
|
||||||
expect(checkoutInfo.ref).toBe('refs/tags/my-tag')
|
|
||||||
expect(checkoutInfo.startPoint).toBeFalsy()
|
|
||||||
})
|
|
||||||
|
|
||||||
it('getCheckoutInfo unqualified branch only', async () => {
|
|
||||||
git.branchExists = jest.fn(async (remote: boolean, pattern: string) => {
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
|
|
||||||
const checkoutInfo = await refHelper.getCheckoutInfo(git, 'my/branch', '')
|
|
||||||
|
|
||||||
expect(checkoutInfo.ref).toBe('my/branch')
|
|
||||||
expect(checkoutInfo.startPoint).toBe('refs/remotes/origin/my/branch')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('getCheckoutInfo unqualified tag only', async () => {
|
|
||||||
git.branchExists = jest.fn(async (remote: boolean, pattern: string) => {
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
git.tagExists = jest.fn(async (pattern: string) => {
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
|
|
||||||
const checkoutInfo = await refHelper.getCheckoutInfo(git, 'my-tag', '')
|
|
||||||
|
|
||||||
expect(checkoutInfo.ref).toBe('refs/tags/my-tag')
|
|
||||||
expect(checkoutInfo.startPoint).toBeFalsy()
|
|
||||||
})
|
|
||||||
|
|
||||||
it('getCheckoutInfo unqualified ref only, not a branch or tag', async () => {
|
|
||||||
git.branchExists = jest.fn(async (remote: boolean, pattern: string) => {
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
git.tagExists = jest.fn(async (pattern: string) => {
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
|
|
||||||
try {
|
|
||||||
await refHelper.getCheckoutInfo(git, 'my-ref', '')
|
|
||||||
throw new Error('Should not reach here')
|
|
||||||
} catch (err) {
|
|
||||||
expect(err.message).toBe(
|
|
||||||
"A branch or tag with the name 'my-ref' could not be found"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('getRefSpec requires ref or commit', async () => {
|
|
||||||
assert.throws(
|
|
||||||
() => refHelper.getRefSpec('', ''),
|
|
||||||
/Args ref and commit cannot both be empty/
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('getRefSpec sha + refs/heads/', async () => {
|
|
||||||
const refSpec = refHelper.getRefSpec('refs/heads/my/branch', commit)
|
|
||||||
expect(refSpec.length).toBe(1)
|
|
||||||
expect(refSpec[0]).toBe(`+${commit}:refs/remotes/origin/my/branch`)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('getRefSpec sha + refs/pull/', async () => {
|
|
||||||
const refSpec = refHelper.getRefSpec('refs/pull/123/merge', commit)
|
|
||||||
expect(refSpec.length).toBe(1)
|
|
||||||
expect(refSpec[0]).toBe(`+${commit}:refs/remotes/pull/123/merge`)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('getRefSpec sha + refs/tags/', async () => {
|
|
||||||
const refSpec = refHelper.getRefSpec('refs/tags/my-tag', commit)
|
|
||||||
expect(refSpec.length).toBe(1)
|
|
||||||
expect(refSpec[0]).toBe(`+${commit}:refs/tags/my-tag`)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('getRefSpec sha only', async () => {
|
|
||||||
const refSpec = refHelper.getRefSpec('', commit)
|
|
||||||
expect(refSpec.length).toBe(1)
|
|
||||||
expect(refSpec[0]).toBe(commit)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('getRefSpec unqualified ref only', async () => {
|
|
||||||
const refSpec = refHelper.getRefSpec('my-ref', '')
|
|
||||||
expect(refSpec.length).toBe(2)
|
|
||||||
expect(refSpec[0]).toBe('+refs/heads/my-ref*:refs/remotes/origin/my-ref*')
|
|
||||||
expect(refSpec[1]).toBe('+refs/tags/my-ref*:refs/tags/my-ref*')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('getRefSpec refs/heads/ only', async () => {
|
|
||||||
const refSpec = refHelper.getRefSpec('refs/heads/my/branch', '')
|
|
||||||
expect(refSpec.length).toBe(1)
|
|
||||||
expect(refSpec[0]).toBe(
|
|
||||||
'+refs/heads/my/branch:refs/remotes/origin/my/branch'
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('getRefSpec refs/pull/ only', async () => {
|
|
||||||
const refSpec = refHelper.getRefSpec('refs/pull/123/merge', '')
|
|
||||||
expect(refSpec.length).toBe(1)
|
|
||||||
expect(refSpec[0]).toBe('+refs/pull/123/merge:refs/remotes/pull/123/merge')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('getRefSpec refs/tags/ only', async () => {
|
|
||||||
const refSpec = refHelper.getRefSpec('refs/tags/my-tag', '')
|
|
||||||
expect(refSpec.length).toBe(1)
|
|
||||||
expect(refSpec[0]).toBe('+refs/tags/my-tag:refs/tags/my-tag')
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -1,87 +0,0 @@
|
|||||||
import * as core from '@actions/core'
|
|
||||||
import {RetryHelper} from '../lib/retry-helper'
|
|
||||||
|
|
||||||
let info: string[]
|
|
||||||
let retryHelper: any
|
|
||||||
|
|
||||||
describe('retry-helper tests', () => {
|
|
||||||
beforeAll(() => {
|
|
||||||
// Mock @actions/core info()
|
|
||||||
jest.spyOn(core, 'info').mockImplementation((message: string) => {
|
|
||||||
info.push(message)
|
|
||||||
})
|
|
||||||
|
|
||||||
retryHelper = new RetryHelper(3, 0, 0)
|
|
||||||
})
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
// Reset info
|
|
||||||
info = []
|
|
||||||
})
|
|
||||||
|
|
||||||
afterAll(() => {
|
|
||||||
// Restore
|
|
||||||
jest.restoreAllMocks()
|
|
||||||
})
|
|
||||||
|
|
||||||
it('first attempt succeeds', async () => {
|
|
||||||
const actual = await retryHelper.execute(async () => {
|
|
||||||
return 'some result'
|
|
||||||
})
|
|
||||||
expect(actual).toBe('some result')
|
|
||||||
expect(info).toHaveLength(0)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('second attempt succeeds', async () => {
|
|
||||||
let attempts = 0
|
|
||||||
const actual = await retryHelper.execute(() => {
|
|
||||||
if (++attempts == 1) {
|
|
||||||
throw new Error('some error')
|
|
||||||
}
|
|
||||||
|
|
||||||
return Promise.resolve('some result')
|
|
||||||
})
|
|
||||||
expect(attempts).toBe(2)
|
|
||||||
expect(actual).toBe('some result')
|
|
||||||
expect(info).toHaveLength(2)
|
|
||||||
expect(info[0]).toBe('some error')
|
|
||||||
expect(info[1]).toMatch(/Waiting .+ seconds before trying aga | |||||||