Skip to content

Commit

Permalink
Real test the repository dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Nov 18, 2024
1 parent 2ca2b71 commit 3431c65
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 74 deletions.
2 changes: 2 additions & 0 deletions .github/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ docker:
helm:
folders:
- tests
dispatch:
- {}
25 changes: 3 additions & 22 deletions .github/workflows/repository-dispatch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,8 @@ on:
types:
- published
inputs:
type:
description: The type of the event
required: true
name:
description: The package name
folder:
description: The package folder
version:
description: The package version
tag:
description: The package tag
repository:
description: The repository name or URL
version_type:
description: The version type
content:
description: Published content
required: true

jobs:
Expand All @@ -31,10 +18,4 @@ jobs:
steps:
- name: Print the event
run: |
echo "Event type: ${{ github.event.client_payload.type }}"
echo "Package name: ${{ github.event.client_payload.name }}"
echo "Package folder: ${{ github.event.client_payload.folder }}"
echo "Package version: ${{ github.event.client_payload.version }}"
echo "Package tag: ${{ github.event.client_payload.tag }}"
echo "Repository: ${{ github.event.client_payload.repository }}"
echo "Version type: ${{ github.event.client_payload.version_type }}"
echo "Publish: ${{ toJson(github.event.client_payload.content) }}"
43 changes: 40 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ permissions:
packages: write
# To publish Python packages using OIDC
id-token: write
# To publish Helm charts
# To publish Helm charts and send repository dispatch notifications
contents: write
```
Install the package in the worklow:
```yaml
- name: Install tag-publish
run: pip install c2cciutils-publish
run: pip install tag-publish
```
Do the publishing:
Expand Down Expand Up @@ -77,7 +77,7 @@ The configuration file is `.github/publish.yaml`, the schema is `https://raw.git

### Dry run

Dry run publish: `GITHUB_REF=... c2cciutils-publish --dry-run ...`
Dry run publish: `GITHUB_REF=... tag-publish --dry-run ...`

### To pypi

