Skip to content

Commit 55d1430

Browse files
Merge pull request #219 from ntsim/gitlab-support
Add GitLab support
2 parents ea31c99 + d7e5e8f commit 55d1430

40 files changed

+11372
-1690
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
.idea/
2+
*.iml
3+
14
config.json
25
config.*.json
6+
!config.test.json
37
!config.example.json
48
node_modules/
59
staticman_key

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ cache:
55
notifications:
66
email: false
77
node_js:
8-
- '6'
8+
- '8.11.3'

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM node:6.7.0
1+
FROM node:8.11.3
22

33
# Create app directory
44
RUN mkdir -p /app

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<img src="logo.png" width="300">
22

3-
# Staticman [![coverage](https://img.shields.io/badge/coverage-53%25-red.svg?style=flat)](https://github.com/eduardoboucas/staticman) [![Build Status](https://travis-ci.org/eduardoboucas/staticman.svg?branch=master)](https://travis-ci.org/eduardoboucas/staticman) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
3+
# Staticman [![coverage](https://img.shields.io/badge/coverage-82%25-yellow.svg?style=flat)](https://github.com/eduardoboucas/staticman) [![Build Status](https://travis-ci.org/eduardoboucas/staticman.svg?branch=master)](https://travis-ci.org/eduardoboucas/staticman) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
44

55
> Static sites with superpowers
66
@@ -14,7 +14,7 @@ You can download and run the Staticman API on your own infrastructure, or you ca
1414

1515
## Requirements
1616

