Skip to content

Commit

Permalink
Merge pull request #754 from conda-forge/add-live-automerge-test
Browse files Browse the repository at this point in the history
test: add live test for automerge
  • Loading branch information
beckermr authored Nov 1, 2024
2 parents f1e9638 + d7f6a0c commit 9c090dc
Show file tree
Hide file tree
Showing 6 changed files with 276 additions and 15 deletions.
57 changes: 57 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,60 @@ jobs:
pytest -vvs --branch=${branch} test_live_version_update.py
env:
GH_TOKEN: ${{ secrets.CF_ADMIN_GITHUB_TOKEN }}

live-tests-automerge:
name: live-tests-automerge
runs-on: "ubuntu-latest"
needs:
- tests
concurrency:
group: ${{ github.event.pull_request.head.repo.fork != 'true' && 'live-tests-automerge' || format('{0}-{1}', github.workflow, github.ref) }}
steps:
- uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4
if: ${{ !github.event.pull_request.head.repo.fork }}
with:
fetch-depth: 0

- name: setup conda
if: ${{ !github.event.pull_request.head.repo.fork }}
uses: mamba-org/setup-micromamba@f8b8a1e23a26f60a44c853292711bacfd3eac822 # v1
with:
environment-file: conda-lock.yml
environment-name: webservices
condarc: |
show_channel_urls: true
channel_priority: strict
channels:
- conda-forge
- name: generate token
if: ${{ !github.event.pull_request.head.repo.fork }}
id: generate_token
uses: actions/create-github-app-token@5d869da34e18e7287c1daad50e0b8ea0f506ce69 # v1
with:
app-id: ${{ secrets.CF_CURATOR_APP_ID }}
private-key: ${{ secrets.CF_CURATOR_PRIVATE_KEY }}
owner: ${{ github.repository_owner }}

- name: install code
if: ${{ !github.event.pull_request.head.repo.fork }}
run: |
git config --global user.email "79913779+conda-forge-curator[bot]@users.noreply.github.com"
git config --global user.name "conda-forge-curator[bot]"
git config --global pull.rebase false
pip install --no-deps --no-build-isolation -e .
- name: run automerge tests
if: ${{ !github.event.pull_request.head.repo.fork }}
run: |
if [[ "${GITHUB_HEAD_REF}" != "" ]]; then
branch="${GITHUB_HEAD_REF}"
else
branch="${GITHUB_REF_NAME}"
fi
cd tests
pytest -vvs --branch=${branch} test_live_automerge.py
env:
GHA_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
GH_TOKEN: ${{ steps.generate_token.outputs.token }}
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,14 @@ def main_automerge(repo, sha):

LOGGER.info("Running automerge for conda-forge/%s@%s", repo, sha)

found_pr = False
full_repo_name = f"conda-forge/{repo}"
_, gh = create_api_sessions()
gh_repo = gh.get_repo(full_repo_name)
for pr in gh_repo.get_pulls():
if pr.head.sha == sha:
automerge_pr(repo, pr)
automerge_pr(gh_repo, pr)
found_pr = True

if not found_pr:
raise RuntimeError(f"No PR found for {full_repo_name}@{sha}!")
46 changes: 32 additions & 14 deletions conda_forge_webservices/webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,21 +741,39 @@ def _dispatch_automerge_job(repo, sha):
return

gh = get_gh_client()
ref = __version__.replace("+", ".")
workflow = gh.get_repo("conda-forge/conda-forge-webservices").get_workflow(
"automerge.yml"
)
running = workflow.create_dispatch(
ref=ref,
inputs={
"repo": repo,
"sha": sha,
},
)
if running:
LOGGER.info(" automerge job dispatched: conda-forge/%s@%s", repo, sha)

skip_test_pr = False
if repo == "cf-autotick-bot-test-package-feedstock":
gh_repo = gh.get_repo("conda-forge/cf-autotick-bot-test-package-feedstock")
for pr in gh_repo.get_pulls():
if pr.head.sha == sha:
if pr.head.ref.startswith("automerge-live-test-head-branch-"):
skip_test_pr = True
break

if not skip_test_pr:
ref = __version__.replace("+", ".")
workflow = gh.get_repo("conda-forge/conda-forge-webservices").get_workflow(
"automerge.yml"
)
running = workflow.create_dispatch(
ref=ref,
inputs={
"repo": repo,
"sha": sha,
},
)

if running:
LOGGER.info(" automerge job dispatched: conda-forge/%s@%s", repo, sha)
else:
LOGGER.info(" automerge job dispatch failed")
else:
LOGGER.info(" automerge job dispatch failed")
LOGGER.info(
" automerge job dispatch skipped for testing: conda-forge/%s@%s",
repo,
sha,
)


