Skip to content

Commit

Permalink
chore(ci): add an action to create a release PR;
Browse files Browse the repository at this point in the history
  • Loading branch information
DigitalBrainJS committed Jan 5, 2023
1 parent abd2cb8 commit 8651bf1
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 23 deletions.
73 changes: 73 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: Release PR
on:
workflow_dispatch:
inputs:
type:
type: choice
description: Choose release type
options:
- auto
- patch
- minor
- major
default: auto
beta:
type: boolean
description: Prerelease
default: false
jobs:
releaseIt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: git config
run: |
git config user.name "${GITHUB_ACTOR}"
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
- name: Setup node
uses: actions/setup-node@v3
with:
node-version: 16
cache: npm
- name: npm credentials
run: npm config set //registry.npmjs.org/:_authToken $NPM_TOKEN
- run: npm install
- name: release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TYPE_ARG: ${{ fromJSON('{"auto":"", "patch":"--patch", "minor":"--minor", "major":"--major"}')[github.event.inputs.type] }}
BETA_ARG: ${{ github.event.inputs.beta == 'true' && '--preRelease=beta' || '' }}
run: npm run release -- --ci --verbose $TYPE_ARG $BETA_ARG
- name: get-npm-version
id: package-version
uses: martinbeentjes/npm-get-version-action@main
- name: Extract release notes
id: extract-release-notes
uses: ffurrer2/extract-release-notes@v1
- name: Generate PR body
id: body
uses: mathiasvr/command-output@v1
with:
run: node ./bin/pr.js
- name: Create pull request
uses: peter-evans/create-pull-request@v4
with:
branch: release
delete-branch: true
title: '[Release] v${{ steps.package-version.outputs.current-version}}'
body: |
${{ steps.body.outputs.stdout }}
## Release notes:
${{ steps.extract-release-notes.outputs.release_notes }}
labels: |
release
automated pr
signoff: false
team-reviewers: |
owners
maintainers
#assignees: jasonsaayman
#reviewers: jasonsaayman
draft: true
75 changes: 75 additions & 0 deletions bin/pr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import util from "util";
import cp from "child_process";
import Handlebars from "handlebars";
import fs from "fs/promises";
import prettyBytes from 'pretty-bytes';
import {gzipSize} from 'gzip-size';

const exec = util.promisify(cp.exec);

const getBlobHistory = async (filepath, maxCount= 5) => {
const log = (await exec(
`git log --max-count=${maxCount} --no-walk --tags=v* --oneline --format=%H%d -- ${filepath}`
)).stdout;

const commits = [];

let match;

const regexp = /^(\w+) \(tag: (v?[.\d]+)\)$/gm;

while((match = regexp.exec(log))) {
commits.push({
sha: match[1],
tag: match[2],
size: await getBlobSize(filepath, match[1])
})
}

return commits;
}

const getBlobSize = async (filepath, sha ='HEAD') => {
const size = (await exec(
`git cat-file -s ${sha}:${filepath}`
)).stdout;

return size ? +size : 0;
}

const generateFileReport = async (files) => {
const stat = {};

for(const [name, file] of Object.entries(files)) {
const commits = await getBlobHistory(file);

stat[file] = {
name,
size: (await fs.stat(file)).size,
path: file,
gzip: await gzipSize(String(await fs.readFile(file))),
commits,
history: commits.map(({tag, size}) => `${prettyBytes(size)} (${tag})`).join(' ← ')
}
}

return stat;
}

const generateBody = async ({files, template = './templates/pr.hbs'} = {}) => {
const data = {
files: await generateFileReport(files)
};

Handlebars.registerHelper('filesize', (bytes)=> prettyBytes(bytes));

return Handlebars.compile(String(await fs.readFile(template)))(data);
}

console.log(await generateBody({
files: {
'Browser build (UMD)' : './dist/axios.min.js',
'Browser build (ESM)' : './dist/esm/axios.min.js',
}
}));

88 changes: 66 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"fs-extra": "^10.1.0",
"get-stream": "^3.0.0",
"gulp": "^4.0.2",
"gzip-size": "^7.0.0",
"handlebars": "^4.7.7",
"husky": "^8.0.2",
"istanbul-instrumenter-loader": "^3.0.1",
Expand All @@ -109,6 +110,7 @@
"minimist": "^1.2.7",
"mocha": "^10.0.0",
"multer": "^1.4.4",
"pretty-bytes": "^6.0.0",
"release-it": "^15.5.1",
"rollup": "^2.67.0",
"rollup-plugin-auto-external": "^2.0.0",
Expand Down Expand Up @@ -186,4 +188,4 @@
"@commitlint/config-conventional"
]
}
}
}
12 changes: 12 additions & 0 deletions templates/pr.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{#if files}}

### Build info

{{#each files}}
- {{ name}} ({{ path }}) {{ filesize size }} ({{ filesize gzip }} gzipped)
{{#each commits}}
- {{ tag }} - {{filesize size}}
{{/each}}
{{/each}}

{{/if}}

0 comments on commit 8651bf1

Please sign in to comment.