Metadata-Version: 2.4
Name: canonicalwebteam.launchpad
Version: 0.10.0
Summary: Trigger builds of snaps and ubuntu images through the launchpad API.
Home-page: https://github.com/canonical-web-and-design/canonicalwebteam.launchpad
Author: Canonical webteam
Author-email: webteam@canonical.com
Description-Content-Type: text/markdown
Requires-Dist: python-gnupg>=0.4.6
Requires-Dist: pytimeparse==1.1.8
Requires-Dist: humanize==4.9.0
Dynamic: author
Dynamic: author-email
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: requires-dist
Dynamic: summary

# canonicalwebteam.launchpad

Classes for triggering builds of snaps and Ubuntu images through the [Launchpad API](https://launchpad.net/+apidoc/devel.html).

## Usage

### Local development

For local development, it's best to test this module with one of our website projects like [ubuntu.com](https://github.com/canonical-web-and-design/ubuntu.com/). For more information, follow [this guide (internal only)](https://discourse.canonical.com/t/how-to-run-our-python-modules-for-local-development/308).

### SnapBuilder

``` python3
from canonicalwebteam.launchpad import SnapBuilder

snap_builder = SnapBuilder(
    username="build.snapcraft.io",
    token=os.getenv("SNAP_BUILDER_TOKEN"),
    secret=os.getenv("SNAP_BUILDER_SECRET"),
)

snap_name = "new-test-snap"
git_repo = "https://github.com/build-staging-snapcraft-io/test1"
snap_builder.create_snap(snap_name, git_repo)

new_snap = snap_builder.get_snap_by_store_name("new-test-snap")
```

`create_snap` authorizes store uploads using a root macaroon (typically obtained from the store's `package_upload` ACL endpoint). If that macaroon carries a third-party (SSO) caveat, Launchpad needs the matching `discharge_macaroon` as well, otherwise it will accept the authorization but never actually be able to upload builds — leaving every build stuck as "Unscheduled" ("Won't release") with no error surfaced:

``` python3
snap_builder.create_snap(
    snap_name, git_repo, macaroon, discharge_macaroon=discharge_macaroon
)
```

Snaps that were created before a discharge macaroon was available can be repaired without recreating them, using `complete_snap_authorization`:

``` python3
snap_builder.complete_snap_authorization(
    lp_snap_name, macaroon, discharge_macaroon=discharge_macaroon
)
```

### ImageBuilder

``` python3
from canonicalwebteam.launchpad import ImageBuilder

image_builder = ImageBuilder(
    username="image.build",
    token=os.getenv("IMAGE_BUILDER_TOKEN"),
    secret=os.getenv("IMAGE_BUILDER_SECRET"),
)

image_builder.build_image(
    board="cm3", system="core16", snaps=["code", "toto"]
)
```

## Test fixtures

Tests check calls against fixtures representing the Launchpad API. These fixtures are generated using [vcrpy](https://pypi.org/project/vcrpy/), based on real calls to the API when the test was first run.

To new tests that rely on new API responses, or if we need to regenerate existing fixtures because the API has changed, the secrets need to be provided to authenticate with the API as follows:

``` bash
export SNAP_BUILDER_TOKEN={token}
export SNAP_BUILDER_SECRET={secret}
export IMAGE_BUILDER_TOKEN={token}
export IMAGE_BUILDER_SECRET={secret}

rm tests/cassettes/...  # Remove any fixtures you need to regenerate

./setup.py test  # Run tests again to regenerate fixtures
```
