Exemple #1
0
def main(args, repo):
  commit_files = helpers.oei_fs(args, repo)

  if not commit_files:
    pprint.err('No files to commit')
    pprint.err_exp('use gl track f if you want to track changes to file f')
    return False

  curr_b = repo.current_branch
  partials = None
  if args.p:
    partials = _do_partial_selection(commit_files, curr_b)

  msg = args.m if args.m else commit_dialog.show(commit_files, repo)
  if not msg.strip():
    if partials:
      git.reset('HEAD', partials)
    raise ValueError('Missing commit message')

  _auto_track(commit_files, curr_b)
  ci = curr_b.create_commit(commit_files, msg, partials=partials)
  pprint.ok('Commit succeeded')

  pprint.blank()
  pprint.commit(ci)

  if curr_b.fuse_in_progress:
    _op_continue(curr_b.fuse_continue, 'Fuse')
  elif curr_b.merge_in_progress:
    _op_continue(curr_b.merge_continue, 'Merge')

  return True
Exemple #2
0
def main(args, repo):
  commit_files = helpers.oei_fs(args, repo)

  if not commit_files:
    pprint.err('No files to commit')
    pprint.err_exp('use gl track f if you want to track changes to file f')
    return False

  curr_b = repo.current_branch
  partials = None
  if args.p:
    partials = _do_partial_selection(commit_files, curr_b)

  if not _author_info_is_ok(repo):
    return False

  msg = args.m if args.m else commit_dialog.show(commit_files, repo)
  if not msg.strip():
    if partials:
      git.reset('HEAD', partials)
    raise ValueError('Missing commit message')

  _auto_track(commit_files, curr_b)
  ci = curr_b.create_commit(commit_files, msg, partials=partials)
  pprint.ok('Commit on branch {0} succeeded'.format(repo.current_branch))

  pprint.blank()
  pprint.commit(ci)

  if curr_b.fuse_in_progress:
    _op_continue(curr_b.fuse_continue, 'Fuse')
  elif curr_b.merge_in_progress:
    _op_continue(curr_b.merge_continue, 'Merge')

  return True
Exemple #3
0
    def run(self, sorted_repos, versions_file):
        self.log("`git checkout` repositories...")

        versions = None
        with open(versions_file, 'r') as f:
            versions = json.load(f)

        cd(self._basedir)

        for repo in sorted_repos:
            if repo not in versions:
                self.log("skipping {0}, no version specified.".format(repo))
                continue

            where = versions[repo]  # where to checkout
            self.log("Checkout {0} -> {1}".format(repo, where))

            with push_pop(repo):
                git.fetch()
                git.checkout("--quiet", where)

                # just in case that we didn't just cloned but updated:
                git.reset("--hard", where)

        self.log("done checking out repos.")
