repobee_plug Module Reference

This is the internal API of repobee_plug, which is not stable.

_apimeta

Metaclass for API implementations.

APIMeta defines the behavior required of platform API implementations, based on the methods in APISpec. With platform API, we mean for example the GitHub REST API, and the GitLab REST API. The point is to introduce another layer of indirection such that higher levels of RepoBee can use different platforms in a platform-independent way. API is a convenience class so consumers don’t have to use the metaclass directly.

Any class implementing a platform API should derive from API. It will enforce that all public methods are one of the method defined py APISpec, and give a default implementation (that just raises NotImplementedError) for any unimplemented API methods.

class repobee_plug._apimeta.API(base_url, token, org_name, user)[source]

API base class that all API implementations should inherit from. This class functions similarly to an abstract base class, but with a few key distinctions that affect the inheriting class.

  1. Public methods must override one of the public methods of APISpec. If an inheriting class defines any other public method, an APIError is raised when the class is defined.
  2. All public methods in APISpec have a default implementation that simply raise a NotImplementedError. There is no requirement to implement any of them.
class repobee_plug._apimeta.APIMeta[source]

Metaclass for an API implementation. All public methods must be a specified api method, but all api methods do not need to be implemented.

class repobee_plug._apimeta.APIObject[source]

Base wrapper class for platform API objects.

class repobee_plug._apimeta.APISpec(base_url, token, org_name, user)[source]

Wrapper class for API method stubs.

Important

This class should not be inherited from directly, it serves only to document the behavior of a platform API. Classes that implement this behavior should inherit from API.

add_repos_to_review_teams(team_to_repos, issue=None)[source]

Add repos to review teams. For each repo, an issue is opened, and every user in the review team is assigned to it. If no issue is specified, sensible defaults for title and body are used.

Parameters:
  • team_to_repos (Mapping[str, Iterable[str]]) – A mapping from a team name to an iterable of repo names.
  • issue (Optional[Issue]) – An optional Issue tuple to override the default issue.
Return type:

None

close_issue(title_regex, repo_names)[source]

Close any issues in the given repos in the target organization, whose titles match the title_regex.

Parameters:
  • title_regex (str) – A regex to match against issue titles.
  • repo_names (Iterable[str]) – Names of repositories to close issues in.
Return type:

None

create_repos(repos)[source]

Create repos in the target organization according the those specced by the repos argument. Repos that already exist are skipped.

Parameters:repos (Iterable[Repo]) – Repos to be created.
Return type:List[str]
Returns:A list of urls to the repos specified by the repos argument, both those that were created and those that already existed.
delete_teams(team_names)[source]

Delete all teams in the target organizatoin that exactly match one of the provided team_names. Skip any team name for which no match is found.

Parameters:team_names (Iterable[str]) – A list of team names for teams to be deleted.
Return type:None
discover_repos(teams)[source]

Return all repositories related to the provided teams.

Parameters:teams (Iterable[Team]) – Team tuples.
Return type:Generator[Repo, None, None]
Returns:A list of Repo tuples.
ensure_teams_and_members(teams, permission=<TeamPermission.PUSH: 'push'>)[source]

Ensure that the teams exist, and that their members are added to the teams.

Teams that do not exist are created, teams that already exist are fetched. Members that are not in their teams are added, members that do not exist or are already in their teams are skipped.

Parameters:
  • teams (Iterable[Team]) – A list of teams specifying student groups.
  • permission (TeamPermission) – The permission these teams (or members of them) should be given in regards to associated repositories.
Return type:

List[Team]

Returns:

A list of Team API objects of the teams provided to the function, both those that were created and those that already existed.

extract_repo_name(repo_url)[source]

Extract a repo name from the provided url.

Parameters:repo_url (str) – A URL to a repository.
Return type:str
Returns:The name of the repository corresponding to the url.
get_issues(repo_names, state=<IssueState.OPEN: 'open'>, title_regex='')[source]

Get all issues for the repos in repo_names an return a generator that yields (repo_name, issue generator) tuples. Will by default only get open issues.

Parameters:
  • repo_names (Iterable[str]) – An iterable of repo names.
  • state (IssueState) – Specifies the state of the issue.
  • title_regex (str) – If specified, only issues matching this regex are
  • Defaults to the empty string (returned.) –
