Skip to content

Commit

Permalink
Merge pull request #9 from liuderchi/poc/custom-template
Browse files Browse the repository at this point in the history
Custom Templates
  • Loading branch information
liuderchi committed Nov 22, 2017
2 parents c9d73bd + 1cf1483 commit de09870
Show file tree
Hide file tree
Showing 17 changed files with 342 additions and 111 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ plugins:
parserOptions:
ecmaVersion: 8
sourceType: module
ecmaFeatures:
experimentalObjectRestSpread: true
rules:
prettier/prettier:
- off
Expand All @@ -28,7 +30,7 @@ rules:
- error
- arrays: always-multiline
objects: always-multiline
functions: ignore
functions: never
indent:
- error
- 2
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- `/template issue` for general issues
- `/template feature` to *Propose a Feature* in issue
- `/template pr` for general pull request
- `/template ReplyDup` to fetch your own `.github/ReplyDup.md` template
- `/template` to ask :robot: for help

3. Refresh the page. Template Bot will create Markdown Template for you :tada:
Expand All @@ -42,7 +43,7 @@

Forgot command? Don't worry. Bot will help you :100:

<img width="650" alt="demo3" src="https://user-images.githubusercontent.com/4994705/32692267-6f6cf030-c6da-11e7-900d-f2c137ca4a1c.png">
<img width="650" alt="demo3" src="https://user-images.githubusercontent.com/4994705/33139825-ff63b434-cf73-11e7-87c3-076007336a95.png">


## Why
Expand Down Expand Up @@ -119,9 +120,9 @@ MIT license https://liuderchi.mit-license.org
[gh-issue-doc]: https://help.github.com/articles/creating-an-issue-template-for-your-repository/ "gh-issue-doc"
[prob-to-solve]: https://github.com/RichardLitt/knowledge/issues/9 "prob-to-solve"

[demo1]: https://user-images.githubusercontent.com/4994705/32691982-f0bc901e-c6d5-11e7-8328-2a06bedf2db6.gif "demo1"
[demo2]: https://user-images.githubusercontent.com/4994705/32692022-8b8167dc-c6d6-11e7-954c-e8fbb39b4ad0.gif "demo2"
[demo3]: https://user-images.githubusercontent.com/4994705/32692267-6f6cf030-c6da-11e7-900d-f2c137ca4a1c.png "demo3"
[demo1]: https://user-images.githubusercontent.com/4994705/33142241-588e1f8e-cf7b-11e7-8e6d-73b355ac3a3a.gif "demo1"
[demo2]: https://user-images.githubusercontent.com/4994705/33141626-5dfc957e-cf79-11e7-9c37-42dd15c4d2db.gif "demo2"
[demo3]: https://user-images.githubusercontent.com/4994705/33139825-ff63b434-cf73-11e7-87c3-076007336a95.png "demo3"

