Skip to content

Commit 420181e

Browse files
committed
feat: added github automation script
1 parent 8d189cb commit 420181e

10 files changed

+214
-0
lines changed

Github-Automation/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/venv

Github-Automation/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Github-Automation
2+
3+
## About This Project
4+
This script allows user to completely automate github workflow. This script keeps track of changed files and generates a diff, then auto adds and commits(asks for a commit message) the changed files.
5+
6+
## Features
7+
8+
- Auto fetches repository info(uri , working branch) from local dir
9+
- Keeps track of changed files/dir including new added files
10+
- Calculates diff for changed files
11+
- Auto performs git commands for changed files(Need to pass commit messages)
12+
13+
## How To Run
14+
15+
To run the script use following commands
16+
17+
18+
1. Run the installation script
19+
```bash
20+
python install.py
21+
```
22+
2. Enter the project directory where changes are to be listened
23+
```bash
24+
Enter installation directory: project_dir
25+
```
26+
3. goto your project directory
27+
```bash
28+
cd project_dir
29+
```
30+
31+
4. Run the python script to listen for changes
32+
```python
33+
python main.py
34+
```
35+

Github-Automation/diffcalc.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import difflib
2+
3+
def calcDiff(firstFile , secondFile):
4+
diff = difflib.ndiff(firstFile , secondFile)
5+
delta = ''.join(x[2:] for x in diff if x.startswith('- '))
6+
deltaNoSpace = delta.split('\n')
7+
print('CHANGED LINES ARE:-\n-----------------')
8+
for ele in deltaNoSpace:
9+
print(ele.strip())
10+
print('-----------------\n')

Github-Automation/filechange.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import os
2+
from os import listdir
3+
from os.path import isfile, join
4+
import gitcommands as git
5+
import time
6+
import diffcalc
7+
mypath = os.getcwd()
8+
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
9+
def read_file():
10+
filecontent = []
11+
for file in onlyfiles:
12+
with open(onlyfiles[onlyfiles.index(file)], "r") as f:
13+
filecontent.append(f.readlines())
14+
return filecontent
15+
16+
def ischanged(url , branch):
17+
changedfile = []
18+
print('Listening for changes....')
19+
initial = list(read_file())
20+
while True:
21+
current = list(read_file())
22+
changeditem = []
23+
previtem = []
24+
if(current != initial):
25+
# Calculating Previous Version of File
26+
for ele in initial:
27+
if ele not in current:
28+
for item in ele:
29+
previtem.append(item)
30+
# Calculating New Version of File
31+
for ele in current:
32+
if ele not in initial:
33+
changeditem.append(ele)
34+
# calculating changed file's name
35+
for i in range(0 ,len(changeditem)):
36+
print('loop :-' , i)
37+
changedfile.append(onlyfiles[current.index(changeditem[i])])
38+
print('Changed file is:-' , changedfile,'\n')
39+
# Calculating Diff for previous and changed version of file
40+
diffcalc.calcDiff(previtem , changeditem[0])
41+
42+
# Performing Git Commands For Changed File
43+
# Performing Git Add
44+
git.add(changedfile)
45+
# Performing Git Commit
46+
if(git.commit(changedfile) == False):
47+
print('Reverting Push')
48+
# Performing Git Push
49+
elif(len(changedfile) == 0):
50+
git.push(url , branch)
51+
initial = current
52+
53+
54+
55+

Github-Automation/gitcommands.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from subprocess import call
2+
from sys import platform as _platform
3+
def init():
4+
print('git init')
5+
# git add <filename>
6+
def createReadme():
7+
if _platform == "linux" or _platform == "linux2":
8+
print('touch README.md')
9+
elif _platform == "darwin":
10+
print('touch README.md')
11+
elif _platform == "win32":
12+
print('type nul>README.md')
13+
14+
15+
# Windows
16+
def add(filelist):
17+
for file in filelist:
18+
# perform git add on file
19+
print("Adding" , file)
20+
print(('git add ' + file))
21+
22+
# git commit -m "passed message"
23+
def commit(filelist):
24+
for file in filelist:
25+
# ask user for commit message
26+
msg = str(input('Enter the commit message for ' + file + ' or enter -r to reject commit'))
27+
# if msg == -r reject commit
28+
if(msg == '-r'):
29+
filelist.remove(file)
30+
print('commit rejected')
31+
call('cls', shell=True)
32+
return False
33+
# else execute git commit for the file
34+
#added a comment
35+
else:
36+
filelist.remove(file)
37+
print('git commit -m "' + msg + '"')
38+
call('cls' , shell=True)
39+
# git push
40+
def push(url , branch):
41+
print('git push -u ' + url + ' ' + branch)
42+
#added a comment

Github-Automation/install.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from shutil import copy , copyfile
2+
import os
3+
dir = os.getcwd()
4+
files = []
5+
# files = ['diffcalc.py' , 'main.py' , 'filechange.py' , 'gitcommands.py' , 'repoInfo.py']
6+
for file in os.listdir(dir):
7+
if(file.endswith('.py') and file != 'install.py'):
8+
files.append(file)
9+
print(files)
10+
def copyfiles(file:str , dst:str):
11+
copy(file , dst)
12+
print('Installation Successful\n')
13+
14+
def installfiles():
15+
location = input('Enter installation directory: ')
16+
print('Installing Files')
17+
for file in files:
18+
print('Installing %s' % file)
19+
copyfiles(file , location)
20+
21+
installfiles()

Github-Automation/main.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
import repoInfo,filechange
3+
import gitcommands as git
4+
def init():
5+
info = repoInfo.checkinfoInDir()
6+
if('n' in info):
7+
info.remove('n')
8+
git.init()
9+
git.createReadme()
10+
git.add(['README.md'])
11+
git.commit(['README.md'])
12+
print('initial setup done :)')
13+
filechange.ischanged(info[0] , info[1])
14+
git.push(info[0] , info[1])
15+
else:
16+
print('Retrieving info from git directory')
17+
filechange.ischanged(info[0] , info[1])
18+
19+
if __name__ == '__main__':
20+
init()

Github-Automation/repoInfo.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import os
2+
mypath = os.getcwd()
3+
infofile = mypath + '/.git/config'
4+
5+
def takeInfo():
6+
print('No Existing repo info found\n')
7+
url = str(input('Enter the Github Repo URL: '))
8+
branch = str(input('Enter the branch: '))
9+
info = ['n' , url , branch]
10+
return info
11+
12+
def checkinfoInDir():
13+
if (os.path.exists(infofile)):
14+
print('Repo Info Found:-')
15+
with open(infofile, "r") as f:
16+
info = f.readlines()
17+
# print(info)
18+
for ele in info:
19+
if('url' in ele):
20+
url = info[info.index(ele)].split()[2]
21+
22+
if('branch' in ele):
23+
branch = info[info.index(ele)].split()[1].split('"')[1]
24+
info = [url , branch]
25+
else:
26+
info = takeInfo()
27+
return info
28+
29+
print(checkinfoInDir())

Github-Automation/requirements.txt

Whitespace-only changes.

Github-Automation/samplefile.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
To test the script's functionality make changes to these files.

0 commit comments

Comments
 (0)