Skip to content

Commit

Permalink
improve workflows (#1098)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasoppermann authored Nov 29, 2024
1 parent 694b24e commit 68b7a35
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 34 deletions.
52 changes: 25 additions & 27 deletions .github/workflows/diff-design-tokens.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,40 +88,32 @@ jobs:
// get config
const diffConfig = ${{ needs.setup.outputs.diffs }}
const output = []
// create sections
// create diff for each config
for (const config of diffConfig) {
await diffFiles(config.folder, config.globString, config.outputFile)
// read diff file
const diff = JSON.parse(fs.readFileSync(config.outputFile, 'utf8'))
// create sections
const sections = []
for (const {title, body} of diff) {
sections.push({
title,
body
})
}
output.push({
file: config.outputFile,
title: config.title,
sections
})
await diffFiles(config, 'base')
}
// set output
core.setOutput('diffFilePath', output);
- name: Write summary
uses: actions/github-script@v7
with:
script: |
const fs = require('fs')
const addSummary = require('./.github/workflows/utilities/addSummary.cjs')
const diffFileArray = ${{ steps.diff-files.outputs.diffFilePath }}
const diffConfig = ${{ needs.setup.outputs.diffs }}
const output = []
// read diff files
for (const config of diffConfig) {
const diff = JSON.parse(fs.readFileSync(config.outputFile, 'utf8'))
// add to output
output.push({
title: config.title,
sections: diff
})
}
// add summary
addSummary(diffFileArray, true)
addSummary(output, true)
- name: Comment on pr
if: github.event_name == 'pull_request'
Expand All @@ -131,12 +123,18 @@ jobs:
GITHUB_RUN_ID: ${{ github.run_id }}
with:
script: |
const fs = require('fs')
const addComment = require('./.github/workflows/utilities/addComment.cjs')
const diffFilePath = ${{ steps.diff-files.outputs.diffFilePath }}
const diffConfig = ${{ needs.setup.outputs.diffs }}
const WORKFLOW_SUMMARY_URL = `https://github.com/${{env.GITHUB_REPOSITORY}}/actions/runs/${{env.GITHUB_RUN_ID}}`
for (const diff of diffFilePath) {
await addComment(diff, WORKFLOW_SUMMARY_URL, context, github)
// read diff files
for (const config of diffConfig) {
const diff = JSON.parse(fs.readFileSync(config.outputFile, 'utf8'))
await addComment({
title: config.title,
sections: diff
}, WORKFLOW_SUMMARY_URL, context, github)
}
remove_comment:
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/utilities/addComment.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const findCommentByContent = (searchContent, comments) => {
return comments.filter(comment => comment.body.includes(searchContent));
}

const COMMENT_CHAR_LIMIT = 65536

module.exports = async ({title, body, sections}, summaryLink, context, github) => {
console.log(`Preparing comment content for ${title}`)
// prepare body
Expand All @@ -25,7 +27,7 @@ module.exports = async ({title, body, sections}, summaryLink, context, github) =
}

// overwrite body if to long
if(summaryLink && commentBody.length > 65536) {
if(summaryLink && commentBody.length > COMMENT_CHAR_LIMIT) {
commentBody = `## ${title}\n\nThe message is too long to be displayed here. For more details, please check the <a href="${summaryLink}">job summary</a>.`
}

Expand All @@ -42,7 +44,7 @@ module.exports = async ({title, body, sections}, summaryLink, context, github) =
console.log(`Found ${existingComment.length} comments, with the title "## ${title}"`)
// delete comment if no body or sections
if(!body && (!sections || sections.length === 0)) {
if(existingComment.length > 0 &&) {
if(existingComment.length > 0) {
console.log(`Deleting comment with the title "## ${title}" and the id ${existingComment[0].id}`)
// if comment exists, delete it
await github.rest.issues.deleteComment({
Expand Down
12 changes: 10 additions & 2 deletions .github/workflows/utilities/addSummary.cjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const core = require('@actions/core')

const SUMMARY_CHAR_LIMIT = 1024000

module.exports = (content, overwrite = false) => {
if(!Array.isArray(content)) {
content = [content]
}
// core.summary.clear()

for (const {title, body, sections} of content) {
// if no body or sections, skip
Expand All @@ -27,7 +28,14 @@ module.exports = (content, overwrite = false) => {
`${body}\n`+
'```\n\n'+
'</details>'
core.summary.addRaw(section, true)

if(core.summary.stringify().length + section.length <= SUMMARY_CHAR_LIMIT) {
core.summary.addRaw(section, true)
}
else {
core.warning('Summary character limit reached, content will be truncated.')
core.info('---------------',`${title}:`, body, '---------------')
}
})
}
}
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/utilities/diffFiles.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const shell = require('shelljs')
const fs = require('fs')
const glob = require('@actions/glob')

module.exports = async (folder, globString, outputFile) => {
module.exports = async ({folder, globString, outputFile}, originPath) => {
const globber = await glob.create(folder + globString)
const files = await globber.glob()
// create output file
Expand All @@ -13,15 +13,15 @@ module.exports = async (folder, globString, outputFile) => {
const regexRunnerPath = new RegExp('^[a-z\/]+\/dist', 'g')
const filename = file.replaceAll(regexRunnerPath,'')
// if file is new
if (!fs.existsSync(file.replace(folder, 'base/' + folder))) {
if (!fs.existsSync(file.replace(folder, `${originPath}/` + folder))) {
console.info('⚠️ File is new: ' + file + '\n')
return {
title: file.replaceAll('dist/', ''),
body: ''
}
}
// run diff & store in file
const diff = shell.exec(`diff -u ${file.replace(folder, 'base/' + folder)} ${file}`)
const diff = shell.exec(`diff -u ${file.replace(folder, `${originPath}/` + folder)} ${file}`)

console.log('Checking diff for ' + filename + '...')

Expand Down

0 comments on commit 68b7a35

Please sign in to comment.