Exemple #4
0
    def add_to_blacklist(self, items_to_blacklist, username, code_permissions):
        # Check if we're on master
        if git("rev-parse", "--abbrev-ref", "HEAD").strip() != "master":
            return (False, "Not currently on master.")

        # Check that we're up-to-date with origin (GitHub)
        git.remote.update()
        if git("rev-parse", "refs/remotes/origin/master").strip() != git("rev-parse", "master").strip():
            return (False, "HEAD isn't at tip of origin's master branch")

        # Check that blacklisted_websites.txt isn't modified locally. That could get ugly fast
        if "blacklisted_websites.txt" in git.status():  # Also ugly
            return (False, "blacklisted_websites.txt modified locally. This is probably bad.")

        # Store current commit hash
        current_commit = git("rev-parse", "HEAD").strip()

        # Add items to file
        with open("blacklisted_websites.txt", "a+") as blacklisted_websites:
            last_character = blacklisted_websites.read()[-1:]
            if last_character != "\n":
                blacklisted_websites.write("\n")
            blacklisted_websites.write("\n".join(items_to_blacklist) + "\n")

        # Checkout a new branch (mostly unnecessary, but may help if we create PRs in the future
        branch = "auto-blacklist-{0}".format(str(time.time()))
        git.checkout("-b", branch)

        # Clear HEAD just in case
        git.reset("HEAD")

        git.add("blacklisted_websites.txt")
        git.commit("-m", "Auto blacklist of {0} by {1} --autopull".format(", ".join(items_to_blacklist), username))

        if code_permissions:
            git.checkout("master")
            git.merge(branch)
            git.push()
        else:
            git.push("origin", branch)
            git.checkout("master")

            if GlobalVars.github_username is None or GlobalVars.github_password is None:
                return (False, "tell someone to set a GH password")

            payload = {"title": "{0}: Blacklist {1}".format(username, ", ".join(items_to_blacklist)),
                       "body": "{0} requests blacklist of domains: \n\n - {1}".format(username, "\n - ".join(items_to_blacklist)),
                       "head": branch,
                       "base": "master"}
            response = requests.post("https://api.github.com/repos/Charcoal-SE/SmokeDetector/pulls", auth=HTTPBasicAuth(GlobalVars.github_username, GlobalVars.github_password), data=json.dumps(payload))
            print(response.json())
            return (True, "You don't have code privileges, but I've [created a pull request for you]({0}).".format(response.json()["html_url"]))

        git.checkout(current_commit)  # Return to old commit to await CI. This will make Smokey think it's in reverted mode if it restarts

        if not code_permissions:
            return (False, "Unable to perform action due to lack of code-level permissions. [Branch pushed](https://github.com/Charcoal-SE/SmokeDetector/tree/{0}), PR at your leisure.".format(branch))

        return (True, "Blacklisted {0} - the entry will be applied via autopull if CI succeeds.".format(", ".join(items_to_blacklist)))
Exemple #5
0
 def cleanup(self):
     """
     Does a ``git reset --hard`` to clean up any left-over temporary
     files in the repository.
     """
     if self.device.group.repository.lock.acquired:
         os.chdir(self.device.group.repository.path)
         git.reset('--hard')
Exemple #6
0
 def need_update(p: str) -> Union[bool, int]:
     exists = os.path.exists(join(args.root, p))
     k = {'_cwd': args.root}
     git.checkout('i18n/i18n', '--', p, **k)
     git.reset('HEAD', '--', p, **k)
     if not exists:
         return True
     else:
         return self.file_is_modified(join(args.root, p))
Exemple #7
0
def de_mark_commit(current_branch, mark_message):
    code, output = subprocess.getstatusoutput(
        f'git log -1 --pretty=format:"%s" {current_branch}')
    if code == 0:
        if output == mark_message:
            git.reset('HEAD~')
    else:
        print("查看commit message出错!")
        exit(-1)
Exemple #8
0
    def post_run(self):
        git.reset('--hard')
        try:
            # This hook can be called by a simple git commit --amend
            # without anything to actually get from the stash. In this
            # case, this command will break and we can safely ignore it.
            git.stash('pop', '--quiet', '--index')
        except Exception:
            pass

        sys.exit(self.result)
def updateRepo(url, name, local_storage):
    repo_path = os.path.join(local_storage, name)

    if os.path.exists(repo_path) and os.path.exists(os.path.join(repo_path, '.git')):
        git.reset(hard= True, _cwd= repo_path)
        git.pull(_cwd= repo_path)
    else:
        if os.path.exists(repo_path):
            shutil.rmtree(repo_path)
        git.clone(url, name, _cwd= local_storage)
        git.config('core.fileMode', 'false', _cwd= repo_path)
        git.config('core.sharedRepository', 'true', _cwd= repo_path)
