Skip to content

Commit

Permalink
Merge pull request #15 from urcomputeringpal/use
Browse files Browse the repository at this point in the history
use exported types
  • Loading branch information
jnewland authored May 14, 2023
2 parents 2898eca + 5a59469 commit 2518fd4
Show file tree
Hide file tree
Showing 18 changed files with 5,005 additions and 32 deletions.
File renamed without changes.
4,879 changes: 4,879 additions & 0 deletions .github/package-lock.json

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions .github/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "ts-scripts",
"version": "0.0.1",
"private": true,
"scripts": {
"format": "prettier --write .",
"format:check": "prettier --check .",
"build": "tsc",
"test": "jest"
},
"dependencies": {
"@urcomputeringpal/github-script-ts": "0.0.1"
},
"devDependencies": {
"@types/jest": "29.5.1",
"@types/node": "16.18.30",
"jest": "29.5.0",
"prettier": "2.8.8",
"ts-jest": "29.1.0",
"ts-loader": "9.4.2",
"typescript": "4.9.5"
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion src/getLabel.test.ts → .github/src/getLabel.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GitHubScriptArguments } from "./types";
import { GitHubScriptArguments } from "@urcomputeringpal/github-script-ts";
import { getLabel } from "./getLabel";
import { Context } from "@actions/github/lib/context";
import * as core from "./__mocks__/core";
Expand Down
2 changes: 1 addition & 1 deletion src/getLabel.ts → .github/src/getLabel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GitHubScriptArguments } from "./types";
import { GitHubScriptArguments } from "@urcomputeringpal/github-script-ts";

export async function getLabel(args: GitHubScriptArguments): Promise<String> {
const { github, context, core } = args;
Expand Down
2 changes: 2 additions & 0 deletions .github/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { prNumber } from "./prNumber";
export { getLabel } from "./getLabel";
2 changes: 1 addition & 1 deletion src/prNumber.test.ts → .github/src/prNumber.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GitHubScriptArguments } from "./types";
import { GitHubScriptArguments } from "@urcomputeringpal/github-script-ts";
import { prNumber } from "./prNumber";
import { Context } from "@actions/github/lib/context";
import * as core from "./__mocks__/core";
Expand Down
2 changes: 1 addition & 1 deletion src/prNumber.ts → .github/src/prNumber.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GitHubScriptArguments } from "./types";
import { GitHubScriptArguments } from "@urcomputeringpal/github-script-ts";

export async function prNumber(args: GitHubScriptArguments): Promise<String> {
const { github, context, core } = args;
Expand Down
10 changes: 10 additions & 0 deletions .github/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"target": "es5",
"strict": true
},
"include": ["src/*.ts"],
"exclude": ["node_modules", "**/*.test.ts"]
}
10 changes: 4 additions & 6 deletions .github/workflows/npm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
- uses: ./
name: Setup
id: setup
with:
build: npm run format && npm run test && npm run build
path: ./
- run: npm ci
- run: npm run format
- run: npm run test
- run: npm run build
- uses: JS-DevTools/npm-publish@541aa6b21b4a1e9990c95a92c21adc16b35e9551 # v2.1.0
with:
access: public
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
id: setup
with:
build: npm run format && npm run test && npm run build
path: ./
path: ./.github

- name: Run prNumber
id: prNumber
Expand All @@ -25,7 +25,7 @@ jobs:
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
function: prNumber
path: ./
path: ./.github

- name: Validate result
if: github.event_name == 'pull_request'
Expand All @@ -42,7 +42,7 @@ jobs:
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
function: getLabel
path: ./
path: ./.github

- if: github.event.act != 'true' && github.event_name == 'pull_request'
run: |
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ web_modules/

# TypeScript cache
*.tsbuildinfo
*.js
!jest.config.js
*.d.ts

# Optional npm cache directory
.npm
Expand Down
64 changes: 63 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,71 @@
# github-script-ts

A workflow wrapping https://github.com/actions/github-script with Typescript functionality. Export functions from a module like the one in this repo and call them easily by name.
A workflow wrapping https://github.com/actions/github-script with Typescript functionality.

## Features

- Enables easily running Typescript functions exported from a module like the one in [`.github/`](./.github/) in Actions workflows. Builds and caches build results automatically.
- Allows writing tests for your scripts and running them locally with `npm test`.
- Provides a superior experience to editing Javascript embedded in YAML.

## Usage

### Writing scripts