Return type:

Generator[Tuple[str, Generator[Issue, None, None]], None, None]

Returns:

A generator that yields (repo_name, issue_generator) tuples.

get_repo_urls(master_repo_names, org_name=None, teams=None)[source]

Get repo urls for all specified repo names in the organization. As checking if every single repo actually exists takes a long time with a typical REST API, this function does not in general guarantee that the urls returned actually correspond to existing repos.

If the org_name argument is supplied, urls are computed relative to that organization. If it is not supplied, the target organization is used.

If the teams argument is supplied, student repo urls are computed instead of master repo urls.

Parameters:
  • master_repo_names (Iterable[str]) – A list of master repository names.
  • org_name (Optional[str]) – Organization in which repos are expected. Defaults to the target organization of the API instance.
  • teams (Optional[List[Team]]) – A list of teams specifying student groups. Defaults to None.
Return type:

List[str]

Returns:

a list of urls corresponding to the repo names.

get_review_progress(review_team_names, teams, title_regex)[source]

Get the peer review progress for the specified review teams and student teams by checking which review team members have opened issues in their assigned repos. Only issues matching the title regex will be considered peer review issues. If a reviewer has opened an issue in the assigned repo with a title matching the regex, the review will be considered done.

Note that reviews only count if the student is in the review team for that repo. Review teams must only have one associated repo, or the repo is skipped.

Parameters:
  • review_team_names (Iterable[str]) – Names of review teams.
  • teams (Iterable[Team]) – Team API objects specifying student groups.
  • title_regex (str) – If an issue title matches this regex, the issue is considered a potential peer review issue.
Return type:

Mapping[str, List]

Returns:

a mapping (reviewer -> assigned_repos), where reviewer is a str and assigned_repos is a _repobee.tuples.Review.

get_teams()[source]

Get all teams related to the target organization.

Return type:List[Team]
Returns:A list of Team API object.
open_issue(title, body, repo_names)[source]

Open the specified issue in all repos with the given names, in the target organization.

Parameters:
  • title (str) – Title of the issue.
  • body (str) – Body of the issue.
  • repo_names (Iterable[str]) – Names of repos to open the issue in.
Return type:

None

static verify_settings(user, org_name, base_url, token, master_org_name=None)[source]

Verify the following (to the extent that is possible and makes sense for the specifi platform):

  1. Base url is correct
  2. The token has sufficient access privileges
  3. Target organization (specifiend by org_name) exists
    • If master_org_name is supplied, this is also checked to exist.
  4. User is owner in organization (verify by getting
    • If master_org_name is supplied, user is also checked to be an owner of it.

organization member list and checking roles)

Should raise an appropriate subclass of _repobee.exception.APIError when a problem is encountered.

Parameters:
  • user (str) – The username to try to fetch.
  • org_name (str) – Name of the target organization.
  • base_url (str) – A base url to a github API.
  • token (str) – A secure OAUTH2 token.
  • org_name – Name of the master organization.
Returns:

True if the connection is well formed.

Raises:

_repobee.exception.APIError

class repobee_plug._apimeta.Issue[source]

Wrapper class for an Issue API object.

static from_dict(asdict)[source]

Take a dictionary produced by Issue.to_dict and reconstruct the corresponding instance. The implementation field is lost in a to_dict -> from_dict roundtrip.

Return type:Issue
to_dict()[source]

Return a dictionary representation of this namedtuple, without the implementation field.

class repobee_plug._apimeta.IssueState[source]

Enum specifying a possible issue state.

class repobee_plug._apimeta.Repo[source]

Wrapper class for a Repo API object.

class repobee_plug._apimeta.Team[source]

Wrapper class for a Team API object.

class repobee_plug._apimeta.TeamPermission[source]

Enum specifying team permissions on creating teams. On GitHub, for example, this can be e.g. push or pull.

repobee_plug._apimeta.check_init_params(reference_params, compare_params)[source]

Check that the compare __init__’s parameters are a subset of the reference class’s version.

repobee_plug._apimeta.check_parameters(reference, compare)[source]

Check if the parameters match, one by one. Stop at the first diff and raise an exception for that parameter.

An exception is made for __init__, for which the compare may be a subset of the reference in no particular order.

repobee_plug._apimeta.methods(attrdict)[source]

Return all public methods and __init__ for some class.