Exemple #10
0
def main():
    options = json.load(sys.stdin)

    github_remote_url = f"{get_github_url()}/{options['target_remote']}.git"
    if len(list(pathlib.Path('.').iterdir())) > 0:
        print("Found existing files in work directory", file=sys.stderr)
        assert pathlib.Path('.git').exists(
        ), "if files are present in the work dir, it must be a git work tree"
        remote = "origin"
        if options["source_remote_name"] == remote:
            remote = remote + "2"
        add_or_set_git_remote(remote, github_remote_url)
        print(f"Fetching from {github_remote_url}", file=sys.stderr)
        git.fetch(remote)
        print(
            f"Checking out {options['target_branch']} from {remote}/{options['target_branch']}",
            file=sys.stderr)
        git.checkout("-B", options['target_branch'],
                     f"{remote}/{options['target_branch']}")
        print(f"Cleaning work tree", file=sys.stderr)
        git.reset("--hard", "HEAD")
        git.clean("-fdx")
    else:
        print(f"Cloning {options['target_branch']} from {github_remote_url}",
              file=sys.stderr)
        git.clone("--branch", options['target_branch'], github_remote_url, ".")

    if options['target_remote'] != options['source_remote']:
        source_remote_name = options['source_remote_name']
        add_or_set_git_remote(
            source_remote_name,
            f"{get_github_url()}/{options['source_remote']}.git")
        print(
            f"Fetching from {get_github_url()}/{options['source_remote']}.git",
            file=sys.stderr)
        git.fetch(source_remote_name)

    set_git_author_info(f"GitHub Action {os.environ['GITHUB_ACTION']}",
                        "action@localhost")

    try:
        git("cherry-pick", options['source_commits'])
        print(
            f"Source commits ({options['source_commits']}) were successfully cherry-picked "
            f"onto {options['target_remote']}:{options['target_branch']}",
            file=sys.stderr)
    except sh.ErrorReturnCode:
        print(
            f"Source commits ({options['source_commits']}) could not be cherry-picked "
            f"onto {options['target_remote']}:{options['target_branch']}",
            file=sys.stderr)
        raise
            def need_update(path: str) -> bool:
                """
                Check if the file is different in the i18n repo.
                """

                exists = os.path.exists(join(args.root, path))
                k = {"_cwd": args.root}
                git.checkout(args.target, "--", path, **k)
                git.reset("HEAD", "--", path, **k)
                if not exists:
                    return True

                return self.file_is_modified(join(args.root, path))
Exemple #12
0
def main(all_files=False):
    git.stash(u=True, keep_index=True)

    try:
        results = nosetests('cardscript/', with_doctest=True)
        result = True
    except sh.ErrorReturnCode as exc:
        # If anything goes wrong, let us know, but abandon the commit.
        git.reset(hard=True)  # Code is wrong, reset it.
        print("ERROR: Tests FAILED; Commit ABORTED.")
        print(exc.stderr)
        result = False
    finally:
        git.stash('pop', q=True)  # Restore anything in our working directory.
    return result
Exemple #13
0
def main(all_files=False):
    git.stash(u=True, keep_index=True)

    try:
        results = nosetests('cardscript/', with_doctest=True)
        result = True
    except sh.ErrorReturnCode as exc:
        # If anything goes wrong, let us know, but abandon the commit.
        git.reset(hard=True) # Code is wrong, reset it.
        print("ERROR: Tests FAILED; Commit ABORTED.")
        print(exc.stderr)
        result = False
    finally:
        git.stash('pop', q=True) # Restore anything in our working directory.
    return result
Exemple #14
0
      def _add_and_commit_site(self, path):
            git_wt = git.bake(work_tree = path)


            git_wt.add('.',ignore_removal=True)
            status = git.st('-s', '-uno', '--porcelain')

            if status:
                  try:
                        message = "Published {0} to Github.".format(self.target_branch)
                        git_wt.commit(m=message)
                  except sh.ErrorReturnCode_1:
                        print("Can't commit.")
            else:
                  print('no changes to publish')

            git.reset(hard=True)
Exemple #15
0
def merge_and_check(base, head):
    """Merge <head> into <base>, then run some tests.

    Only modifies the working tree---doesn't actually create a merge
    commit. Resets and cleans the repo and leaves it in headless mode.

    Raises sh.ErrorReturnCode if the merge or the tests fail.
    """
    # Make sure we're up to date
    git.fetch()
    # Make sure we can do a clean checkout
    git.reset(hard=True)
    git.clean('-dfx')
    git.checkout('origin/' + base)
    # Merge the working tree, but don't modify the index
    git.merge('origin/' + head, no_commit=True)
    # Check the PR!
    check()
