Skip to content

Commit

Permalink
Merge branch 'main' into pagination-and-rate-limit
Browse files Browse the repository at this point in the history
  • Loading branch information
sondrelg authored Feb 12, 2023
2 parents 2ff9ce0 + 7a5a61d commit 108bfb6
Show file tree
Hide file tree
Showing 12 changed files with 631 additions and 406 deletions.
18 changes: 0 additions & 18 deletions .github/workflows/build.yml

This file was deleted.

6 changes: 2 additions & 4 deletions .github/workflows/clean_packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ jobs:
echo "FROM scratch as $randomString" > Dockerfile.test
cat Dockerfile.test
docker buildx build . -f Dockerfile.test -t "${{ env.IMAGE }}:latest" --cache-to=type=inline --cache-from="ghcr.io/sondrelg/ghcr-retention-policy-test:latest" --push
- name: Build latest
run: |
clean-ghcr:
needs: build-containers
Expand All @@ -65,10 +63,10 @@ jobs:
run: |
images="$(cat skip-image-versions.txt)"
echo $images
echo "::set-output name=image-versions::$images"
echo "image-versions=$images" >> $GITHUB_OUTPUT
- name: Delete images more than 2 seconds old
uses: sondrelg/container-retention-policy@sondrelg/experiment
uses: snok/container-retention-policy@${{ github.ref_name }}
id: delete-images
with:
image-names: ghcr-retention-policy-test
Expand Down
33 changes: 14 additions & 19 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.10.5"
python-version: "3.11.2"
- uses: actions/cache@v3
id: cache-venv
id: cache
with:
path: .venv
key: venv-0
path: |
.venv
~/.cache/pre-commit
key: venv-1
- run: |
python -m venv .venv --upgrade-deps
source .venv/bin/activate
pip install pre-commit
if: steps.cache-venv.outputs.cache-hit != 'true'
- uses: actions/cache@v3
id: pre-commit-cache
with:
path: ~/.cache/pre-commit
key: key-0
if: steps.cache.outputs.cache-hit != 'true'
- run: |
source .venv/bin/activate
pre-commit run --all-files
Expand All @@ -39,21 +36,19 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.10.5"
python-version: "3.11.2"
- uses: actions/cache@v3
id: cache
with:
path: ~/.local
key: poetry-cache-0
path: |
~/.local
.venv
key: ${{ hashFiles('**/poetry.lock') }}-1
- uses: snok/install-poetry@v1
with:
virtualenvs-in-project: true
- uses: actions/cache@v3
id: cache-venv
with:
path: .venv
key: ${{ hashFiles('**/poetry.lock') }}-0
- run: poetry install --no-interaction --no-root
if: steps.cache-venv.outputs.cache-hit != 'true'
if: steps.cache.outputs.cache-hit != 'true'
- run: source $VENV && pytest main_tests.py --cov-report=xml
- uses: codecov/codecov-action@v3
with:
Expand Down
16 changes: 8 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
repos:
- repo: https://github.com/ambv/black
rev: 22.6.0
rev: 23.1.0
hooks:
- id: black
args: ['--quiet']
- repo: https://github.com/pycqa/isort
rev: 5.10.1
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
rev: v4.4.0
hooks:
- id: check-ast
- id: check-added-large-files
Expand All @@ -24,12 +24,12 @@ repos:
- id: mixed-line-ending
- id: trailing-whitespace
- repo: https://github.com/asottile/pyupgrade
rev: v2.37.1
rev: v3.3.1
hooks:
- id: pyupgrade
args: ['--py3-plus', '--py36-plus', '--py37-plus', '--py38-plus', '--py39-plus', '--py310-plus']
- repo: https://gitlab.com/pycqa/flake8
rev: 3.9.2
args: ['--py3-plus', '--py36-plus', '--py37-plus', '--py38-plus', '--py39-plus', '--py310-plus', '--py311-plus']
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
additional_dependencies: [
Expand All @@ -40,7 +40,7 @@ repos:
'flake8-type-checking',
]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v0.961'
rev: 'v1.0.0'
hooks:
- id: mypy
additional_dependencies: [types-dateparser]
16 changes: 0 additions & 16 deletions Dockerfile

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ name: Delete old container images

on:
schedule:
- cron: '0 0 * * *' # every day at midnight
- cron: "0 0 1 * *" # every day at midnight


jobs:
Expand Down
49 changes: 35 additions & 14 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,43 @@ inputs:
outputs:
needs-github-assistance:
description: 'Comma-separated list of image names and tags, for image versions that are public and have more than 5000 downloads.'
value: ${{ steps.container-retention-policy.outputs.needs-github-assistance }}
deleted:
description: 'Comma-separated list of image names and tags, for image versions that were deleted during the run.'
value: ${{ steps.container-retention-policy.outputs.deleted }}
failed:
description: 'Comma-separated list of image names and tags, for image versions that we failed to delete during the run, for an unknown reason.'
value: ${{ steps.container-retention-policy.outputs.failed }}
runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.account-type }}
- ${{ inputs.org-name }}
- ${{ inputs.image-names }}
- ${{ inputs.timestamp-to-use }}
- ${{ inputs.cut-off }}
- ${{ inputs.token }}
- ${{ inputs.untagged-only }}
- ${{ inputs.skip-tags }}
- ${{ inputs.keep-at-least }}
- ${{ inputs.filter-tags }}
- ${{ inputs.filter-include-untagged }}
using: 'composite'
steps:
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: "3.11"