class StatusMonitorPayloadHookHandler(tornado.web.RequestHandler):
Expand Down
13 changes: 13 additions & 0 deletions tests/conda-forge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
bot:
automerge: true
conda_forge_output_validation: true
github:
branch_name: main
tooling_branch_name: main
azure:
store_build_artifacts: true
conda_build:
pkg_format: 2
provider:
linux_64: azure
osx_64: github_actions
37 changes: 37 additions & 0 deletions tests/meta.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{% set name = "cf-autotick-bot-test-package" %}
{% set version = "0.14" %}

package:
name: {{ name|lower }}
version: {{ version }}

source:
url: https://github.com/regro/cf-autotick-bot-test-package/archive/v{{ version }}.tar.gz
sha256: f6c45d5788f51dbe1cc55e1010f3e9ebd18b6c0f21907fc35499468a59827eef

build:
number: 0
skip: true # [py != 39 or win or aarch64 or ppc64le or s390x]

requirements:
host:
- python
- pip
- setuptools
run:
- python

test:
commands:
- echo "works!"

about:
home: https://github.com/regro/cf-scripts
license: BSD-3-Clause
license_family: BSD
license_file: LICENSE
summary: testing feedstock for the regro-cf-autotick-bot

extra:
recipe-maintainers:
- conda-forge-daemon
131 changes: 131 additions & 0 deletions tests/test_live_automerge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import os
import subprocess
import tempfile
import time
import uuid

import github
from conda_forge_webservices.utils import pushd

TEST_BASE_BRANCH = "automerge-live-test-base-branch"
TEST_HEAD_BRANCH = f"automerge-live-test-head-branch-h{uuid.uuid4().hex[:6]}"
DEBUG = False


def _run_git_cmd(*args):
subprocess.run(["git", *list(args)], check=True)


def test_live_automerge(pytestconfig):
branch = pytestconfig.getoption("branch")

print("making an edit to the head ref...", flush=True)
with tempfile.TemporaryDirectory() as tmpdir:
with pushd(tmpdir):
print("cloning...", flush=True)
_run_git_cmd(
"clone",
f"https://x-access-token:{os.environ['GH_TOKEN']}@github.com/conda-forge/"
"cf-autotick-bot-test-package-feedstock.git",
)

with pushd("cf-autotick-bot-test-package-feedstock"):
pr = None
try:
print("checkout branch...", flush=True)
_run_git_cmd("checkout", TEST_BASE_BRANCH)
_run_git_cmd("checkout", "-b", TEST_HEAD_BRANCH)

print("adding a correct recipe and conda-forge.yml...", flush=True)
test_dir = os.path.dirname(__file__)
subprocess.run(
["cp", f"{test_dir}/conda-forge.yml", "."],
check=True,
)
subprocess.run(
["cp", f"{test_dir}/meta.yaml", "recipe/meta.yaml"],
check=True,
)

print("rerendering...", flush=True)
subprocess.run(
[
"conda",
"smithy",
"rerender",
"-c",
"auto",
"--no-check-uptodate",
],
check=True,
)

print("making a commit...", flush=True)
_run_git_cmd("add", ".")
_run_git_cmd(
"commit", "--allow-empty", "-m", "test commit for automerge"
)

print("push to branch...", flush=True)
_run_git_cmd("push", "-u", "origin", TEST_HEAD_BRANCH)

print("making a PR...", flush=True)
gh = github.Github(auth=github.Auth.Token(os.environ["GH_TOKEN"]))
repo = gh.get_repo(
"conda-forge/cf-autotick-bot-test-package-feedstock"
)

pr = repo.create_pull(
TEST_BASE_BRANCH,
TEST_HEAD_BRANCH,
title="[DO NOT TOUCH] test pr for automerge",
body=(
"This is a test PR for automerge from "
f"GHA run {os.environ['GHA_URL']}. "
"Please do not make any changes!"
),
maintainer_can_modify=True,
draft=False,
)
pr.add_to_labels("automerge")

print("waiting for the PR to be merged...", flush=True)
tot = 0
merged = False
while tot < 600:
time.sleep(10)
tot += 10
print(f" slept {tot} seconds out of 600", flush=True)
if tot % 30 == 0:
if pr.is_merged():
print("PR was merged!", flush=True)
merged = True
break
elif tot > 0:
cfws_repo = gh.get_repo(
"conda-forge/conda-forge-webservices"
)
workflow = cfws_repo.get_workflow("automerge.yml")
workflow.create_dispatch(
ref=branch,
inputs={
"repo": (
"cf-autotick-bot-test-package-feedstock"
),
"sha": pr.head.sha,
},
)

if not merged:
raise RuntimeError(f"PR {pr.number} was not merged!")

finally:
if not DEBUG:
print("closing PR if it is open...", flush=True)
if pr is not None and not pr.is_merged():
pr.edit(state="closed")

print("deleting the test branch...", flush=True)
_run_git_cmd("checkout", TEST_BASE_BRANCH)
_run_git_cmd("branch", "-d", TEST_HEAD_BRANCH)
_run_git_cmd("push", "-d", "origin", TEST_HEAD_BRANCH)

0 comments on commit 9c090dc

Please sign in to comment.