Skip to content

api

GithubPullRequest

Bases: BaseModel

Data needed to create a pull request.

Attributes:

Name Type Description
title str

...

body str

...

head str

...

base str

...

Source code in m/github/api.py
class GithubPullRequest(BaseModel):
    """Data needed to create a pull request."""

    title: str
    body: str
    head: str
    base: str

GithubShaStatus dataclass

Data needed to create a pull request.

Source code in m/github/api.py
@dataclass
class GithubShaStatus:
    """Data needed to create a pull request."""

    sha: str
    context: str
    state: str
    description: str
    url: Optional[str] = None

commit_status(token, owner, repo, sha_info)

Set a status for a sha.

The valid states are::

  • pending
  • success
  • failure
  • error

Parameters:

Name Type Description Default
token str

A github PAT.

required
owner str

The owner of the repo.

required
repo str

The name of the repo.

required
sha_info GithubShaStatus

An instance of a GithubShaStatus.

required

Returns:

Type Description
Res[Any]

The response from Github after setting a commit status.

Source code in m/github/api.py
def commit_status(
    token: str,
    owner: str,
    repo: str,
    sha_info: GithubShaStatus,
) -> Res[Any]:
    """Set a status for a sha.

    The valid states are::

    - pending
    - success
    - failure
    - error

    Args:
        token: A github PAT.
        owner: The owner of the repo.
        repo: The name of the repo.
        sha_info: An instance of a `GithubShaStatus`.

    Returns:
        The response from Github after setting a commit status.
    """
    endpoint = _repos(owner, repo, 'statuses', sha_info.sha)
    payload = {
        'context': sha_info.context,
        'state': sha_info.state,
        'description': sha_info.description,
    }
    if sha_info.url:
        payload['target_url'] = sha_info.url
    return request(token, endpoint, HttpMethod.post, payload)

create_pr(token, owner, repo, pr_info)

Send a payload to create a pull request in github.

Parameters:

Name Type Description Default
token str

A github PAT.

required
owner str

The owner of the repo.

required
repo str

The name of the repo.

required
pr_info GithubPullRequest

The pull request information.

required

Returns:

Type Description
Res[Any]

The Github response if successful.

Source code in m/github/api.py
def create_pr(
    token: str,
    owner: str,
    repo: str,
    pr_info: GithubPullRequest,
) -> Res[Any]:
    """Send a payload to create a pull request in github.

    Args:
        token: A github PAT.
        owner: The owner of the repo.
        repo: The name of the repo.
        pr_info: The pull request information.

    Returns:
        The Github response if successful.
    """
    endpoint = _repos(owner, repo, 'pulls')
    return request(token, endpoint, HttpMethod.post, pr_info.model_dump())

create_release(token, owner, repo, version, branch=None)

Send a payload to create a release in github.

Parameters:

Name Type Description Default
token str

A github PAT.

required
owner str

The owner of the repo.

required
repo str

The name of the repo.

required
version str

The version that marks the release.

required
branch str | None

Optional branch to tag, defaults to the default branch.

None

Returns:

Type Description
Res[Any]

The Github response after a release is created.

Source code in m/github/api.py
def create_release(
    token: str,
    owner: str,
    repo: str,
    version: str,
    branch: str | None = None,
) -> Res[Any]:
    """Send a payload to create a release in github.

    Args:
        token: A github PAT.
        owner: The owner of the repo.
        repo: The name of the repo.
        version: The version that marks the release.
        branch: Optional branch to tag, defaults to the default branch.

    Returns:
        The Github response after a release is created.
    """
    endpoint = _repos(owner, repo, 'releases')
    base = 'https://github.com'
    link = f'{base}/{owner}/{repo}/blob/master/CHANGELOG.md#{version}'
    payload = {
        'tag_name': version,
        'name': f'{version}',
        'body': f'**See [CHANGELOG]({link}).**',
        'draft': False,
        'prerelease': False,
    }
    if branch:
        payload['target_commitish'] = branch
    return request(token, endpoint, HttpMethod.post, payload)

merge_pr(token, owner, repo, pr_number, commit_title)

Send a payload to merge a pull request in github.

Parameters:

Name Type Description Default
token str

A github PAT.

required
owner str

The owner of the repo.

required
repo str

The name of the repo.

required
pr_number int

The number of the pull request.

required
commit_title str | None

An optional commit title to use when merging.

required

Returns:

Type Description
Res[Any]

The payload provided by Github if successful.

Source code in m/github/api.py
def merge_pr(
    token: str,
    owner: str,
    repo: str,
    pr_number: int,
    commit_title: str | None,
) -> Res[Any]:
    """Send a payload to merge a pull request in github.

    Args:
        token: A github PAT.
        owner: The owner of the repo.
        repo: The name of the repo.
        pr_number: The number of the pull request.
        commit_title: An optional commit title to use when merging.

    Returns:
        The payload provided by Github if successful.
    """
    endpoint = _repos(owner, repo, 'pulls', str(pr_number), 'merge')
    payload = {'commit_title': commit_title} if commit_title else {}
    return request(token, endpoint, HttpMethod.put, payload)