Skip to content

Commit

Permalink
Merge pull request #1 from OlafPaladin/main
Browse files Browse the repository at this point in the history
feat: apply paladin studios changes
  • Loading branch information
Ornitoring committed May 15, 2024
2 parents 55e8ab7 + 7f7b5db commit a32eec9
Show file tree
Hide file tree
Showing 13 changed files with 3,051 additions and 2,903 deletions.
5,587 changes: 2,884 additions & 2,703 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,25 @@
},
"homepage": "https://github.com/codebrewery/UPM-Proxy-GitHub#readme",
"engines": {
"node": "16.4.0"
"node": "18.7.0"
},
"dependencies": {
"@hapi/joi": "^16.1.8",
"@octokit/graphql": "^4.5.7",
"@octokit/core": "^4.1.0",
"@octokit/webhooks": "^10.3.0",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"dotenv": "^16.0.2",
"express": "^4.17.1",
"express-bearer-token": "^2.4.0",
"follow-redirects": "^1.13.0",
"helmet": "^3.21.2",
"helmet": "^6.0.0",
"joi": "^17.6.1",
"node-cache": "^5.1.2",
"pug": "^3.0.2",
"winston": "^3.2.1"
},
"devDependencies": {
"foreman": "^3.0.1",
"standard": "^16.0.1"
"standard": "^17.0.0"
},
"config": {
"registry": "https://npm.pkg.github.com"
Expand Down
65 changes: 5 additions & 60 deletions src/controllers/api/upm-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,80 +10,25 @@
*/
'use strict'

const Joi = require('@hapi/joi')

const Joi = require('joi')
const schema = Joi.object({
scope: Joi.required()
})
const { graphql } = require('@octokit/graphql')

const logger = require('../../logic/winston')
const utils = require('../../logic/utils')
const { getOrgPackageData } = require('../../logic/retrievePackageData')

function controller (req, res) {
const token = req.token || process.env.GITHUB_TOKEN
const { scope } = req.params
const context = (process.env.SCOPE_TYPE === 'USER') ? 'user' : 'organization' // defaults to organization

query(scope, token, context).then((result) => {
utils.successfulJsonResponse(req, res, transformJson(result, context))
logger.debug(result)
const host = req.secure ? `https://${req.headers.host}` : `http://${req.headers.host}`
getOrgPackageData(scope, token, host).then((result) => {
utils.successfulJsonResponse(req, res, result)
}).catch((err) => {
res.status(500).json(err.message)
})
}

/**
* Create a JSON response for /-/all endpoint
* @param queryResult
* @param context
* @returns {{_updated: number}}
*/
function transformJson (queryResult, context) {
const json = {
_updated: 99999
}
queryResult[context].packages.nodes.forEach(element => {
if (element.packageType === 'NPM') {
json[element.name] = element.latestVersion
}
})
return json
}

async function query (scope, token, context) {
const graphqlWithAuth = graphql.defaults({
headers: {
authorization: `bearer ${token}`
}
})
const query = {
query: ` query {
${context}(login: "${scope}") {
packages(first: 100) {
totalCount
nodes {
name
latestVersion {
version
package {
name
}
}
packageType
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
`
}
return await graphqlWithAuth(query)
}

module.exports = {
schema,
controller
Expand Down
30 changes: 0 additions & 30 deletions src/controllers/api/upm-clear.js

This file was deleted.

20 changes: 0 additions & 20 deletions src/controllers/api/upm-download.js

This file was deleted.

26 changes: 14 additions & 12 deletions src/controllers/api/upm.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
'use strict'

const Joi = require('@hapi/joi')
const Joi = require('joi')
const utils = require('../../logic/utils')
const config = require('../../../package.json').config

const { getOrgPackageData } = require('../../logic/retrievePackageData')
const schema = Joi.object({
scope: Joi.required(),
packageName: Joi.required()
})

function controller (req, res) {
const { packageName, scope } = req.params
const host = req.secure ? `https://${req.headers.host}` : `http://${req.headers.host}`
const token = req.token || process.env.GITHUB_TOKEN
const { scope, packageName } = req.params

const host = req.secure ? `https://${req.headers.host}` : `http://${req.headers.host}`
getPackageData(scope, token, host).then(() => {
const packageData = utils.getCachedPackageData(token, packageName)
utils.successfulJsonResponse(req, res, packageData)
})
}

utils.request(`${config.registry}/${scope}%2F${packageName}`, token, host)
.then(result => {
utils.successfulJsonResponse(req, res, result)
})
.catch(err => {
res.status(500).json(err)
})
async function getPackageData (scope, token, host) {
if (utils.hasCachedPackageData(token) === false) {
// The package data for this user is not cached, retrieve the data
await getOrgPackageData(scope, token, host)
}
}

module.exports = {
Expand Down
11 changes: 11 additions & 0 deletions src/controllers/webhook/package.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

const utils = require('../../logic/utils')

function packageHandler (eventObject) {
utils.flushCache()
}

module.exports = {
packageHandler
}
51 changes: 51 additions & 0 deletions src/logic/retrievePackageData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict'

const { Octokit } = require('@octokit/core')
const utils = require('./utils')
const config = require('../../package.json').config

async function getOrgPackageData (scope, token, host) {
// This is the object all the data is added to and cached in memory once
const resultObject = {
all: {},
packages: {}
}

// Set-up octokit with PAT
const octokit = new Octokit({ auth: token })

// Get all org packages
const packages = await octokit.request('GET /orgs/{org}/packages', {
org: scope,
package_type: 'npm',
per_page: 100
})

// Create all parallel requests
const promises = packages.data.map(element => {
return getPackage(scope, element.name, token, host, resultObject)
})

// Run all parallel requests
await Promise.all(promises)

// Save the data in memory
utils.cachePackageData(token, resultObject)

// Return the data from memory
return utils.getCachedLatestVersions(token)
}

async function getPackage (scope, packageName, token, host, resultObject) {
// Run the async request
const packageData = await utils.request(`${config.registry}/@${scope}/${packageName}`, token, host)

// Append to the result object
resultObject.all[`${packageName}`] = packageData['dist-tags'].latest
resultObject.packages[`${packageName}`] = packageData
return packageData
}

module.exports = {
getOrgPackageData
}
4 changes: 4 additions & 0 deletions src/logic/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const path = require('path')
const helmet = require('helmet')
const cors = require('cors')
const routeMapper = require('./routeMapper')
const webhooks = require('../webhooks')
const logger = require('./winston')

// middleware
Expand All @@ -32,6 +33,9 @@ app.use(cache)
// Routes
routeMapper.mapRoutes(app)

// WebHooks
webhooks.register(app)

// Error handlers
app.use(function (req, res) {
logger.debug(req.url)
Expand Down
Loading

0 comments on commit a32eec9

Please sign in to comment.