Exemple #16
0
def merge_and_check(base, head):
    """Merge <head> into <base>, then run some tests.

    Only modifies the working tree---doesn't actually create a merge
    commit. Resets and cleans the repo and leaves it in headless mode.

    Raises sh.ErrorReturnCode if the merge or the tests fail.
    """
    # Make sure we're up to date
    git.fetch()
    # Make sure we can do a clean checkout
    git.reset(hard=True)
    git.clean('-dfx')
    git.checkout('origin/' + base)
    # Merge the working tree, but don't modify the index
    git.merge('origin/' + head, no_commit=True)
    # Check the PR!
    check()
Exemple #17
0
def sync(path, git_reference):
    new_env = resetEnv()
    logger.debug('Syncing {}'.format(path))
    old_path = os.getcwd()
    jobid = get_current_job().id
    _open_console(jobid)

    try:
        os.chdir(path)

        _log_console(jobid, 'Syncing project with Git.\n')
        _l = lambda line: _log_console(jobid, str(line))
        git.fetch(_out=_l, _err=_l, _env=new_env).wait()
        git.reset(
            '--hard',
            'origin/{}'.format(git_reference),
            _out=_l,
            _err=_l,
            _env=new_env).wait()
        git.submodule(
            'sync',
            _out=_l,
            _err=_l,
            _env=new_env).wait()
        git.submodule(
            'update',
            _out=_l,
            _err=_l,
            _env=new_env).wait()

    except:
        logger.error(
            'Failed to sync project at {}'.format(path),
            exc_info=True
        )

    _close_console(jobid)

    os.chdir(old_path)
Exemple #18
0
def switch(target_branch):
    mark_message = '***mark***'
    try:
        res = git.status()
    except ErrorReturnCode:
        print(get_stderr(res))
        print('失败!该文件夹没有初始化')
        exit(-1)
    is_clean = re.match(".*nothing to commit, working tree clean.*", get_stdout(res), re.S)
    if not is_clean:
        print('工作区不干净,mark提交...')
        git.add('--all')
        git.commit('-a', '-m', mark_message)
    git.switch(target_branch)

    code, output = subprocess.getstatusoutput('git log -1 --pretty=format:"%s" ' + target_branch)
    if code == 0:
        if output == mark_message:
            git.reset('HEAD~')
    else:
        print("查看commit message出错!")
        exit(-1)
    print('切换到%s' % target_branch)
Exemple #19
0
 def rollback(self, paths, msg="Rolling-Back"):
     """
         Rollback last commit to restore previous.
         configuration and commit changes automatically
     """
     for path in paths:
         global git
         git = git.bake("--git-dir={0}/.git".format(path),
                        "--work-tree={0}".format(path))
         if os.path.isdir(path):
             if not os.path.isdir(path + "/.git"):
                 Log.error(
                     self,
                     "Unable to find a git repository at {0}".format(path))
             try:
                 Log.debug(self,
                           "WOGit: git reset HEAD~ at {0}".format(path))
                 git.reset("HEAD~", "--hard")
             except ErrorReturnCode as e:
                 Log.debug(self, "{0}".format(e))
                 Log.error(self, "Unable to git reset at {0} ".format(path))
         else:
             Log.debug(self, "WOGit: Path {0} not present".format(path))
def main():
    github_remote_url = f"{get_github_url()}/{get_input('target_remote')}.git"
    work_dir = pathlib.Path(get_input("work_dir"))
    if work_dir.is_dir() and len(list(work_dir.iterdir())) > 0:
        os.chdir(work_dir)
        remote = "origin"
        if get_input("source_remote_name") == remote:
            remote = remote + "2"
        add_or_set_git_remote(remote, github_remote_url)
        git.fetch(remote)
        git.checkout("-B", get_input("target_branch"),
                     f"{remote}/{get_input('target_branch')}")
        git.reset("--hard", "HEAD")
        git.clean("-fdx")
    else:
        git.clone("--branch", get_input("target_branch"), github_remote_url,
                  str(work_dir))
        os.chdir(work_dir)

    if get_input("target_remote") != get_input("source_remote"):
        source_remote_name = get_input("source_remote_name")
        add_or_set_git_remote(
            source_remote_name,
            f"{get_github_url()}/{get_input('source_remote')}.git")
        git.fetch(source_remote_name)

    set_git_author_info(f"GitHub Action {os.environ['GITHUB_ACTION']}",
                        "action@localhost")

    try:
        git("cherry-pick", get_input("source_commits"))
        print("Source commits were cherry-picked successfully",
              file=sys.stderr)
    except sh.ErrorReturnCode:
        print("Source commits could not be cherry-picked", file=sys.stderr)
        raise
