Skip to content

Commit

Permalink
chore: Set git identity before committing changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ackuq committed Jun 3, 2024
1 parent bac581d commit 9300103
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 12 deletions.
36 changes: 30 additions & 6 deletions actions/update-standpoints/dist/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27873,15 +27873,20 @@ module.exports = parseParams

/* harmony export */ __nccwpck_require__.d(__webpack_exports__, {
/* harmony export */ "BP": () => (/* binding */ BRANCH_NAME),
/* harmony export */ "J1": () => (/* binding */ GIT_USERNAME),
/* harmony export */ "QH": () => (/* binding */ MAIN_BRANCH),
/* harmony export */ "gK": () => (/* binding */ TEMP_BRANCH_NAME)
/* harmony export */ "gK": () => (/* binding */ TEMP_BRANCH_NAME),
/* harmony export */ "oT": () => (/* binding */ GIT_EMAIL)
/* harmony export */ });
// @ts-check

const MAIN_BRANCH = "main";
const BRANCH_NAME = "action-update-standpoints";
const TEMP_BRANCH_NAME = `temp-${BRANCH_NAME}`;

const GIT_USERNAME = "github-actions[bot]";
const GIT_EMAIL = "github-actions[bot]@users.noreply.github.com";


/***/ }),

Expand All @@ -27893,14 +27898,29 @@ const TEMP_BRANCH_NAME = `temp-${BRANCH_NAME}`;
/* harmony export */ "Pp": () => (/* binding */ checkIfBranchExists),
/* harmony export */ "Qj": () => (/* binding */ createBranch),
/* harmony export */ "VF": () => (/* binding */ push),
/* harmony export */ "a9": () => (/* binding */ setGitIdentity),
/* harmony export */ "q1": () => (/* binding */ checkHasDiff),
/* harmony export */ "q7": () => (/* binding */ forcePush),
/* harmony export */ "th": () => (/* binding */ commit)
/* harmony export */ });
/* harmony import */ var _actions_exec__WEBPACK_IMPORTED_MODULE_0__ = __nccwpck_require__(4260);
/* harmony import */ var _actions_core__WEBPACK_IMPORTED_MODULE_0__ = __nccwpck_require__(4237);
/* harmony import */ var _actions_exec__WEBPACK_IMPORTED_MODULE_1__ = __nccwpck_require__(4260);
// @ts-check



/**
* @param {string} name
* @param {string} email
*/

async function setGitIdentity(name, email) {
const command = ["config", "--local", "user.name", name];
await exec(command);
const emailCommand = ["config", "--local", "user.email", email];
await exec(emailCommand);
}

