Skip to content

Commit

Permalink
Revert "Add support for GitHub Apps"
Browse files Browse the repository at this point in the history
This reverts commit d800786.
  • Loading branch information
BloggerBust-bot committed Apr 11, 2019
1 parent 2be29f7 commit 5069eb7
Show file tree
Hide file tree
Showing 9 changed files with 3,460 additions and 5,172 deletions.
14 changes: 1 addition & 13 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,14 @@ const schema = {
default: 'https://github.com/login/oauth/access_token',
env: 'GITHUB_ACCESS_TOKEN_URI'
},
githubAppID: {
doc: 'ID of the GitHub App.',
format: String,
default: null,
env: 'GITHUB_APP_ID'
},
githubBaseUrl: {
doc: 'Base URL for the GitHub API.',
format: String,
default: 'https://api.github.com',
env: 'GITHUB_BASE_URL'
},
githubPrivateKey: {
doc: 'Private key for the GitHub App.',
format: String,
default: null,
env: 'GITHUB_PRIVATE_KEY'
},
githubToken: {
doc: 'Access token to the GitHub account (legacy)',
doc: 'Access token to the GitHub account being used to push files with.',
format: String,
default: null,
env: 'GITHUB_TOKEN'
Expand Down
1 change: 0 additions & 1 deletion config.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"gitlabToken": "r4e3w2q1",
"githubToken": "1q2w3e4r",
"rsaPrivateKey": "-----BEGIN RSA PRIVATE KEY-----MIIEpAIBAAKCAQEA2nX81/5w6nZLolEh0uKks3//FlqizKxFWi8GaEJYbud8FYwLH6l+LDfZkjQiDXvm6mxngDGCjlG9b7hgfa/sfWSuqtJUQ2D1Nenn11gwAUaI7OQNSassE+nVFL2BGedl6DOgpZFkKrDTQT7jyvvc3r/2IqWahAYyFWXImD50qwNWIGyZ2Sry2/WXeCydiUWV8ZG3GOlGZLgtlK8igdmLje/6Ja+1oRBFwdbWrf/nKsMB0fCL3R5MkWTnumR8sGx2Xud8Q3mF7cOYWQiy2AqrFQOzJ+QwH6hO6CGVk8eBh8xRkrjaZEsvolZ33N+0aS9NBxHuOyGq0Te9HMLFqwVGzwIDAQABAoIBAQD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xLoguGuvc0FaqSsVIP/cdb5Ve5t29/uheVjLgDXSkV5P/RFfk1ygH1gNjmUrsoxfsGWQa3KMTk292maYRj7GPpOalSlp8BYyiQ9omPdOgusVzY4CEjMiDkKal0YrBswXKV8E2lTokEEkbdrQpToVdBmRCzT1ixAoGBAMNMBQ2CyO6ulEr75CyBU3O5QirhWE+uICFMNnvFNvd14VxYQgt9alcwL3jy+4QJYgJcLrWHRWfNU39Oe1MTOF+BVuIEnV7vdifNn7i+srd/nl7xOFHIG53DWMrSc5oQ8DIDo+LxCHqb0SHWY4pQ2qQ6JqQ1O/lPaFVvI5cxcevhAoGALyNtDQNgaAqsnCabe8hWz3INihVcFRHB9UDgMyIYnWiXt5ziK+TLVYqLBsL94eBH8tLBb2TSBXBPb3GST5N44SuwfCCEIt7/1OIymowv7/TjnCX7zpjvTtdgdVjPlz1d1RG2q49P0CnTnwW3801QwGZVXS6dOq1AjsguQRdlsoECgYBrFqKa3S48UbGOKkocgMb/1HzVs2sxNWMu+/jEiBvI4RrCiun9MALtrVtohgHRml4yZJaOojVk9F0z/MAak9eTgSmHpwxYZMqR1PvM4c/ekfun4hQoK04Thoexi/Z0RfsI6nBdBk8MAra03ldDvnYZY4Fat21YuNBspbrbTW8UoA==\n-----END RSA PRIVATE KEY-----",
"email": {
"apiKey": "key-1q2w3e4r"
},
Expand Down
3 changes: 1 addition & 2 deletions controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ module.exports = (req, res) => {
.then(requestAccessToken)
.then((accessToken) => {
const git = gitFactory.create(req.params.service, {
oauthToken: accessToken,
version: req.params.version
oauthToken: accessToken
})

// TODO: Simplify this when v2 support is dropped.
Expand Down
78 changes: 17 additions & 61 deletions lib/GitHub.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const config = require('../config')
const errorHandler = require('./ErrorHandler')
const githubApi = require('@octokit/rest')
const GitService = require('./GitService')
const jsonwebtoken = require('jsonwebtoken')
const Review = require('./models/Review')
const User = require('./models/User')

Expand All @@ -23,84 +22,41 @@ class GitHub extends GitService {
timeout: 5000
})

const isAppAuth = config.get('githubAppID') &&
config.get('githubPrivateKey')
const isLegacyAuth = config.get('githubToken') &&
['1', '2'].includes(options.version)

this.authentication = Promise.resolve()

if (options.oauthToken) {
this.api.authenticate({
type: 'oauth',
token: options.oauthToken
})
} else if (isLegacyAuth) {
} else if (options.token) {
this.api.authenticate({
type: 'token',
token: config.get('githubToken')
token: options.token
})
} else if (isAppAuth) {
this.authentication = this._authenticate(
options.username,
options.repository
)
} else {
throw new Error('Require an `oauthToken` or `token` option')
}
}

_authenticate (username, repository) {
const now = Math.floor(Date.now() / 1000)
const payload = {
iat: now,
exp: now + 60,
iss: config.get('githubAppID')
}
const bearer = jsonwebtoken.sign(payload, config.get('githubPrivateKey'), {
algorithm: 'RS256'
})

this.api.authenticate({
type: 'app',
token: bearer
})

return this.api.apps.findRepoInstallation({
owner: username,
repo: repository
}).then(({data}) => {
return this.api.apps.createInstallationToken({
installation_id: data.id
})
}).then(({data}) => {
this.api.authenticate({
type: 'token',
token: data.token
})
})
}

_pullFile (filePath, branch) {
return this.authentication.then(() => this.api.repos.getContents({
return this.api.repos.getContents({
owner: this.username,
repo: this.repository,
path: filePath,
ref: branch
}))
})
.then(normalizeResponse)
.catch(err => Promise.reject(errorHandler('GITHUB_READING_FILE', {err})))
}

_commitFile (filePath, content, commitMessage, branch) {
return this.authentication.then(() => this.api.repos.createFile({
return this.api.repos.createFile({
owner: this.username,
repo: this.repository,
path: filePath,
message: commitMessage,
content,
branch
}))
})
.then(normalizeResponse)
}

Expand Down Expand Up @@ -128,50 +84,50 @@ class GitHub extends GitService {
}

getBranchHeadCommit (branch) {
return this.authentication.then(() => this.api.repos.getBranch({
return this.api.repos.getBranch({
owner: this.username,
repo: this.repository,
branch
}))
})
.then(res => res.data.commit.sha)
}

createBranch (branch, sha) {
return this.authentication.then(() => this.api.git.createRef({
return this.api.gitdata.createReference({
owner: this.username,
repo: this.repository,
ref: `refs/heads/${branch}`,
sha
}))
})
.then(normalizeResponse)
}

deleteBranch (branch) {
return this.authentication.then(() => this.api.git.deleteRef({
return this.api.gitdata.deleteReference({
owner: this.username,
repo: this.repository,
ref: `heads/${branch}`
}))
})
}

createReview (reviewTitle, branch, reviewBody) {
return this.authentication.then(() => this.api.pullRequests.create({
return this.api.pullRequests.create({
owner: this.username,
repo: this.repository,
title: reviewTitle,
head: branch,
base: this.branch,
body: reviewBody
}))
})
.then(normalizeResponse)
}

getReview (reviewId) {
return this.authentication.then(() => this.api.pullRequests.get({
return this.api.pullRequests.get({
owner: this.username,
repo: this.repository,
number: reviewId
}))
})
.then(normalizeResponse)
.then(({base, body, head, merged, state, title}) =>
new Review(
Expand All @@ -195,7 +151,7 @@ class GitHub extends GitService {
}

getCurrentUser () {
return this.authentication.then(() => this.api.users.getAuthenticated({}))
return this.api.users.getAuthenticated({})
.then(normalizeResponse)
.then(({login, email, avatar_url, name, bio, company, blog}) =>
new User('github', login, email, name, avatar_url, bio, blog, company)
Expand Down
6 changes: 2 additions & 4 deletions lib/GitLab.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ class GitLab extends GitService {
constructor (options = {}) {
super(options.username, options.repository, options.branch)

const token = config.get('gitlabToken')

if (options.oauthToken) {
this.api = new GitLabApi({
url: config.get('gitlabBaseUrl'),
oauthToken: options.oauthToken
})
} else if (token) {
} else if (options.token) {
this.api = new GitLabApi({
url: config.get('gitlabBaseUrl'),
token
token: options.token
})
} else {
throw new Error('Require an `oauthToken` or `token` option')
Expand Down
34 changes: 11 additions & 23 deletions lib/Staticman.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,17 @@ const yaml = require('js-yaml')
const Staticman = function (parameters) {
this.parameters = parameters

const {
branch,
repository,
service,
username,
version
} = parameters
const token = parameters.service === 'gitlab'
? config.get('gitlabToken')
: config.get('githubToken')

// Initialise the Git service API
this.git = gitFactory.create(service, {
branch,
repository,
username,
version
})
this.git = gitFactory.create(parameters.service, {
username: parameters.username,
repository: parameters.repository,
branch: parameters.branch,
token
})

// Generate unique id
this.uid = uuidv1()
Expand Down Expand Up @@ -177,10 +173,7 @@ Staticman.prototype._checkAuth = function () {
return Promise.reject(errorHandler('AUTH_TOKEN_INVALID'))
}

const git = gitFactory.create(this.options['auth-type'], {
oauthToken,
version: this.parameters.version
})
const git = gitFactory.create(this.options['auth-type'], {oauthToken})

return git.getCurrentUser().then(user => {
this.gitUser = user
Expand All @@ -205,10 +198,7 @@ Staticman.prototype._checkAuthV2 = function () {
return Promise.reject(errorHandler('GITHUB_AUTH_TOKEN_INVALID'))
}

const git = gitFactory.create('github', {
oauthToken,
version: this.parameters.version
})
const git = gitFactory.create('github', {oauthToken})

return git.api.users.getAuthenticated({}).then(({data}) => {
this.gitUser = data
Expand Down Expand Up @@ -504,8 +494,6 @@ Staticman.prototype.processEntry = function (fields, options) {
this.fields = Object.assign({}, fields)
this.options = Object.assign({}, options)

this._initialiseGit

return this.getSiteConfig().then(config => {
return this._checkAuth()
}).then(() => {
Expand Down
Loading

0 comments on commit 5069eb7

Please sign in to comment.