c2cciutils 1.7.0.dev334__py3-none-any.whl → 1.7.0.dev338__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of c2cciutils might be problematic. Click here for more details.

c2cciutils/__init__.py CHANGED
@@ -11,7 +11,6 @@ import sys
11
11
  from re import Match, Pattern
12
12
  from typing import Any, Optional, TypedDict, cast
13
13
 
14
- import magic
15
14
  import requests
16
15
  import ruamel.yaml
17
16
 
@@ -126,8 +125,6 @@ def get_config() -> c2cciutils.configuration.Configuration:
126
125
 
127
126
  default_config = {
128
127
  "publish": publish_config,
129
- "pr-checks": c2cciutils.configuration.PULL_REQUEST_CHECKS_DEFAULT,
130
- "audit": c2cciutils.configuration.AUDIT_DEFAULT,
131
128
  }
132
129
  merge(default_config, config)
133
130
 
@@ -411,160 +408,6 @@ def graphql(query_file: str, variables: dict[str, Any], default: Any = None) ->
411
408
  return cast(dict[str, Any], json_response["data"])
412
409
 
413
410
 
414
- def get_git_files_mime(
415
- mime_type: Optional[list[str]] = None,
416
- extensions: Optional[list[str]] = None,
417
- ignore_patterns_re: Optional[list[str]] = None,
418
- ) -> list[str]:
419
- """
420
- Get list of paths from git with all the files that have the specified mime type.
421
-
422
- Arguments:
423
- mime_type: The considered MIME type
424
- extensions: The considered extensions
425
- ignore_patterns_re: A list of regular expressions of files that we should ignore
426
- """
427
- if mime_type is None:
428
- mime_type = ["text/x-python", "text/x-script.python"]
429
- if extensions is None:
430
- extensions = [".py"]
431
- ignore_patterns_compiled = [re.compile(p) for p in ignore_patterns_re or []]
432
- result = []
433
-
434
- for filename in subprocess.check_output(["git", "ls-files"]).decode().strip().split("\n"):
435
- if os.path.isfile(filename) and (
436
- os.path.splitext(filename)[1] in extensions or magic.from_file(filename, mime=True) in mime_type
437
- ):
438
- accept = True
439
- for pattern in ignore_patterns_compiled:
440
- if pattern.search(filename):
441
- accept = False
442
- break
443
- if accept:
444
- result.append(filename)
445
- return result
446
-
447
-
448
- def get_branch(branch: Optional[str], master_branch: str = "master") -> str:
449
- """
450
- Get the branch name.
451
-
452
- Arguments:
453
- branch: The forced to use branch name
454
- master_branch: The master branch name, can be used as default value
455
-
456
- Return the branch name
457
- """
458
- if branch is not None:
459
- return branch
460
- try:
461
- branch = (
462
- subprocess.run(["git", "rev-parse", "--abbrev-ref", "HEAD"], check=True, stdout=subprocess.PIPE)
463
- .stdout.decode()
464
- .strip()
465
- )
466
- except subprocess.CalledProcessError as exception:
467
- print(f"Error getting branch: {exception}")
468
- branch = "HEAD"
469
-
470
- if branch == "HEAD":
471
- branch = os.environ.get("GITHUB_HEAD_REF", master_branch)
472
- assert branch is not None
473
- return branch
474
-
475
-
476
- def get_based_on_master(
477
- repo: list[str],
478
- override_current_branch: Optional[str],
479
- master_branch: str,
480
- config: c2cciutils.configuration.Configuration,
481
- ) -> bool:
482
- """
483
- Check that we are not on a release branch (to avoid errors in versions check).
484
-
485
- This function will check the last 20 commits in current branch,
486
- and for each other branch (max 50) check if any commit in last 10 commits is the current one.
487
-
488
- Arguments:
489
- repo: The repository [<organization>, <name>]
490
- override_current_branch: The branch to use instead of the current one
491
- master_branch: The master branch name
492
- config: The full configuration
493
- """
494
- if os.environ.get("GITHUB_REF", "").startswith("refs/tags/"):
495
- # The tags are never consider as based on master
496
- return False
497
- current_branch = get_branch(override_current_branch, master_branch)
498
- if current_branch == master_branch:
499
- return True
500
- branches_re = compile_re(config["version"].get("branch_to_version_re", []))
501
- if does_match(current_branch, branches_re):
502
- return False
503
- if os.environ.get("GITHUB_BASE_REF"):
504
- return os.environ.get("GITHUB_BASE_REF") == master_branch
505
- commits_repository_json = graphql(
506
- "commits.graphql", {"name": repo[1], "owner": repo[0], "branch": current_branch}
507
- ).get("repository", {})
508
- commits_json = (
509
- commits_repository_json.get("ref", {}).get("target", {}).get("history", {}).get("nodes", [])
510
- if commits_repository_json.get("ref")
511
- else []
512
- )
513
- branches_json = [
514
- branch
515
- for branch in (
516
- graphql("branches.graphql", {"name": repo[1], "owner": repo[0]})["repository"]["refs"]["nodes"]
517
- )
518
- if branch["name"] != current_branch and does_match(branch["name"], branches_re)
519
- ]
520
- based_branch = master_branch
521
- found = False
522
- for commit in commits_json:
523
- for branch in branches_json:
524
- commits = [
525
- branch_commit
526
- for branch_commit in branch["target"]["history"]["nodes"]
527
- if commit["oid"] == branch_commit["oid"]
528
- ]
529
- if commits:
530
- based_branch = branch["name"]
531
- found = True
532
- break
533
- if found:
534
- break
535
- return based_branch == master_branch
536
-
537
-
538
- def get_codespell_command(config: c2cciutils.configuration.Configuration, fix: bool = False) -> list[str]:
539
- """
540
- Get the codespell command.
541
-
542
- Arguments:
543
- config: The full configuration
544
- fix: If we should fix the errors
545
- """
546
- codespell_config = config.get("codespell", {})
547
- codespell_config = codespell_config if isinstance(codespell_config, dict) else {}
548
- command = ["codespell"]
549
- if fix:
550
- command.append("--write-changes")
551
- for spell_ignore_file in (
552
- ".github/spell-ignore-words.txt",
553
- "spell-ignore-words.txt",
554
- ".spell-ignore-words.txt",
555
- ):
556
- if os.path.exists(spell_ignore_file):
557
- command.append(f"--ignore-words={spell_ignore_file}")
558
- break
559
- dictionaries = codespell_config.get(
560
- "internal_dictionaries", c2cciutils.configuration.CODESPELL_DICTIONARIES_DEFAULT
561
- )
562
- if dictionaries:
563
- command.append("--builtin=" + ",".join(dictionaries))
564
- command += codespell_config.get("arguments", c2cciutils.configuration.CODESPELL_ARGUMENTS_DEFAULT)
565
- return command
566
-
567
-
568
411
  def snyk_exec() -> tuple[str, dict[str, str]]:
569
412
  """Get the Snyk cli executable path."""
570
413
  if not os.path.exists(os.path.join(os.path.dirname(__file__), "node_modules")):
@@ -580,58 +423,3 @@ def snyk_exec() -> tuple[str, dict[str, str]]:
580
423
  subprocess.run(["snyk", "config", "set", f"org={env['SNYK_ORG']}"], check=True, env=env)
581
424
 
582
425
  return os.path.join(os.path.dirname(os.path.abspath(__file__)), "node_modules/snyk/bin/snyk"), env
583
-
584
-
585
- def create_pull_request_if_needed(
586
- current_branch: str,
587
- new_branch: str,
588
- commit_message: str,
589
- pull_request_extra_arguments: Optional[list[str]] = None,
590
- ) -> bool:
591
- """
592
- Create a pull request if there are changes.
593
- """
594
- if pull_request_extra_arguments is None:
595
- pull_request_extra_arguments = ["--fill"]
596
-
597
- diff_proc = subprocess.run(["git", "diff", "--quiet"]) # pylint: disable=subprocess-run-check
598
- if diff_proc.returncode != 0:
599
- print("::group::Diff")
600
- sys.stdout.flush()
601
- sys.stderr.flush()
602
- subprocess.run(["git", "diff"], check=True)
603
- print("::endgroup::")
604
-
605
- git_hash = subprocess.run(
606
- ["git", "rev-parse", "HEAD"], check=True, stdout=subprocess.PIPE, encoding="utf-8"
607
- ).stdout.strip()
608
- subprocess.run(["git", "checkout", "-b", new_branch], check=True)
609
- subprocess.run(["git", "add", "--all"], check=True)
610
- subprocess.run(["git", "commit", f"--message={commit_message}"], check=True)
611
- if os.environ.get("TEST") != "TRUE":
612
- subprocess.run(
613
- ["git", "push", "--force", "origin", new_branch],
614
- check=True,
615
- )
616
- env = os.environ.copy()
617
- if "GH_TOKEN" not in env:
618
- if "GITHUB_TOKEN" in env:
619
- env["GH_TOKEN"] = env["GITHUB_TOKEN"]
620
- else:
621
- env["GH_TOKEN"] = str(c2cciutils.gopass("gs/ci/github/token/gopass"))
622
- subprocess.run(
623
- [
624
- "gh",
625
- "pr",
626
- "create",
627
- f"--base={current_branch}",
628
- *pull_request_extra_arguments,
629
- ],
630
- check=True,
631
- env=env,
632
- )
633
- else:
634
- subprocess.run(["git", "reset", "--hard"], check=True)
635
- subprocess.run(["git", "checkout", git_hash], check=True)
636
-
637
- return diff_proc.returncode != 0