Skip to content

Commit df5b7cd

Browse files
authored
Merge pull request #59 from facelessuser/feature/rework-template
Rework template config to be looked for in the local config
2 parents 0863874 + f10ba0c commit df5b7cd

File tree

3 files changed

+39
-46
lines changed

3 files changed

+39
-46
lines changed

.github/labels.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Wildcard labels
22

3+
template: 'facelessuser:master-labels:labels.yml:master'
4+
35
brace_expansion: true
46

57
rules:

README.md

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,28 +60,25 @@ For pull requests, the configuration file in the pull gets used except when issu
6060
All commands issued in the body of a pull request, issue, or comment in an issue or pull request are restricted to
6161
owners and collaborators.
6262

63-
## Using a Master Configuration Template
64-
65-
You can use a master configuration file and share it across repositories. This is a good way to define common labels
66-
that you wish to reuse. Point to the master configuration file by setting the environmental variable `GH_CONFIG` on your
67-
server. It should be in the form `user:repo:path/to/config:ref`. In our case it is
68-
`facelessuser:master-labels:labels.yml:master`.
69-
70-
When merging the master configuration and the local repo configuration, merging will occur as follows:
71-
72-
- keys that contain string or bool will be override the master by the local.
73-
- keys that contain lists will append the local list to the master list.
74-
- keys that contain hashes will append the key value pair of the local to the master. In the case of duplicates, the
75-
local will override.
76-
- One exception is with `lgtm_add`. The keys `pull_request` and `issue` will append values from the local to the master.
77-
In the future, `lgtm_add` may get broken up into two separate options for consistency. This would not occur until
78-
version 2.0.
79-
80-
Even with a master configuration file, you still must specify a `.github/labels.yml` file in your repository, even if
81-
all it contains is an empty hash `{}`. If you specify a master configuration, if either the master or local
82-
configuration file fails, an empty set of options will be returned. Since repository label syncing will not occur when
83-
the `labels` option is missing, this will prevent all your repository labels from getting wiped out in the case of a
84-
failure.
63+
## Using a Configuration Template
64+
65+
You can use a configuration template file and share it across repositories. This is a good way to define common labels
66+
that you wish to reuse. You can set the template to use in your local repository file in `.github/labels.yml`. In our
67+
case it is `facelessuser:master-labels:labels.yml:master`.
68+
69+
When merging the template configuration and the local repo configuration, merging will occur as follows:
70+
71+
- keys that contain string or bool will override the template with the local value.
72+
- keys that contain list values will append the local list to the template list.
73+
- keys that contain hash values will append the key value pair of the local to the template. In the case of duplicates,
74+
the local will override.
75+
- One exception is with `lgtm_add`. The keys `pull_request` and `issue` will append values from the local to the
76+
template. In the future, `lgtm_add` may get broken up into two separate options for consistency. This would not occur
77+
until version 2.0.
78+
79+
If either the template or local configuration file fails to be acquired, an empty set of options will be returned. Since
80+
repository label syncing will not occur when the `labels` option is missing, this will prevent all your repository
81+
labels from getting wiped out in the case of a failure.
8582

8683
## Triage Labels
8784

label_bot/util.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import yaml
55
import traceback
66
import sys
7-
import os
87
from gidgethub import sansio
98
try:
109
from yaml import CLoader as Loader
@@ -114,28 +113,8 @@ async def get_config(self, gh):
114113
"""Get label configuration file."""
115114

116115
await asyncio.sleep(1)
117-
master_config = {}
118-
master = os.environ.get("GH_CONFIG", '')
119-
if master:
120-
try:
121-
user, repo, path, ref = master.split(':')
122-
result = await gh.getitem(
123-
'https://api.github.com/repos/{user}/{repo}/contents/{path}/{?ref}',
124-
{
125-
'user': user,
126-
'repo': repo,
127-
'path': path,
128-
'ref': ref
129-
}
130-
)
131-
content = base64.b64decode(result['content']).decode('utf-8')
132-
master_config = yaml.load(content, Loader=Loader)
133-
except Exception:
134-
traceback.print_exc(file=sys.stdout)
135-
# Return an empty config which will cause label sync not to run
136-
return master_config
137-
138116
config = {}
117+
template_config = {}
139118
try:
140119
result = await gh.getitem(
141120
self.contents_url,
@@ -146,8 +125,23 @@ async def get_config(self, gh):
146125
)
147126
content = base64.b64decode(result['content']).decode('utf-8')
148127
config = yaml.load(content, Loader=Loader)
128+
template = config.get('template', '')
129+
if template:
130+
user, repo, path, ref = template.split(':')
131+
result = await gh.getitem(
132+
'https://api.github.com/repos/{user}/{repo}/contents/{path}/{?ref}',
133+
{
134+
'user': user,
135+
'repo': repo,
136+
'path': path,
137+
'ref': ref
138+
}
139+
)
140+
content = base64.b64decode(result['content']).decode('utf-8')
141+
template_config = yaml.load(content, Loader=Loader)
149142
except Exception:
150143
traceback.print_exc(file=sys.stdout)
151-
return config
144+
# Return an empty configuration if anything went wrong
145+
return template_config
152146

153-
return self.merge_config(master_config, config)
147+
return self.merge_config(template_config, config)

0 commit comments

Comments
 (0)