diff --git a/tests/utils/github-action-fixtures/already-has-cache/not-remove-actions-cache-step/actual.yml b/tests/utils/github-action-fixtures/already-has-cache/not-remove-actions-cache-step/actual.yml new file mode 100644 index 0000000..48bce7d --- /dev/null +++ b/tests/utils/github-action-fixtures/already-has-cache/not-remove-actions-cache-step/actual.yml @@ -0,0 +1,24 @@ +name: Release +on: + push: + branches: + - main + +jobs: + release: + name: release + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/cache@v1 + with: + path: ~/.npm + key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-node- + - run: npm ci + - run: npm run build + - run: npx semantic-release + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NPM_TOKEN: ${{ secrets.PROBOTBOT_NPM_TOKEN }} \ No newline at end of file diff --git a/tests/utils/github-action-fixtures/already-has-cache/not-remove-actions-cache-step/expected.yml b/tests/utils/github-action-fixtures/already-has-cache/not-remove-actions-cache-step/expected.yml new file mode 100644 index 0000000..aa3f499 --- /dev/null +++ b/tests/utils/github-action-fixtures/already-has-cache/not-remove-actions-cache-step/expected.yml @@ -0,0 +1,24 @@ +name: Release +on: + push: + branches: + - main + +jobs: + release: + name: release + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/cache@v1 + with: + path: ~/.npm + key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-node- + - run: npm ci + - run: npm run build + - run: npx semantic-release + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NPM_TOKEN: ${{ secrets.PROBOTBOT_NPM_TOKEN }} diff --git a/tests/utils/github-action-fixtures/pending-to-add-cache/example2/actual.yml b/tests/utils/github-action-fixtures/pending-to-add-cache/example2/actual.yml index 89a1445..d4d16f2 100644 --- a/tests/utils/github-action-fixtures/pending-to-add-cache/example2/actual.yml +++ b/tests/utils/github-action-fixtures/pending-to-add-cache/example2/actual.yml @@ -17,13 +17,6 @@ jobs: with: node-version: ${{ matrix.node-version }} - - name: Cache node_modules - id: cache-node_modules - uses: actions/cache@v2 - with: - path: node_modules - key: node_modules-${{ matrix.os }}-${{ matrix.node-version }}-${{ hashFiles('package-lock.json') }} - - name: npm ci if: steps.cache-node_modules.outputs.cache-hit != 'true' run: npm ci diff --git a/tests/utils/github-action-fixtures/pending-to-add-cache/example2/expected.yml b/tests/utils/github-action-fixtures/pending-to-add-cache/example2/expected.yml index 4efe1a3..dd84367 100644 --- a/tests/utils/github-action-fixtures/pending-to-add-cache/example2/expected.yml +++ b/tests/utils/github-action-fixtures/pending-to-add-cache/example2/expected.yml @@ -18,13 +18,6 @@ jobs: node-version: ${{ matrix.node-version }} cache: npm - - name: Cache node_modules - id: cache-node_modules - uses: actions/cache@v2 - with: - path: node_modules - key: node_modules-${{ matrix.os }}-${{ matrix.node-version }}-${{ hashFiles('package-lock.json') }} - - name: npm ci if: steps.cache-node_modules.outputs.cache-hit != 'true' run: npm ci diff --git a/tests/utils/github-action-fixtures/pending-to-add-cache/not-remove-actions-cache-step-node_modules/actual.yml b/tests/utils/github-action-fixtures/pending-to-add-cache/not-remove-actions-cache-step-node_modules/actual.yml new file mode 100644 index 0000000..89a1445 --- /dev/null +++ b/tests/utils/github-action-fixtures/pending-to-add-cache/not-remove-actions-cache-step-node_modules/actual.yml @@ -0,0 +1,35 @@ +name: Test +on: + push: + branches: + - "main" + pull_request: +jobs: + main: + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, windows-latest, macOS-latest] + node-version: [10.x, 12.x, 14.x, 15.x] + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + + - name: Cache node_modules + id: cache-node_modules + uses: actions/cache@v2 + with: + path: node_modules + key: node_modules-${{ matrix.os }}-${{ matrix.node-version }}-${{ hashFiles('package-lock.json') }} + + - name: npm ci + if: steps.cache-node_modules.outputs.cache-hit != 'true' + run: npm ci + - name: Jest + run: npx --no-install jest + - name: CLI sanity + run: npm run test:cli-sanity + - name: CLI sanity warning + run: npm run test:cli-sanity-warning \ No newline at end of file diff --git a/tests/utils/github-action-fixtures/pending-to-add-cache/not-remove-actions-cache-step-node_modules/expected.yml b/tests/utils/github-action-fixtures/pending-to-add-cache/not-remove-actions-cache-step-node_modules/expected.yml new file mode 100644 index 0000000..4efe1a3 --- /dev/null +++ b/tests/utils/github-action-fixtures/pending-to-add-cache/not-remove-actions-cache-step-node_modules/expected.yml @@ -0,0 +1,36 @@ +name: Test +on: + push: + branches: + - "main" + pull_request: null +jobs: + main: + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, windows-latest, macOS-latest] + node-version: [10.x, 12.x, 14.x, 15.x] + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: ${{ matrix.node-version }} + cache: npm + + - name: Cache node_modules + id: cache-node_modules + uses: actions/cache@v2 + with: + path: node_modules + key: node_modules-${{ matrix.os }}-${{ matrix.node-version }}-${{ hashFiles('package-lock.json') }} + + - name: npm ci + if: steps.cache-node_modules.outputs.cache-hit != 'true' + run: npm ci + - name: Jest + run: npx --no-install jest + - name: CLI sanity + run: npm run test:cli-sanity + - name: CLI sanity warning + run: npm run test:cli-sanity-warning diff --git a/tests/utils/github-action-fixtures/pending-to-add-cache/remove-actions-cache-step-v1/actual.yml b/tests/utils/github-action-fixtures/pending-to-add-cache/remove-actions-cache-step-v1/actual.yml new file mode 100644 index 0000000..7e91ee2 --- /dev/null +++ b/tests/utils/github-action-fixtures/pending-to-add-cache/remove-actions-cache-step-v1/actual.yml @@ -0,0 +1,27 @@ +name: Release +on: + push: + branches: + - main + +jobs: + release: + name: release + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: "12.x" + - uses: actions/cache@v1 + with: + path: ~/.npm + key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-node- + - run: npm ci + - run: npm run build + - run: npx semantic-release + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NPM_TOKEN: ${{ secrets.PROBOTBOT_NPM_TOKEN }} \ No newline at end of file diff --git a/tests/utils/github-action-fixtures/pending-to-add-cache/remove-actions-cache-step-v1/expected.yml b/tests/utils/github-action-fixtures/pending-to-add-cache/remove-actions-cache-step-v1/expected.yml new file mode 100644 index 0000000..a067c45 --- /dev/null +++ b/tests/utils/github-action-fixtures/pending-to-add-cache/remove-actions-cache-step-v1/expected.yml @@ -0,0 +1,22 @@ +name: Release +on: + push: + branches: + - main + +jobs: + release: + name: release + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: "12.x" + cache: npm + - run: npm ci + - run: npm run build + - run: npx semantic-release + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NPM_TOKEN: ${{ secrets.PROBOTBOT_NPM_TOKEN }} diff --git a/tests/utils/github-action-fixtures/pending-to-add-cache/remove-actions-cache-step-v2/actual.yml b/tests/utils/github-action-fixtures/pending-to-add-cache/remove-actions-cache-step-v2/actual.yml new file mode 100644 index 0000000..c31009c --- /dev/null +++ b/tests/utils/github-action-fixtures/pending-to-add-cache/remove-actions-cache-step-v2/actual.yml @@ -0,0 +1,27 @@ +name: Release +on: + push: + branches: + - main + +jobs: + release: + name: release + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: "12.x" + - uses: actions/cache@v1 + with: + path: ~/.npm + key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-node- + - run: npm ci + - run: npm run build + - run: npx semantic-release + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NPM_TOKEN: ${{ secrets.PROBOTBOT_NPM_TOKEN }} \ No newline at end of file diff --git a/tests/utils/github-action-fixtures/pending-to-add-cache/remove-actions-cache-step-v2/expected.yml b/tests/utils/github-action-fixtures/pending-to-add-cache/remove-actions-cache-step-v2/expected.yml new file mode 100644 index 0000000..a067c45 --- /dev/null +++ b/tests/utils/github-action-fixtures/pending-to-add-cache/remove-actions-cache-step-v2/expected.yml @@ -0,0 +1,22 @@ +name: Release +on: + push: + branches: + - main + +jobs: + release: + name: release + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: "12.x" + cache: npm + - run: npm ci + - run: npm run build + - run: npx semantic-release + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NPM_TOKEN: ${{ secrets.PROBOTBOT_NPM_TOKEN }} diff --git a/tests/utils/github-action-fixtures/pending-to-add-cache/remove-actions-cache-step-with-id-and-name/actual.yml b/tests/utils/github-action-fixtures/pending-to-add-cache/remove-actions-cache-step-with-id-and-name/actual.yml new file mode 100644 index 0000000..18730f3 --- /dev/null +++ b/tests/utils/github-action-fixtures/pending-to-add-cache/remove-actions-cache-step-with-id-and-name/actual.yml @@ -0,0 +1,35 @@ +name: Test +on: + push: + branches: + - "main" + pull_request: +jobs: + main: + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, windows-latest, macOS-latest] + node-version: [10.x, 12.x, 14.x, 15.x] + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + + - name: Cache node_modules + id: cache-node_modules + uses: actions/cache@v2 + with: + path: ~/.npm + key: node_modules-${{ matrix.os }}-${{ matrix.node-version }}-${{ hashFiles('package-lock.json') }} + + - name: npm ci + if: steps.cache-node_modules.outputs.cache-hit != 'true' + run: npm ci + - name: Jest + run: npx --no-install jest + - name: CLI sanity + run: npm run test:cli-sanity + - name: CLI sanity warning + run: npm run test:cli-sanity-warning \ No newline at end of file diff --git a/tests/utils/github-action-fixtures/pending-to-add-cache/remove-actions-cache-step-with-id-and-name/expected.yml b/tests/utils/github-action-fixtures/pending-to-add-cache/remove-actions-cache-step-with-id-and-name/expected.yml new file mode 100644 index 0000000..dd84367 --- /dev/null +++ b/tests/utils/github-action-fixtures/pending-to-add-cache/remove-actions-cache-step-with-id-and-name/expected.yml @@ -0,0 +1,29 @@ +name: Test +on: + push: + branches: + - "main" + pull_request: null +jobs: + main: + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, windows-latest, macOS-latest] + node-version: [10.x, 12.x, 14.x, 15.x] + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: ${{ matrix.node-version }} + cache: npm + + - name: npm ci + if: steps.cache-node_modules.outputs.cache-hit != 'true' + run: npm ci + - name: Jest + run: npx --no-install jest + - name: CLI sanity + run: npm run test:cli-sanity + - name: CLI sanity warning + run: npm run test:cli-sanity-warning diff --git a/utils/yaml-parser.js b/utils/yaml-parser.js index 23a3f8a..fca626b 100644 --- a/utils/yaml-parser.js +++ b/utils/yaml-parser.js @@ -3,9 +3,27 @@ import prettier from "prettier"; const { parseDocument } = YAML; +/** + * Checks if the job has cache and there is a step using 'bahmutov/npm-install' + * @param {boolean} hasCache + * @param {string[]} stepUses + * + * @return {boolean} + */ const usesBahmutovNpmInstall = (hasCache, stepUses) => hasCache && stepUses && stepUses.includes("bahmutov/npm-install"); +/** + * Checks if the job has cache and there is a step using 'actions/cache' + * @param {boolean} hasCache + * @param {string[]} stepUses + * @param {object} stepWith + * + * @return {boolean} + */ +const hasActionCacheStep = (hasCache, stepUses, stepWith) => + hasCache && stepUses && stepUses.includes("actions/cache") && stepWith && stepWith.get('path') === '~/.npm'; + /** * @param {string} cache * @@ -38,6 +56,8 @@ export function getAddCacheToSetupNodeFunction(cache) { if (usesBahmutovNpmInstall(jobHasCache, stepUses)) { step.set("run", "npm ci"); step.delete("uses") + } else if (hasActionCacheStep(jobHasCache, stepUses, stepWith)) { + steps.deleteIn([steps.items.indexOf(step)]) } else if ( stepUses && stepUses.includes("actions/setup-node") &&