Skip to content

Commit

Permalink
Merge pull request #16 from snow-actions/feature/tweet
Browse files Browse the repository at this point in the history
Tweet
  • Loading branch information
SnowCait authored Aug 24, 2020
2 parents 37fd47c + 4002764 commit 322588d
Show file tree
Hide file tree
Showing 12 changed files with 33,832 additions and 572 deletions.
12 changes: 11 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,20 @@ jobs:
npm install
- run: |
npm run all
env:
CONSUMER_API_KEY: ${{ secrets.CONSUMER_API_KEY }}
CONSUMER_API_SECRET_KEY: ${{ secrets.CONSUMER_API_SECRET_KEY }}
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
ACCESS_TOKEN_SECRET: ${{ secrets.ACCESS_TOKEN_SECRET }}
test: # make sure the action works on a clean machine without building
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ./
env:
CONSUMER_API_KEY: ${{ secrets.CONSUMER_API_KEY }}
CONSUMER_API_SECRET_KEY: ${{ secrets.CONSUMER_API_SECRET_KEY }}
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
ACCESS_TOKEN_SECRET: ${{ secrets.ACCESS_TOKEN_SECRET }}
with:
milliseconds: 1000
status: ${{ github.job }} ${{ github.run_id }} ${{ github.run_number }} ${{ github.ref }} ${{ github.sha }}
142 changes: 52 additions & 90 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,101 +1,63 @@
<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>
<a href="https://github.com/snow-actions/tweet/actions"><img alt="snow-actions/tweet status" src="https://github.com/snow-actions/tweet/workflows/build-test/badge.svg"></a>
</p>

# Create a JavaScript Action using TypeScript

Use this template to bootstrap the creation of a TypeScript action.:rocket:

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

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 Main

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

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

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()
# Tweet action
Tweet via GitHub Actions.

## Usage

1. Create your Twitter App in [developer.twitter.com](https://developer.twitter.com/en/apps).
1. Set secrets `TWITTER_CONSUMER_API_KEY`, `TWITTER_CONSUMER_API_SECRET_KEY`, `TWITTER_ACCESS_TOKEN`, `TWITTER_ACCESS_TOKEN_SECRET` in settings.
1. Create workflow YAML.

```yml
name: 'tweet'
on:
push:
branches:
- main

jobs:
tweet:
runs-on: ubuntu-latest
steps:
- name: Tweet
id: tweet
uses: snow-actions/[email protected]
env:
CONSUMER_API_KEY: ${{ secrets.TWITTER_CONSUMER_API_KEY }}
CONSUMER_API_SECRET_KEY: ${{ secrets.TWITTER_CONSUMER_API_SECRET_KEY }}
ACCESS_TOKEN: ${{ secrets.TWITTER_ACCESS_TOKEN }}
ACCESS_TOKEN_SECRET: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }}
with:
status: ${{ github.run_id }}-${{ github.run_number }} ${{ github.sha }}
- run: echo ${{ steps.tweet.outputs.response }}
```
See the [toolkit documentation](https://github.com/actions/toolkit/blob/master/README.md#packages) for the various packages.
## Environments
Authentication parameters.
## Publish to a distribution branch
|name|required|description|
|---|---|---|
|CONSUMER_API_KEY|required|Consumer API key|
|CONSUMER_API_SECRET_KEY|required|Consumer API secret key|
|ACCESS_TOKEN|required|Access token|
|ACCESS_TOKEN_SECRET|required|Access token secret|
Actions are run from GitHub repos so we will checkin the packed dist folder.
## Inputs & Outputs
See [action.yml](action.yml) and [Twitter API reference](https://developer.twitter.com/en/docs/twitter-api/v1/tweets/post-and-engage/api-reference/post-statuses-update).
Then run [ncc](https://github.com/zeit/ncc) and push the results:
```bash
$ npm run package
$ 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))

```yaml
uses: ./
with:
milliseconds: 1000
```
### Inputs
Request parameters.
See the [actions tab](https://github.com/actions/javascript-action/actions) for runs of this action! :rocket:
|name|required|description|
|---|---|---|
|status|required|The text of the status update. URL encode as necessary. [t.co link wrapping](https://developer.twitter.com/en/docs/basics/tco) will affect character counts.|
## Usage:
### Outputs
Response.
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
|name|description|
|---|---|
|response|Response JSON|
17 changes: 4 additions & 13 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
import {wait} from '../src/wait'
import {tweet} from '../src/tweet'
import * as process from 'process'
import * as cp from 'child_process'
import * as path from 'path'

test('throws invalid number', async () => {
const input = parseInt('foo', 10)
await expect(wait(input)).rejects.toThrow('milliseconds not a number')
})

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)
test('tweet timestamp', async () => {
const response = await tweet(Date.now().toString())
})

// shows how the runner will run a javascript action with env / stdout protocol
test('test runs', () => {
process.env['INPUT_MILLISECONDS'] = '500'
process.env['INPUT_STATUS'] = new Date().toLocaleString('ja-JP')
const ip = path.join(__dirname, '..', 'lib', 'main.js')
const options: cp.ExecSyncOptions = {
env: process.env
Expand Down
17 changes: 11 additions & 6 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
name: 'Your name here'
description: 'Provide a description here'
author: 'Your name or organization here'
name: 'Tweet'
description: 'Tweet action'
author: 'SnowCait'
inputs:
milliseconds: # change this
status:
required: true
description: 'input description here'
default: 'default value if applicable'
description: 'The text of the status update. URL encode as necessary. t.co link wrapping will affect character counts.'
outputs:
response:
description: 'Response JSON'
runs:
using: 'node12'
main: 'dist/index.js'
branding:
icon: 'edit'
color: 'blue'
Loading

0 comments on commit 322588d

Please sign in to comment.