Skip to content

Commit

Permalink
Merge pull request #53 from bioconda/toggleVisibility
Browse files Browse the repository at this point in the history
Toggle visibility
  • Loading branch information
dpryan79 authored Nov 1, 2022
2 parents 9ba7f5d + 5275593 commit e914480
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 30 deletions.
58 changes: 29 additions & 29 deletions .github/workflows/bot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
runs-on: ubuntu-18.04
env:
IMAGE_NAME: bot
IMAGE_VERSION: '1.1.22'
IMAGE_VERSION: '1.1.23'

steps:
- uses: actions/checkout@v2
Expand Down Expand Up @@ -63,35 +63,35 @@ jobs:
done
buildah rmi --prune || true
- name: Check Tags
run: |
# FIX upstream: Quay.io does not support immutable images currently.
# => Try to use the REST API to check for duplicate tags.
respone="$(
curl -sL \
'https://quay.io/api/v1/repository/bioconda/${{ steps.buildah-build.outputs.image }}/image'
)"
#- name: Check Tags
# run: |
# # FIX upstream: Quay.io does not support immutable images currently.
# # => Try to use the REST API to check for duplicate tags.
# respone="$(
# curl -sL \
# 'https://quay.io/api/v1/repository/bioconda/${{ steps.buildah-build.outputs.image }}/image'
# )"

existing_tags="$(
printf %s "${respone}" \
| jq -r '.images[].tags[]'
)" \
|| {
printf %s\\n \
'Could not get list of image tags.' \
'Does the repository exist on Quay.io?' \
'Quay.io REST API response was:' \
"${respone}"
exit 1
}
for tag in ${{ steps.buildah-build.outputs.tags }} ; do
if [ \! "${tag}" = '${{ matrix.tag }}' ] ; then
if printf %s "${existing_tags}" | grep -qxF "${tag}" ; then
printf 'Tag %s already exists!\n' "${tag}"
exit 1
fi
fi
done
# existing_tags="$(
# printf %s "${respone}" \
# | jq -r '.images[].tags[]'
# )" \
# || {
# printf %s\\n \
# 'Could not get list of image tags.' \
# 'Does the repository exist on Quay.io?' \
# 'Quay.io REST API response was:' \
# "${respone}"
# exit 1
# }
# for tag in ${{ steps.buildah-build.outputs.tags }} ; do
# if [ \! "${tag}" = '${{ matrix.tag }}' ] ; then
# if printf %s "${existing_tags}" | grep -qxF "${tag}" ; then
# printf 'Tag %s already exists!\n' "${tag}"
# exit 1
# fi
# fi
# done

- if: ${{ github.ref == 'refs/heads/main' }}
name: Push
Expand Down
3 changes: 2 additions & 1 deletion images/bot/Dockerfile.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ RUN . /usr/local/env-activate.sh && \
bioconda-bot --help && \
bioconda-bot comment --help && \
bioconda-bot merge --help && \
bioconda-bot update --help
bioconda-bot update --help && \
bioconda-bot change --help
63 changes: 63 additions & 0 deletions images/bot/src/bioconda_bot/changeVisibility.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import logging
import os
import re
import sys
from asyncio import gather, sleep
from asyncio.subprocess import create_subprocess_exec
from enum import Enum, auto
from pathlib import Path
from shutil import which
from typing import Any, Dict, List, Optional, Set, Tuple
from zipfile import ZipFile, ZipInfo

from aiohttp import ClientSession
from yaml import safe_load

from .common import (
async_exec,
fetch_pr_sha_artifacts,
get_job_context,
get_pr_comment,
get_pr_info,
is_bioconda_member,
send_comment,
)

logger = logging.getLogger(__name__)
log = logger.info


# Ensure uploaded containers are in repos that have public visibility
# TODO: This should ping @bioconda/core if it fails
async def toggle_visibility(session: ClientSession, container_repo: str) -> None:
url = f"https://quay.io/api/v1/repository/biocontainers/{container_repo}/changevisibility"
QUAY_OAUTH_TOKEN = os.environ["QUAY_OAUTH_TOKEN"]
headers = {
"Authorization": f"Bearer {QUAY_OAUTH_TOKEN}",
"Content-Type": "application/json",
}
body = {"visibility": "public"}
rc = 0
try:
async with session.post(url, headers=headers, json=body) as response:
rc = response.status
except:
# Do nothing
pass
log("Trying to toggle visibility (%s) returned %d", url, rc)


# This requires that a JOB_CONTEXT environment variable, which is made with `toJson(github)`
async def main() -> None:
job_context = await get_job_context()
issue_number, original_comment = await get_pr_comment(job_context)
if issue_number is None or original_comment is None:
return

comment = original_comment.lower()
if comment.startswith(("@bioconda-bot", "@biocondabot")):
if " please toggle visibility" in comment:
pkg = comment.split("please change visibility")[1].strip().split()[0]
async with ClientSession() as session:
await toggle_visibility(session, pkg)
await send_comment(session, issue_number, "Visibility changed.")
10 changes: 10 additions & 0 deletions images/bot/src/bioconda_bot/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ def run_command() -> None:
parser.set_defaults(run_command=run_command)


def build_parser_changeVisibility(parser: ArgumentParser) -> None:
def run_command() -> None:
from .changeVisibility import main as main_

run(main_())

parser.set_defaults(run_command=run_command)


def get_argument_parser() -> ArgumentParser:
parser = ArgumentParser(
prog="bioconda-bot",
Expand All @@ -55,6 +64,7 @@ def get_argument_parser() -> ArgumentParser:
("merge", build_parser_merge),
("update", build_parser_update),
("automerge", build_parser_automerge),
("change", build_parser_changeVisibility),
):
sub_parser = sub_parsers.add_parser(
command_name,
Expand Down

0 comments on commit e914480

Please sign in to comment.