Exemple #21
0
def reset():
    try:
        git.reset('--hard', 'refs/original/refs/heads/master')

    except ErrorReturnCode_128 as ex:
        raise RebundantResetException from ex
Exemple #22
0
    def add_to_blacklist(**kwargs):
        blacklist = kwargs.get("blacklist", "")
        item_to_blacklist = kwargs.get("item_to_blacklist", "")
        username = kwargs.get("username", "")
        chat_profile_link = kwargs.get("chat_profile_link", "http://chat.stackexchange.com/users")
        code_permissions = kwargs.get("code_permissions", False)

        # Make sure git credentials are set up
        if git.config("--global", "--get", "user.name") == "":
            return (False, "Tell someone to run `git config --global user.name \"SmokeDetector\"`")

        if git.config("--global", "--get", "user.name") == "":
            return (False, "Tell someone to run `git config --global user.email \"[email protected]\"`")

        if blacklist == "":
            # If we broke the code, and this isn't assigned, error out before doing anything, but do
            # so gracefully with a nice error message.
            return (False, "Programming Error - Critical information missing for GitManager: blacklist")

        if item_to_blacklist == "":
            # If we broke the code, and this isn't assigned, error out before doing anything, but do
            # so gracefully with a nice error message.
            return (False, "Programming Error - Critical information missing for GitManager: item_to_blacklist")

        item_to_blacklist = item_to_blacklist.replace("\s", " ")

        if blacklist == "website":
            blacklist_file_name = "blacklisted_websites.txt"
            ms_search_option = "&body_is_regex=1&body="
        elif blacklist == "keyword":
            blacklist_file_name = "bad_keywords.txt"
            ms_search_option = "&body_is_regex=1&body="
        elif blacklist == "username":
            blacklist_file_name = "blacklisted_usernames.txt"
            ms_search_option = "&username_is_regex=1&username="******"Invalid blacklist type specified, something has broken badly!")

        git.checkout("master")
        try:
            git.pull()
        except:
            pass

        # Check that we're up-to-date with origin (GitHub)
        git.remote.update()
        if git("rev-parse", "refs/remotes/origin/master").strip() != git("rev-parse", "master").strip():
            return (False, "HEAD isn't at tip of origin's master branch")

        # Check that blacklisted_websites.txt isn't modified locally. That could get ugly fast
        if blacklist_file_name in git.status():  # Also ugly
            return (False, "{0} is modified locally. This is probably bad.".format(blacklist_file_name))

        # Add item to file
        with open(blacklist_file_name, "a+") as blacklist_file:
            last_character = blacklist_file.read()[-1:]
            if last_character != "\n":
                blacklist_file.write("\n")
            blacklist_file.write(item_to_blacklist + "\n")

        # Checkout a new branch (for PRs for non-code-privileged people)
        branch = "auto-blacklist-{0}".format(str(time.time()))
        git.checkout("-b", branch)

        # Clear HEAD just in case
        git.reset("HEAD")

        git.add(blacklist_file_name)
        git.commit("-m", u"Auto blacklist of {0} by {1} --autopull".format(item_to_blacklist, username))

        if code_permissions:
            git.checkout("master")
            git.merge(branch)
            git.push()
        else:
            git.push("origin", branch)
            git.checkout("master")

            if GlobalVars.github_username is None or GlobalVars.github_password is None:
                return (False, "Tell someone to set a GH password")

            payload = {"title": u"{0}: Blacklist {1}".format(username, item_to_blacklist),
                       "body": u"[{0}]({1}) requests the blacklist of the {2} {3}. See the Metasmoke search [here]"
                               "(https://metasmoke.erwaysoftware.com/search?utf8=%E2%9C%93{4}{5})\n"
                               u"<!-- METASMOKE-BLACKLIST-{6} {3} -->".format(username, chat_profile_link, blacklist,
                                                                              item_to_blacklist, ms_search_option,
                                                                              item_to_blacklist.replace(" ", "+"),
                                                                              blacklist.upper()),
                       "head": branch,
                       "base": "master"}
            response = requests.post("https://api.github.com/repos/Charcoal-SE/SmokeDetector/pulls",
                                     auth=HTTPBasicAuth(GlobalVars.github_username, GlobalVars.github_password),
                                     data=json.dumps(payload))
            print(response.json())
            try:
                git.checkout("deploy")  # Return to deploy, pending the accept of the PR in Master.
                return (True, "You don't have code privileges, but I've [created a pull request for you]({0}).".format(
                    response.json()["html_url"]))
            except KeyError:
                # Error capture/checking for any "invalid" GH reply without an 'html_url' item,
                # which will throw a KeyError.
                if "bad credentials" in str(response.json()['message']).lower():
                    # Capture the case when GH credentials are bad or invalid
                    return (False, "Something is wrong with the GH credentials, tell someone to check them.")
                else:
                    # Capture any other invalid response cases.
                    return (False, "A bad or invalid reply was received from GH, the message was: %s" %
                            response.json()['message'])

        git.checkout("deploy")  # Return to deploy to await CI.

        return (True, "Blacklisted {0}".format(item_to_blacklist))
