Default author and committer to actions.yml defaults

This commit is contained in:
Peter Evans 2020-07-19 20:23:36 +09:00
parent 2f62e00e1b
commit 76c09b178c
6 changed files with 35 additions and 256 deletions

View file

@ -3,7 +3,6 @@ import {createOrUpdateBranch} from './create-or-update-branch'
import {GitHubHelper} from './github-helper'
import {GitCommandManager} from './git-command-manager'
import {GitAuthHelper} from './git-auth-helper'
import {GitIdentityHelper} from './git-identity-helper'
import * as utils from './utils'
export interface Inputs {
@ -113,28 +112,25 @@ export async function createPullRequest(inputs: Inputs): Promise<void> {
`Pull request branch to create or update set to '${inputs.branch}'`
)
// Determine the committer and author
// Configure the committer and author
core.startGroup('Configuring the committer and author')
const gitIdentityHelper = new GitIdentityHelper(git)
const identity = await gitIdentityHelper.getIdentity(
inputs.author,
inputs.committer
)
const parsedAuthor = utils.parseDisplayNameEmail(inputs.author)
const parsedCommitter = utils.parseDisplayNameEmail(inputs.committer)
git.setIdentityGitOptions([
'-c',
`author.name=${identity.authorName}`,
`author.name=${parsedAuthor.name}`,
'-c',
`author.email=${identity.authorEmail}`,
`author.email=${parsedAuthor.email}`,
'-c',
`committer.name=${identity.committerName}`,
`committer.name=${parsedCommitter.name}`,
'-c',
`committer.email=${identity.committerEmail}`
`committer.email=${parsedCommitter.email}`
])
core.info(
`Configured git committer as '${identity.committerName} <${identity.committerEmail}>'`
`Configured git committer as '${parsedCommitter.name} <${parsedCommitter.email}>'`
)
core.info(
`Configured git author as '${identity.authorName} <${identity.authorEmail}>'`
`Configured git author as '${parsedAuthor.name} <${parsedAuthor.email}>'`
)
core.endGroup()

View file

@ -1,98 +0,0 @@
import * as core from '@actions/core'
import {GitCommandManager} from './git-command-manager'
import * as utils from './utils'
// Default the committer and author to the GitHub Actions bot
const DEFAULT_COMMITTER = 'GitHub <noreply@github.com>'
const DEFAULT_AUTHOR =
'github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>'
interface GitIdentity {
authorName: string
authorEmail: string
committerName: string
committerEmail: string
}
export class GitIdentityHelper {
private git: GitCommandManager
constructor(git: GitCommandManager) {
this.git = git
}
private async getGitIdentityFromConfig(): Promise<GitIdentity | undefined> {
if (
(await this.git.configExists('user.name')) &&
(await this.git.configExists('user.email'))
) {
const userName = await this.git.getConfigValue('user.name')
const userEmail = await this.git.getConfigValue('user.email')
return {
authorName: userName,
authorEmail: userEmail,
committerName: userName,
committerEmail: userEmail
}
}
if (
(await this.git.configExists('committer.name')) &&
(await this.git.configExists('committer.email')) &&
(await this.git.configExists('author.name')) &&
(await this.git.configExists('author.email'))
) {
const committerName = await this.git.getConfigValue('committer.name')
const committerEmail = await this.git.getConfigValue('committer.email')
const authorName = await this.git.getConfigValue('author.name')
const authorEmail = await this.git.getConfigValue('author.email')
return {
authorName: authorName,
authorEmail: authorEmail,
committerName: committerName,
committerEmail: committerEmail
}
}
return undefined
}
async getIdentity(author: string, committer: string): Promise<GitIdentity> {
// If either committer or author is supplied they will be cross used
if (!committer && author) {
core.info('Supplied author will also be used as the committer.')
committer = author
}
if (!author && committer) {
core.info('Supplied committer will also be used as the author.')
author = committer
}
// If no committer/author has been supplied, try and fetch identity
// configuration already existing in git config.
if (!committer && !author) {
const identity = await this.getGitIdentityFromConfig()
if (identity) {
core.info('Retrieved a pre-configured git identity.')
return identity
}
}
// Set defaults if no committer/author has been supplied and no
// existing identity configuration was found.
if (!committer && !author) {
core.info('Action defaults set for the author and committer.')
committer = DEFAULT_COMMITTER
author = DEFAULT_AUTHOR
}
const parsedAuthor = utils.parseDisplayNameEmail(author)
const parsedCommitter = utils.parseDisplayNameEmail(committer)
return {
authorName: parsedAuthor.name,
authorEmail: parsedAuthor.email,
committerName: parsedCommitter.name,
committerEmail: parsedCommitter.email
}
}
}