Skip to content

Commit

Permalink
Merge pull request #2 from kanga333/initial-impl
Browse files Browse the repository at this point in the history
Initial impl
  • Loading branch information
kanga333 authored Jul 25, 2020
2 parents ccadde2 + a2cc916 commit fb3fe17
Show file tree
Hide file tree
Showing 14 changed files with 12,733 additions and 3,493 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
on:
push:
tags:
- 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10

name: Create Release

jobs:
build:
name: Create Release
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
draft: false
prerelease: false
14 changes: 13 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/[email protected]
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const output = `Hide me.`;
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
- uses: ./
with:
milliseconds: 1000
github_token: ${{ secrets.GITHUB_TOKEN }}
124 changes: 30 additions & 94 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,101 +1,37 @@
<p align="center">
<a href="https://github.com/actions/typescript-action/actions"><img alt="typescript-action status" src="https://github.com/actions/typescript-action/workflows/build-test/badge.svg"></a>
</p>
# Github Action to hide Bot comments

# Create a JavaScript Action using TypeScript
Comment-hider action automatically hides bot comments posted to PR.

Use this template to bootstrap the creation of a TypeScript action.:rocket:
- Automatically hide certain users' comments posted to PR. (The default is `github-actions[bot]`.)

This template includes compilation support, tests, a validation workflow, publishing, and versioning guidance.
## Sample Workflows

If you are new, there's also a simpler introduction. See the [Hello World JavaScript Action](https://github.com/actions/hello-world-javascript-action)

## Create an action from this template

Click the `Use this Template` and provide the new repo details for your action

## Code in Master

Install the dependencies
```bash
$ npm install
```

Build the typescript and package it for distribution
```bash
$ npm run build && npm run pack
```

Run the tests :heavy_check_mark:
```bash
$ npm test

PASS ./index.test.js
✓ throws invalid number (3ms)
wait 500 ms (504ms)
test runs (95ms)

...
```

## Change action.yml

The action.yml contains defines the inputs and output for your action.

Update the action.yml with your name, description, inputs and outputs for your action.

See the [documentation](https://help.github.com/en/articles/metadata-syntax-for-github-actions)

## Change the Code

Most toolkit and CI/CD operations involve async operations so the action is run in an async function.

```javascript
import * as core from '@actions/core';
...

async function run() {
try {
...
}
catch (error) {
core.setFailed(error.message);
}
}

run()
```

See the [toolkit documentation](https://github.com/actions/toolkit/blob/master/README.md#packages) for the various packages.

## Publish to a distribution branch

Actions are run from GitHub repos so we will checkin the packed dist folder.

Then run [ncc](https://github.com/zeit/ncc) and push the results:
```bash
$ npm run pack
$ git add dist
$ git commit -a -m "prod dependencies"
$ git push origin releases/v1
```

Your action is now published! :rocket:

See the [versioning documentation](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md)

## Validate

You can now validate the action by referencing `./` in a workflow in your repo (see [test.yml](.github/workflows/test.yml))
Posting the results of CI/CD in the PR comments is a common practice. Comment-hider action is useful for hiding outdated posts in these cases.

```yaml
uses: ./
with:
milliseconds: 1000
on:
pull_request:
steps:
- uses: actions/checkout@v2

- uses: kanga333/kanga333/comment-hider@master
name: Hide bot comments
with:
github_token: ${{ secrets.GITHUB_TOKEN }}

- id: cicd
run: |
echo "Run some kind of CI/CD."
- uses: actions/[email protected]
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const output = `${{ steps.cicd.outputs.stdout }}`;
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
```
See the [actions tab](https://github.com/actions/javascript-action/actions) for runs of this action! :rocket:
## Usage:
After testing you can [create a v1 tag](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md) to reference the stable and latest V1 action
16 changes: 16 additions & 0 deletions __tests__/list_comment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"id": 1,
"node_id": "not hide me",
"user": {
"login": "user"
}
},
{
"id": 2,
"node_id": "hide me",
"user": {
"login": "bot"
}
}
]
39 changes: 17 additions & 22 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
import {wait} from '../src/wait'
import * as process from 'process'
import * as cp from 'child_process'
import {Client} from '../src/client'
import listComment from './list_comment.json'
import * as path from 'path'
import nock from 'nock'

test('throws invalid number', async () => {
const input = parseInt('foo', 10)
await expect(wait(input)).rejects.toThrow('milliseconds not a number')
})
describe('Hide Comments', () => {
beforeEach(() => {
nock.cleanAll()
process.env.GITHUB_EVENT_PATH = path.join(__dirname, 'payload.json')
})

test('wait 500 ms', async () => {
const start = new Date()
await wait(500)
const end = new Date()
var delta = Math.abs(end.getTime() - start.getTime())
expect(delta).toBeGreaterThan(450)
})
it('should only select the bot comment id', async () => {
const client = new Client('secrets', 'owner', 'repo', 1)
const github = nock('https://api.github.com')
.get(`/repos/owner/repo/issues/1/comments`)
.reply(200, listComment)

const response = await client.SelectComments(`bot`)

// shows how the runner will run a javascript action with env / stdout protocol
test('test runs', () => {
process.env['INPUT_MILLISECONDS'] = '500'
const ip = path.join(__dirname, '..', 'lib', 'main.js')
const options: cp.ExecSyncOptions = {
env: process.env
}
console.log(cp.execSync(`node ${ip}`, options).toString())
expect(response).toStrictEqual(['hide me'])
})
})
15 changes: 15 additions & 0 deletions __tests__/payload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"action": "opened",
"repository": {
"owner": {
"login": "user"
},
"name": "test"
},
"issue": {
"number": 1
},
"sender": {
"type": "User"
}
}
23 changes: 17 additions & 6 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
name: 'Your name here'
description: 'Provide a description here'
author: 'Your name or organization here'
name: 'comment-hider'
description: 'Github Action to hide Bot comments'
branding:
color: gray-dark
icon: check
author: 'kanga333'
inputs:
milliseconds: # change this
description: 'input description here'
default: 'default value if applicable'
github_token:
description: 'The GitHub token used to create an authenticated client'
required: true
hide_user_name:
description: 'Github username to hide comments'
default: 'github-actions[bot]'
required: false
hide_reason:
description: 'Reasons to hide comments'
default: 'OUTDATED'
required: false
runs:
using: 'node12'
main: 'dist/index.js'
Loading

0 comments on commit fb3fe17

Please sign in to comment.