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

feat(git config): Set default user.name and user.email in git config #2012

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
fix tests and update index.js
  • Loading branch information
chouetz committed Jan 22, 2025
commit ab5d862ce859c0e3a909ea13d61a0112d9018d3b
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -40,12 +40,12 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/
#
# Default: ${{ github.token }}
token: ''
# Github slug used to configure local user.name and user.email for git.
# This is required to push a commit from a Github Action Workflow
# Set to '' to disable this configuration
# Default: "github-action[bot]
git-config: ''

# Github slug used to configure local user.name and user.email for git. This is
# required to push a commit from a Github Action Workflow. Set to '' to disable
# this configuration.
# Default: github-action[bot]
git-user: ''

# SSH key used to fetch the repository. The SSH key is configured with the local
# git config, which enables your scripts to run authenticated git commands. The
11 changes: 6 additions & 5 deletions __test__/git-auth-helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as core from '@actions/core'
import * as fs from 'fs'
import * as gitAuthHelper from '../lib/git-auth-helper'
import * as gitAuthHelper from '../src/git-auth-helper'
import * as io from '@actions/io'
import * as os from 'os'
import * as path from 'path'
import * as stateHelper from '../lib/state-helper'
import {IGitCommandManager} from '../lib/git-command-manager'
import {IGitSourceSettings} from '../lib/git-source-settings'
import * as stateHelper from '../src/state-helper'
import {IGitCommandManager} from '../src/git-command-manager'
import {IGitSourceSettings} from '../src/git-source-settings'

const isWindows = process.platform === 'win32'
const testWorkspace = path.join(__dirname, '_temp', 'git-auth-helper')
@@ -824,7 +824,8 @@ async function setup(testName: string): Promise<void> {
sshUser: '',
workflowOrganizationId: 123456,
setSafeDirectory: true,
githubServerUrl: githubServerUrl
githubServerUrl: githubServerUrl,
gitUser: 'github-action[bot]'
}
}

4 changes: 2 additions & 2 deletions __test__/git-command-manager.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as exec from '@actions/exec'
import * as fshelper from '../lib/fs-helper'
import * as commandManager from '../lib/git-command-manager'
import * as fshelper from '../src/fs-helper'
import * as commandManager from '../src/git-command-manager'

let git: commandManager.IGitCommandManager
let mockExec = jest.fn()
4 changes: 2 additions & 2 deletions __test__/git-directory-helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as core from '@actions/core'
import * as fs from 'fs'
import * as gitDirectoryHelper from '../lib/git-directory-helper'
import * as gitDirectoryHelper from '../src/git-directory-helper'
import * as io from '@actions/io'
import * as path from 'path'
import {IGitCommandManager} from '../lib/git-command-manager'
import {IGitCommandManager} from '../src/git-command-manager'

const testWorkspace = path.join(__dirname, '_temp', 'git-directory-helper')
let repositoryPath: string
8 changes: 4 additions & 4 deletions __test__/input-helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as core from '@actions/core'
import * as fsHelper from '../lib/fs-helper'
import * as fsHelper from '../src/fs-helper'
import * as github from '@actions/github'
import * as inputHelper from '../lib/input-helper'
import * as inputHelper from '../src/input-helper'
import * as path from 'path'
import * as workflowContextHelper from '../lib/workflow-context-helper'
import {IGitSourceSettings} from '../lib/git-source-settings'
import * as workflowContextHelper from '../src/workflow-context-helper'
import {IGitSourceSettings} from '../src/git-source-settings'

const originalGitHubWorkspace = process.env['GITHUB_WORKSPACE']
const gitHubWorkspace = path.resolve('/checkout-tests/workspace')
4 changes: 2 additions & 2 deletions __test__/ref-helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'assert'
import * as refHelper from '../lib/ref-helper'
import {IGitCommandManager} from '../lib/git-command-manager'
import * as refHelper from '../src/ref-helper'
import {IGitCommandManager} from '../src/git-command-manager'

