-
Notifications
You must be signed in to change notification settings - Fork 24
chore: automate release #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5c44888
1675bf8
af8170e
e263c91
59065d1
afe00ca
a46ddc6
a603301
fc0f186
eea302e
8dbb6d4
e0c7bb7
741cf26
49c74c3
8652040
c74b2de
60c3cc4
3712cfc
fd76229
b60b1ef
3b119d1
28d1198
0b9931e
1160d7b
703d47f
cdd385c
2d33b1a
8bdb9ed
f09b622
acb8492
3aa36a8
e167e52
8b77ef1
968719b
f1f8f7c
6a34b36
3d25889
ed5a9f5
7971ac9
4b9acfd
5ce225f
d097db7
f6c367b
ba9017d
98f6706
1d83f15
47a2307
b9e765d
5c1fdc6
5bb53d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
name: Process release | ||
on: | ||
issues: | ||
types: | ||
- closed | ||
jobs: | ||
build: | ||
name: Release | ||
runs-on: ubuntu-20.04 | ||
if: "startsWith(github.event.issue.title, 'chore: release')" | ||
steps: | ||
- uses: actions/checkout@v2 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Setup | ||
id: setup | ||
uses: ./.github/actions/setup | ||
|
||
- run: ./scripts/release/process-release.js | ||
env: | ||
EVENT_NUMBER: ${{ github.event.issue.number }} | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"releasedTag": "released", | ||
"mainBranch": "main", | ||
"owner": "algolia", | ||
"repo": "api-clients-automation" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import execa from 'execa'; // https://github.com/sindresorhus/execa/tree/v5.1.1 | ||
|
||
import openapitools from '../../openapitools.json'; | ||
import config from '../../release.config.json'; | ||
|
||
export const RELEASED_TAG = config.releasedTag; | ||
export const MAIN_BRANCH = config.mainBranch; | ||
export const OWNER = config.owner; | ||
export const REPO = config.repo; | ||
|
||
type Run = ( | ||
command: string, | ||
options?: Partial<{ | ||
errorMessage: string; | ||
}> | ||
) => execa.ExecaReturnBase<string>['stdout']; | ||
|
||
export const run: Run = (command, { errorMessage = undefined } = {}) => { | ||
let result: execa.ExecaSyncReturnValue<string>; | ||
try { | ||
result = execa.commandSync(command); | ||
} catch (err) { | ||
if (errorMessage) { | ||
throw new Error(`[ERROR] ${errorMessage}`); | ||
} else { | ||
throw err; | ||
} | ||
} | ||
return result.stdout; | ||
}; | ||
|
||
export const LANGS = [ | ||
...new Set( | ||
Object.keys(openapitools['generator-cli'].generators).map( | ||
(key) => key.split('-')[0] | ||
) | ||
), | ||
]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
/* eslint-disable no-console */ | ||
import { Octokit } from '@octokit/rest'; | ||
import dotenv from 'dotenv'; | ||
import semver from 'semver'; | ||
|
||
import openapitools from '../../openapitools.json'; | ||
|
||
import { RELEASED_TAG, MAIN_BRANCH, OWNER, REPO, LANGS, run } from './common'; | ||
import TEXT from './text'; | ||
|
||
dotenv.config(); | ||
|
||
type Version = { | ||
current: string; | ||
langName: string; | ||
next?: string; | ||
noCommit?: boolean; | ||
skipRelease?: boolean; | ||
}; | ||
|
||
type Versions = { | ||
[lang: string]: Version; | ||
}; | ||
|
||
function readVersions(): Versions { | ||
const versions = {}; | ||
|
||
const generators = openapitools['generator-cli'].generators; | ||
|
||
Object.keys(generators).forEach((generator) => { | ||
const lang = generator.split('-')[0]; | ||
if (!versions[lang]) { | ||
versions[lang] = { | ||
current: generators[generator].additionalProperties.packageVersion, | ||
langName: lang, | ||
next: undefined, | ||
}; | ||
} | ||
}); | ||
return versions; | ||
} | ||
|
||
if (!process.env.GITHUB_TOKEN) { | ||
throw new Error('Environment variable `GITHUB_TOKEN` does not exist.'); | ||
} | ||
|
||
if (run('git rev-parse --abbrev-ref HEAD') !== MAIN_BRANCH) { | ||
throw new Error( | ||
`You can run this script only from \`${MAIN_BRANCH}\` branch.` | ||
); | ||
} | ||
|
||
if (run('git status --porcelain')) { | ||
throw new Error( | ||
'Working directory is not clean. Commit all the changes first.' | ||
); | ||
} | ||
|
||
run(`git rev-parse --verify refs/tags/${RELEASED_TAG}`, { | ||
errorMessage: '`released` tag is missing in this repository.', | ||
}); | ||
|
||
// Reading versions from `openapitools.json` | ||
const versions = readVersions(); | ||
|
||
console.log('Pulling from origin...'); | ||
run(`git pull origin ${MAIN_BRANCH}`); | ||
|
||
console.log('Pushing to origin...'); | ||
run(`git push origin ${MAIN_BRANCH}`); | ||
|
||
const commitsWithoutScope: string[] = []; | ||
const commitsWithNonLanguageScope: string[] = []; | ||
|
||
// Reading commits since last release | ||
type LatestCommit = { | ||
hash: string; | ||
type: string; | ||
lang: string; | ||
message: string; | ||
raw: string; | ||
}; | ||
const latestCommits = run(`git log --oneline ${RELEASED_TAG}..${MAIN_BRANCH}`) | ||
.split('\n') | ||
.filter(Boolean) | ||
.map((commit) => { | ||
const hash = commit.slice(0, 7); | ||
let message = commit.slice(8); | ||
let type = message.slice(0, message.indexOf(':')); | ||
shortcuts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const matchResult = type.match(/(.+)\((.+)\)/); | ||
if (!matchResult) { | ||
commitsWithoutScope.push(commit); | ||
return undefined; | ||
} | ||
message = message.slice(message.indexOf(':') + 1).trim(); | ||
type = matchResult[1]; | ||
const lang = matchResult[2]; | ||
|
||
if (!LANGS.includes(lang)) { | ||
commitsWithNonLanguageScope.push(commit); | ||
return undefined; | ||
} | ||
|
||
return { | ||
hash, | ||
type, // `fix` | `feat` | `chore` | ... | ||
lang, // `javascript` | `php` | `java` | ... | ||
message, | ||
raw: commit, | ||
}; | ||
}) | ||
.filter(Boolean) as LatestCommit[]; | ||
|
||
console.log('[INFO] Skipping these commits due to lack of language scope:'); | ||
console.log(commitsWithoutScope.map((commit) => ` ${commit}`).join('\n')); | ||
|
||
console.log(''); | ||
console.log('[INFO] Skipping these commits due to wrong scopes:'); | ||
console.log( | ||
commitsWithNonLanguageScope.map((commit) => ` ${commit}`).join('\n') | ||
); | ||
|
||
LANGS.forEach((lang) => { | ||
const commits = latestCommits.filter( | ||
(lastestCommit) => lastestCommit.lang === lang | ||
); | ||
const currentVersion = versions[lang].current; | ||
|
||
if (commits.length === 0) { | ||
versions[lang].next = currentVersion; | ||
versions[lang].noCommit = true; | ||
return; | ||
} | ||
|
||
if (semver.prerelease(currentVersion)) { | ||
// if version is like 0.1.2-beta.1, it increases to 0.1.2-beta.2, even if there's a breaking change. | ||
versions[lang].next = semver.inc(currentVersion, 'prerelease'); | ||
return; | ||
} | ||
|
||
if (commits.some((commit) => commit.message.includes('BREAKING CHANGE'))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should note somewhere that introducing a breaking change requires our commit to contain it then There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It actually comes from https://www.conventionalcommits.org/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a small |
||
versions[lang].next = semver.inc(currentVersion, 'major'); | ||
return; | ||
} | ||
|
||
const commitTypes = new Set(commits.map(({ type }) => type)); | ||
if (commitTypes.has('feat')) { | ||
versions[lang].next = semver.inc(currentVersion, 'minor'); | ||
return; | ||
} | ||
|
||
versions[lang].next = semver.inc(currentVersion, 'patch'); | ||
if (!commitTypes.has('fix')) { | ||
versions[lang].skipRelease = true; | ||
} | ||
}); | ||
|
||
const versionChanges = LANGS.map((lang) => { | ||
const { current, next, noCommit, skipRelease, langName } = versions[lang]; | ||
|
||
if (noCommit) { | ||
return `- ~${langName}: v${current} (${TEXT.noCommit})~`; | ||
} | ||
|
||
if (!current) { | ||
return `- ~${langName}: (${TEXT.currentVersionNotFound})~`; | ||
} | ||
|
||
const checked = skipRelease ? ' ' : 'x'; | ||
return [ | ||
`- [${checked}] ${langName}: v${current} -> v${next}`, | ||
skipRelease && TEXT.descriptionForSkippedLang(langName), | ||
] | ||
.filter(Boolean) | ||
.join('\n'); | ||
}).join('\n'); | ||
|
||
const changelogs = LANGS.filter( | ||
(lang) => !versions[lang].noCommit && versions[lang].current | ||
) | ||
.flatMap((lang) => { | ||
if (versions[lang].noCommit) { | ||
return []; | ||
} | ||
|
||
return [ | ||
`### ${versions[lang].langName}`, | ||
...latestCommits | ||
.filter((commit) => commit.lang === lang) | ||
.map((commit) => `- ${commit.raw}`), | ||
]; | ||
}) | ||
.join('\n'); | ||
|
||
const body = [ | ||
TEXT.header, | ||
TEXT.versionChangeHeader, | ||
versionChanges, | ||
TEXT.changelogHeader, | ||
TEXT.changelogDescription, | ||
changelogs, | ||
TEXT.approvalHeader, | ||
TEXT.approval, | ||
].join('\n\n'); | ||
|
||
const octokit = new Octokit({ | ||
auth: `token ${process.env.GITHUB_TOKEN}`, | ||
}); | ||
|
||
octokit.rest.issues | ||
.create({ | ||
owner: OWNER, | ||
repo: REPO, | ||
title: `chore: release ${new Date().toISOString().split('T')[0]}`, | ||
body, | ||
}) | ||
.then((result) => { | ||
const { | ||
data: { number, html_url: url }, | ||
} = result; | ||
|
||
console.log(''); | ||
console.log(`Release issue #${number} is ready for review.`); | ||
console.log(` > ${url}`); | ||
}); |
Uh oh!
There was an error while loading. Please reload this page.