Exemple #23
0
    def add_to_blacklist(**kwargs):
        if 'windows' in str(platform.platform()).lower():
            log('warning', "Git support not available in Windows.")
            return (False, "Git support not available in Windows.")

        blacklist = kwargs.get("blacklist", "")
        item_to_blacklist = kwargs.get("item_to_blacklist", "")
        username = kwargs.get("username", "")
        chat_profile_link = kwargs.get("chat_profile_link", "http://chat.stackexchange.com/users")
        code_permissions = kwargs.get("code_permissions", False)

        # Make sure git credentials are set up
        if git.config("--global", "--get", "user.name", _ok_code=[0, 1]) == "":
            return (False, "Tell someone to run `git config --global user.name \"SmokeDetector\"`")

        if git.config("--global", "--get", "user.email", _ok_code=[0, 1]) == "":
            return (False, "Tell someone to run `git config --global user.email \"[email protected]\"`")

        if blacklist == "":
            # If we broke the code, and this isn't assigned, error out before doing anything, but do
            # so gracefully with a nice error message.
            return (False, "Programming Error - Critical information missing for GitManager: blacklist")

        if item_to_blacklist == "":
            # If we broke the code, and this isn't assigned, error out before doing anything, but do
            # so gracefully with a nice error message.
            return (False, "Programming Error - Critical information missing for GitManager: item_to_blacklist")

        item_to_blacklist = item_to_blacklist.replace("\s", " ")

        if blacklist == "website":
            blacklist_file_name = "blacklisted_websites.txt"
            ms_search_option = "&body_is_regex=1&body="
        elif blacklist == "keyword":
            blacklist_file_name = "bad_keywords.txt"
            ms_search_option = "&body_is_regex=1&body="
        elif blacklist == "username":
            blacklist_file_name = "blacklisted_usernames.txt"
            ms_search_option = "&username_is_regex=1&username="******"Invalid blacklist type specified, something has broken badly!")

        git.checkout("master")
        try:
            git.pull()
        except:
            pass

        # Check that we're up-to-date with origin (GitHub)
        git.remote.update()
        if git("rev-parse", "refs/remotes/origin/master").strip() != git("rev-parse", "master").strip():
            return (False, "HEAD isn't at tip of origin's master branch")

        # Check that blacklisted_websites.txt isn't modified locally. That could get ugly fast
        if blacklist_file_name in git.status():  # Also ugly
            return (False, "{0} is modified locally. This is probably bad.".format(blacklist_file_name))

        # Add item to file
        with open(blacklist_file_name, "a+") as blacklist_file:
            last_character = blacklist_file.read()[-1:]
            if last_character != "\n":
                blacklist_file.write("\n")
            blacklist_file.write(item_to_blacklist + "\n")

        # Checkout a new branch (for PRs for non-code-privileged people)
        branch = "auto-blacklist-{0}".format(str(time.time()))
        git.checkout("-b", branch)

        # Clear HEAD just in case
        git.reset("HEAD")

        git.add(blacklist_file_name)
        git.commit("--author='SmokeDetector <*****@*****.**>'",
                   "-m", u"Auto blacklist of {0} by {1} --autopull".format(item_to_blacklist, username))

        if code_permissions:
            git.checkout("master")
            git.merge(branch)
            git.push("origin", "master")
            git.branch('-D', branch)  # Delete the branch in the local git tree since we're done with it.
        else:
            git.push("origin", branch)
            git.checkout("master")

            if GlobalVars.github_username is None or GlobalVars.github_password is None:
                return (False, "Tell someone to set a GH password")

            payload = {"title": u"{0}: Blacklist {1}".format(username, item_to_blacklist),
                       "body": u"[{0}]({1}) requests the blacklist of the {2} {3}. See the Metasmoke search [here]"
                               "(https://metasmoke.erwaysoftware.com/search?utf8=%E2%9C%93{4}{5})\n"
                               u"<!-- METASMOKE-BLACKLIST-{6} {3} -->".format(username, chat_profile_link, blacklist,
                                                                              item_to_blacklist, ms_search_option,
                                                                              item_to_blacklist.replace(" ", "+"),
                                                                              blacklist.upper()),
                       "head": branch,
                       "base": "master"}
            response = requests.post("https://api.github.com/repos/Charcoal-SE/SmokeDetector/pulls",
                                     auth=HTTPBasicAuth(GlobalVars.github_username, GlobalVars.github_password),
                                     data=json.dumps(payload))
            log('debug', response.json())
            try:
                git.checkout("deploy")  # Return to deploy, pending the accept of the PR in Master.
                git.branch('-D', branch)  # Delete the branch in the local git tree since we're done with it.
                return (True, "You don't have code privileges, but I've [created a pull request for you]({0}).".format(
                    response.json()["html_url"]))
            except KeyError:
                git.checkout("deploy")  # Return to deploy

                # Delete the branch in the local git tree, we'll create it again if the
                # command is run again. This way, we keep things a little more clean in
                # the local git tree
                git.branch('-D', branch)

                # Error capture/checking for any "invalid" GH reply without an 'html_url' item,
                # which will throw a KeyError.
                if "bad credentials" in str(response.json()['message']).lower():
                    # Capture the case when GH credentials are bad or invalid
                    return (False, "Something is wrong with the GH credentials, tell someone to check them.")
                else:
                    # Capture any other invalid response cases.
                    return (False, "A bad or invalid reply was received from GH, the message was: %s" %
                            response.json()['message'])

        git.checkout("deploy")  # Return to deploy to await CI.

        return (True, "Blacklisted {0}".format(item_to_blacklist))
