Skip to content

Commit

Permalink
Add committer_message option and skip commit if files are identicals
Browse files Browse the repository at this point in the history
  • Loading branch information
lowlighter committed Mar 14, 2021
1 parent b0b16c9 commit c0f0363
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
19 changes: 16 additions & 3 deletions source/app/action/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import metrics from "../metrics/index.mjs"
import fs from "fs/promises"
import paths from "path"
import sgit from "simple-git"
process.on("unhandledRejection", error => { throw error }) //eslint-disable-line max-statements-per-line, brace-style

//Debug message buffer
Expand Down Expand Up @@ -66,7 +67,7 @@
filename, optimize, verify,
debug, "debug.flags":dflags, "use.mocked.data":mocked, dryrun,
"plugins.errors.fatal":die,
"committer.token":_token, "committer.branch":_branch,
"committer.token":_token, "committer.branch":_branch, "committer.message":_message,
"use.prebuilt.image":_image,
retries, "retries.delay":retries_delay,
"output.action":_action,
Expand Down Expand Up @@ -127,6 +128,7 @@
//Compute committer informations
committer.token = _token || token
committer.commit = true
committer.message = _message.replace(/[$][{]filename[}]/g, filename)
committer.pr = /^pull-request/.test(_action)
committer.merge = _action.match(/^pull-request-(?<method>merge|squash|rebase)$/)?.groups?.method ?? null
committer.branch = _branch || github.context.ref.replace(/^refs[/]heads[/]/, "")
Expand Down Expand Up @@ -265,10 +267,21 @@
info(`Save to /metrics_renders/${filename}`, "ok")
}

//Check editions
if ((committer.commit)||(committer.pr)) {
const git = sgit()
const sha = await git.hashObject(filename)
info("Current render sha", sha)
if (committer.sha === sha) {
info(`Commit to branch ${committer.branch}`, "(no changes)")
committer.commit = false
}
}

//Commit metrics
if (committer.commit) {
await committer.rest.repos.createOrUpdateFileContents({
...github.context.repo, path:filename, message:`Update ${filename} - [Skip GitHub Action]`,
...github.context.repo, path:filename, message:committer.message,
content:Buffer.from(rendered).toString("base64"),
branch:committer.pr ? committer.head : committer.branch,
...(committer.sha ? {sha:committer.sha} : {}),
Expand All @@ -291,7 +304,7 @@
const q = `repo:${github.context.repo.owner}/${github.context.repo.repo}+type:pr+state:open+Auto-generated metrics for run #${github.context.runId}+in:title`
const prs = (await committer.rest.search.issuesAndPullRequests({q})).data.items.filter(({user:{login}}) => login === "github-actions[bot]")
if (prs.length < 1)
throw new Error("0 matching prs. Cannot preoceed.")
throw new Error("0 matching prs. Cannot proceed.")
if (prs.length > 1)
throw new Error(`Found more than one matching prs: ${prs.map(({number}) => `#${number}`).join(", ")}. Cannot proceed.`)
;({number} = prs.shift())
Expand Down
7 changes: 7 additions & 0 deletions source/plugins/core/metadata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ inputs:
type: string
default: "" # Defaults to your repository default branch

# Commit message
# Use "${filename}" to display filename
committer_message:
description: Commit message
type: string
default: Update ${filename} - [Skip GitHub Action]

# Rendered metrics output path, relative to repository's root
filename:
description: Rendered metrics output path
Expand Down

0 comments on commit c0f0363

Please sign in to comment.