const commit = '1234567890123456789012345678901234567890'
let git: IGitCommandManager
2 changes: 1 addition & 1 deletion __test__/retry-helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as core from '@actions/core'
import {RetryHelper} from '../lib/retry-helper'
import {RetryHelper} from '../src/retry-helper'

let info: string[]
let retryHelper: any
23 changes: 21 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1357,6 +1357,15 @@ function getSource(settings) {
core.setOutput('commit', commitSHA.trim());
// Check for incorrect pull request merge commit
yield refHelper.checkCommitInfo(settings.authToken, commitInfo, settings.repositoryOwner, settings.repositoryName, settings.ref, settings.commit, settings.githubServerUrl);
if (settings.gitUser) {
if (!(yield git.configExists('user.name', true))) {
yield git.config('user.name', settings.gitUser, true);
}
if (!(yield git.configExists('user.email', true))) {
const userId = yield githubApiHelper.getUserId(settings.gitUser, settings.authToken, settings.githubServerUrl);
yield git.config('user.email', `${userId}+${settings.gitUser}@users.noreply.github.com`, true);
}
}
}
finally {
// Remove auth
@@ -1546,6 +1555,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.downloadRepository = downloadRepository;
exports.getDefaultBranch = getDefaultBranch;
exports.getUserId = getUserId;
const assert = __importStar(__nccwpck_require__(9491));
const core = __importStar(__nccwpck_require__(2186));
const fs = __importStar(__nccwpck_require__(7147));
@@ -1663,6 +1673,15 @@ function downloadArchive(authToken, owner, repo, ref, commit, baseUrl) {
return Buffer.from(response.data); // response.data is ArrayBuffer
});
}
function getUserId(username, authToken, baseUrl) {
return __awaiter(this, void 0, void 0, function* () {
const octokit = github.getOctokit(authToken, {
baseUrl: (0, url_helper_1.getServerApiUrl)(baseUrl)
});
const user = yield octokit.rest.users.getByUsername({ username, });
return user.data.id;
});
}


/***/ }),
@@ -1813,8 +1832,8 @@ function getInputs() {
core.debug(`recursive submodules = ${result.nestedSubmodules}`);
// Auth token
result.authToken = core.getInput('token', { required: true });
// Configure user
result.gitUser = (core.getInput('git-user') || 'github-action[bot]')
// Git user
result.gitUser = core.getInput('git-user') || 'github-action[bot]';
// SSH
result.sshKey = core.getInput('ssh-key');
result.sshKnownHosts = core.getInput('ssh-known-hosts');
16 changes: 9 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 13 additions & 6 deletions src/git-source-provider.ts
Original file line number Diff line number Diff line change
@@ -275,14 +275,21 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
settings.githubServerUrl
)
if (settings.gitUser) {
if (!await git.configExists('user.name', true)) {
if (!(await git.configExists('user.name', true))) {
await git.config('user.name', settings.gitUser, true)
}
if (!await git.configExists('user.email', true)) {

const userId = await githubApiHelper.getUserId(settings.gitUser, settings.authToken, settings.githubServerUrl);
await git.config('user.email', `${userId}+${settings.gitUser}@users.noreply.github.com`, true)
}
if (!(await git.configExists('user.email', true))) {
const userId = await githubApiHelper.getUserId(
settings.gitUser,
settings.authToken,
settings.githubServerUrl
)
await git.config(
'user.email',
`${userId}+${settings.gitUser}@users.noreply.github.com`,
true
)
}
}
} finally {
// Remove auth
2 changes: 1 addition & 1 deletion src/github-api-helper.ts
Original file line number Diff line number Diff line change
@@ -152,6 +152,6 @@ export async function getUserId(
const octokit = github.getOctokit(authToken, {
baseUrl: getServerApiUrl(baseUrl)
})
const user = await octokit.rest.users.getByUsername({username,});
const user = await octokit.rest.users.getByUsername({username})
return user.data.id
}