Skip to content
This repository was archived by the owner on Feb 25, 2021. It is now read-only.

Commit 51abf73

Browse files
committed
refactor: functions
1 parent 428beb2 commit 51abf73

File tree

5 files changed

+90
-12
lines changed

5 files changed

+90
-12
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Your source for all things Netlify functions.
66
- [Function examples](https://functions.netlify.com/examples)
77
- [Function tutorials](https://functions.netlify.com/tutorials)
88

9+
[Adding examples](#-adding-an-example)
10+
911
## Configuration
1012

1113
Edit `./_site-config.js`
@@ -21,3 +23,9 @@ npm install
2123
```
2224
npm start
2325
```
26+
27+
## Adding an example
28+
29+
Update the [data/examples.json](./data/examples.json) to add an example.
30+
31+
Update the [data/tutorials.json](./data/tutorials.json) to add a tutorial.

functions/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "add-example",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "add-example.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "MIT",
11+
"dependencies": {
12+
"@octokit/rest": "^16.3.0",
13+
"git-url-parse": "^11.1.2"
14+
}
15+
}

functions/utils/createPullRequest.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
module.exports = octokitCreatePullRequest
2+
3+
function octokitCreatePullRequest (octokit) {
4+
octokit.createPullRequest = createPullRequest.bind(null, octokit)
5+
}
6+
7+
async function createPullRequest (octokit, { owner, repo, title, body, base, head, changes }) {
8+
let response
9+
10+
if (!base) {
11+
response = await octokit.repos.get({ owner, repo })
12+
base = response.data.default_branch
13+
}
14+
15+
response = await octokit.repos.listCommits({
16+
owner,
17+
repo,
18+
sha: base,
19+
per_page: 1
20+
})
21+
let latestCommitSha = response.data[0].sha
22+
const treeSha = response.data[0].commit.tree.sha
23+
24+
response = await octokit.git.createTree({
25+
owner,
26+
repo,
27+
base_tree: treeSha,
28+
tree: Object.keys(changes.files).map(path => {
29+
return {
30+
path,
31+
mode: '100644',
32+
content: changes.files[path]
33+
}
34+
})
35+
})
36+
const newTreeSha = response.data.sha
37+
38+
response = await octokit.git.createCommit({
39+
owner,
40+
repo,
41+
message: changes.commit,
42+
tree: newTreeSha,
43+
parents: [latestCommitSha]
44+
})
45+
latestCommitSha = response.data.sha
46+
47+
await octokit.git.createRef({
48+
owner,
49+
repo,
50+
sha: latestCommitSha,
51+
ref: `refs/heads/${head}`
52+
})
53+
54+
response = await octokit.pulls.create({
55+
owner,
56+
repo,
57+
head,
58+
base,
59+
title,
60+
body
61+
})
62+
return response
63+
}

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
"dev": "npm run develop",
1515
"start": "npm run dev",
1616
"serve": "gatsby serve",
17-
"prebuild": "cd functions/add-example && npm install",
1817
"build": "gatsby build",
1918
"clean": "rm -rf public && rm -rf .cache",
2019
"lint:js": "eslint --ext .js,.jsx .",

src/pages/admin/tutorials/index.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,10 @@ async function saveItem(item) {
2929
if (thing.match(/react-select/)) {
3030
return acc
3131
}
32-
// Force tags to be array
33-
if (thing === 'tags') {
34-
const finalTag = (typeof item[thing] === 'string') ? [item[thing]] : item[thing]
35-
acc[thing] = finalTag
36-
return acc
37-
}
3832
acc[thing] = item[thing]
3933
return acc
4034
}, {})
41-
// console.log('payload', payload)
35+
console.log('payload', payload)
4236
return fetch(`/.netlify/functions/add-tutorial/`, {
4337
method: 'POST',
4438
body: JSON.stringify(payload),
@@ -60,7 +54,6 @@ export default class Admin extends React.Component {
6054
}
6155
componentDidMount() {
6256
const params = paramsParse()
63-
console.log('params', params)
6457
if (params.url) {
6558
const url = (document.getElementsByName('url') || [{value: ''}])[0]
6659
url.value = params.url
@@ -79,8 +72,8 @@ export default class Admin extends React.Component {
7972
saveItem(data)
8073
.then((response) => {
8174
// {"message":"pr created!","url":"https://github.com/DavidWells/functions-site/pull/5"}
82-
console.log('track exampleAdded')
83-
analytics.track('exampleAdded', {
75+
console.log('track tutorialAdded')
76+
analytics.track('tutorialAdded', {
8477
url: url
8578
})
8679
console.log('response', response)
@@ -90,7 +83,7 @@ export default class Admin extends React.Component {
9083
})
9184
}).catch((e) => {
9285
console.log('response err', e)
93-
analytics.track('exampleAdditionFailed')
86+
analytics.track('tutorialAdditionFailed')
9487
})
9588
}
9689
handleSettingsClick = () => {

0 commit comments

Comments
 (0)