Skip to content

Commit

Permalink
Fix GitHub auth, upgrade GitHub API module, allow test suite to execu…
Browse files Browse the repository at this point in the history
…te. (#319)

This is a large change meant to restore the repo to a functioning state. The GitHub library was reworked to use the lastest version of the GitHub API module and changes were made to support authentication as a GitHub application.

Instructions for creating a private instance hosted on the free tier of
Heroku have been added and all references to the public instance have
been removed. Heroku hosting is intended to provide users with a more
reliable alternative to the public instance, which we will be dropping
support for.
  • Loading branch information
alexwaibel authored Dec 11, 2019
1 parent 6129707 commit 0b7ef34
Show file tree
Hide file tree
Showing 20 changed files with 4,322 additions and 4,428 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea/
.vscode/
*.iml

config.json
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ Staticman is a Node.js application that receives user-generated content and uplo

It consists of a small web service that handles the `POST` requests from your forms, runs various forms of validation and manipulation defined by you and finally pushes them to your repository as data files. You can choose to enable moderation, which means files will be pushed to a separate branch and a pull request will be created for your approval, or disable it completely, meaning that files will be pushed to the main branch automatically.

You can download and run the Staticman API on your own infrastructure, or you can simply use the public instance of the Staticman API for free. If using the public instance, you can skip to *[Setting up repository](#setting-up-a-repository)*.
You can download and run the Staticman API on your own infrastructure. The easiest way to get a personal Staticman API instance up and running is to use the free tier of Heroku. If deploying to Heroku you can simply click the button below.

[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy)

## Requirements

Expand Down Expand Up @@ -57,8 +59,6 @@ To add Staticman to a repository, you need to add the bot as a collaborator with
http://your-staticman-url/v2/connect/GITHUB-USERNAME/GITHUB-REPOSITORY
```

If you're using the public instance, the account you want to add is [staticmanapp](https://github.com/staticmanapp) and the URL is https://api.staticman.net/v2/connect/GITHUB-USERNAME/GITHUB-REPOSITORY.

## Site configuration

Staticman will look for a config file. For the deprecated `v1` endpoints, this is a `_config.yml` with a `staticman` property inside; for `v2` endpoints, Staticman looks for a `staticman.yml` file at the root of the repository.
Expand Down
8 changes: 8 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "Staticman",
"description": "A Node.js application that enables dynamic content as part of a fully static website.",
"repository": "https://github.com/eduardoboucas/staticman/",
"logo": "https://staticman.net/assets/images/staticman-avatar.png",
"keywords": ["node", "express", "static", "staticman", "github-pages"]
}

8 changes: 4 additions & 4 deletions controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const oauth = require('../lib/OAuth')
const RSA = require('../lib/RSA')
const Staticman = require('../lib/Staticman')

module.exports = (req, res) => {
const staticman = new Staticman(req.params)
module.exports = async (req, res) => {
const staticman = await new Staticman(req.params)
staticman.setConfigPath()

let requestAccessToken
Expand All @@ -33,8 +33,8 @@ module.exports = (req, res) => {

return staticman.getSiteConfig()
.then(requestAccessToken)
.then((accessToken) => {
const git = gitFactory.create(req.params.service, {
.then(async (accessToken) => {
const git = await gitFactory.create(req.params.service, {
oauthToken: accessToken,
version: req.params.version
})
Expand Down
15 changes: 11 additions & 4 deletions controllers/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,26 @@ const path = require('path')
const config = require(path.join(__dirname, '/../config'))
const GitHub = require(path.join(__dirname, '/../lib/GitHub'))

module.exports = (req, res) => {
module.exports = async (req, res) => {
const ua = config.get('analytics.uaTrackingId')
? require('universal-analytics')(config.get('analytics.uaTrackingId'))
: null

const github = new GitHub({
const github = await new GitHub({
username: req.params.username,
repository: req.params.repository,
branch: req.params.branch,
token: config.get('githubToken')
token: config.get('githubToken'),
version: req.params.version
})

return github.api.repos.listInvitationsForAuthenticatedUser({}).then(({data}) => {
const isAppAuth = config.get('githubAppID') && config.get('githubPrivateKey')

if (isAppAuth) {
return res.send('OK!')
}

return github.api.repos.listInvitationsForAuthenticatedUser({}).then(({ data }) => {
let invitationId = null

const invitation = Array.isArray(data) && data.some(invitation => {
Expand Down
8 changes: 4 additions & 4 deletions controllers/handlePR.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const config = require('../config')
const GitHub = require('../lib/GitHub')
const Staticman = require('../lib/Staticman')

module.exports = (repo, data) => {
module.exports = async (repo, data) => {
const ua = config.get('analytics.uaTrackingId')
? require('universal-analytics')(config.get('analytics.uaTrackingId'))
: null
Expand All @@ -13,13 +13,13 @@ module.exports = (repo, data) => {
return
}

const github = new GitHub({
const github = await new GitHub({
username: data.repository.owner.login,
repository: data.repository.name,
token: config.get('githubToken')
})

return github.getReview(data.number).then((review) => {
return github.getReview(data.number).then(async (review) => {
if (review.sourceBranch.indexOf('staticman_')) {
return null
}
Expand All @@ -34,7 +34,7 @@ module.exports = (repo, data) => {
if (bodyMatch && (bodyMatch.length === 2)) {
try {
const parsedBody = JSON.parse(bodyMatch[1])
const staticman = new Staticman(parsedBody.parameters)
const staticman = await new Staticman(parsedBody.parameters)

staticman.setConfigPath(parsedBody.configPath)
staticman.processMerge(parsedBody.fields, parsedBody.options)
Expand Down
4 changes: 2 additions & 2 deletions controllers/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ function sendResponse (res, data) {
res.status(statusCode).send(payload)
}

module.exports = (req, res, next) => {
const staticman = new Staticman(req.params)
module.exports = async (req, res, next) => {
const staticman = await new Staticman(req.params)

staticman.setConfigPath()
staticman.setIp(req.headers['x-forwarded-for'] || req.connection.remoteAddress)
Expand Down
17 changes: 11 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const StaticmanAPI = require('./server')
(async () => {
try {
const StaticmanAPI = require('./server')
const api = await new StaticmanAPI()

const api = new StaticmanAPI()

api.start(port => {
console.log('Staticman API running on port', port)
})
api.start(port => {
console.log('Staticman API running on port', port)
})
} catch (e) {
console.error(e)
}
})()
Loading

0 comments on commit 0b7ef34

Please sign in to comment.