Skip to content

Commit

Permalink
Simplify download link logic
Browse files Browse the repository at this point in the history
At the minute the tests hang when running on macOS:

> Jest did not exit one second after the test run has completed.
>
> This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

If you run `jest —detectOpenHandles` this seems to suggest the issue is with sync-request:

```
Jest has detected the following 1 open handle potentially keeping Jest from exiting:

  ●  PROCESSWRAP

      at start (node_modules/sync-rpc/lib/index.js:33:13)
      at sendMessage (node_modules/sync-rpc/lib/index.js:133:17)
      at createClient (node_modules/sync-rpc/lib/index.js:173:27)
      at Object.<anonymous> (node_modules/sync-request/lib/index.js:16:14)
```

`sync-request` is only used to fetch the latest version of the kit from the GitHub API. We currently have to do this becuase, although GitHub provides a `/latest/` endpoint to link to the latest release [1], you can only link to assets if you know the asset filename, and the asset filenames are currently based on the version numbers and so not predictable unless you know the latest version number.

However, we can avoid calling the API at all by instead using the version number as defined in the package.json file. This allows us to simplify the code and remove the sync-request library entirely.

This does rely on the most recent release having been tagged (and thus the release created in GitHub) before the equivalent version is deployed to Heroku. As we have ‘Wait for CI to pass before deploy’ enabled in the Heroku settings I believe this should be the case.

[1]: https://docs.github.com/en/free-pro-team@latest/github/administering-a-repository/linking-to-releases#:~:text=To%20link%20directly%20to%20a%20download%20of%20your%20latest%20release%20asset%2C%20link%20to%20%2Fowner%2Fname%2Freleases%2Flatest%2Fdownload%2Fasset-name.zip
  • Loading branch information
36degrees committed Nov 2, 2020
1 parent 5f920cd commit dbbd14a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 43 deletions.
13 changes: 7 additions & 6 deletions docs/documentation_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ const express = require('express')
const marked = require('marked')
const router = express.Router()

// Local dependencies
const utils = require('../lib/utils.js')

// Page routes

// Docs index
Expand All @@ -34,10 +31,14 @@ router.get('/install/:page', function (req, res) {
res.render('install_template', { document: html })
})

// Redirect to the zip of the latest release of the Prototype Kit on GitHub
// Redirect to download the current release zip from GitHub, based on the
// version number from package.json
router.get('/download', function (req, res) {
var url = utils.getLatestRelease()
res.redirect(url)
const version = require('../package.json').version

res.redirect(
`https://github.com/alphagov/govuk-prototype-kit/archive/v${version}.zip`
)
})

// Examples - examples post here
Expand Down
36 changes: 0 additions & 36 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@ const marked = require('marked')
const path = require('path')
const portScanner = require('portscanner')
const inquirer = require('inquirer')
const request = require('sync-request')

// Local dependencies
const config = require('../app/config.js')

// Variables
var releaseUrl = null

// Require core and custom filters, merges to one object
// and then add the methods to Nunjucks environment
exports.addNunjucksFilters = function (env) {
Expand Down Expand Up @@ -131,38 +127,6 @@ exports.forceHttps = function (req, res, next) {
next()
}

// Synchronously get the URL for the latest release on GitHub and cache it
exports.getLatestRelease = function () {
if (releaseUrl !== null) {
// Release URL already exists
console.log('Release url cached:', releaseUrl)
return releaseUrl
} else {
// Release URL doesn't exist
try {
console.log('Getting latest release from GitHub')

var res = request(
'GET',
'https://api.github.com/repos/alphagov/govuk-prototype-kit/releases/latest',
{
headers: { 'user-agent': 'node.js' }
}
)
var data = JSON.parse(res.getBody('utf8'))

// Cache releaseUrl before we return it
releaseUrl = `https://github.com/alphagov/govuk-prototype-kit/archive/${data.name}.zip`

console.log('Release URL is', releaseUrl)
return releaseUrl
} catch (err) {
console.log("Couldn't retrieve release URL")
return 'https://github.com/alphagov/govuk-prototype-kit/releases/latest'
}
}
}

// Try to match a request to a template, for example a request for /test
// would look for /app/views/test.html
// and /app/views/test/index.html
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"nunjucks": "^3.2.1",
"portscanner": "^2.1.1",
"require-dir": "^1.0.0",
"sync-request": "^6.0.0",
"universal-analytics": "^0.4.16",
"uuid": "^7.0.3"
},
Expand Down

0 comments on commit dbbd14a

Please sign in to comment.