Skip to content
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

add changeset auto generate job to workflow #1676

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tasty-buckets-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@channel.io/bezier-icons": minor
---

Add changeset auto generating job to workflow
11 changes: 9 additions & 2 deletions .github/workflows/generate-icon-files.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,19 @@ jobs:
run: echo "pull_request_number=$(gh pr view --json number -q .number || echo "")" >> $GITHUB_OUTPUT
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Add pr description
if: ${{ steps.generate-svg-files.outputs.icon_not_changed == 0}}
run: |
node packages/bezier-icons/scripts/add-pr-description.js ${{ secrets.GITHUB_TOKEN }} ${{ steps.pr.outputs.pull_request_number }}


- name: Add changeset
if: ${{ steps.generate-svg-files.outputs.icon_not_changed == 0}}
run: |
node packages/bezier-icons/scripts/generate-changeset.js
git add .
git commit -m "chore(bezier-icons): add changeset"

- name: Delete icons.json files
run: |
git rm packages/bezier-icons/icons.json
Expand Down
12 changes: 12 additions & 0 deletions packages/bezier-icons/scripts/generate-changeset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { exec } = require('child_process')
const fs = require('fs')
const path = require('path')

const { getChangeLog } = require('./utils/getChangeLog')

const changesetPath = path.resolve(__dirname, `../../../.changeset/icon-update-${new Date().valueOf()}.md`)

exec('git log -1 --name-status --pretty="format:"', async (_undefined, stdout) => {
fs.writeFileSync(changesetPath, getChangeLog(stdout), 'utf-8')
})

32 changes: 32 additions & 0 deletions packages/bezier-icons/scripts/utils/getChangeLog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { getIconNamesByStatus } = require('./getIconNamesByStatus')

const yamlFrontMatter =
`---
"@channel.io/bezier-icons": minor
---\n
`

const statusByKey = {
M: '**CHANGE**',
A: '**ADD**',
D: '**DELETE**',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

마이너: 이 부분도 테이블과 같은 문구를 사용하면 더 일관적일 거 같아요! (대/소문자는 안 맞춰도 될 듯)

const statusByKey = {
  M: 'modified',
  A: 'added',
  D: 'deleted',
}

}

const getIconChangeLog = (iconsByStatus) => {
let changeLog = 'Icon Update\n\n'

for (const [key, icons] of Object.entries(iconsByStatus)) {
changeLog += statusByKey[key]
changeLog += '\n\n'
changeLog += icons.map(icon => `- ${icon}`).join('\n')
changeLog += '\n\n'
}

return changeLog.trim()
}

const getChangeLog = (gitLog) => yamlFrontMatter + getIconChangeLog(getIconNamesByStatus(gitLog))

module.exports = {
getChangeLog,
}
31 changes: 31 additions & 0 deletions packages/bezier-icons/scripts/utils/getChangeLog.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* eslint-disable no-undef */
const { getChangeLog } = require('./getChangeLog')

describe('getChangeLog function', () => {
test('returns changelog from git log', () => {
const gitLog = `M\tpackages/bezier-icons/icons/all.svg
D\tpackages/bezier-icons/icons/home.svg
A\tpackages/bezier-icons/icons/people.svg
A\tpackages/bezier-icons/icons/chevron-right.svg`

expect(getChangeLog(gitLog)).toBe(
`---
"@channel.io/bezier-icons": minor
---

Icon Update
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

마이너: Update icons


**CHANGE**

- all.svg

**DELETE**

- home.svg

**ADD**

- people.svg
- chevron-right.svg`)
})
})
24 changes: 24 additions & 0 deletions packages/bezier-icons/scripts/utils/getIconNamesByStatus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const getIconName = (path) => path.split('/').at(-1)

const getIconNamesByStatus = (gitLog) => gitLog
.trim()
.split('\n')
.map(line => line.split('\t'))
.filter(line => line[1].endsWith('.svg'))
.reduce((acc, cur) => {
const [key, file] = cur
const icon = getIconName(file)

if (!icon) { return acc }

if (!acc[key]) {
acc[key] = [icon]
} else {
acc[key].push(icon)
}
return acc
}, {})

module.exports = {
getIconNamesByStatus,
}
25 changes: 3 additions & 22 deletions packages/bezier-icons/scripts/utils/getPrDescription.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { getIconNamesByStatus } = require('./getIconNamesByStatus')

const statusByKey = {
M: 'modified',
A: 'added',
Expand All @@ -10,8 +12,6 @@ const emojiByKey = {
D: '🗑️',
}

const getIconName = (path) => path.split('/').at(-1)

const getSummary = (iconsByStatus) => {
let res = ''

Expand All @@ -27,32 +27,13 @@ const getTable = (iconsByStatus) => {

for (const [key, icons] of Object.entries(iconsByStatus)) {
for (const icon of icons) {
res += `| ${getIconName(icon)} | ${emojiByKey[key]} |\n`
res += `| ${icon} | ${emojiByKey[key]} |\n`
}
}

return res
}

const getIconNamesByStatus = (gitLog) => gitLog
.trim()
.split('\n')
.map(line => line.split('\t'))
.filter(line => line[1].endsWith('.svg'))
.reduce((acc, cur) => {
const [key, file] = cur
const icon = getIconName(file)

if (!icon) { return acc }

if (!acc[key]) {
acc[key] = [icon]
} else {
acc[key].push(icon)
}
return acc
}, {})

const getDescription = (gitLog) => {
const iconsByStatus = getIconNamesByStatus(gitLog)

Expand Down