Skip to content

Commit

Permalink
fix not setting git user for commits
Browse files Browse the repository at this point in the history
  • Loading branch information
timonmasberg committed Apr 30, 2023
1 parent e8b2ee1 commit 20efc5d
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 37 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ If a new version exists, it automatically runs `nx migrate latest`, installs all
## Usage

The easiest way is to check for new versions with a scheduled workflow.
Make sure to checkout the branch you want apply the migrations in case there is a new version of NX.
Make sure to checkout the branch you want apply the migrations in case there is a new version of NX. The Action will create commits for the applied migrations.

For a detailed description of all parameters check the [action.yml](action.yml).

Expand All @@ -29,7 +29,6 @@ jobs:
with:
repoToken: ${{ secrets.GITHUB_TOKEN }}
# Optional:
commitMessage: 'deps: migrate nx to $VERSION'
prTitle: 'Migrates NX to $VERSION'
includeMigrationsFile: false # `migrations.json` will not be included in this PR.
base: 'dev'
Expand Down
4 changes: 0 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ inputs:
required: false
description: 'The name of the branch you want the changes pulled into.'
default: 'main'
commitMessage:
required: false
description: 'Commit Message where $VERSION is the new version of NX.'
default: 'migrates NX to $VERSION'
includeMigrationsFile:
required: false
description: 'If false, the migrations.json file will be deleted after running the migrations. It is considered best-practice to check-in the migrations.json.'
Expand Down
24 changes: 13 additions & 11 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21210,17 +21210,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.makePRBody = exports.pushChangesToRemote = void 0;
exports.makePRBody = exports.pushChangesToRemote = exports.prepareGit = void 0;
const exec_1 = __nccwpck_require__(1514);
function pushChangesToRemote(commitMessage, repoName, origin) {
function prepareGit(origin, branchName) {
return __awaiter(this, void 0, void 0, function* () {
yield (0, exec_1.exec)(`git checkout -b ${branchName}`);
yield (0, exec_1.exec)(`git config --local user.email "github-actions[bot]@users.noreply.github.com"`);
yield (0, exec_1.exec)(`git config --local user.name "github-actions[bot]"`);
yield (0, exec_1.exec)(`git remote set-url origin ${origin}`);
yield (0, exec_1.exec)(`git checkout -b ${repoName}`);
yield (0, exec_1.exec)('git add .');
yield (0, exec_1.exec)(`git commit -m "${commitMessage}"`);
yield (0, exec_1.exec)(`git push --force-with-lease -u origin ${repoName}`);
});
}
exports.prepareGit = prepareGit;
function pushChangesToRemote(branchName) {
return __awaiter(this, void 0, void 0, function* () {
yield (0, exec_1.exec)(`git push --force-with-lease -u origin ${branchName}`);
});
}
exports.pushChangesToRemote = pushChangesToRemote;
Expand Down Expand Up @@ -21277,7 +21280,6 @@ exports.getInputs = void 0;
const core = __importStar(__nccwpck_require__(2186));
function getInputs() {
const repoToken = core.getInput('repoToken', { required: true });
const commitMessage = core.getInput('commitMessage', { required: false });
const includeMigrationsFile = core.getInput('includeMigrationsFile', {
required: false
});
Expand All @@ -21287,7 +21289,6 @@ function getInputs() {
const prTitle = core.getInput('prTitle', { required: false });
return {
repoToken,
commitMessage,
includeMigrationsFile: Boolean(includeMigrationsFile),
legacyPeerDeps: Boolean(legacyPeerDeps),
prTitle
Expand Down Expand Up @@ -21371,12 +21372,13 @@ function run() {
owner: 'nrwl',
repo: 'nx'
});
core.debug('Setting up git user, origin and branch...');
const origin = `https://x-access-token:${inputs.repoToken}@github.com/${github.context.repo.owner}/${github.context.repo.repo}`;
yield (0, git_1.prepareGit)(origin, branchName);
core.debug('Starting migrations...');
yield (0, nx_migrate_1.migrate)(inputs.includeMigrationsFile, inputs.legacyPeerDeps);
core.debug('Pushing changes...');
const commitMessage = inputs.commitMessage.replace('$VERSION', latestNxVersion);
const origin = `https://x-access-token:${inputs.repoToken}@github.com/${github.context.repo.owner}/${github.context.repo.repo}`;
yield (0, git_1.pushChangesToRemote)(commitMessage, branchName, origin);
yield (0, git_1.pushChangesToRemote)(branchName);
core.info(`Pushed changes to origin/${branchName}`);
core.debug('Creating Pull Request...');
const { data: newPr } = yield octokit.rest.pulls.create(Object.assign(Object.assign({}, github.context.repo), { title: inputs.prTitle.replace('$VERSION', latestNxVersion), body: (0, git_1.makePRBody)(latestNxGHRelease.body || 'No release notes', latestNxGHRelease.created_at, latestNxGHRelease.html_url), head: branchName, base: 'main' }));
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

16 changes: 7 additions & 9 deletions src/git.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import {exec} from '@actions/exec'

export async function pushChangesToRemote(
commitMessage: string,
repoName: string,
origin: string
export async function prepareGit(
origin: string,
branchName: string
): Promise<void> {
await exec(`git checkout -b ${branchName}`)
await exec(
`git config --local user.email "github-actions[bot]@users.noreply.github.com"`
)
await exec(`git config --local user.name "github-actions[bot]"`)
await exec(`git remote set-url origin ${origin}`)
}

await exec(`git checkout -b ${repoName}`)
await exec('git add .')
await exec(`git commit -m "${commitMessage}"`)

await exec(`git push --force-with-lease -u origin ${repoName}`)
export async function pushChangesToRemote(branchName: string): Promise<void> {
await exec(`git push --force-with-lease -u origin ${branchName}`)
}

export function makePRBody(
Expand Down
2 changes: 0 additions & 2 deletions src/inputs-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import Inputs from './inputs.model'

export function getInputs(): Inputs {
const repoToken = core.getInput('repoToken', {required: true})
const commitMessage = core.getInput('commitMessage', {required: false})
const includeMigrationsFile = core.getInput('includeMigrationsFile', {
required: false
})
Expand All @@ -14,7 +13,6 @@ export function getInputs(): Inputs {

return {
repoToken,
commitMessage,
includeMigrationsFile: Boolean(includeMigrationsFile),
legacyPeerDeps: Boolean(legacyPeerDeps),
prTitle
Expand Down
1 change: 0 additions & 1 deletion src/inputs.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export default interface Inputs {
repoToken: string
commitMessage: string
prTitle: string
includeMigrationsFile: boolean
legacyPeerDeps: boolean
Expand Down
14 changes: 7 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as core from '@actions/core'
import * as github from '@actions/github'
import {getInputs} from './inputs-helper'
import {getCurrentNxVersion, getLatestNxVersion} from './nx-version'
import {makePRBody, pushChangesToRemote} from './git'
import {makePRBody, prepareGit, pushChangesToRemote} from './git'
import {migrate} from './nx-migrate'

async function run(): Promise<void> {
Expand Down Expand Up @@ -51,16 +51,16 @@ async function run(): Promise<void> {
}
)

core.debug('Setting up git user, origin and branch...')
const origin = `https://x-access-token:${inputs.repoToken}@github.com/${github.context.repo.owner}/${github.context.repo.repo}`
await prepareGit(origin, branchName)

core.debug('Starting migrations...')
await migrate(inputs.includeMigrationsFile, inputs.legacyPeerDeps)

core.debug('Pushing changes...')
const commitMessage = inputs.commitMessage.replace(
'$VERSION',
latestNxVersion
)
const origin = `https://x-access-token:${inputs.repoToken}@github.com/${github.context.repo.owner}/${github.context.repo.repo}`
await pushChangesToRemote(commitMessage, branchName, origin)

await pushChangesToRemote(branchName)
core.info(`Pushed changes to origin/${branchName}`)

core.debug('Creating Pull Request...')
Expand Down

0 comments on commit 20efc5d

Please sign in to comment.