Skip to content

Commit

Permalink
Support more payload resources (#1)
Browse files Browse the repository at this point in the history
* feat: support fetching payload from paths and URLs

* ci: add prepare and tests jobs

* ci: make self-test depend on 'tests'' job

* test: add cover test

* ci: add self test using path payload

* ci: add dispatch test for payload url
  • Loading branch information
iniva authored Mar 13, 2021
1 parent 70c7c75 commit c41266f
Show file tree
Hide file tree
Showing 16 changed files with 9,633 additions and 227 deletions.
108 changes: 106 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,78 @@ on:
- '*'

jobs:
self-test:
name: Self Test
prepare:
name: Preparing pipeline
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
name: Cloning repository

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"

- name: Getting cache for node_modules
id: yarn-cache
uses: actions/cache@v1
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-build-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-build-
- uses: actions/setup-node@v1
name: Setting Node.js Version
with:
node-version: '12.x'

- name: Installing dependencies
run: yarn install --frozen-lockfile
continue-on-error: false

tests:
needs: prepare
strategy:
matrix:
# Creates jobs for each element in the matrix
test: ['unit', 'lint', 'cover']
# Let individual jobs in the matrix fail without canceling all jobs
fail-fast: false
name: ${{ matrix.test }} tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
name: Cloning repository

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"

- name: Getting cache for node_modules
id: yarn-cache
uses: actions/cache@v1
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-build-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-build-
- uses: actions/setup-node@v1
name: Setting Node.js Version
with:
node-version: '12.x'

- name: Installing dependencies
run: yarn install --frozen-lockfile
continue-on-error: false

- name: Running ${{ matrix.test }} tests
run: yarn test:${{ matrix.test }}
continue-on-error: false

dispatch-string:
needs: tests
name: Dispatch using stringified payload
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -22,3 +92,37 @@ jobs:
eventType: 'test_dispatch'
token: ${{ secrets.REPO_PAT }}
payload: '{"requested_by": "${{github.actor}}"}'

dispatch-path:
needs: tests
name: Dispatch using file path payload
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
name: Cloning repository

- uses: ./
name: Dispatch Event
id: dispatch_action
with:
eventType: 'test_dispatch'
token: ${{ secrets.REPO_PAT }}
payloadType: path
payloadPath: test/files/valid.json

dispatch-url:
needs: tests
name: Dispatch using file URL payload
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
name: Cloning repository

- uses: ./
name: Dispatch Event
id: dispatch_action
with:
eventType: 'test_dispatch'
token: ${{ secrets.REPO_PAT }}
payloadType: url
payloadUrl: https://raw.githubusercontent.com/iniva/action-repository-dispatch/support-more-payload-resources/test/files/valid.json
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,21 @@
- `token`: A GitHub Personal Access Token (PAT) ([more info](#-token))
- required: `true`

- `payload`: Stringified JSON payload
- required: `false`
- default: `'{}'`
- `payloadType`: Type of payload provided. Types: `string`, `path`, `url`
- required: `false`
- default: `'string'`

- `payload`: Stringified JSON payload. Expected when payloadType is not provided or provided as `"string"`
- required: `false`
- default: `'{}'`
- example: `'{"customField": "some value", "anotherField": "another value"}'`

- `payloadPath`: Path to file with JSON payload. Expected when payloadType is provided as `"path"`
- required: `false`

- `payloadUrl`: URL to JSON payload. Expected when payloadType is provided as `"url"`
- required: `false`

### (*) token
:information_source: If you are targeting:
- **Public repository**: The PAT only needs `public_repo` scope
Expand Down
16 changes: 15 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,25 @@ inputs:
description: A GitHub Personal Access Token (PAT) with 'repo' permissions
required: true

payloadType:
description: "Type of payload provided. Types: string, path, url"
required: false
default: 'string'

payload:
description: Stringified JSON payload
description: Stringified JSON payload. Expected when payloadType is not provided or provided as "string"
required: false
default: '{}'

payloadPath:
description: Path to file with JSON payload. Expected when payloadType is provided as "path"
required: false

payloadUrl:
description: URL to JSON payload. Expected when payloadType is provided as "url"
required: false


runs:
using: 'node12'
main: 'dist/index.js'
48 changes: 48 additions & 0 deletions dist/PayloadResolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PayloadResolver = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
const got_1 = require("got");
const process_1 = require("process");
const util_1 = require("util");
class PayloadResolver {
static fromString(payload) {
try {
return JSON.parse(payload);
}
catch (error) {
throw new Error(`An error ocurred while parsing the payload: ${error.message}`);
}
}
static async fromPath(path) {
let content;
try {
const filePath = path_1.resolve(process_1.cwd(), path);
const asyncReadFile = util_1.promisify(fs_1.readFile);
content = await asyncReadFile(filePath, { encoding: 'utf8' });
}
catch (error) {
throw new Error(`An error ocurred while reading the payload from ${path}: ${error.message}`);
}
try {
return JSON.parse(content);
}
catch (error) {
throw new Error(`An error ocurred while parsing the payload from ${path}: ${error.message}`);
}
}
static async fromUrl(url) {
try {
const response = await got_1.default(url, { responseType: 'json' });
return response.body;
}
catch (error) {
if (error.name && error.name === 'ParseError') {
throw new Error(`An error ocurred while parsing the payload from ${url}: ${error.message}`);
}
throw new Error(`An error ocurred while fetching the payload from ${url}: ${error.message}`);
}
}
}
exports.PayloadResolver = PayloadResolver;
35 changes: 35 additions & 0 deletions dist/PayloadType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PayloadType = void 0;
class PayloadType {
constructor(_type) {
this._type = _type;
}
static createFrom(type) {
const allowed = [
this.STRING,
this.PATH,
this.URL
];
if (!allowed.includes(type)) {
throw new Error(`[${type}] is an invalid payload type. Valid: ${allowed.join(', ')}`);
}
return new this(type);
}
isString() {
return this._type === PayloadType.STRING;
}
isPath() {
return this._type === PayloadType.PATH;
}
isURL() {
return this._type === PayloadType.URL;
}
get type() {
return this._type;
}
}
exports.PayloadType = PayloadType;
PayloadType.STRING = 'string';
PayloadType.PATH = 'path';
PayloadType.URL = 'url';
Loading

0 comments on commit c41266f

Please sign in to comment.