Skip to content

Instantly share code, notes, and snippets.

@tobiasraabe
Last active February 4, 2023 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobiasraabe/d4e4b0945a1c63a2ddf4e0874b46febf to your computer and use it in GitHub Desktop.
Save tobiasraabe/d4e4b0945a1c63a2ddf4e0874b46febf to your computer and use it in GitHub Desktop.
Examples for commands with all-repos

all-repos-examples

Here are some examples which show how to use all-repos. Maybe you can also draw inspiration from all-repos-incantations.

Adding skip for concurrent builds to github.yml

Add the following to your continuous integration workflow after the definition of the name (here "name: Continuous Integration Workflow")

concurrency:
  group: ${{ github.head_ref || github.run_id }}
  cancel-in-progress: true
$ all-repos-sed --interactive --branch-name skip-concurrent-builds --commit-msg "Skip concurrent CI builds" \
  '/name: Continuous Integration Workflow/a\\n# Automatically cancel a previous run.\nconcurrency:\n  group: ${{ github.head_ref || github.run_id }}\n  cancel-in-progress: true\n' \
  **/*/main.yml

Deprecating Python 3.6

$ all-repos-sed --dry-run --branch-name deprecate-py36 --commit-msg "Deprecate Python 3.6." \
  's/python_requires = >=3.6.*/python_requires = >=3.7/;s/--py36-plus/--py37-plus/;/    -   id: reorder-python-imports/a\        args: [--py37-plus, --add-import, '"'"'from __future__ import annotations'"'"']' . \
  --repos $(all-repos-grep  --repos -E 'python_requires = >=3.6' -- .)

Adding Python 3.11 to CI

$ all-repos-sed --branch-name py311 --commit-msg "Add Python 3.11 to CI." "s/'3.10'\]/'3.10', '3.11'\]/" **/*/main.yml

Adding a dependabot.yml for updating actions

from __future__ import annotations
from pathlib import Path
import argparse
import functools
import re
import subprocess
import tempfile
from typing import Sequence

from all_repos import autofix_lib
from all_repos.config import Config
from all_repos.grep import repos_matching


def find_repos(config: Config) -> set[str]:
    query = ("--", ".*\.yml")
    return repos_matching(config, query)


_CONFIGURATION = """\
version: 2

updates:
  - package-ecosystem: "github-actions"
    directory: "/"
    schedule:
      interval: "weekly"
"""


def apply_fix() -> None:
    # Add the configuration file.
    with open(".github/dependabot.yml", "w") as f:
        f.write(_CONFIGURATION)
        
    subprocess.run(("git", "add", ".github/dependabot.yml"), check=True)

    # Update some tags manually to avoid follow-up PRs.
    path = Path(".github/workflows/main.yml")
    text = (
        path.read_text()
        .replace("uses: actions/setup-python@v1", "uses: actions/setup-python@v4")
        .replace("- uses: actions/checkout@v2", "- uses: actions/checkout@v3")
    )
    path.write_text(text)

    path = Path(".github/workflows/publish-to-pypi.yml")
    text = (
        path.read_text()
        .replace("uses: actions/setup-python@v1", "uses: actions/setup-python@v4")
        .replace("- uses: actions/checkout@master", "- uses: actions/checkout@v3")
    )
    path.write_text(text)


def main(argv: Sequence[str] | None = None) -> int:
    parser = argparse.ArgumentParser()
    autofix_lib.add_fixer_args(parser)
    args = parser.parse_args(argv)

    repos, config, commit, autofix_settings = autofix_lib.from_cli(
        args,
        find_repos=find_repos,
        msg="Add dependabot for github actions.",
        branch_name="dependabot-github-actions",
    )

    autofix_lib.fix(
        repos,
        apply_fix=apply_fix,
        config=config,
        commit=commit,
        autofix_settings=autofix_settings,
    )
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment