Skip to content

Commit

Permalink
Enable configuration loading from meta repositories (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Aug 23, 2020
1 parent f753e5f commit 2c14136
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 89 deletions.
41 changes: 0 additions & 41 deletions .github/labels.yml

This file was deleted.

28 changes: 0 additions & 28 deletions .github/release-drafter.yml

This file was deleted.

15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ Example of activities performed:

![screenshot](https://sbarnea.com/ss/Screen-Shot-2020-08-16-21-23-29.23.png)

## Configuration

By default the file tries to load the configuration files from local reposity
and falls back to "meta" repository from the same organization. If these are
also missing it will load them from [pycontribs/meta](https://github.com/pycontribs/meta/tree/master/.github)
repository.

Recognized config files:

- [`.github/lables.yml`](https://github.com/pycontribs/meta/blob/master/.github/labels.yml)
- [`.release-drafter.yml`](https://github.com/pycontribs/meta/blob/master/.github/release-drafter.yml)

The file format is the same as the original tools, as tender is seen as a
drop-in replacements for these.

## Background

The project was inspired by existing tools like:
Expand Down
63 changes: 43 additions & 20 deletions tender/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import json
import logging
import os
import re
import sys
import urllib
from copy import deepcopy
from types import SimpleNamespace
from typing import Dict, List, Optional, Set
Expand Down Expand Up @@ -55,29 +57,59 @@ class Config(SimpleNamespace):
"""Config."""

def __init__(self, **kwargs):
self.org = None
self.repo = None

self.__dict__.update(kwargs)

if not self.org or not self.repo:
url = git.repo.base.Repo().remotes.origin.url
gitrepo = giturlparse.parse(url)
self.org = gitrepo.owner
self.repo = gitrepo.name
# it.repo.fun.is_git_dir(".")
_logger.info("Detected %s/%s from context %s", self.org, self.repo, url)

self.labels = {}
for label in self.load_config(".github/labels.yml"):
self.labels[label["name"]] = SimpleNamespace(
color=label["color"], description=label["description"]
)
self.release_drafter = self.load_config(".github/release-drafter.yml")

@classmethod
def load_config(cls, config_file):
config_file = os.path.expanduser(config_file)
try:
with open(config_file, "r") as stream:
def load_config(self, config_file):
def unique(sequence):
seen = set()
return [x for x in sequence if not (x in seen or seen.add(x))]

result = None
for location in unique(
[
os.path.expanduser(config_file),
f"https://raw.githubusercontent.com/{self.org}/meta/master/{config_file}",
f"https://raw.githubusercontent.com/pycontribs/meta/master/{config_file}",
]
):

try:
if re.match(r"https?://", location):
stream = urllib.request.urlopen(location)
else:
stream = open(location, "r")
try:
return yaml.safe_load(stream)
result = yaml.safe_load(stream)
except yaml.YAMLError as exc:
_logger.error(exc)
sys.exit(2)
except FileNotFoundError:
_logger.warning(
"Config file %s not found, defaulting to empty.", config_file
)
return {}
except urllib.error.HTTPError as error:
_logger.info("Config file %s not loaded due to %s", location, error)
continue
except FileNotFoundError:
_logger.info("Config file %s not found", location)
continue
_logger.info("Loaded %s", location)
return result
raise NotImplementedError("Unable to load any configuration file")


class Tender:
Expand All @@ -92,15 +124,6 @@ def __init__(self, cfg: Config):
self.required_labels: Set[str] = set()
self.errors: List[str] = []

if not self.cfg.org or not self.cfg.repo:
url = git.repo.base.Repo().remotes.origin.url
gitrepo = giturlparse.parse(url)
self.cfg.org = gitrepo.owner
self.cfg.repo = gitrepo.name
# it.repo.fun.is_git_dir(".")
_logger.info(
"Detected %s/%s from context %s", self.cfg.org, self.cfg.repo, url
)
self.repo = self.github.get_repo(f"{self.cfg.org}/{self.cfg.repo}")
self.pulls = self.repo.get_pulls(state="all")

Expand Down

0 comments on commit 2c14136

Please sign in to comment.