[doc-gh-app]: https://probot.github.io/docs/development/#configure-a-github-app "doc-gh-app"

Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
"changelog": "github-changes -o liuderchi -r gh-template-bot -b master -f ./CHANGELOG.md -t CHANGELOG --order-semver --use-commit-body"
},
"dependencies": {
"handlebars": "^4.0.11",
"minimist": "^1.2.0",
"probot": "^3.0.0",
"ramda": "^0.25.0"
"ramda": "^0.25.0",
"request": "^2.34",
"request-promise": "^4.2.2"
},
"devDependencies": {
"eslint": "^4.11.0",
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { handleIssueWH, handlePullRequestWH } = require('./webhookHandler')
const { handleIssueWH, handleIssueCommentWH, handlePullRequestWH } = require('./webhookHandler')

module.exports = robot => {
robot.on(['issues.opened', 'issues.edited'], handleIssueWH)
robot.on(['issue_comment.created', 'issue_comment.edited'], handleIssueCommentWH)
robot.on(['pull_request.opened', 'pull_request.edited'], handlePullRequestWH)
}
21 changes: 0 additions & 21 deletions src/templates/dialog.js

This file was deleted.

38 changes: 38 additions & 0 deletions src/templates/dialog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!-- this is result of command `/template`-->

## 🤖 Hello {{ withDefault username "Human" }}! I Understand These Commands:

### `/template issue`

open `.github/ISSUE_TEMPLATE.md` or a *[sample issue][issue]*

### `/template pr`

open `.github/PULL_REQUEST_TEMPLATE.md` or a *[sample pull request][pr]*

### `/template feature`

open *[a template to propose new feature][feature]*

### `/template duplicate --num 23`

open custom [Handlebars Template][handlebarsjs], in this case `.github/duplicate.md` with `num` set to 23

for example, you've had `.github/duplicate.md` like [this][example]:

```markdown
<!-- .github/duplicate.md -->
😢 This is duplicate of #{{ withDefault __ "{{ num }}" }}.
```

I would say `😢 This is duplicate of #23.`

---

> Remember to :recycle:**Refresh This Page** after updating your command.
[issue]: https://github.com/TalAter/open-source-templates
[pr]: https://raw.githubusercontent.com/stevemao/github-issue-templates/master/simple/PULL_REQUEST_TEMPLATE.md
[feature]: https://github.com/TalAter/open-source-templates
[handlebarsjs]: http://handlebarsjs.com/
[example]: https://github.com/liuderchi/gh-template-bot/blob/master/src/templates/duplicate.md
3 changes: 3 additions & 0 deletions src/templates/duplicate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- sample command: /template duplicate --num 23 -->

😢 This is duplicate of #{{ num }}.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// https://www.talater.com/open-source-templates/#/page/98
<!-- https://www.talater.com/open-source-templates/#/page/98 -->

const featureProposal = `
<!--- Provide a general summary of the issue in the Title above -->

## Detailed Description
Expand All @@ -19,9 +18,3 @@ const featureProposal = `
* Environment name and version (e.g. Chrome 39, node.js 5.4):
* Operating System and version (desktop or mobile):
* Link to your project:
`


module.exports = {
featureProposal,
}
11 changes: 2 additions & 9 deletions src/templates/simple_issue.js → src/templates/issue.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// https://raw.githubusercontent.com/stevemao/github-issue-templates/master/simple/ISSUE_TEMPLATE.md
// https://www.talater.com/open-source-templates/#/page/98
<!-- https://raw.githubusercontent.com/stevemao/github-issue-templates/master/simple/ISSUE_TEMPLATE.md -->
<!-- https://www.talater.com/open-source-templates/#/page/98 -->

const simpleIssue = `
<!--- Provide a general summary of the issue in the Title above -->

## Description
Expand Down Expand Up @@ -33,9 +32,3 @@ const simpleIssue = `
* Environment name and version (e.g. Chrome 39, node.js 5.4):
* Operating System and version (desktop or mobile):
* Link to your project:
`


module.exports = {
simpleIssue,
}
10 changes: 10 additions & 0 deletions src/templates/pr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!-- https://www.talater.com/open-source-templates/#/page/99 -->
<!-- https://raw.githubusercontent.com/stevemao/github-issue-templates/master/simple/PULL_REQUEST_TEMPLATE.md -->

Fixes #{{ n }}

## Proposed Changes

-
-
-
17 changes: 0 additions & 17 deletions src/templates/simple_pr.js

This file was deleted.

98 changes: 93 additions & 5 deletions src/utils/__tests__/template.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { getCommand, insertTemplate } = require('../template')
const { getCommand, validateAction, getMDContent } = require('../template')

describe('getCommand()', () => {
test('returns null action when issue body is empty', () => {
Expand All @@ -7,18 +7,47 @@ describe('getCommand()', () => {
action: null,
})
})
test('returns DIALOG action when issue body starts with `/template`, but not match issue|pr|feature', () => {
expect(getCommand('/template foo '))
test('returns null action when issue body starts with `/Template` which is not fully match', () => {
expect(getCommand('/Template foo'))
.toMatchObject({
action: null,
})
})
test('returns Dialog action when issue body starts with `/template`, but not match issue|pr|feature', () => {
expect(getCommand('\n\n /template\t '))
.toMatchObject({
action: 'DIALOG',
})
})
test('returns Specific action when issue body starts with `/template`, but not match issue|pr|feature', () => {
expect(getCommand('/template foo '))
.toMatchObject({
action: 'FOO',
})
expect(getCommand('/template features'))
.toMatchObject({
action: 'DIALOG',
action: 'FEATURES',
})
})
test('returns Specific action with options when issue body contains option syntax', () => {
expect(getCommand('/template issue --number 123'))
.toMatchObject({
action: 'ISSUE',
options: {
number: 123,
},
})
expect(getCommand('/template issue dummy -f --name derek --action filtered'))
.toMatchObject({
action: 'ISSUE',
options: {
f: true,
name: 'derek',
},
})
})
test('returns null action when issue body not starts with `/template`, without special markdown checkbox format', () => {
expect(getCommand('\n\n/template pr'))
expect(getCommand('\nsome words\n/template pr'))
.toMatchObject({
action: null,
})
Expand All @@ -36,3 +65,62 @@ describe('getCommand()', () => {
})

})

describe('getMDContent()', () => {

test('insert dialog without options', async () => {
const newContent = await getMDContent(
{
action: 'DIALOG',
options: {},
},
null,
'/template'
)
expect(newContent.split('\n')[2])
.toBe('## 🤖 Hello Human! I Understand These Commands:')})

test('insert dialog with options', async () => {
const newContent = await getMDContent(
{
action: 'DIALOG',
options: {username: 'derek'},
},
null,
'/template --username derek'
)
expect(newContent.split('\n')[2])
.toBe('## 🤖 Hello derek! I Understand These Commands:')})
})

describe('validateAction()', () => {

test('if action is not a preserve word nor match any templates, modify it to default `DIALOG`', () => {
const command = {
action: 'NO_SUCH_DOC',
options: {},
}
const templates = [
{ name: 'SOME_DOC.md', download_url: 'https://foo.bar/some_doc.md' } ,
]
validateAction(command, templates)
expect(command.action).toBe('DIALOG')
})

test('if action is not a preserve word but match some template, do not modify it', () => {
const action = 'SOME_DOC'
const command = {
action,
options: {},
}
const templates = [
{ name: 'SOME_DOC.md', download_url: 'https://foo.bar/some_doc.md' } ,
]
validateAction(command, templates)
expect(command.action).toBe(action)

command.action = 'pr'
validateAction(command, templates)
expect(command.action).toBe('pr')
})
})
20 changes: 20 additions & 0 deletions src/utils/ghAPI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const R = require('ramda')


const getCustomTemplates = async (gitHubContext, { owner, repo, path='.github' }) => {
const { repos: { getContent } } = gitHubContext
try {
const { data: customTemplates } = await getContent({ owner, repo, path })
return customTemplates
.filter(({ name, type }) => R.match(/\.md$/, name).length > 0 && type === 'file')
.map(R.pick(['name', 'download_url']))
} catch (e) {
console.error(e)
return []
}
}


module.exports = {
getCustomTemplates,
}
8 changes: 8 additions & 0 deletions src/utils/handlebars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

const Handlebars = require('handlebars')

Handlebars.registerHelper('withDefault', (x, defaultValue) => x ? x : defaultValue)

module.exports = {
Handlebars,
}
Loading

0 comments on commit de09870

Please sign in to comment.