repobee_plug._apimeta.parameters(function)[source]

Extract parameter names and default arguments from a function.

_pluginmeta

class repobee_plug._pluginmeta.Plugin[source]

This is a base class for plugin classes. For plugin classes to be picked up by RepoBee, they must inherit from this class.

Public methods must be hook methods. If there are any public methods that are not hook methods, an error is raised on creation of the class. As long as the method has the correct name, it will be recognized as a hook method during creation. However, if the signature is incorrect, the plugin framework will raise a runtime exception once it is called. Private methods (i.e. methods prefixed with _) carry no restrictions.

The signatures of hook methods are not checked until the plugin class is registered by the repobee_plug.manager (an instance of pluggy.manager.PluginManager). Therefore, when testing a plugin, it is a good idea to include a test where it is registered with the manager to ensure that it has the correct signatures.

A plugin class is instantiated exactly once; when RepoBee loads the plugin. This means that any state that is stored in the plugin will be carried throughout the execution of a RepoBee command. This makes plugin classes well suited for implementing tasks that require command line options or configuration values, as well as for implementing extension commands.

_containers

Container classes and enums.

class repobee_plug._containers.BaseParser[source]

Enumeration of base parsers that an extension command can request to have added to it.

BASE

Represents the base parser, which includes the --user, --org-name, --base-url and --token arguments.

STUDENTS

