Skip to content

Commit 543ce33

Browse files
authored
Merge pull request #4 from xnivaxhzne/master
Master -> Production
2 parents ed00f08 + 80f85b7 commit 543ce33

9 files changed

+224
-18
lines changed

.github/workflows/auto-raise-pr.yaml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: "Automatic PR creation"
2+
3+
on:
4+
push:
5+
branches:
6+
- "master"
7+
8+
jobs:
9+
raise-pr:
10+
name: Raise PR to production
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
15+
- uses: actions/github-script@v4
16+
name: "Raise Pull Request"
17+
with:
18+
script: |
19+
const script = require('./.github/workflows/pullRequestAction.cjs')
20+
await script({github, context})
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: "Version Increment and PR Creation"
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
versionType:
7+
description: "Version type (patch, minor, major)"
8+
required: true
9+
type: choice
10+
options:
11+
- patch
12+
- minor
13+
- major
14+
15+
jobs:
16+
version-and-pr:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v4
20+
with:
21+
fetch-depth: 0
22+
23+
- uses: actions/github-script@v4
24+
name: "Increment Version and Raise Pull Request"
25+
with:
26+
script: |
27+
const script = require('./.github/workflows/incrementVersionAndCreatePR.cjs')
28+
await script({
29+
github,
30+
context,
31+
versionType: '${{ github.event.inputs.versionType }}'
32+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const { exec } = require("child_process");
2+
const util = require("util");
3+
4+
const execAsync = util.promisify(exec);
5+
6+
module.exports = async ({ github, context, versionType }) => {
7+
async function configureGit() {
8+
await execAsync('git config user.name "github-actions"');
9+
await execAsync('git config user.email "github-actions@github.com"');
10+
console.log("Configured git user");
11+
}
12+
13+
async function incrementVersion() {
14+
console.log(`Incrementing version with ${versionType}`);
15+
await execAsync(`npm version ${versionType}`);
16+
const packageJson = require("../../package.json");
17+
const version = packageJson.version;
18+
console.log(`Incremented version to v${version}(${versionType})`);
19+
const featureBranch = `chore-increment-verion-${version}-${versionType}-${context.runNumber}`;
20+
return { featureBranch, version };
21+
}
22+
23+
async function pushChangesAndTags(featureBranch) {
24+
await execAsync(`git push origin HEAD:refs/heads/${featureBranch}`);
25+
await execAsync("git push origin --tags");
26+
}
27+
28+
async function getPull(source, target) {
29+
const existingPr = await github.pulls.list({
30+
owner: context.repo.owner,
31+
repo: context.repo.repo,
32+
head: source,
33+
base: target,
34+
});
35+
return existingPr.data.length > 0;
36+
}
37+
38+
async function createPull(title, source, target) {
39+
await github.pulls.create({
40+
title,
41+
body: `${title} PR.`,
42+
owner: context.repo.owner,
43+
repo: context.repo.repo,
44+
head: source,
45+
base: target,
46+
});
47+
}
48+
49+
await configureGit();
50+
const { featureBranch, version } = await incrementVersion();
51+
const hasOpenPr = await getPull(featureBranch, "master");
52+
53+
if (!hasOpenPr) {
54+
await pushChangesAndTags(featureBranch);
55+
const title = `Version Increment to v${version}(${versionType})`;
56+
await createPull(title, featureBranch, "master");
57+
console.log(
58+
`Created pull request for version increment to v${version}(${versionType}).`
59+
);
60+
} else {
61+
throw new Error(
62+
`Pull request already exists for version increment to v${version}(${versionType}).`
63+
);
64+
}
65+
};
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module.exports = async ({ github, context }) => {
2+
async function getPull(source, target, state) {
3+
const existingPr = await github.pulls.list({
4+
owner: context.repo.owner,
5+
repo: context.repo.repo,
6+
head: source,
7+
base: target,
8+
state: state,
9+
});
10+
return existingPr;
11+
}
12+
13+
async function createPull(title, source, target) {
14+
try {
15+
await github.pulls.create({
16+
title: title,
17+
body: `${title} PR.`,
18+
owner: context.repo.owner,
19+
repo: context.repo.repo,
20+
head: source,
21+
base: target,
22+
});
23+
console.log(`Created pull request ${title}.`);
24+
} catch (error) {
25+
console.log(error.errors);
26+
}
27+
}
28+
29+
let sourceBranch = null;
30+
let targetBranch = null;
31+
32+
if (context.ref == "refs/heads/master") {
33+
const existingPr = await getPull("master", "production", "open");
34+
if (existingPr.data.length) {
35+
console.log("Master -> Production PR is already created");
36+
} else {
37+
sourceBranch = "master";
38+
targetBranch = "production";
39+
}
40+
}
41+
42+
if (sourceBranch && targetBranch) {
43+
const sourceName =
44+
sourceBranch.charAt(0).toUpperCase() + sourceBranch.slice(1);
45+
const targetName =
46+
targetBranch.charAt(0).toUpperCase() + targetBranch.slice(1);
47+
const title = `${sourceName} -> ${targetName}`;
48+
await createPull(title, sourceBranch, targetBranch);
49+
}
50+
};

README.md

+15-14
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@ npm i -g @xniva/git-shortcuts
1616

1717
## List of available commands:
1818

19-
| shortcut command | actual git command | action |
20-
| ---------------- | -------------------------------- | -------------------------------------------------------------- |
21-
| `gl` | `git log` | Show the list of commits for the current branch |
22-
| `gs` | `git status` | Show the status of the changes |
23-
| `gb` | `git branch --show-current` | Show the name of the current branch |
24-
| `gcm` | `git checkout master` | Swicth to the master branch |
25-
| `gf` | `git fetch` | Fetch from the remote |
26-
| `gpm` | `git pull origin master` | Pull the changes from the remote master branch |
27-
| `gmm` | `git merge origin master` | Merge the changes from the remote master to the current branch |
28-
| `gph` | `git push origin HEAD` | Push the current branch and its changes to remote |
29-
| `gsr` | `git reset --soft HEAD~1` | Remove the last commit and preserve the changes |
30-
| `ghr` | `git reset --hard HEAD~1` | Completely delete the changes of the last commit |
31-
| `glp` | `git show-branch --no-name HEAD` | Print the last commit message |
32-
| `glc` | `git show-branch --no-name HEAD` | Copy and Print the last commit message |
19+
| shortcut command | actual git command | action |
20+
| ---------------- | ---------------------------------------------------------------------- | -------------------------------------------------------------- |
21+
| `gl` | `git log` | Show the list of commits for the current branch |
22+
| `gs` | `git status` | Show the status of the changes |
23+
| `gb` | `git branch --show-current` | Show the name of the current branch |
24+
| `gcm` | `git checkout master` | Swicth to the master branch |
25+
| `gf` | `git fetch` | Fetch from the remote |
26+
| `gpm` | `git pull origin master` | Pull the changes from the remote master branch |
27+
| `gmm` | `git merge origin master` | Merge the changes from the remote master to the current branch |
28+
| `gph` | `git push origin HEAD` | Push the current branch and its changes to remote |
29+
| `gsr` | `git reset --soft HEAD~1` | Remove the last commit and preserve the changes |
30+
| `ghr` | `git reset --hard HEAD~1` | Completely delete the changes of the last commit |
31+
| `glp` | `git show-branch --no-name HEAD` | Print the last commit message |
32+
| `glc` | `git show-branch --no-name HEAD` | Copy and Print the last commit message |
33+
| `gsf` | `gcm && gpm && git merge --no-commit --no-ff origin/${featureBranch}` | List the changes of a feature branch w.r.t master - for review |

bin/git_see_feature_files.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#! /usr/bin/env node
2+
import main from "./../lib/git_see_feature_files/index.js";
3+
4+
main();

lib/git_see_feature_files/index.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import runCommand from "./../utils/run_command.js";
2+
import inquirer from "inquirer";
3+
4+
const getFeatureBranch = async () => {
5+
const inputFields = [
6+
{
7+
type: "input",
8+
name: "branch",
9+
message: "Feature branch that you want to explore the changed files: ",
10+
},
11+
];
12+
13+
const { branch } = await inquirer.prompt(inputFields);
14+
return branch;
15+
};
16+
17+
const main = async () => {
18+
let featureBranch = process.argv[2];
19+
20+
if (!featureBranch) {
21+
featureBranch = await getFeatureBranch();
22+
}
23+
24+
if (featureBranch) {
25+
runCommand("gcm");
26+
runCommand("gpm");
27+
runCommand(`git merge --no-commit --no-ff origin/${featureBranch}`);
28+
} else {
29+
process.exit(0);
30+
}
31+
};
32+
33+
export default main;

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@xniva/git-shortcuts",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"description": "Shortcuts for the commonly used git commands",
55
"repository": "https://github.com/kavinkuma6/git-shortcuts",
66
"bin": {
@@ -16,7 +16,8 @@
1616
"gsr": "bin/git_soft_reset_last_commit.js",
1717
"glp": "bin/git_print_last_commit_message.js",
1818
"glc": "bin/git_print_copy_last_commit_message.js",
19-
"gr": "bin/git_restore.js"
19+
"gr": "bin/git_restore.js",
20+
"gsf": "bin/git_see_feature_files.js"
2021
},
2122
"author": "Kavinkumar R",
2223
"license": "MIT",

0 commit comments

Comments
 (0)