17-
- Node.js 4.8.3+
17+
- Node.js 8.11.3+
1818
- npm
1919
- A [personal access token](https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/) for the GitHub account you want to run Staticman with
2020
- An SSH key (click [here](https://help.github.com/articles/connecting-to-github-with-ssh/) to learn how to create one)
@@ -102,5 +102,6 @@ Would you like to contribute to Staticman? That's great! Here's how:
102102
- [Tyne Time](https://www.tynetime.com) ([Source](https://github.com/Doocey/tyne-time-hugo))
103103
- [BinaryMist](https://binarymist.io/blog) ([Source](https://github.com/binarymist/BinaryMistBlog))
104104
- [La ruta de la cebada](https://larutadelacebada.com) ([Source](https://github.com/lasocial/larutadelacebada.github.io))
105+
- [Gatsby Central](https://www.gatsbycentral.com) ([Source](https://github.com/GatsbyCentral/gatsbycentral.com))
105106

106107
Are you using Staticman? [Let us know!](https://github.com/eduardoboucas/staticman/edit/master/README.md)

config.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,42 @@ const schema = {
5454
default: 'development',
5555
env: 'NODE_ENV'
5656
},
57+
githubAccessTokenUri: {
58+
doc: 'URI for the GitHub authentication provider.',
59+
format: String,
60+
default: 'https://github.com/login/oauth/access_token',
61+
env: 'GITHUB_ACCESS_TOKEN_URI'
62+
},
63+
githubBaseUrl: {
64+
doc: 'Base URL for the GitHub API.',
65+
format: String,
66+
default: 'https://api.github.com',
67+
env: 'GITHUB_BASE_URL'
68+
},
5769
githubToken: {
5870
doc: 'Access token to the GitHub account being used to push files with.',
5971
format: String,
6072
default: null,
6173
env: 'GITHUB_TOKEN'
6274
},
75+
gitlabAccessTokenUri: {
76+
doc: 'URI for the GitLab authentication provider.',
77+
format: String,
78+
default: 'https://gitlab.com/oauth/token',
79+
env: 'GITLAB_ACCESS_TOKEN_URI'
80+
},
81+
gitlabBaseUrl: {
82+
doc: 'Base URL for the GitLab API.',
83+
format: String,
84+
default: 'https://gitlab.com',
85+
env: 'GITLAB_BASE_URL'
86+
},
87+
gitlabToken: {
88+
doc: 'Access token to the GitLab account being used to push files with.',
89+
format: String,
90+
default: null,
91+
env: 'GITLAB_TOKEN'
92+
},
6393
port: {
6494
doc: 'The port to bind the application to.',
6595
format: 'port',

config.sample.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"gitlabToken": "YOUR_GITLAB_TOKEN",
23
"githubToken": "YOUR_GITHUB_TOKEN",
34
"rsaPrivateKey": "-----BEGIN RSA PRIVATE KEY-----YOUR_KEY-----END RSA PRIVATE KEY-----",
45
"port": 80

config.test.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"gitlabToken": "r4e3w2q1",
23
"githubToken": "1q2w3e4r",
34
"rsaPrivateKey": "-----BEGIN RSA PRIVATE KEY-----MIIEpAIBAAKCAQEA2nX81/5w6nZLolEh0uKks3//FlqizKxFWi8GaEJYbud8FYwLH6l+LDfZkjQiDXvm6mxngDGCjlG9b7hgfa/sfWSuqtJUQ2D1Nenn11gwAUaI7OQNSassE+nVFL2BGedl6DOgpZFkKrDTQT7jyvvc3r/2IqWahAYyFWXImD50qwNWIGyZ2Sry2/WXeCydiUWV8ZG3GOlGZLgtlK8igdmLje/6Ja+1oRBFwdbWrf/nKsMB0fCL3R5MkWTnumR8sGx2Xud8Q3mF7cOYWQiy2AqrFQOzJ+QwH6hO6CGVk8eBh8xRkrjaZEsvolZ33N+0aS9NBxHuOyGq0Te9HMLFqwVGzwIDAQABAoIBAQDPD4gAnb0erdMQXT/m50TekdIQuQWXYy00xl+XUFMLg0L8FUmxz++0L5d72Qfxqd97kBYlzkeFZ3pbOvHSD33ieByJ8mNFnc+tMy+4z3BotRcxGRJzIdfcZAS/7MJB8C6KAO0iIQVE5WbGb7pu+XwmcOH1gutKeajc2SVhD8l7ECRARPuV3JxlhbVt+ylM05h7+5HwwnEmodNQHPeGvLEWp7eODNdcLLE5EFVjmN0dwGu9D+xCFna9bL7p921GOTGf349l3021ONaGvWd77Frhl9tHL1k40hqq4LiXDk3RkiP2oDb65+ohW128Q1QPgjMQuXbbFTCa3++pbz7OMbUhAoGBAPxt6A5B3s5GnnaCTE0YJ13aTeUyqrLimBxWEp7L/WXug8SDBW5tVAmBNHlZC4GuR089E7g26BVr0i3k/dOknKa0GLyGGJNphxrrcD05p+XB6Gd6/N1Yr6amM+8XqU1+UKLL2CBrLOSi6r4J4fPS8sb9362+FROwCJZfxd5Wznd/AoGBAN2NEz+anDBgulxLoguGuvc0FaqSsVIP/cdb5Ve5t29/uheVjLgDXSkV5P/RFfk1ygH1gNjmUrsoxfsGWQa3KMTk292maYRj7GPpOalSlp8BYyiQ9omPdOgusVzY4CEjMiDkKal0YrBswXKV8E2lTokEEkbdrQpToVdBmRCzT1ixAoGBAMNMBQ2CyO6ulEr75CyBU3O5QirhWE+uICFMNnvFNvd14VxYQgt9alcwL3jy+4QJYgJcLrWHRWfNU39Oe1MTOF+BVuIEnV7vdifNn7i+srd/nl7xOFHIG53DWMrSc5oQ8DIDo+LxCHqb0SHWY4pQ2qQ6JqQ1O/lPaFVvI5cxcevhAoGALyNtDQNgaAqsnCabe8hWz3INihVcFRHB9UDgMyIYnWiXt5ziK+TLVYqLBsL94eBH8tLBb2TSBXBPb3GST5N44SuwfCCEIt7/1OIymowv7/TjnCX7zpjvTtdgdVjPlz1d1RG2q49P0CnTnwW3801QwGZVXS6dOq1AjsguQRdlsoECgYBrFqKa3S48UbGOKkocgMb/1HzVs2sxNWMu+/jEiBvI4RrCiun9MALtrVtohgHRml4yZJaOojVk9F0z/MAak9eTgSmHpwxYZMqR1PvM4c/ekfun4hQoK04Thoexi/Z0RfsI6nBdBk8MAra03ldDvnYZY4Fat21YuNBspbrbTW8UoA==-----END RSA PRIVATE KEY-----",
45
"email": {

controllers/auth.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'use strict'
2+
3+
const gitFactory = require('../lib/GitServiceFactory')
4+
const oauth = require('../lib/OAuth')
5+
const RSA = require('../lib/RSA')
6+
const Staticman = require('../lib/Staticman')
7+
8+
module.exports = (req, res) => {
9+
const staticman = new Staticman(req.params)
10+
staticman.setConfigPath()
11+
12+
let requestAccessToken
13+
14+
switch (req.params.service) {
15+
case 'gitlab':
16+
requestAccessToken = siteConfig =>
17+
oauth.requestGitLabAccessToken(
18+
req.query.code,
19+
siteConfig.get('gitlabAuth.clientId'),
20+
siteConfig.get('gitlabAuth.clientSecret'),
21+
siteConfig.get('gitlabAuth.redirectUri')
22+
)
23+
break
24+
default:
25+
requestAccessToken = siteConfig =>
26+
oauth.requestGitHubAccessToken(
27+
req.query.code,
28+
siteConfig.get('githubAuth.clientId'),
29+
siteConfig.get('githubAuth.clientSecret'),
30+
siteConfig.get('githubAuth.redirectUri')
31+
)
32+
}
33+
34+
return staticman.getSiteConfig()
35+
.then(requestAccessToken)
36+
.then((accessToken) => {
37+
const git = gitFactory.create(req.params.service, {
38+
oauthToken: accessToken
39+
})
40+
41+
// TODO: Simplify this when v2 support is dropped.
42+
const getUser = req.params.version === '2' && req.params.service === 'github'
43+
? git.api.users.get({}).then(({data}) => data)
44+
: git.getCurrentUser()
45+
46+
return getUser
47+
.then((user) => {
48+
res.send({
49+
accessToken: RSA.encrypt(accessToken),
50+
user
51+
})
52+
})
53+
})
54+
.catch((err) => {
55+
console.log('ERR:', err)
56+
57+
const statusCode = err.statusCode || 401
58+
59+
res.status(statusCode).send({
60+
statusCode,
61+
message: err.message
62+
})
63+
})
64+
}

controllers/connect.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@ module.exports = (req, res) => {
1212
const github = new GitHub({
1313
username: req.params.username,
1414
repository: req.params.repository,
15-
branch: req.params.branch
15+
branch: req.params.branch,
16+
token: config.get('githubToken')
1617
})
1718

18-
github.authenticateWithToken(config.get('githubToken'))
19+
return github.api.users.getRepoInvites({}).then(({data}) => {
20+
let invitationId = null
1921

20-
return github.api.users.getRepoInvites({}).then(response => {
21-
let invitationId
22-
23-
const invitation = response.some(invitation => {
22+
const invitation = data.some(invitation => {
2423
if (invitation.repository.full_name === (req.params.username + '/' + req.params.repository)) {
2524
invitationId = invitation.id
2625

@@ -30,7 +29,7 @@ module.exports = (req, res) => {
3029

3130
if (invitation) {
3231
return github.api.users.acceptRepoInvite({
33-
id: invitationId
32+
invitation_id: invitationId
3433
})
3534
} else {
3635
res.status(404).send('Invitation not found')

controllers/githubAuth.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)