Represents the students parser, which includes the --students and –students-file` arguments.

REPO_NAMES

Represents the repo names parser, which includes the --master-repo-names argument.

REPO_DISCOVERY

Represents the repo discovery parser, which adds both the --master-repo-names and the --discover-repos arguments.

MASTER_ORG

Represents the master organization parser, which includes the --master-org argument.

class repobee_plug._containers.Deprecation
Parameters:
  • replacement (str) – The functionality that replaces the deprecated functionality.
  • remove_by_version (str) – A version number on the form MAJOR.MINOR.PATCH by which the deprecated functionality will be removed.

Create new instance of Deprecation(replacement, remove_by_version)

remove_by_version

Alias for field number 1

replacement

Alias for field number 0

class repobee_plug._containers.ExtensionCommand(parser, name, help, description, callback, requires_api=False, requires_base_parsers=None)[source]

Class defining an extension command for the RepoBee CLI.

Parameters:
  • parser (ExtensionParser) – The parser to use for the CLI.
  • name (str) – Name of the command.
  • help (str) – Text that will be displayed when running repobee -h
  • description (str) – Text that will be displayed when calling the -h option for this specific command. Should be elaborate in describing the usage of the command.
  • callback (Callable[[Namespace, Optional[API]], Optional[Mapping[str, Result]]]) – A callback function that is called if this command is used on the CLI. It is passed the parsed namespace and the platform API. It may optionally return a result mapping on the form (name: str -> List[Result]) that’s reported by RepoBee.
  • requires_api (bool) – If True, the base arguments required for the platform API are added as options to the extension command, and the platform API is then passed to the callback function. It is then important not to have clashing option names. If False, the base arguments are not added to the CLI, and None is passed in place of the API. If you include REPO_DISCOVERY in requires_base_parsers, then you must set this to True.
  • requires_base_parsers (Optional[Iterable[BaseParser]]) – A list of repobee_plug.BaseParser that decide which base parsers are added to this command. For example, setting requires_base_parsers = [BaseParser.STUDENTS] adds the --students and --students-file options to this extension command’s parser.
class repobee_plug._containers.ExtensionParser[source]

An ArgumentParser specialized for RepoBee extension commands.

repobee_plug._containers.HookResult(hook, status, msg, data=None)[source]

Backwards compat function.

Deprecated since version 0.12.0: Replaced by Result.

Return type:Result
class repobee_plug._containers.Result(name, status, msg, data=None)[source]

Container for storing results from hooks.

Parameters:
  • name (str) – Name to associate with this result. This is typically the name of the plugin that returns this result.
  • status (Status) – Status of the plugin execution.
  • msg (str) – A free-form result message.
  • data (Optional[Mapping[Any, Any]]) – Semi-structured data in the form of a dictionary. All of the contents of the dictionary should be serializable as this is primarily used for JSON storage.
class repobee_plug._containers.Review
Parameters:
  • repo (Repo) – The reviewed repository.
  • done (bool) – Whether or not the review is done.

Create new instance of Review(repo, done)

done

Alias for field number 1

repo

Alias for field number 0

class repobee_plug._containers.ReviewAllocation
Parameters:
  • review_team (Team) – The team of reviewers.
  • reviewed_team (Team) – The team that is to be reviewed.

Create new instance of ReviewAllocation(review_team, reviewed_team)

review_team

Alias for field number 0

reviewed_team

Alias for field number 1

class repobee_plug._containers.Status[source]

Status codes enums for Results.

SUCCESS

Signifies a plugin execution without any complications.

WARNING

Signifies a plugin execution with non-critical failures.

ERROR

Signifies a critical error during execution.

_corehooks

Hookspecs for repobee core hooks.

Core hooks provide the basic functionality of repobee. These hooks all have default implementations, but are overridden by any other implementation. All hooks in this module should have the firstresult=True option to the hookspec to allow for this dynamic override.

class repobee_plug._corehooks.APIHook[source]

Hooks related to platform APIs.

api_init_requires()[source]

Return which of the arguments to apimeta.APISpec.__init__ that the given API requires. For example, the GitHubAPI requires all, but the GitLabAPI does not require user.

Return type:Tuple[str]
Returns:Names of the required arguments.
get_api_class()[source]

Return an API platform class. Must be a subclass of apimeta.API.

Returns:An apimeta.API subclass.
class repobee_plug._corehooks.PeerReviewHook[source]

Hook functions related to allocating peer reviews.

generate_review_allocations(teams, num_reviews)[source]

Generate ReviewAllocation tuples from the provided teams, given that this concerns reviews for a single master repo.

The provided teams of students should be treated as units. That is to say, if there are multiple members in a team, they should always be assigned to the same review team. The best way to merge two teams team_a and team_b into one review team is to simply do:

team_c = apimeta.Team(members=team_a.members + team_b.members)

This can be scaled to however many teams you would like to merge. As a practical example, if teams team_a and team_b are to review team_c, then the following ReviewAllocation tuple, here called allocation, should be contained in the returned list.

review_team = apimeta.Team(members=team_a.members + team_b.members)
allocation = containers.ReviewAllocation(
    review_team=review_team,
    reviewed_team=team_c,
)

Note

Respecting the num_reviews argument is optional: only do it if it makes sense. It’s good practice to issue a warning if num_reviews is ignored, however.

Parameters:
  • team – A list of Team tuples.
  • num_reviews (int) – Amount of reviews each student should perform (and consequently amount of reviewers per repo)
Return type:

List[ReviewAllocation]

Returns:

A list of ReviewAllocation

tuples.

_exthooks

Hookspecs for repobee extension hooks.

Extension hooks add something to the functionality of repobee, but are not necessary for its operation. Currently, all extension hooks are related to cloning repos.

class repobee_plug._exthooks.CloneHook[source]

Hook functions related to cloning repos.

act_on_cloned_repo(path, api)[source]

Do something with a cloned repo.

Deprecated since version 0.12.0: This hook is has been replaced by TaskHooks.clone_task(). Once all known, existing plugins have been migrated to the new hook, this hook will be removed.

Parameters:
  • path (Union[str, Path]) – Path to the repo.
  • api (API) – An instance of repobee.github_api.GitHubAPI.
Return type:

Optional[Result]

Returns:

optionally returns a Result namedtuple for reporting the outcome of the hook. May also return None, in which case no reporting will be performed for the hook.

clone_parser_hook(clone_parser)[source]

Do something with the clone repos subparser before it is used used to parse CLI options. The typical task is to add options to it.

Deprecated since version 0.12.0: This hook is has been replaced by TaskHooks.clone_task(). Once all known, existing plugins have been migrated to the new hook, this hook will be removed.

Parameters:clone_parser (ArgumentParser) – The clone subparser.
Return type:None
config_hook(config_parser)[source]

Hook into the config file parsing.

Parameters:config – the config parser after config has been read.
Return type:None
parse_args(args)[source]

Get the raw args from the parser. Only called for the clone parser. The typical task is to fetch any values from options added in clone_parser_hook().

Parameters:args (Namespace) – The full namespace returned by argparse.ArgumentParser.parse_args()
Return type:None
class repobee_plug._exthooks.ExtensionCommandHook[source]

Hooks related to extension commands.

create_extension_command()[source]

Create an extension command to add to the RepoBee CLI. The command will be added as one of the top-level subcommands of RepoBee. This hook is called precisely once, and should return an ExtensionCommand.

def command(args: argparse.Namespace, api: apimeta.API)

The command function will be called if the extension command is used on the command line.

Note that the RepoBeeExtensionParser class is just a thin wrapper around argparse.ArgumentParser, and can be used in an identical manner. The following is an example definition of this hook that adds a subcommand called example-command, that can be called with repobee example-command.

import repobee_plug as plug

def callback(args: argparse.Namespace, api: plug.API) -> None:
    LOGGER.info("callback called with: {}, {}".format(args, api))

@plug.repobee_hook
def create_extension_command():
    parser = plug.RepoBeeExtensionParser()
    parser.add_argument("-b", "--bb", help="A useless argument")
    return plug.ExtensionCommand(
        parser=parser,
        name="example-command",
        help="An example command",
        description="Description of an example command",
        callback=callback,
    )

Important

If you need to use the api, you set requires_api=True in the ExtensionCommand. This will automatically add the options that the API requires to the CLI options of the subcommand, and initialize the api and pass it in.

See the documentation for ExtensionCommand for more details on it.

Return type:ExtensionCommand
Returns:A ExtensionCommand.
class repobee_plug._exthooks.TaskHooks[source]

Hook functions relating to RepoBee tasks.

clone_task()[source]

Create a task to run on a copy of a cloned student repo. This hook replaces the old act_on_cloned_repo hook.

Implementations of this hook should return a Task, which defines a callback that is called after all student repos have been cloned. See the definition of Task for details.

Return type:Task
Returns:A Task instance defining a RepoBee task.
setup_task()[source]

Create a task to run on a copy of the master repo before it is pushed out to student repositories. This can for example be pre-flight checks of the master repo, or something else entirely.

Implementations of this hook should return a Task, which defines a callback that is called after the master repo has been safely copied, but before that copy is pushed out to student repositories. Note that any changes to the repository must be committed to actually show up in the student repositories.

Note

Structural changes to the master repo are not currently supported. Changes to the repository during the callback will not be reflected in the generated repositories. Support for preprocessing (such that changes do take effect) is a potential future feature.

Return type:Task

_exceptions

Exceptions for repobee_plug.

exception repobee_plug._exceptions.APIImplementationError(*args, **kwargs)[source]

Raise when an API is defined incorrectly.

Instantiate a PlugError.

Parameters:
  • args – List of positionals. These are passed directly to Exception. Typically, you should only pass an error message here.
  • kwargs – Keyword arguments to indicate what went wrong. For example, if the argument a caused the error, then you should pass a=a as a kwarg so it can be introspected at a later time.
exception repobee_plug._exceptions.ExtensionCommandError(*args, **kwargs)[source]

Raise when an :py:class:~repobee_plug.containers.ExtensionCommand: is incorrectly defined.

Instantiate a PlugError.

Parameters:
  • args – List of positionals. These are passed directly to Exception. Typically, you should only pass an error message here.
  • kwargs – Keyword arguments to indicate what went wrong. For example, if the argument a caused the error, then you should pass a=a as a kwarg so it can be introspected at a later time.
exception repobee_plug._exceptions.HookNameError(*args, **kwargs)[source]

Raise when a public method in a class that inherits from Plugin does not have a hook name.

Instantiate a PlugError.

Parameters:
  • args – List of positionals. These are passed directly to Exception. Typically, you should only pass an error message here.
  • kwargs – Keyword arguments to indicate what went wrong. For example, if the argument a caused the error, then you should pass a=a as a kwarg so it can be introspected at a later time.
exception repobee_plug._exceptions.PlugError(*args, **kwargs)[source]

Base class for all repobee_plug exceptions.

Instantiate a PlugError.

Parameters:
  • args – List of positionals. These are passed directly to Exception. Typically, you should only pass an error message here.
  • kwargs – Keyword arguments to indicate what went wrong. For example, if the argument a caused the error, then you should pass a=a as a kwarg so it can be introspected at a later time.

_name

Utility functions relating to RepoBee’s naming conventions.

repobee_plug._name.generate_repo_name(team_name, master_repo_name)[source]

Construct a repo name for a team.

Parameters:
  • team_name (str) – Name of the associated team.
  • master_repo_name (str) – Name of the template repository.
Return type:

str

repobee_plug._name.generate_repo_names(team_names, master_repo_names)[source]

Construct all combinations of generate_repo_name(team_name, master_repo_name) for the provided team names and master repo names.

Parameters:
  • team_names (Iterable[str]) – One or more names of teams.
  • master_repo_names (Iterable[str]) – One or more names of master repositories.
Return type:

Iterable[str]

Returns:

a list of repo names for all combinations of team and master repo.

repobee_plug._name.generate_review_team_name(student, master_repo_name)[source]

Generate a review team name.

Parameters:
  • student (str) – A student username.
  • master_repo_name (str) – Name of a master repository.
Return type:

str

Returns:

a review team name for the student repo associated with this master repo and student.

_serialize

JSON serialization/deserialization functions.

repobee_plug._serialize.json_to_result_mapping(json_string)[source]

Deserialize a JSON string to a mapping repo_name: str -> hook_results: List[Result]

Return type:Mapping[str, List[Result]]
repobee_plug._serialize.result_mapping_to_json(result_mapping)[source]

Serialize a result mapping repo_name: str -> hook_results: List[Result] to JSON.

Return type:str

_tasks

Task data structure and related functionality.

class repobee_plug._tasks.Task(act, add_option=None, handle_args=None, persist_changes=False)[source]

A data structure for describing a task. Tasks are operations that plugins can define to run on for example cloned student repos (a clone task) or on master repos before setting up student repos (a setup task). Only the act attribute is required, all other attributes can be omitted.

The callback methods should have the following headers.

def act(
    path: pathlib.Path, api: repobee_plug.API
) -> Optional[containers.Result]:

def add_option(parser: argparse.ArgumentParser) -> None:

def handle_args(args: argparse.Namespace) -> None:

Note

The functions are called in the following order: add_option -> handle_args -> act.

Important

The act callback should never change the Git repository it acts upon (e.g. running commands such as git add, git checkout or git commit). This can have adverse and unexpected effects on RepoBee’s functionality. It is however absolutely fine to change the files in the Git working tree, as long as nothing is added or committed.

Each callback is called at most once. They are not guaranteed to execute, because there may be an unexpected crash somewhere else, or the plugin may not come into scope (for example, a clone task plugin will not come into scope if repobee setup is run). The callbacks can do whatever is appropriate for the plugin, except for changing any Git repositories. For information on the types used in the callbacks, see the Python stdlib documentation for argparse.

As an example, a simple clone task can be defined like so:

import repobee_plug as plug

def act(path, api):
    return plug.Result(
        name="example",
        msg="IT LIVES!",
        status=plug.Status.SUCCESS
    )

@plug.repobee_hook
def clone_task():
    return plug.Task(act=act)

If your task plugin also needs to access the configuration file, then implement the separate config_hook hook. For more elaborate instructions on creating tasks, see the tutorial.

Parameters:
  • act (Callable[[Path, API], Result]) – A required callback function that takes the path to a repository worktree and an API instance, and optionally returns a Result to report results.
  • add_option (Optional[Callable[[ArgumentParser], None]]) – An optional callback function that adds options to the CLI parser.
  • handle_args (Optional[Callable[[Namespace], None]]) – An optional callback function that receives the parsed CLI args.
  • persist_changes (bool) – If True, the task requires that changes to the repository that has been acted upon be persisted. This means different things in different contexts (e.g. whether the task is executed in a clone context or in a setup context), and may not be supported for all contexts.

_deprecation

Module with functions for dealing with deprecation.

repobee_plug._deprecation.deprecate(remove_by_version, replacement=None)[source]

Return a function that can be used to deprecate functions. Currently this is only used for deprecation of hook functions, but it may be expanded to deprecated other things in the future.

Parameters:
  • remove_by_version (str) – A string that should contain a version number.
  • replacement (Optional[str]) – An optional string with the name of the replacing function.
Return type:

Callable[[~T], ~T]

Returns:

A function

repobee_plug._deprecation.deprecated_hooks()[source]
Return type:Mapping[str, Deprecation]
Returns:A mapping of hook names to Deprecation tuples.