Expand Down Expand Up @@ -247,6 +247,43 @@ git commit --allow-empty -m "Initialize gh-pages branch"
git push origin gh-pages
```

## Dispatch

The minimal config is like this:

```yaml
dispatch:
- {}
```

The required permission is `contents: write`.

This will create a repository dispatch of type `published` on own repository with the `content` e.g.:

```json
{
"version": "1.2.3",
"version_type": "version_tag",
"repository": "camptocamp/tag-publish",
"items": [
{
"type": "docker",
"image": "camptocamp/tag-publish",
"repository": "ghcr.io",
"tag": "1.2.3"
},
{
"type": "pypi",
"path": "."
},
{
"type": "helm",
"path": "."
}
]
}
```

## Contributing

Install the pre-commit hooks:
Expand Down
2 changes: 1 addition & 1 deletion config.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ _Tag Publish configuration file_
- **`docker`**: Refer to _[#/definitions/docker](#definitions/docker)_.
- **`pypi`**: Refer to _[#/definitions/pypi](#definitions/pypi)_.
- **`helm`**: Refer to _[#/definitions/helm](#definitions/helm)_.
- **`dispatch`** _(array)_: Default: `[{}]`.
- **`dispatch`** _(array)_: Default: `[]`.
- **Items** _(object)_: Send a dispatch event to an other repository. Default: `{}`.
- **`repository`** _(string)_: The repository name to be triggered. Default: `"camptocamp/argocd-gs-gmf-apps"`.
- **`event-type`** _(string)_: The event type to be triggered. Default: `"published"`.
Expand Down
5 changes: 1 addition & 4 deletions tag_publish/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,7 @@ class PublishedPayload(TypedDict, total=False):
"""

type: str
name: str
image: str
folder: str
version: str
tag: str
repository: str
version_type: str
id: int
59 changes: 23 additions & 36 deletions tag_publish/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import json
import os
import os.path
import random
import re
import subprocess # nosec
import sys
Expand Down Expand Up @@ -197,7 +196,7 @@ def main() -> None:
local,
)
success &= _handle_helm_publish(args.dry_run, config, version, version_type, github, published_payload)
_trigger_dispatch_events(config, published_payload, github)
_trigger_dispatch_events(config, version, version_type, published_payload, github)

if not success:
sys.exit(1)
Expand Down Expand Up @@ -228,14 +227,7 @@ def _handle_pypi_publish(
print(f"{'Publishing' if publish else 'Checking'} '{folder}' to pypi, skipping (dry run)")
else:
success &= tag_publish.publish.pip(package, version, version_type, publish, github)
published_payload.append(
{
"type": "pypi",
"folder": folder,
"version": version,
"version_type": version_type,
}
)
published_payload.append({"type": "pypi", "folder": folder})
return success


Expand Down Expand Up @@ -360,7 +352,6 @@ def _handle_docker_publish(
tag_src,
tags,
images_full,
version_type,
published_payload,
)

Expand Down Expand Up @@ -504,39 +495,35 @@ def _handle_helm_publish(
else:
token = os.environ["GITHUB_TOKEN"]
success &= tag_publish.publish.helm(folder, version, owner, repo, commit_sha, token)
published_payload.append(
{
"type": "helm",
"folder": folder,
"version": version,
"version_type": version_type,
}
)
published_payload.append({"type": "helm", "folder": folder})
return success


def _trigger_dispatch_events(
config: tag_publish.configuration.Configuration,
version: str,
version_type: str,
published_payload: list[tag_publish.PublishedPayload],
github: tag_publish.GH,
) -> None:
for published in published_payload:
for dispatch_config in config.get("dispatch", []):
repository = dispatch_config.get("repository")
event_type = dispatch_config.get(
"event-type", tag_publish.configuration.DISPATCH_EVENT_TYPE_DEFAULT
)

id_ = random.randint(1, 100000) # nosec # noqa: S311
published["id"] = id_

if repository:
print(f"Triggering {event_type}:{id_} on {repository} with {json.dumps(published)}")
github_repo = github.github.get_repo(repository)
else:
print(f"Triggering {event_type}:{id_} with {json.dumps(published)}")
github_repo = github.repo
github_repo.create_repository_dispatch(event_type, published) # type: ignore[arg-type]
for dispatch_config in config.get("dispatch", []):
repository = dispatch_config.get("repository")
event_type = dispatch_config.get("event-type", tag_publish.configuration.DISPATCH_EVENT_TYPE_DEFAULT)

published = {
"version": version,
"version_type": version_type,
"repository": github.repo.full_name,
"items": published_payload,
}

if repository:
print(f"Triggering {event_type} on {repository} with {json.dumps(published)}")
github_repo = github.github.get_repo(repository)
else:
print(f"Triggering {event_type} with {json.dumps(published)}")
github_repo = github.repo
github_repo.create_repository_dispatch(event_type, {"content": published})


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions tag_publish/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ class Configuration(TypedDict, total=False):
Dispatch.
default:
- {}
[]
"""


DISPATCH_CONFIG_DEFAULT: Dict[str, Any] = {}
""" Default value of the field path 'Dispatch item' """


DISPATCH_DEFAULT = [{}]
DISPATCH_DEFAULT: List[Any] = []
""" Default value of the field path 'configuration dispatch' """


Expand Down
7 changes: 2 additions & 5 deletions tag_publish/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ def docker(
tag_src: str,
dst_tags: list[str],
images_full: list[str],
version_type: str,
published: Optional[list[tag_publish.PublishedPayload]] = None,
) -> bool:
"""
Expand Down Expand Up @@ -160,9 +159,8 @@ def docker(
{
"type": "docker",
"repository": config["server"],
"name": image_config["name"],
"image": image_config["name"],
"tag": tag,
"version_type": version_type,
}
)
else:
Expand All @@ -182,9 +180,8 @@ def docker(
{
"type": "docker",
"repository": "docker.io",
"name": image_config["name"],
"image": image_config["name"],
"tag": tag,
"version_type": version_type,
}
)
new_images_full.append(f"{image_config['name']}:{tag}")
Expand Down
2 changes: 1 addition & 1 deletion tag_publish/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@
"dispatch": {
"title": "Dispatch",
"type": "array",
"default": [{}],
"default": [],

"items": {
"title": "dispatch config",
Expand Down

0 comments on commit 3431c65

Please sign in to comment.