Skip to content

Commit

Permalink
wip-fixme
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaqq committed May 28, 2024
1 parent 03f373f commit 5cc512a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .github/actions/update-charm-pins/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ inputs:
description: Whitespace-separated paths to the local workflow file, relative to repository root
required: true
gh-pat:
description: Personal access token to check out external repos from GitHub
description: Personal access token to check out external repos from github
required: true

runs:
Expand All @@ -21,7 +21,7 @@ runs:
with:
python-version: "3.12"

- run: python -m pip install ruamel.yaml==0.18.6 httpx==0.27.0
- run: python -m pip install -r .github/actions/update-charm-pins/requirements.txt
shell: bash
- run: python .github/actions/update-charm-pins/main.py '${{ inputs.workflows }}'
shell: bash
Expand Down
20 changes: 7 additions & 13 deletions .github/actions/update-charm-pins/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@


def update_charm_pins(workflow):
"""Update pinned versions of charms in the given github actions workflow."""
"""Update pinned versions of charms in the given GitHub Actions workflow."""
with open(workflow) as file:
doc = yaml.load(file)

Expand All @@ -31,24 +31,18 @@ def update_charm_pins(workflow):

for idx, item in enumerate(doc["jobs"][job_name]["strategy"]["matrix"]["include"]):
charm_repo = item["charm-repo"]

resp = github.get(f"{charm_repo}/commits", params={"per_page": 1})
resp.raise_for_status()
commit = resp.json()[0]["sha"]
timestamp = resp.json()[0]["commit"]["committer"]["date"]

resp = github.get(f"{charm_repo}/tags")
resp.raise_for_status()
commit = github.get(f"{charm_repo}/commits").raise_for_status().json()[0]
data = github.get(f"{charm_repo}/tags").raise_for_status().json()
comment = " ".join(
[tag["name"] for tag in resp.json() if tag["commit"]["sha"] == commit]
+ [timestamp]
[tag["name"] for tag in data if tag["commit"]["sha"] == commit["sha"]]
+ [commit["commit"]["committer"]["date"]]
)

# A YAML node, as opposed to a plain value, can be updated in place to tweak comments
node = doc.mlget(
["jobs", job_name, "strategy", "matrix", "include", idx], list_ok=True
)
node["commit"] = commit
node["commit"] = commit["sha"]
node.yaml_add_eol_comment(comment, key="commit")

with open(workflow, "w") as file:
Expand All @@ -57,5 +51,5 @@ def update_charm_pins(workflow):

if __name__ == "__main__":
logging.basicConfig(level="INFO")
for workflow in sys.argv[1].split():
for workflow in " ".join(sys.argv[1:]).split():
update_charm_pins(workflow)
27 changes: 27 additions & 0 deletions .github/actions/update-charm-pins/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Update Charm Pins

## GitHub Actions Usage

Inputs:

- `workflows`: space or newline-separated list of workflow YAML files relative to this repository root
- `gh-pat`: personal access token to query external repositories hosted at GitHub

This action will update the `workflows` in the current checkout. It is the responsibility of the caller
to do something with these changes.

## Local Usage

```command
# set up a venv and install the deps
pip install -r requirements.txt

# set the GITHUB_TOKEN env var with a personal access token
export GITHUB_TOKEN=ghp_0123456789

# run the script
python main.py path-to/.github/workflows/one.yaml path-to/.github/workflows/another.yaml

# check the modifications in the current branch
git diff
```
2 changes: 2 additions & 0 deletions .github/actions/update-charm-pins/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ruamel.yaml==0.18.6
httpx==0.27.0
4 changes: 2 additions & 2 deletions .github/workflows/update-charm-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
git config --global user.name "github-actions"
git config --global user.email "github-actions@github.com"
git switch -C auto-update-external-charm-pins
git commit --allow-empty -am "ci: update external charm pins"
git commit --allow-empty -am "chore: update charm pins"
echo "Total changes in charm pins"
git --no-pager diff main HEAD
git push -f --set-upstream origin auto-update-external-charm-pins
Expand All @@ -47,7 +47,7 @@ jobs:
gh pr close -c stale "$PR";
elif [[ -z "$PR" && -n "$CHANGES" ]]; then
echo "Opening new PR"
gh pr create --base main --head auto-update-external-charm-pins --title "Update Charm Pins" --body "This is an automated PR to update pins of the external repositories that the operator framework is tested against";
gh pr create --base main --head auto-update-external-charm-pins --title "chore: update charm pins" --body "This is an automated PR to update pins of the external repositories that the operator framework is tested against";
fi
env:
GITHUB_TOKEN: ${{ secrets.UPDATE_CHARM_PINS_ACCESS_TOKEN }}
5 changes: 5 additions & 0 deletions HACKING.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ your charm to a controller using that version of Juju. For example, with microk8
3. Run `GOBIN=/path/to/your/juju/_build/linux_amd64/bin:$GOBIN /path/to/your/juju bootstrap`
4. Add a model and deploy your charm as normal

### Regression testing against existing charms

We rely on automation to [update charm pins](.github/action/update-charm-pins/) of
a bunch of charms that use the operator framework. The script can be ran locally too.

# Documentation

In general, new functionality
Expand Down

0 comments on commit 5cc512a

Please sign in to comment.