/**
* @param {string} branch
*/
Expand Down Expand Up @@ -27968,7 +27988,8 @@ async function checkHasDiff() {
* @param {boolean} throwOnError
*/
async function exec(command, throwOnError = true) {
const response = await (0,_actions_exec__WEBPACK_IMPORTED_MODULE_0__.getExecOutput)("git", command);
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info(`Running \`git ${command.join(" ")}\``);
const response = await (0,_actions_exec__WEBPACK_IMPORTED_MODULE_1__.getExecOutput)("git", command);

if (throwOnError && response.exitCode !== 0) {
throw new Error(`Error running git command: ${response.stderr}`);
Expand All @@ -27994,7 +28015,6 @@ __nccwpck_require__.a(__webpack_module__, async (__webpack_handle_async_dependen


_actions_core__WEBPACK_IMPORTED_MODULE_0__.startGroup("Checking if changes exist");

const hasDiff = await (0,_git_helper_mjs__WEBPACK_IMPORTED_MODULE_2__/* .checkHasDiff */ .q1)();
if (!hasDiff) {
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info("No changes detected, exiting...");
Expand All @@ -28003,13 +28023,17 @@ if (!hasDiff) {
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info("Changes detected, continuing...");
_actions_core__WEBPACK_IMPORTED_MODULE_0__.endGroup();

_actions_core__WEBPACK_IMPORTED_MODULE_0__.startGroup("Setting git identity");
await (0,_git_helper_mjs__WEBPACK_IMPORTED_MODULE_2__/* .setGitIdentity */ .a9)(_constants_mjs__WEBPACK_IMPORTED_MODULE_1__/* .GIT_USERNAME */ .J1, _constants_mjs__WEBPACK_IMPORTED_MODULE_1__/* .GIT_EMAIL */ .oT);
_actions_core__WEBPACK_IMPORTED_MODULE_0__.endGroup();

_actions_core__WEBPACK_IMPORTED_MODULE_0__.startGroup("Create branch and commit changes");
const branchExists = await (0,_git_helper_mjs__WEBPACK_IMPORTED_MODULE_2__/* .checkIfBranchExists */ .Pp)(_constants_mjs__WEBPACK_IMPORTED_MODULE_1__/* .BRANCH_NAME */ .BP);
if (branchExists) {
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info(`Branch ${_constants_mjs__WEBPACK_IMPORTED_MODULE_1__/* .BRANCH_NAME */ .BP} already exists, rebasing...`);
// Create temp branch to store changes
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info("Creating temp branch to store changes");
await (0,_git_helper_mjs__WEBPACK_IMPORTED_MODULE_2__/* .createBranch */ .Qj)(_constants_mjs__WEBPACK_IMPORTED_MODULE_1__/* .TEMP_BRANCH_NAME */ .gK, _constants_mjs__WEBPACK_IMPORTED_MODULE_1__/* .MAIN_BRANCH */ .QH);
// Checkout temp branch
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info("Checking out temporary branch");
await (0,_git_helper_mjs__WEBPACK_IMPORTED_MODULE_2__/* .checkout */ .JE)(_constants_mjs__WEBPACK_IMPORTED_MODULE_1__/* .TEMP_BRANCH_NAME */ .gK);
} else {
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info(`Branch ${_constants_mjs__WEBPACK_IMPORTED_MODULE_1__/* .BRANCH_NAME */ .BP} does not exist, creating...`);
Expand Down
3 changes: 3 additions & 0 deletions actions/update-standpoints/src/constants.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
export const MAIN_BRANCH = "main";
export const BRANCH_NAME = "action-update-standpoints";
export const TEMP_BRANCH_NAME = `temp-${BRANCH_NAME}`;

export const GIT_USERNAME = "github-actions[bot]";
export const GIT_EMAIL = "github-actions[bot]@users.noreply.github.com";
14 changes: 14 additions & 0 deletions actions/update-standpoints/src/git-helper.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
// @ts-check
import core from "@actions/core";
import { getExecOutput } from "@actions/exec";

/**
* @param {string} name
* @param {string} email
*/

export async function setGitIdentity(name, email) {
const command = ["config", "--local", "user.name", name];
await exec(command);
const emailCommand = ["config", "--local", "user.email", email];
await exec(emailCommand);
}

/**
* @param {string} branch
*/
Expand Down Expand Up @@ -68,6 +81,7 @@ export async function checkHasDiff() {
* @param {boolean} throwOnError
*/
async function exec(command, throwOnError = true) {
core.info(`Running \`git ${command.join(" ")}\``);
const response = await getExecOutput("git", command);

if (throwOnError && response.exitCode !== 0) {
Expand Down
20 changes: 14 additions & 6 deletions actions/update-standpoints/src/main.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// @ts-check
import core from "@actions/core";

import { BRANCH_NAME, MAIN_BRANCH, TEMP_BRANCH_NAME } from "./constants.mjs";
import {
BRANCH_NAME,
GIT_EMAIL,
GIT_USERNAME,
MAIN_BRANCH,
TEMP_BRANCH_NAME,
} from "./constants.mjs";
import {
checkHasDiff,
checkIfBranchExists,
Expand All @@ -10,10 +16,10 @@ import {
createBranch,
forcePush,
push,
setGitIdentity,
} from "./git-helper.mjs";

core.startGroup("Checking if changes exist");

const hasDiff = await checkHasDiff();
if (!hasDiff) {
core.info("No changes detected, exiting...");
Expand All @@ -22,20 +28,22 @@ if (!hasDiff) {
core.info("Changes detected, continuing...");
core.endGroup();

core.startGroup("Setting git identity");
await setGitIdentity(GIT_USERNAME, GIT_EMAIL);
core.endGroup();

core.startGroup("Create branch and commit changes");
const branchExists = await checkIfBranchExists(BRANCH_NAME);
if (branchExists) {
core.info(`Branch ${BRANCH_NAME} already exists, rebasing...`);
// Create temp branch to store changes
core.info("Creating temp branch to store changes");
await createBranch(TEMP_BRANCH_NAME, MAIN_BRANCH);
// Checkout temp branch
core.info("Checking out temporary branch");
await checkout(TEMP_BRANCH_NAME);
} else {
core.info(`Branch ${BRANCH_NAME} does not exist, creating...`);
// Create branch
await createBranch(BRANCH_NAME, MAIN_BRANCH);
}
// Add and commit changes
await commit("Update standpoints");
core.endGroup();

Expand Down

0 comments on commit 9300103

Please sign in to comment.