- name: Install dependencies
shell: bash
run: |
pip --disable-pip-version-check install \
regex==2022.3.2 \
httpx \
dateparser \
pydantic
- name: Run Container Retention Policy
shell: bash
id: container-retention-policy
run: |
python ${{ github.action_path }}/main.py \
"${{ inputs.account-type }}" \
"${{ inputs.org-name }}" \
"${{ inputs.image-names }}" \
"${{ inputs.timestamp-to-use }}" \
"${{ inputs.cut-off }}" \
"${{ inputs.token }}" \
"${{ inputs.untagged-only }}" \
"${{ inputs.skip-tags }}" \
"${{ inputs.keep-at-least }}" \
"${{ inputs.filter-tags }}" \
"${{ inputs.filter-include-untagged }}"
11 changes: 6 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import asyncio
import re
import time
import os
from asyncio import Semaphore
from datetime import datetime, timedelta
from enum import Enum
Expand All @@ -16,7 +17,6 @@
from pydantic import BaseModel, conint, validator

if TYPE_CHECKING:

from httpx import Response

BASE_URL = 'https://api.github.com'
Expand Down Expand Up @@ -92,7 +92,7 @@ async def list_org_packages(*, org_name: str, http_client: AsyncClient) -> list[
:param http_client: HTTP client.
:return: List of packages.
"""
response = await http_client.get(f'{BASE_URL}/orgs/{org_name}/packages?package_type=container')
response = await http_client.get(f'{BASE_URL}/orgs/{org_name}/packages?package_type=container&per_page=100')
response.raise_for_status()
return [PackageResponse(**i) for i in response.json()]

Expand All @@ -104,7 +104,7 @@ async def list_packages(*, http_client: AsyncClient) -> list[PackageResponse]:
:param http_client: HTTP client.
:return: List of packages.
"""
response = await http_client.get(f'{BASE_URL}/user/packages?package_type=container')
response = await http_client.get(f'{BASE_URL}/user/packages?package_type=container&per_page=100')
response.raise_for_status()
return [PackageResponse(**i) for i in response.json()]

Expand Down Expand Up @@ -352,7 +352,6 @@ async def get_and_delete_old_versions(image_name: ImageName, inputs: Inputs, htt

async with sem:
for version in versions:

# Parse either the update-at timestamp, or the created-at timestamp
# depending on which on the user has specified that we should use
updated_or_created_at = getattr(version, inputs.timestamp_to_use.value)
Expand Down Expand Up @@ -550,7 +549,9 @@ async def main(
('failed', failed),
]:
comma_separated_list = ','.join(l)
print(f'::set-output name={name}::{comma_separated_list}')

with open(os.environ['GITHUB_ENV'], 'a') as f:
f.write(f'{name}={comma_separated_list}')


if __name__ == '__main__':
Expand Down
27 changes: 20 additions & 7 deletions main_tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import asyncio
import os
import tempfile
from asyncio import Semaphore
from copy import deepcopy
from datetime import datetime, timedelta, timezone
Expand Down Expand Up @@ -39,6 +41,16 @@
mock_http_client.delete.return_value = mock_response


@pytest.fixture(autouse=True)
def github_env():
"""
Create a GITHUB_ENV env value to mock the Github actions equivalent.
"""
with tempfile.NamedTemporaryFile() as temp:
os.environ['GITHUB_ENV'] = temp.name
yield


@pytest.mark.asyncio
async def test_list_org_package_version():
await list_org_package_versions(org_name='test', image_name=ImageName('test', 'test'), http_client=mock_http_client)
Expand Down Expand Up @@ -544,7 +556,7 @@ def json(self):


@pytest.mark.asyncio
async def test_outputs_are_set(mocker, capsys):
async def test_outputs_are_set(mocker):
mock_list_response = Mock()
mock_list_response.headers = {'x-ratelimit-remaining': '1', 'link': ''}
mock_list_response.is_error = True
Expand Down Expand Up @@ -577,11 +589,12 @@ async def test_outputs_are_set(mocker, capsys):
'token': 'test',
}
)
captured = capsys.readouterr()
out = captured.out
with open(os.environ['GITHUB_ENV']) as f:
env_vars = f.readlines()[0]

for i in [
'::set-output name=needs-github-assistance::',
'::set-output name=deleted::',
'::set-output name=failed::',
'needs-github-assistance=',
'deleted=',
'failed=',
]:
assert i in out
assert i in env_vars
Loading

0 comments on commit 108bfb6

Please sign in to comment.