Skip to content

Commit

Permalink
Added grep util and force delete branch
Browse files Browse the repository at this point in the history
  • Loading branch information
merlijn-sebrechts committed May 29, 2020
1 parent 2df7f37 commit bf4d9e1
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 33 deletions.
2 changes: 2 additions & 0 deletions ghtt/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from .search import search
from .assignment import assignment
from .util import util


@click.group()
Expand All @@ -18,6 +19,7 @@ def cli(ctx):

cli.add_command(search)
cli.add_command(assignment)
cli.add_command(util)


if __name__ == "__main__":
Expand Down
47 changes: 20 additions & 27 deletions ghtt/assignment.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env python3
import subprocess

from functools import wraps
import os
import subprocess
from urllib.parse import urlparse

import click
import github3
Expand All @@ -20,10 +22,6 @@ def assignment(ctx):

@assignment.command()
@click.pass_context
@click.option(
'--organization', '-o',
help='Github organization where student repos are located',
required="True")
@click.option(
'--branch',
help='Name of the branch to create in students repos',
Expand All @@ -39,10 +37,12 @@ def assignment(ctx):
@click.option(
'--source', '-s',
help='Source directory')
def create_pr(ctx, organization, branch, title, body, source):
def create_pr(ctx, branch, title, body, source):
"""Pushes updated code to a new branch on students repositories and creates a pr to merge that
branch into master.
"""
organization = urlparse(ctx.obj['url']).path.rstrip("/").rsplit("/", 1)[-1]

click.secho("# Organization: '{}'".format(organization), fg="green")
click.secho("# Branch: '{}'".format(branch), fg="green")
click.secho("# title: '{}'".format(title), fg="green")
Expand Down Expand Up @@ -92,10 +92,6 @@ def generate_from_template(path, username, clone_url, repo_name):

@assignment.command()
@click.pass_context
@click.option(
'--organization', '-o',
help='Github organization where student repos are located',
required="True")
@click.option(
'--source',
help='path to repo with start code',
Expand All @@ -104,14 +100,16 @@ def generate_from_template(path, username, clone_url, repo_name):
'--students',
help='comma-separated list of students',
required="True")
def create_repos(ctx, organization, source, students):
"""Create student repositories in the specified organization.
def create_repos(ctx, source, students):
"""Create student repositories in the organization specified by the url.
Each repository will contain a copy of the specified source and will have force-pushing disabled
so students can not rewrite history.
Note: this command does not grant students access to those repositories. See `assignment grant`.
"""
organization = urlparse(ctx.obj['url']).path.rstrip("/").rsplit("/", 1)[-1]
students = students.split(",")

click.secho("# Creating student repositories..", fg="green")
click.secho("# Org: '{}'".format(organization), fg="green")
click.secho("# Path: '{}'".format(source), fg="green")
Expand All @@ -132,9 +130,8 @@ def create_repos(ctx, organization, source, students):
repo = g.repository(organization, reponame)

subprocess.check_call(["git", "checkout", "master"], cwd=source)
subprocess.call(["git", "checkout", "-b", student], cwd=source)
subprocess.check_call(["git", "checkout", student], cwd=source)
subprocess.check_call(["git", "merge", "--strategy-option", "theirs", "master"], cwd=source)
subprocess.call(["git", "branch", "-D", student], cwd=source)
subprocess.check_call(["git", "checkout", "-b", student], cwd=source)

if os.path.isfile("{}/README.md.jinja".format(source)):
generate_from_template(
Expand All @@ -158,18 +155,15 @@ def create_repos(ctx, organization, source, students):

@assignment.command()
@click.pass_context
@click.option(
'--organization', '-o',
help='Github organization where student repos are located',
required="True")
@click.option(
'--students',
help='list of students',
required="True")
def grant(ctx, organization, students):
"""Grant each student push access (the collaborator role) to their repository in the specified
organization.
def grant(ctx, students):
"""Grant each student push access (the collaborator role) to their repository in the
organization specified by the url.
"""
organization = urlparse(ctx.obj['url']).path.rstrip("/").rsplit("/", 1)[-1]
students = students.split(",")

click.secho("# Granting students write permission to their repository..", fg="green")
Expand All @@ -188,18 +182,17 @@ def grant(ctx, organization, students):

@assignment.command()
@click.pass_context
@click.option(
'--organization', '-o',
help='Github organization where student repos are located',
required="True")
@click.option(
'--students',
help='list of students',
required="True")
def remove_grant(ctx, organization, students):
def remove_grant(ctx, students):
"""Removes students' push access to their repository and cancels any open invitation for that
student.
"""
organization = urlparse(ctx.obj['url']).path.rstrip("/").rsplit("/", 1)[-1]
students = students.split(",")

click.secho("# Removing students write permission to their repository..", fg="green")
click.secho("# Org: '{}'".format(organization), fg="green")
click.secho("# Students: '{}'".format(students), fg="green")
Expand Down
14 changes: 8 additions & 6 deletions ghtt/auth.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
import subprocess
from functools import wraps
import subprocess
from urllib.parse import urlparse

import click
import github3
Expand All @@ -16,25 +17,25 @@ def authenticate(url, token):
else:
username = ""
password = ""

url = urlparse(url)

if not url.startswith("http"):
url = "https://" + url
if "github.com" in url:
if url.netloc == "github.com":
gh = github3.github.GitHub(
token=token,
username=username,
password=password)
else:
gh = github3.github.GitHubEnterprise(
url,
'https://{url.netloc}/'.format(url=url),
token=token,
username=username,
password=password)


# Protect using the pygithub library because it's not supported with github3.py
pyg = pygithub.Github(
base_url="{}/api/v3".format(url),
base_url="https://{url.netloc}/api/v3".format(url=url),
login_or_token=token)

return (gh, pyg)
Expand All @@ -55,5 +56,6 @@ def needs_auth(f):
@click.pass_context
def wrapper(ctx, *args, url=None, token=None, **kwargs):
(ctx.obj['gh'], ctx.obj['pyg']) = authenticate(url, token)
ctx.obj['url'] = url
return f(*args, **kwargs)
return wrapper
37 changes: 37 additions & 0 deletions ghtt/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python3
import subprocess
from functools import wraps
import os

import click
import github3
import requests
import jinja2
import github as pygithub

from .auth import needs_auth

@click.group()
def util():
pass

@util.command()
@click.argument("path", required="True")
@click.argument("strings", required="True")
def grep_in(path, strings):
"""Prints each line which contains one of the strings in the provided comma-separated list.
FILENAME: name of file to search
STRINGS: Comma-separated list of strings to search for
"""
strings = strings.split(",")

with open(path, "r") as f:
lines = f.readlines()

for line in lines:
for string in strings:
if string in line:
click.secho(line.strip())
break

0 comments on commit bf4d9e1

Please sign in to comment.