Skip to content

refactor(script): extract parser function in its own file for testing #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"description": "Add cache parameter to GitHub Actions using setup-node",
"scripts": {
"start": "node cli.js",
"test": "node script.js"
"test": "uvu tests"
},
"repository": "https://github.com/oscard0m/octoherd-script-add-cache-to-node-github-action",
"keywords": [
Expand All @@ -23,7 +23,10 @@
"prettier": "^2.3.2",
"yaml": "^2.0.0-6"
},
"devDependencies": {},
"devDependencies": {
"esm": "^3.2.25",
"uvu": "^0.5.1"
},
"release": {
"branches": [
"main"
Expand Down
45 changes: 2 additions & 43 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// @ts-check

import { composeCreatePullRequest } from "octokit-plugin-create-pull-request";
import prettier from "prettier";
import YAML from "yaml";

const { parseDocument } = YAML;
import { getAddCacheToSetupNodeFunction } from "./utils/yaml-parser";

const BRANCH_NAME = "add-cache-to-node-workflows";
const PATH = ".github/workflows";
Expand Down Expand Up @@ -62,45 +59,7 @@ export async function script(octokit, repository, { cache = "npm" }) {
const filesToEdit = {};

workflowFiles.forEach((element) => {
filesToEdit[element.path] = ({ content, encoding }) => {
const yamlDocument = parseDocument(
Buffer.from(content, encoding).toString("utf-8")
);
const jobs = yamlDocument.get("jobs");
let cacheAdded = false;

for (const { value: job } of jobs.items) {
const steps = job.get("steps");
for (const step of steps.items) {
const stepUses = step.get("uses");
const stepWith = step.get("with");

if (
stepUses &&
stepUses.includes("actions/setup-node") &&
(!stepWith || !stepWith.get("cache"))
) {
if (!stepWith) {
step.set("with", { cache });
} else {
stepWith.set("cache", cache);
}

if (stepUses === "actions/setup-node@v1") {
step.set("uses", "actions/setup-node@v2");
}

cacheAdded = true;
}
}
}

return cacheAdded
? prettier.format(yamlDocument.toString(), {
parser: "yaml",
})
: null;
};
filesToEdit[element.path] = getAddCacheToSetupNodeFunction(cache)
});

const prCreated = await composeCreatePullRequest(octokit, {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Build
on:
push:
branches-ignore:
- main
- staging
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
cache: npm
- run: npm ci
- run: npm run build --if-present
env:
CI: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Build
on:
push:
branches-ignore:
- main
- staging
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
cache: npm
- run: npm ci
- run: npm run build --if-present
env:
CI: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Build
on:
push:
branches-ignore:
- main
- staging
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
cache: npm
- run: npm ci
- run: npm run build --if-present
env:
CI: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Build
on:
push:
branches-ignore:
- main
- staging
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
cache: npm
- run: npm ci
- run: npm run build --if-present
env:
CI: true
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Loading