The `@urcomputeringpal/github-script-ts` package contains a type definition for the [arguments](https://github.com/actions/github-script#actionsgithub-script) passed to your script. Put all of these files in `.github` to create scripts of your own:

#### `src/function1.ts`

```typescript
import { GitHubScriptArguments } from "@urcomputeringpal/github-script-ts";

export async function function1(args: GitHubScriptArguments): Promise<String> {
// const { github, context, core } = args;
// const { glob, io, exec, fetch } = args;
// ...
return 'string';
}
```

#### `src/index.ts`

```typescript
export { function1 } from "./function1";
```

#### `package.json`

```json
{
"name": "ts-scripts",
"version": "0.0.1",
"private": true,
"scripts": {
"build": "tsc",
},
"dependencies": {
"@urcomputeringpal/github-script-ts": "0.0.1"
}
}
```

#### `tsconfig.json`

```json
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"target": "es5",
"strict": true
},
"include": ["src/*.ts"],
"exclude": ["node_modules", "**/*.test.ts"]
}
```

### Running your script in a workflow

```yaml
- name: Checkout repository
uses: actions/checkout@v3
Expand Down
15 changes: 9 additions & 6 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ inputs:
description: "Path to the functions"
default: ./.github
required: true
module:
description: The generated file exporting all of the functions from the module. Defaults to 'src/index.js'.
default: src/index.js
build:
description: Build command. This command is expected to generate output in the `dist` directory.
description: Build command. This command is expected to generate an index.js in the path directory.
default: npm run build
function:
description: "Name of the function to call"
Expand Down Expand Up @@ -72,9 +75,9 @@ runs:
path: |
${{ steps.npm-cache-dir.outputs.dir }}
key: |
github-script-ts-npm-${{ runner.os }}-${{ hashFiles(format('{0}/{1}', inputs.path, 'package-lock.json')) }}
github-script-ts-npm-v2-${{ runner.os }}-${{ hashFiles(format('{0}/{1}', inputs.path, 'package-lock.json')) }}
restore-keys: |
github-script-ts-npm-${{ runner.os }}-
github-script-ts-npm-v2-${{ runner.os }}-
- name: Install dependencies
if: inputs.function == ''
Expand All @@ -88,9 +91,9 @@ runs:
id: build-cache
with:
path: |
${{ inputs.path }}/dist
${{ inputs.path }}
key: |
github-script-ts-build-${{ runner.os }}-${{ hashFiles(format('{0}/{1}', inputs.path, 'package-lock.json')) }}-${{ hashFiles(format('{0}/{1}', inputs.path, '**/*.ts')) }}
github-script-ts-build-v2-${{ runner.os }}-${{ hashFiles(format('{0}/{1}', inputs.path, 'package-lock.json')) }}-${{ hashFiles(format('{0}/{1}', inputs.path, '**/*.ts')) }}
- name: Build
if: inputs.function == '' && steps.build-cache.outputs.cache-hit != 'true'
Expand All @@ -114,5 +117,5 @@ runs:
previews: ${{ inputs.previews }}
retry-exempt-status-codes: ${{ inputs.retry-exempt-status-codes }}
script: |
const { ${{ env.FUNCTION_NAME }} } = await import('${{ github.workspace }}/${{ inputs.path }}/dist/index.js')
const { ${{ env.FUNCTION_NAME }} } = await import('${{ github.workspace }}/${{ inputs.path }}/${{ inputs.module }}')
return await ${{ env.FUNCTION_NAME }}(${{ inputs.args }});
8 changes: 2 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
"description": "actions/github-script for Typescript",
"repository": "https://github.com/urcomputeringpal/github-script-ts",
"files": [
"dist/**/*"
"src/**/*.d.ts",
"src/**/*.js"
],
"main": "dist/index.js",
"types": "dist/types.d.ts",
"scripts": {
"format": "prettier --write .",
"format:check": "prettier --check .",
"build": "tsc",
"test": "jest",
"integration": "./test.sh"
},
"prepublishOnly": "npm run format && npm run test && npm run build",
"dependencies": {
"@actions/core": "1.10.0",
"@actions/exec": "1.1.1",
Expand All @@ -27,11 +26,8 @@
"node-fetch": "3.3.1"
},
"devDependencies": {
"@types/jest": "29.5.1",
"@types/node": "16.18.30",
"jest": "29.5.0",
"prettier": "2.8.8",
"ts-jest": "29.1.0",
"ts-loader": "9.4.2",
"typescript": "4.9.5"
}
Expand Down
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
export { prNumber } from "./prNumber";
export { getLabel } from "./getLabel";
7 changes: 3 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"compilerOptions": {
"module": "CommonJS",
"module": "commonjs",
"declaration": true,
"target": "ES5",
"strict": true,
"outDir": "dist"
"target": "es5",
"strict": true
},
"include": ["src/*.ts"],
"exclude": ["node_modules", "**/*.test.ts"]
Expand Down

0 comments on commit 2518fd4

Please sign in to comment.