Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmad Nassri committed Feb 4, 2021
1 parent 98ee11c commit fe8f79f
Show file tree
Hide file tree
Showing 12 changed files with 689 additions and 1 deletion.
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM node:slim

LABEL com.github.actions.name="GitHub Action: Workflow Run Wait" \
com.github.actions.description="wait for all `workflow_run` required workflows to be successful" \
com.github.actions.icon="clock" \
com.github.actions.color="blue" \
maintainer="Ahmad Nassri <[email protected]>"

RUN mkdir /action
WORKDIR /action

COPY action ./

RUN npm ci --only=prod

ENTRYPOINT ["node", "/action/index.js"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Ahmad Nassri <[email protected]>

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.
56 changes: 55 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,56 @@
# action-workflow-run-wait
# GitHub Action: Workflow Run Wait

wait for all `workflow_run` required workflows to be successful

[![license][license-img]][license-url]
[![release][release-img]][release-url]
[![super linter][super-linter-img]][super-linter-url]
[![test][test-img]][test-url]
[![semantic][semantic-img]][semantic-url]

## Usage

``` yaml
on:
workflow_run:
workflows: [ test-client, test-server ]
branches: [ master ]
types: [ completed ]

jobs:
xyz:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: ahmadnassri/action-workflow-run-wait@v1

# only runs additional steps if [ test-client, test-server ] were successful
```

### Inputs

| input | required | default | description |
|----------------|----------|----------------|---------------------------------------------------------|
| `github-token` || `github.token` | The GitHub token used to post comments on pull requests |
| `timeout` || `30000` | timeout before we stop trying (in milliseconds) |
| `delay` || `5000` | delay between status checks (in milliseconds) |

----
> Author: [Ahmad Nassri](https://www.ahmadnassri.com/) &bull;
> Twitter: [@AhmadNassri](https://twitter.com/AhmadNassri)
[license-url]: LICENSE
[license-img]: https://badgen.net/github/license/ahmadnassri/action-workflow-run-wait

[release-url]: https://github.com/ahmadnassri/action-workflow-run-wait/releases
[release-img]: https://badgen.net/github/release/ahmadnassri/action-workflow-run-wait

[super-linter-url]: https://github.com/ahmadnassri/action-workflow-run-wait/actions?query=workflow%3Asuper-linter
[super-linter-img]: https://github.com/ahmadnassri/action-workflow-run-wait/workflows/super-linter/badge.svg

[test-url]: https://github.com/ahmadnassri/action-workflow-run-wait/actions?query=workflow%3Atest
[test-img]: https://github.com/ahmadnassri/action-workflow-run-wait/workflows/test/badge.svg

[semantic-url]: https://github.com/ahmadnassri/action-workflow-run-wait/actions?query=workflow%3Arelease
[semantic-img]: https://badgen.net/badge/📦/semantically%20released/blue
23 changes: 23 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Terraform Pull Request Report Generator
description: Updates Pull Requests with visual diff of Terraform Plan changes

branding:
color: blue
icon: clock

inputs:
github-token:
description: The GitHub token used to post comments on pull requests
default: ${{ github.token }}

timeout:
description: timeout before we stop trying (in milliseconds)
default: 30000

delay:
description: delay between status checks (in milliseconds)
default: 5000

runs:
using: docker
image: docker://ghcr.io/ahmadnassri/action-workflow-run-wait
38 changes: 38 additions & 0 deletions action/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// packages
import core from '@actions/core'
import github from '@actions/github'

// modules
import main from './lib/index.js'

// exit early
if (github.context.eventName !== 'workflow_run') {
core.error('action triggered outside of a workflow_run')
process.exit(1)
}

// parse inputs
const inputs = {
token: core.getInput('github-token', { required: true }),
delay: core.getInput('delay', { required: true }),
timeout: core.getInput('timeout', { required: true })
}

// error handler
function errorHandler ({ message, stack, request }) {
core.error(`${message}\n${stack}`)

// debugging for API calls
if (request) {
const { method, url, body, headers } = request
core.debug(`${method} ${url}\n\n${inspect(headers)}\n\n${inspect(body)}`)
}

process.exit(1)
}

// catch errors and exit
process.on('unhandledRejection', errorHandler)
process.on('uncaughtException', errorHandler)

await main(inputs)
43 changes: 43 additions & 0 deletions action/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// packages
import core from '@actions/core'
import github from '@actions/github'

import runs from './runs.js'
import workflows from './workflows.js'

// sleep function
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))

export default async function ({ token, delay, timeout }) {
// init octokit
const octokit = github.getOctokit(token)

let timer = 0

const flows = await workflows(octokit)

// check runs
let result = await runs(octokit, flows)

while (result.find(run => run.conclusion !== 'success')) {
timer += delay

// time out!
if (timer >= timeout) {
core.setFailed('workflow-run-wait timed out')
process.exit(1)
}

for (const run of result) {
core.info(`${run.id}: ${run.name} => ${run.conclusion || 'pending'}`)
}

core.info(`runs were not successful, or have not started, try again in ${delay}`)

// zzz
await sleep(delay)

// get the data again
result = await runs(octokit, flows)
}
}
16 changes: 16 additions & 0 deletions action/lib/runs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import github from '@actions/github'

export default async function (octokit, workflows) {
// extract sha
const { sha } = github.context

const { data: { workflow_runs } } = await octokit.request('GET /repos/{owner}/{repo}/actions/runs', { // eslint-disable-line camelcase
...github.context.repo
})

return workflow_runs
// filter to relevant runs
.filter(run => workflows.includes(run.name) && run.head_sha === sha)
// pick properties
.map(run => ({ id: run.id, name: run.name, conclusion: run.conclusion }))
}
33 changes: 33 additions & 0 deletions action/lib/workflows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import yaml from 'yaml'
import github from '@actions/github'

export default async function (octokit) {
// extract sha
const { ref, runId: run_id } = github.context // eslint-disable-line camelcase

// get workflow id from run id
const { data: { workflow_id } } = await octokit.request('GET /repos/{owner}/{repo}/actions/runs/{run_id}', { // eslint-disable-line camelcase
owner: 'hashtagpaid',
repo: 'template-api',
run_id
})

// get the file name from the workflow
const { data: { path } } = await octokit.request('GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}', {
owner: 'hashtagpaid',
repo: 'template-api',
workflow_id
})

// get the workflow content
const { data: { content } } = await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
owner: 'hashtagpaid',
repo: 'template-api',
ref,
path
})

const { on: { workflow_run: { workflows } } } = yaml.parse(Buffer.from(content, 'base64').toString())

return workflows
}
Loading

0 comments on commit fe8f79f

Please sign in to comment.