|
| 1 | +// https://github.com/patrickhulce/hulk |
| 2 | +/** |
| 3 | +The MIT License (MIT) |
| 4 | +
|
| 5 | +Copyright (c) 2018 Patrick Hulce <[email protected]> (https://patrickhulce.com/) |
| 6 | +
|
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +of this software and associated documentation files (the "Software"), to deal |
| 9 | +in the Software without restriction, including without limitation the rights |
| 10 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +copies of the Software, and to permit persons to whom the Software is |
| 12 | +furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | +The above copyright notice and this permission notice shall be included in |
| 15 | +all copies or substantial portions of the Software. |
| 16 | +
|
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | +THE SOFTWARE. |
| 24 | + */ |
| 25 | + |
| 26 | +const changelogLib = require('@patrickhulce/scripts/lib/shared/changelog.js'); |
| 27 | +const parseCommit = require('conventional-commits-parser').sync; |
| 28 | +const shelljs = require('shelljs'); |
| 29 | + |
| 30 | +const lastTag = process.argv[2]; |
| 31 | +const nextTag = process.argv[3]; |
| 32 | + |
| 33 | +const GIT_BODY_DELIMITER = '______MARK_THE_BODY______' |
| 34 | +const GIT_BODY = `${GIT_BODY_DELIMITER}"%B"${GIT_BODY_DELIMITER}` |
| 35 | +const GIT_LOG_JSON_FORMAT = `{"hash": "%H", "date": "%aI", "subject": "%s", "body": ${GIT_BODY}}` |
| 36 | + |
| 37 | +const RELEASE_TYPE = { |
| 38 | + MAJOR: 2, |
| 39 | + MINOR: 1, |
| 40 | + PATCH: 0, |
| 41 | +} |
| 42 | + |
| 43 | +const exec = cmd => shelljs.exec(cmd, {silent: true}) |
| 44 | + |
| 45 | +function getCommitsAndReleaseType(lastVersion) { |
| 46 | + const commitRange = `${lastVersion.tag}...HEAD` |
| 47 | + const flags = `--pretty=format:'${GIT_LOG_JSON_FORMAT}' --no-merges` |
| 48 | + const command = `git log ${commitRange} ${flags}` |
| 49 | + let logs = exec(command).stdout |
| 50 | + // Replace all the newlines in the body so it's valid JSON |
| 51 | + const regex = new RegExp(`${GIT_BODY_DELIMITER}"((.|[\n\r\f])*?)"${GIT_BODY_DELIMITER}}`, 'gim') |
| 52 | + logs = logs.replace(regex, (s, body) => `"${body.replace(/\r?\n/g, '\\n')}"}`) |
| 53 | + |
| 54 | + const commits = logs |
| 55 | + .split('\n') |
| 56 | + .filter(Boolean) |
| 57 | + .map(l => { |
| 58 | + try { |
| 59 | + return JSON.parse(l) |
| 60 | + } catch (err) { |
| 61 | + console.error('Unable to parse message:', l) |
| 62 | + return undefined |
| 63 | + } |
| 64 | + }) |
| 65 | + .filter(Boolean) |
| 66 | + .map(commit => { |
| 67 | + const parsed = parseCommit(commit.body) |
| 68 | + parsed.hash = commit.hash |
| 69 | + parsed.date = commit.date |
| 70 | + |
| 71 | + let releaseType = RELEASE_TYPE.PATCH |
| 72 | + if (parsed.type === 'feat') releaseType = RELEASE_TYPE.MINOR |
| 73 | + if (commit.body.includes('BREAKING CHANGE')) releaseType = RELEASE_TYPE.MAJOR |
| 74 | + |
| 75 | + return {...commit, releaseType, parsed} |
| 76 | + }) |
| 77 | + |
| 78 | + const releaseType = commits.reduce( |
| 79 | + (type, commit) => Math.max(type, commit.releaseType), |
| 80 | + RELEASE_TYPE.PATCH, |
| 81 | + ) |
| 82 | + |
| 83 | + return {releaseType, commits} |
| 84 | + } |
| 85 | + |
| 86 | +const options = {}; |
| 87 | +const tag = nextTag; |
| 88 | +const lastVersion = {tag: lastTag}; |
| 89 | +const nextVersion = {tag}; |
| 90 | +const repository = { |
| 91 | + owner: 'GoogleChrome', |
| 92 | + name: 'lighthouse-ci', |
| 93 | +}; |
| 94 | +const {commits} = getCommitsAndReleaseType(lastVersion); |
| 95 | +changelogLib.get(options, {repository, lastVersion, nextVersion, commits, tag}).then(console.log); |
0 commit comments