Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
abskmj committed Jul 5, 2020
0 parents commit 254a558
Show file tree
Hide file tree
Showing 12 changed files with 2,506 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Publish NPM Package

on:
release:
types: [published]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- run: npm ci
- run: npm test

publish-npm:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
registry-url: https://registry.npmjs.org/
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

publish-gpr:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
registry-url: https://npm.pkg.github.com/abskmj
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2018 abskmj@gmail.com

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
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Hukum
Hukum is an NPM module that prints Github Actions (GA) Workflow status in the terminal. It works for workflows that run when a commit is pushed to Github repo. Once a commit is pushed, Hukum will print status updates in realtime.

Hukum aims to improve your development process with immediate feedback of the GA Workflow on your terminal.

# Installation
```bash
npm install hukum
```
# How it works?
Hukum uses [Github Actions API](https://developer.github.com/v3/actions/) to get the related workflow to the recent git push and its status. It keeps on calling the APIs every 1 second to update the status on the terminal.

Below is a sample output for one of the workflows.

```
Add post - Setup Snapcraft in a Github Action Workflow
✔ deploy 16s
✔ Set up job 2s
✔ Run actions/checkout@v2 0s
✔ Install hugo 8s
✔ Install hugo theme 1s
✔ Build 0s
✔ Deploy 5s
✔ Post Run actions/checkout@v2 0s
✔ Complete job 0s
```

# Configuration
Hukum can work out of the box without any configuration. However, it provides a few configuration options. Include a `.hukumrc` file in the root of the project with the following contents.

```json
{
"github": {
"token": "<token>"
}
}
```

## Github Personal token
```
github.token
```

Hukum uses [Github Actions API](https://developer.github.com/v3/actions/). It is possible to use these APIs without any authentication for public repositories. However, for unauthenticated requests, the rate limit allows for up to 60 requests per hour (Details at [developer.github.com](https://developer.github.com/v3/#rate-limiting)) which can exhaust quickly. Authenticated requests have higher limits, up to 5000 requests per hour.


You can follow these steps at [docs.github.com](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) to create a personal token.

# Fixes & Improvements
Head over to the issues tab at [github.com](https://github.com/abskmj/hukum/issues) to report a bug or suggest an improvement. Feel free to contribute to the code or documentation by raising a pull request.
13 changes: 13 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#! /usr/bin/env node
const hukum = require('../lib/hukum')

// exit the process when enter is pressed
process.stdin.resume()
process.stdin.on('data', d => {
process.exit(0)
})

hukum.start()
.then(() => {
process.exit(0)
})
3 changes: 3 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const rc = require('rcfile')

module.exports = rc('hukum')
35 changes: 35 additions & 0 deletions lib/gh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const axios = require('axios')
const config = require('./config')

module.exports.getRepo = (repo) => {
const options = { baseURL: `https://api.github.com/repos/${repo}/actions` }

if (config && config.github && config.github.token) {
options.headers = {}
options.headers.Authorization = `bearer ${config.github.token}`
}

const http = axios.create(options)

const getWorkflows = () => {
return http.get('/workflows')
}

const getRuns = (branch) => {
return http.get(`/runs?branch=${branch}`)
}

const getJobs = (run) => {
return http.get(`/runs/${run}/jobs`)
}

const getLogs = (run) => {
return http.get(`/runs/${run}/logs`)
}
return {
getWorkflows,
getRuns,
getJobs,
getLogs
}
}
26 changes: 26 additions & 0 deletions lib/git.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

const git = require('simple-git')()

module.exports.getInfo = async () => {
const info = {}

const status = await git.status()
info.branch = status.current

const log = await git.log()

info.hash = log.latest.hash
info.author = log.latest.author_email

const remotes = await git.getRemotes(true)
const gh = remotes.find((remote) => remote.refs.push.includes('github.com'))

info.repo = gh.refs.push
.replace('https://github.com/', '')
.replace('git@github.com:', '')
.replace('.git', '')

console.log(info)

return info
}
48 changes: 48 additions & 0 deletions lib/hukum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

const link = require('terminal-link')

const gh = require('./gh')
const git = require('./git')
const printer = require('./print')

const sleep = (ms = 1000) => new Promise((resolve) => {
setTimeout(resolve, ms)
})

module.exports.start = async () => {
try {
const gitInfo = await git.getInfo()

const repo = gh.getRepo(gitInfo.repo)

const runRes = await repo.getRuns(gitInfo.branch)

const run = runRes.data.workflow_runs.find(async (run) => {
return run.head_branch === gitInfo.branch && run.head_sha === gitInfo.hash
})

console.log(' ', link(run.head_commit.message, run.html_url))

while (true) {
const jobRes = await repo.getJobs(run.id)

let jobsComplete = true

jobRes.data.jobs.map((job) => {
jobsComplete = jobsComplete && job.status === 'completed'

printer.printItem(job, ' ')

job.steps.map((step) => {
printer.printItem(step, ' ')
})
})

if (jobsComplete) break

await sleep()
}
} catch (err) {
console.error(err)
}
}
38 changes: 38 additions & 0 deletions lib/print.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const figures = require('figures')
const colors = require('ansi-colors')
const moment = require('moment')
require('draftlog').into(console)

const cache = {}

module.exports.printItem = (item, indent) => {
const index = item.id || item.number
let draft = cache[index]

if (!draft) {
cache[index] = draft = console.draft('')
}

if (item.status === 'completed') {
if (item.conclusion === 'success') {
draft(indent,
colors.green(`${figures.tick} ${item.name}`),
colors.grey(`${moment(item.completed_at).diff(moment(item.started_at), 'seconds')}s`)
)
} else if (item.conclusion === 'failure') {
draft(indent,
colors.red(`${figures.cross} ${item.name}`),
colors.grey(`${moment(item.completed_at).diff(moment(item.started_at), 'seconds')}s`)
)
}
} else if (item.status === 'in_progress') {
draft(indent,
colors.yellow(`${figures.play} ${item.name}`),
colors.grey(`${moment().diff(moment(item.started_at), 'seconds')}s`)
)
} else if (item.status === 'queued') {
draft(indent,
colors.grey(`${figures.ellipsis} ${item.name}`)
)
}
}
Loading

0 comments on commit 254a558

Please sign in to comment.