Exemple #24
0
import os
from datetime import datetime
from sh import git

ips = []
__folder__ = os.path.split(__file__)[0]
if __folder__:
    os.chdir(__folder__)
regex = re.compile(r"\/(.+)\/")
l = requests.get(
    "https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf"
).text

print("load changes")
git.pull("--all")
git.reset("origin/master", "--hard")
git.pull()

print("Finish download")
for i in l.split("\n")[1:]:
    if i and regex.search(i):
        ips.append(regex.search(i).groups()[0])

with open("rawlist.txt", "w") as fp:
    for i in ips:
        fp.write(i + "\n")

with open("list.txt", 'w') as fp:
    # s = "[AutoProxy 0.2.9]\n"
    for i in ips:
        fp.write("||{}\n".format(i))
import re
import base64
import os
from datetime import datetime
from sh import git

ips = []
__folder__ = os.path.split(__file__)[0]
if __folder__:
    os.chdir(__folder__)
regex = re.compile(r"\/(.+)\/")
l = requests.get("https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf").text

print("load changes")
git.pull("--all")
git.reset("origin/master", "--hard")
git.pull()

print("Finish download")
for i in l.split("\n")[1:]:
    if i and regex.search(i):
        ips.append(regex.search(i).groups()[0])


with open("rawlist.txt", "w") as fp:
    for i in ips:
        fp.write(i+"\n")

