Skip to content

Commit

Permalink
Merge pull request #560 from alphagov/simpler-downloads
Browse files Browse the repository at this point in the history
Simplify download latest release logic
  • Loading branch information
36degrees committed Jul 23, 2018
2 parents 7d1aee6 + f469a10 commit 7be8557
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 29 deletions.
2 changes: 1 addition & 1 deletion docs/documentation/install/install-the-kit.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Download the kit as a zip

The simplest way to get the kit is to <a href="/prototype-admin/download-latest" data-link="download">download it as a zip</a>. You'll use a new copy of the kit for each new prototype you make. That way your prototypes don’t interfere with each other.
The simplest way to get the kit is to <a href="/docs/download" data-link="download">download it as a zip</a>. You'll use a new copy of the kit for each new prototype you make. That way your prototypes don’t interfere with each other.

### Decide where you want to keep your prototypes

Expand Down
9 changes: 7 additions & 2 deletions docs/documentation_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ router.get('/', function (req, res) {
})

router.get('/install', function (req, res) {
var url = utils.getLatestRelease()
res.render('install', { 'releaseURL': url })
res.render('install')
})

// Pages in install folder are markdown
Expand All @@ -35,6 +34,12 @@ 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
router.get('/download', function (req, res) {
var url = utils.getLatestRelease()
res.redirect(url)
})

// Examples - examples post here
router.post('/tutorials-and-examples', function (req, res) {
res.redirect('tutorials-and-examples')
Expand Down
2 changes: 1 addition & 1 deletion docs/views/install.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ <h1 class="govuk-heading-xl">Installing the kit</h1>

<h2 class="govuk-heading-m">1. Download the kit</h2>
<p>
<a href="{{releaseURL}}" data-link="download">Download (zip)</a>
<a href="/docs/download" data-link="download">Download (zip)</a>
</p>
<h2 class="govuk-heading-m">2. Install the kit</h2>

Expand Down
2 changes: 1 addition & 1 deletion docs/views/tutorials-and-examples.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ <h2 class="govuk-heading-m">Tutorials</h2>
<h3 class="govuk-heading-s">Getting started</h3>
<ul class="govuk-list govuk-list--bullet">
<li>
<a href="/prototype-admin/download-latest" data-link="download">Download kit (zip)</a>
<a href="/docs/download" data-link="download">Download kit (zip)</a>
</li>
<li>
<a href="/docs/install/introduction">Simple install guide</a>
Expand Down
32 changes: 14 additions & 18 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,38 +158,34 @@ exports.forceHttps = function (req, res, next) {

// Synchronously get the URL for the latest release on GitHub and cache it
exports.getLatestRelease = function () {
var url = ''

if (releaseUrl !== null) {
// Release URL already exists
console.log('Release url cached:', releaseUrl)
return releaseUrl
} else {
// Release URL doesn't exist
var options = {
headers: {'user-agent': 'node.js'}
}
var gitHubUrl = 'https://api.github.com/repos/alphagov/govuk-prototype-kit/releases/latest'
try {
console.log('Getting latest release from GitHub')

var res = request('GET', gitHubUrl, options)
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'))
var zipballUrl = data['zipball_url']
var releaseVersion = zipballUrl.split('/').pop()
var urlStart = 'https://github.com/alphagov/govuk-prototype-kit/archive/'
var urlEnd = '.zip'
var zipUrl = urlStart + releaseVersion + urlEnd

console.log('Release URL is', zipUrl)
releaseUrl = zipUrl
url = releaseUrl

// 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) {
url = 'https://github.com/alphagov/govuk-prototype-kit/releases/latest'
console.log("Couldn't retrieve release URL")
return 'https://github.com/alphagov/govuk-prototype-kit/releases/latest'
}
}
return url
}

// Try to match a request to a template, for example a request for /test
Expand Down
6 changes: 0 additions & 6 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,6 @@ if (typeof (routes) !== 'function') {
app.use('/', routes)
}

// Redirect to the zip of the latest release of the Prototype Kit on GitHub
app.get('/prototype-admin/download-latest', function (req, res) {
var url = utils.getLatestRelease()
res.redirect(url)
})

if (useDocumentation) {
// Clone app locals to documentation app locals
// Use Object.assign to ensure app.locals is cloned to prevent additions from
Expand Down

0 comments on commit 7be8557

Please sign in to comment.