Skip to content

Commit

Permalink
adding plain text support
Browse files Browse the repository at this point in the history
* adding plaintext support w/test

* updating readme

* fix typo

* bumping version

Co-authored-by: Brad Gregory <brad_gregory@intuit.com>
  • Loading branch information
bgreg1012 and Brad Gregory authored Mar 26, 2021
1 parent 38ed5ae commit 40a2ee0
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ config.yaml

#vim
*.swp
.idea
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com),
and this project adheres to [Semantic Versioning](https://semver.org).

## [3.2.0] - 2021-03-26
### Added
- Added plain text file support

## [3.1.1] - 2020-12-11
### Fix
- Remove '.git' from repo_name
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ optional arguments:
-l PR_LABELS [PR_LABELS ...], --labels PR_LABELS [PR_LABELS ...]
List of space separated label names you wish to add to
your pull request(s) (default: [])
-F FILE, --file FILE
File to change, currently only supported with
PlainTextUpdater. (default: None)
-M, --major Bump the major version. (default: None)
-m, --minor Bump the minor version. (default: None)
-p, --patch Bump the patch version. (default: None)
Expand Down
12 changes: 12 additions & 0 deletions gordian/files/plaintext_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from . import BaseFile

class PlainTextFile(BaseFile):

def __init__(self, github_file, repo):
super().__init__(github_file, repo)

def _load_objects(self):
return self.file_contents

def _dump(self):
return self.file_contents
8 changes: 8 additions & 0 deletions gordian/gordian.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ def get_basic_parser():
dest='pr_labels',
help='List of space separated label names you wish to add to your pull request(s)'
)
parser.add_argument(
'-F', '--file',
required=False,
action='store',
dest='file',
help='The file to look for for the search/replace transformations'
)

fork = parser.add_mutually_exclusive_group(required=False)
fork.add_argument(
'-f', '--fork',
Expand Down
3 changes: 3 additions & 0 deletions gordian/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import time
from retry import retry
from gordian.files import *
from gordian.files.plaintext_file import PlainTextFile

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -72,6 +73,8 @@ def get_objects(self, filename, klass=None):
if ext == '.md':
return MarkdownFile(file, self)

return PlainTextFile(file, self)

def get_files(self):
if not self.files:
contents = self._get_repo_contents('')
Expand Down
27 changes: 27 additions & 0 deletions gordian/transformations.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import logging
import os

import yaml
import re
import jsonpatch
import copy
from deepdiff import DeepDiff

from gordian.files.plaintext_file import PlainTextFile

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -79,3 +83,26 @@ def run(self):
logger.debug(file_str)
self.repo.update_file(f, file_str, message, self.dry_run)
return changes


class PlainTextUpdater(Transformation):

def __init__(self, args, repo):
super().__init__(args, repo)

def run(self):
changes = False
regex = re.compile(str.encode(f"{self.args.search[0]}"), re.MULTILINE|re.DOTALL)
for file in self.repo.get_files():
if os.path.basename(file.path) == self.args.file:
logger.info(f'Found file {self.args.file} at the path {file.path}')
file_objects = self.repo.get_objects(file.path, PlainTextFile)
content_updated = regex.sub(str.encode(f"{self.args.replace[0]}"), file_objects.file_contents)
if content_updated != file_objects.file_contents:
logger.info(f'Updating file {file.path}')
file_objects.file_contents = content_updated
logger.debug(f"File has been changed:\n{file_objects._dump()}")
if not self.dry_run:
file_objects.save(f"Updated the file `{file.path}` to search for: {self.args.search[0]} and replace by {self.args.replace[0]}", self.dry_run)
changes = True
return changes
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup_reqs = ['pytest', 'pytest-cov', 'pytest-runner', 'flake8']
setuptools.setup(
name="gordian",
version="3.1.1",
version="3.2.0",
author="Intuit",
author_email="cg-sre@intuit.com",
description="A tool to search and replace files in a Git repo",
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/content.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
iam
16 changes: 14 additions & 2 deletions tests/test_transformations.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import unittest

from gordian.files.plaintext_file import PlainTextFile
from gordian.repo import Repo
from gordian.transformations import SearchAndReplace
from gordian.transformations import SearchAndReplace, PlainTextUpdater
from unittest.mock import MagicMock, patch

from tests.utils import Utils


class TestSearchAndReplaceTransformation(unittest.TestCase):

class Args(object):
def __init__(self, dry_run=False, search=['iam'], replace=['hello']):
def __init__(self, dry_run=False, search=['iam'], replace=['hello'], file='content.txt'):
self.dry_run = dry_run
self.search = search
self.replace = replace
self.file = file

@patch('gordian.repo.Github')
def setUp(self, mock_git):
Expand Down Expand Up @@ -57,3 +62,10 @@ def test_update_files_empty(self):
self.sandr.run()
self.mock_repo.update_file.assert_not_called()
self.mock_repo.create_pull.assert_not_called()

def test_PlainTextUpdater(self):
self.file = Utils.create_github_content_file(file='content.txt')
self.ptf = PlainTextFile(self.file, self.instance)
self.instance.files = [self.ptf.github_file]
self.ptu = PlainTextUpdater(TestSearchAndReplaceTransformation.Args(), self.instance)
self.assertIs(self.ptu.run(), True)
4 changes: 3 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

class Utils:

def create_github_content_file(file='content.yaml'):
@staticmethod
def create_github_content_file(file=None):
file = file or 'content.yaml'
f = open(f'./tests/fixtures/{file}', 'r')
contents = str(base64.b64encode(bytearray(f.read(), 'utf-8')), 'utf-8')
attributes = {'name': file, 'path': f'/{file}','encoding': 'base64','content': contents}
Expand Down

0 comments on commit 40a2ee0

Please sign in to comment.