with open("list.txt", 'w') as fp:
    # s = "[AutoProxy 0.2.9]\n"
    for i in ips:
Exemple #26
0
 def cleanup(self):
     if self.device.group.repository.lock.acquired:
         os.chdir(self.device.group.repository.path)
         git.reset('--hard')
Exemple #27
0
    def add_to_blacklist(self, **kwargs):
        blacklist = kwargs.get("blacklist", "website")
        items_to_blacklist = kwargs.get("items_to_blacklist", [])
        username = kwargs.get("username", "")
        chat_profile_link = kwargs.get("chat_profile_link",
                                       "http://chat.stackexchange.com/users")
        code_permissions = kwargs.get("code_permissions", False)

        if blacklist == "website":
            blacklist_file_name = "blacklisted_websites.txt"
        elif blacklist == "keyword":
            blacklist_file_name = "bad_keywords.txt"
        elif blacklist == "username":
            blacklist_file_name = "blacklisted_usernames.txt"

        # Check if we're on master
        if git("rev-parse", "--abbrev-ref", "HEAD").strip() != "master":
            return (False, "Not currently on master.")

        # Check that we're up-to-date with origin (GitHub)
        git.remote.update()
        if git("rev-parse", "refs/remotes/origin/master").strip() != git(
                "rev-parse", "master").strip():
            return (False, "HEAD isn't at tip of origin's master branch")

        # Check that blacklisted_websites.txt isn't modified locally. That could get ugly fast
        if blacklist_file_name in git.status():  # Also ugly
            return (False,
                    "{0} modified locally. This is probably bad.".format(
                        blacklist_file_name))

        # Store current commit hash
        current_commit = git("rev-parse", "HEAD").strip()

        # Add items to file
        with open(blacklist_file_name, "a+") as blacklist_file:
            last_character = blacklist_file.read()[-1:]
            if last_character != "\n":
                blacklist_file.write("\n")
            blacklist_file.write("\n".join(items_to_blacklist) + "\n")

        # Checkout a new branch (for PRs for non-code-privileged people)
        branch = "auto-blacklist-{0}".format(str(time.time()))
        git.checkout("-b", branch)

        # Clear HEAD just in case
        git.reset("HEAD")

        git.add(blacklist_file_name)
        git.commit(
            "-m", u"Auto blacklist of {0} by {1} --autopull".format(
                ", ".join(items_to_blacklist), username))

        if code_permissions:
            git.checkout("master")
            git.merge(branch)
            git.push()
        else:
            git.push("origin", branch)
            git.checkout("master")

            if GlobalVars.github_username is None or GlobalVars.github_password is None:
                return (False, "Tell someone to set a GH password")

            list_of_domains = ""

            for domain in range(len(items_to_blacklist)):
                list_of_domains += "\n - {0} - [MS search](https://metasmoke.erwaysoftware.com/search?utf8=%E2%9C%93&body_is_regex=1&body={0})".format(
                    items_to_blacklist[domain])

            payload = {
                "title":
                "{0}: Blacklist {1}".format(username,
                                            ", ".join(items_to_blacklist)),
                "body":
                "[{0}]({1}) requests the blacklist of the following {2}(s): \n{3}\n<!-- METASMOKE-BLACKLIST {4} -->"
                .format(username, chat_profile_link, blacklist,
                        list_of_domains, "|".join(items_to_blacklist)),
                "head":
                branch,
                "base":
                "master"
            }
            response = requests.post(
                "https://api.github.com/repos/Charcoal-SE/SmokeDetector/pulls",
                auth=HTTPBasicAuth(GlobalVars.github_username,
                                   GlobalVars.github_password),
                data=json.dumps(payload))
            print(response.json())
            return (
                True,
                "You don't have code privileges, but I've [created a pull request for you]({0})."
                .format(response.json()["html_url"]))

        git.checkout(
            current_commit
        )  # Return to old commit to await CI. This will make Smokey think it's in reverted mode if it restarts

        return (
            True,
            "Blacklisted {0} - the entry will be applied via autopull if CI succeeds."
            .format(", ".join(items_to_blacklist)))