Skip to content

Commit

Permalink
[#1] fix githubrelease cron job crash
Browse files Browse the repository at this point in the history
  • Loading branch information
feelform committed Nov 9, 2021
1 parent df56a2b commit fe5cf1f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,20 @@ test('index.js client payload test', (t) => {
const regex = /name=markdown::<!--\s<latestReleaseNotes\.md>/gm
const match = regex.exec(node)
t.true(match.length > 0, 'match markdown')

delete process.env['INPUT_CLIENT_PAYLOAD']
t.end()
})

test('index.js schedule test', (t) => {
process.env['INPUT_TEMPLATE_MARKDOWN_FILE'] = './.github/actions/markdown-template-engine-action/__test__/main.test.md'
process.env['INPUT_SECTION_GITHUB_PATH'] = 'feelform/pinpoint'
process.env['INPUT_DISABLE_BRANCH'] = 'YES'
process.env['INPUT_DISABLE_SYNC_CHANGES'] = 'YES'
const ip = path.join(__dirname, '../', 'index.js')
const node = cp.execSync(`node ${ip}`, { env: process.env }).toString()
const regex = /name=markdown::<!--\s<latestReleaseNotes\.md>/gm
const match = regex.exec(node)
t.true(match.length > 0, 'match markdown')
t.end()
})
17 changes: 5 additions & 12 deletions .github/actions/markdown-template-engine-action/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,12 @@ const run = async () => {
}

let githubRelease = await GithubRelease.make()
let engine
let email
let authorName
if (githubRelease) {
email = githubRelease.getAuthorEmail()
authorName = githubRelease.getAuthorName()
engine = new TemplateEngine(template, githubRelease)
} else {
email = '41898282+github-actions[bot]@users.noreply.github.com'
authorName = 'github-actions[bot]'
githubRelease = await ReleaseNotes.makeByLatestGithubReleaseNotes()
engine = new TemplateEngine(template, githubRelease)
if (!githubRelease) {
githubRelease = await GithubRelease.makeByLatestGithubReleaseNotes()
}
let engine = new TemplateEngine(template)
const email = githubRelease.getAuthorEmail()
const authorName = githubRelease.getAuthorName()
core.info(`email: ${email}, authorName: ${authorName}`)

const markdownContent = await engine.markdownContent(githubRelease)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
const axios = require('axios')
const github = require('@actions/github')
const core = require('@actions/core')
const ReleaseNotes = require('./release-notes')

class GithubRelease {
static async latest() {
Expand All @@ -24,6 +25,22 @@ class GithubRelease {
return new GithubRelease(payload, { name: data.name, email: `${payload.username}@users.noreply.github.com` })
}

static async makeByLatestGithubReleaseNotes() {
const { data } = await axios.get(`https://api.github.com/repos/pinpoint-apm/pinpoint/releases/latest`)
const tagName = ReleaseNotes.tagName(data.tag_name)
return new GithubRelease({
release: {
name: data.name,
body: ReleaseNotes.formattedReleaseNotes(tagName, data.body),
tag_name: tagName,
html_url: data.url
}
}, {
name: 'github-actions[bot]',
email: '41898282+github-actions[bot]@users.noreply.github.com'
})
}

constructor(payload, author) {
this.name = payload.release.name
this.body = payload.release.body
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

const axios = require('axios')
const GithubRelease = require('./github-release')

const whatsNewTempate = `# What's New in v__VERSION__
__BODY__
Expand All @@ -27,25 +26,17 @@ class ReleaseNotes {

static makeLatestReleaseNotes(release) {
const tagName = ReleaseNotes.tagName(release.getTagName())
const latestReleaseNotes = whatsNewTempate.replace('__VERSION__', tagName)
.replace('__BODY__', release.getBody())
const latestReleaseNotes = ReleaseNotes.formattedReleaseNotes(tagName, release.getBody())
return new ReleaseNotes(latestReleaseNotes, tagName)
}

static tagName(version) {
return version.startsWith('v') ? version.substring(1) : version
static formattedReleaseNotes(tagName, contents) {
return whatsNewTempate.replace('__VERSION__', tagName)
.replace('__BODY__', contents)
}

static async makeByLatestGithubReleaseNotes() {
const { data } = await axios.get(`https://api.github.com/repos/pinpoint-apm/pinpoint/releases/latest`)
const tagName = ReleaseNotes.tagName(data.tag_name)
const latestReleaseNotes = whatsNewTempate.replace('__VERSION__', tagName)
.replace('__BODY__', data.body)
return makeLatestReleaseNotes(new GithubRelease({
release: {

}
}))
static tagName(version) {
return version.startsWith('v') ? version.substring(1) : version
}

static makeOfMarkdownContents(contents) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,30 @@ class TemplateEngine {
this.template = template
}

async markdownContent(release) {
async markdownContent(githubRelease) {
let tagExpression = /<!--\s<(?!\/)(?<markdownfile>.*.md)>\s-->\n/gm
let tag
let result = this.template
while ((tag = tagExpression.exec(this.template)) !== null) {
const markdownFile = await this.markdownContentFromGithub(tag[1], release)
const markdownFile = await this.markdownContentFromGithub(tag[1], githubRelease)
result = result.replace(RegExp(`<!--\\s<${tag[1]}>\\s-->\\n[\\s\\S]*<!--\\s<\\/${tag[1]}>\\s-->`, 'm'), `<!-- <${tag[1]}> -->\n${markdownFile}\n<!-- </${tag[1]}> -->`)
}
return result
}

async markdownContentFromGithub(filename, release) {
async markdownContentFromGithub(filename, githubRelease) {
if (githubs.keys.includes(filename)) {
return githubs.valueOfFilename(filename, release)
return githubs.valueOfFilename(filename, githubRelease)
}
return (await MarkdownContents.makeMarkdownContentsFromPinpointGithub(filename)).contents
}
}

const githubs = {
valueOfFilename: async function (filename, release) {
valueOfFilename: async function (filename, githubRelease) {
switch (filename) {
case 'latestReleaseNotes.md':
return ReleaseNotes.makeLatestReleaseNotes(release).contents
return ReleaseNotes.makeLatestReleaseNotes(githubRelease).contents
default:
return (await MarkdownContents.makeMarkdownContentsFromPinpointReadme(filename)).contents
}
Expand Down

0 comments on commit fe5cf1f

Please sign in to comment.