Skip to content

Commit b8719ab

Browse files
committed
Add changesemail and adapt changesgenerate behavior
Modify default behavior of changesgenerate to add packager's name AND email by default, instead of only the former, in the generated changes entry header. This also adds changesemail, a new parameter for _service files, that must be used along with changesauthor whenever configuring them manually is needed. Signed-off-by: Luciano Santos <[email protected]>
1 parent 23a15f8 commit b8719ab

File tree

7 files changed

+143
-43
lines changed

7 files changed

+143
-43
lines changed

TarSCM/changes.py

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def write_changes_revision(self, url, outdir, new_revision):
176176
if changed:
177177
xml_tree.write(os.path.join(outdir, "_servicedata"))
178178

179-
def write_changes(self, changes_filename, changes, version, author):
179+
def write_changes(self, changes_filename, changes, version, author, email):
180180
"""Add changes to given *.changes file."""
181181
if changes is None:
182182
logging.debug(
@@ -196,7 +196,7 @@ def write_changes(self, changes_filename, changes, version, author):
196196
dtime = datetime.datetime.utcnow().strftime('%a %b %d %H:%M:%S UTC %Y')
197197

198198
text = '-' * 67 + '\n'
199-
text += "%s - %s\n" % (dtime, author)
199+
text += "%s - %s <%s>\n" % (dtime, author, email)
200200
text += '\n'
201201
text += "- Update to version %s:\n" % version
202202
for line in changes:
@@ -231,25 +231,71 @@ def get_changesauthor(self, args):
231231
return args.changesauthor
232232

233233
# return changesauthor if set by osc
234-
if os.getenv('VC_MAILADDR'):
235-
logging.debug("Found changesauthor in VC_MAILADDR='%s'",
236-
os.environ['VC_MAILADDR'])
237-
return os.environ['VC_MAILADDR']
234+
if os.getenv('VC_REALNAME'):
235+
logging.debug("Found changesauthor in VC_REALNAME='%s'",
236+
os.environ['VC_REALNAME'])
237+
return os.environ['VC_REALNAME']
238238

239239
# return default changesauthor if running on server side
240240
if os.getenv('OBS_SERVICE_DAEMON'):
241241
logging.debug("Running in daemon mode. Using DEFAULT_AUHTOR='%s'",
242242
Cli.DEFAULT_AUTHOR)
243243
return Cli.DEFAULT_AUTHOR
244244

245-
# exit if running locally (non server mode) and now changesauthor
246-
# could be determined
245+
# exit if running locally (non server mode) and no changesauthor could
246+
# be determined, hint user on what to do
247+
raise SystemExit(
248+
"""No 'changesauthor' has been defined! You can do it by:\n\n"""
249+
"""Configuring:\n"""
250+
""" * The 'realname' entry for the default API section """
251+
"""(api.opensuse.org) -- or whatever OBS\n"""
252+
""" instance you're working with -- in your OSCRC file """
253+
"""with:\n\n"""
254+
""" osc config "https://api.opensuse.org" realname John """
255+
""""Doe"\n\n"""
256+
""" * The '<param name="changesauthor">John Doe</param> tag """
257+
"""nested under the obs_scm/tar_scm\n"""
258+
""" service tag in your _service file.\n\n"""
259+
"""Passing:\n"""
260+
""" * '--changesauthor "John Doe"' on the CLI, when using """
261+
"""the obs_scm/tar_scm script manually.\n"""
262+
)
263+
264+
def get_changesemail(self, args):
265+
# return changesemail if given as cli option
266+
if args.changesemail:
267+
logging.debug("Found changesemail in args.changesemail='%s'",
268+
args.changesemail)
269+
return args.changesemail
270+
271+
# return changesemail if set by osc
272+
if os.getenv('VC_MAILADDR'):
273+
logging.debug("Found changesemail in VC_MAILADDR='%s'",
274+
os.environ['VC_MAILADDR'])
275+
return os.environ['VC_MAILADDR']
276+
277+
# return default changesemail if running on server side
278+
if os.getenv('OBS_SERVICE_DAEMON'):
279+
logging.debug("Running in daemon mode. Using DEFAULT_EMAIL='%s'",
280+
Cli.DEFAULT_EMAIL)
281+
return Cli.DEFAULT_EMAIL
282+
283+
# exit if running locally (non server mode) and no changesemail could
284+
# be determined, hint user on what to do
247285
raise SystemExit(
248-
"""No changesauthor defined!\n"""
249-
"""You can define it by:\n"""
250-
""" * configure 'email=' in ~/.config/osc/oscrc """
251-
"""in your default api section\n"""
252-
""" * configure <param name="changesauthor">"""
253-
"""...</param> in your _service file\n"""
254-
""" * using '--changesauthor' on the cli\n"""
286+
"""No 'changesemail' has been defined! You can do it by:\n\n"""
287+
"""Configuring:\n"""
288+
""" * The 'email' entry for the default API section """
289+
"""(api.opensuse.org) -- or whatever OBS\n"""
290+
""" instance you're working with -- in your OSCRC file """
291+
"""with:\n\n"""
292+
""" osc config "https://api.opensuse.org" email """
293+
""""[email protected]"\n\n"""
294+
""" * The '<param name="changesemail">[email protected]"""
295+
"""</param> tag nested under the\n"""
296+
""" obs_scm/tar_scm service tag in your _service file.\n\n"""
297+
"""Passing:\n"""
298+
""" * '--changesemail "[email protected]"' on the CLI, when """
299+
"""using the obs_scm/tar_scm script\n"""
300+
""" manually.\n"""
255301
)

TarSCM/cli.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ def check_locale(loc):
4545
class Cli():
4646
# pylint: disable=too-few-public-methods
4747
# pylint: disable=too-many-instance-attributes
48-
DEFAULT_AUTHOR = 'obs-service-tar-scm@invalid'
48+
DEFAULT_AUTHOR = 'service_run'
49+
DEFAULT_EMAIL = 'obs-service-tar-scm@invalid'
4950
outdir = None
5051

5152
def __init__(self):
@@ -120,10 +121,15 @@ def parse_args(self, options):
120121
'parent revision (see changesrevision).')
121122
parser.add_argument('--changesauthor',
122123
help='The author of the changes file entry to be '
123-
'written, defaults to first email entry in '
124-
'osc rc files or "%s" '
125-
'if there is no ~/.oscrc found.' %
124+
'written. Defaults to VC_REALNAME env '
125+
'variable, set by osc. Or "%s", otherwise.' %
126126
self.DEFAULT_AUTHOR)
127+
parser.add_argument('--changesemail',
128+
help='The author\'s email of the changes file '
129+
'entry to be written. Defaults to VC_MAILADDR'
130+
' env variable, set by osc. Or "%s", '
131+
'otherwise.' %
132+
self.DEFAULT_EMAIL)
127133
parser.add_argument('--subdir', default='',
128134
help='Package just a subdirectory of the sources')
129135
parser.add_argument('--submodules',

TarSCM/tasks.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,9 @@ def _dstname(self, scm_object, version):
283283

284284
def _process_changes(self, args, ver, changesversion, detected_changes):
285285
changesauthor = self.changes.get_changesauthor(args)
286+
changesemail = self.changes.get_changesemail(args)
286287

287-
logging.debug("AUTHOR: %s", changesauthor)
288+
logging.debug("AUTHOR <EMAIL>: %s <%s>", changesauthor, changesemail)
288289

289290
if not ver:
290291
args.version = "_auto_"
@@ -296,7 +297,9 @@ def _process_changes(self, args, ver, changesversion, detected_changes):
296297
shutil.copy(filename, new_changes_file)
297298
self.changes.write_changes(new_changes_file,
298299
detected_changes['lines'],
299-
changesversion, changesauthor)
300+
changesversion,
301+
changesauthor,
302+
changesemail)
300303
self.changes.write_changes_revision(args.url, args.outdir,
301304
detected_changes['revision'])
302305

tar_scm.service.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,10 @@ which get maintained in the SCM. Can be used multiple times.</description>
195195
<allowedvalue>disable</allowedvalue>
196196
</parameter>
197197
<parameter name="changesauthor">
198-
<description>Specify author of the changes file entry to be written. Defaults to first email entry in ~/.oscrc, or "obs-service-tar-scm@invalid" if there is no .oscrc found.</description>
198+
<description>Specify author of the changes file entry to be written. Defaults to VC_REALNAME environment variable, set by osc, or "service_run" otherwise.</description>
199+
</parameter>
200+
<parameter name="changesemail">
201+
<description>Specify author's email of the changes file entry to be written. Defaults to VC_MAILADDR environment variable, set by osc, or "obs-service-tar-scm@invalid" otherwise.</description>
199202
</parameter>
200203
<parameter name="locale">
201204
<description>DEPRECATED - Please use "encoding" instead. Set locale while execution of service</description>

tests/gitsvntests.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class GitSvnTests(CommonTests):
1717

1818
def _tar_scm_changesgenerate(self, mode, **kwargs):
1919
self.tar_scm_std(
20-
'--changesauthor', '[email protected]',
20+
'--changesauthor', 'spam',
21+
'--changesemail', '[email protected]',
2122
'--changesgenerate', mode,
2223
**kwargs
2324
)
@@ -90,13 +91,13 @@ def test_changesgenerate_new_commit_no_changes_file(self): # pylint: disable=C0
9091
self._tar_scm_changesgenerate('enable')
9192
self._check_servicedata(revision=3)
9293

93-
def _new_change_entry_regexp(self, author, changes): # pylint: disable=R0201
94+
def _new_change_entry_regexp(self, author, email, changes): # pylint: disable=R0201
9495
return textwrap.dedent("""\
9596
^-------------------------------------------------------------------
96-
\w{3} \w{3} [ \d]\d \d\d:\d\d:\d\d [A-Z]{3} 20\d\d - %s
97+
\w{3} \w{3} [ \d]\d \d\d:\d\d:\d\d [A-Z]{3} 20\d\d - %s <%s>
9798
9899
%s
99-
""") % (author, changes)
100+
""") % (author, email, changes)
100101

101102
def _check_changes(self, orig_changes, expected_changes_regexp):
102103
new_changes_file = os.path.join(self.outdir, 'pkg.changes')
@@ -129,7 +130,7 @@ def _write_servicedata(self, rev):
129130
</service>
130131
</servicedata>""" % (self.fixtures.repo_url, self.changesrevision(rev))))
131132

132-
def _test_changesgenerate_new_commit_and_changes_file(self, author=None): # pylint: disable=C0103
133+
def _test_changesgenerate_new_commit_and_changes_file(self, author=None, email=None): # pylint: disable=C0103
133134
self._write_servicedata(2)
134135
orig_changes = self._write_changes_file()
135136
self.fixtures.create_commits(3)
@@ -138,7 +139,10 @@ def _test_changesgenerate_new_commit_and_changes_file(self, author=None): # pyl
138139
tar_scm_args = self.tar_scm_args()
139140

140141
if author is not None:
141-
tar_scm_args += ['--changesauthor', self.fixtures.user_email]
142+
tar_scm_args += ['--changesauthor', self.fixtures.user_name]
143+
144+
if email is not None:
145+
tar_scm_args += ['--changesemail', self.fixtures.user_email]
142146

143147
print("XXXX 2")
144148
self.tar_scm_std(*tar_scm_args)
@@ -149,9 +153,11 @@ def _test_changesgenerate_new_commit_and_changes_file(self, author=None): # pyl
149153
rev = self.changesrevision(rev, abbrev=True)
150154

151155
print("XXXX 4")
152-
expected_author = author or 'obs-service-tar-scm@invalid'
156+
expected_author = author or 'geeko'
157+
expected_email = email or 'obs-service-tar-scm@invalid'
153158
expected_changes_regexp = self._new_change_entry_regexp(
154159
expected_author,
160+
expected_email,
155161
textwrap.dedent("""\
156162
- Update to version 0.6.%s:
157163
\* 5
@@ -170,7 +176,8 @@ def test_changesgenerate_new_commit_and_changes_file_no_version(self): # pylint
170176
tar_scm_args = [
171177
'--changesgenerate', 'enable',
172178
'--version', '',
173-
'--changesauthor', self.fixtures.user_email
179+
'--changesauthor', self.fixtures.user_name,
180+
'--changesemail', self.fixtures.user_email
174181
]
175182
self.tar_scm_std(*tar_scm_args)
176183

@@ -179,9 +186,11 @@ def test_changesgenerate_new_commit_and_changes_file_no_version(self): # pylint
179186
rev = self.changesrevision(rev, abbrev=True)
180187
ver_regex = self.changesregex(rev)
181188

182-
expected_author = self.fixtures.user_email
189+
expected_author = self.fixtures.user_name
190+
expected_email = self.fixtures.user_email
183191
expected_changes_regexp = self._new_change_entry_regexp(
184192
expected_author,
193+
expected_email,
185194
textwrap.dedent("""\
186195
- Update to version %s:
187196
\* 5
@@ -202,16 +211,19 @@ def test_changesgenerate_new_commit_and_changes_file_with_subdir(self): # pyli
202211

203212
tar_scm_args += [
204213
'--subdir', 'another_subdir',
205-
'--changesauthor', self.fixtures.user_email,
214+
'--changesauthor', self.fixtures.user_name,
215+
'--changesemail', self.fixtures.user_email
206216
]
207217

208218
self.tar_scm_std(*tar_scm_args)
209219

210220
self._check_servicedata(revision=rev, expected_dirents=3)
211221

212-
expected_author = self.fixtures.user_email
222+
expected_author = self.fixtures.user_name
223+
expected_email = self.fixtures.user_email
213224
expected_changes_regexp = self._new_change_entry_regexp(
214225
expected_author,
226+
expected_email,
215227
textwrap.dedent("""\
216228
- Update to version 0.6.%s:
217229
\* 8
@@ -236,16 +248,19 @@ def test_changesgenerate_old_servicedata(self): # pylint: disable=C0103
236248
tar_scm_args = self.tar_scm_args()
237249

238250
tar_scm_args += [
239-
'--changesauthor', self.fixtures.user_email,
251+
'--changesauthor', self.fixtures.user_name,
252+
'--changesemail', self.fixtures.user_email
240253
]
241254

242255
self.tar_scm_std(*tar_scm_args)
243256

244257
self._check_servicedata(revision=rev, expected_dirents=3)
245258

246-
expected_author = self.fixtures.user_email
259+
expected_author = self.fixtures.user_name
260+
expected_email = self.fixtures.user_email
247261
expected_changes_regexp = self._new_change_entry_regexp(
248262
expected_author,
263+
expected_email,
249264
textwrap.dedent("""\
250265
- Update to version 0.6.%s:
251266
\* 5

tests/gittests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,9 @@ def test_changesgenerate_unicode(self):
326326
tar_scm_args = self.tar_scm_args()
327327

328328
tar_scm_args += [
329-
'--changesauthor', self.fixtures.user_email,
329+
'--changesauthor', self.fixtures.user_name,
330+
'--changesemail', self.fixtures.user_email
330331
]
331-
332332
self.tar_scm_std(*tar_scm_args)
333333

334334
self._check_servicedata(revision=rev, expected_dirents=3)

tests/unittestcases.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,17 @@ def test_changes_get_chga_args(self):
133133
'''Test if getting changesauthor from cli args works'''
134134
chg = Changes()
135135
cli = copy.copy(self.cli)
136-
cli.changesauthor = '[email protected]'
136+
cli.changesauthor = 'geeko'
137137
author = chg.get_changesauthor(cli)
138-
self.assertEqual(author, '[email protected]')
138+
self.assertEqual(author, 'geeko')
139139

140140
def test_changes_get_chga_oscrc(self):
141141
'''Test if getting changesauthor from .oscrc works'''
142-
os.environ["VC_MAILADDR"] = '[email protected]'
142+
os.environ["VC_REALNAME"] = 'geeko'
143143
chg = Changes()
144144
author = chg.get_changesauthor(self.cli)
145-
self.assertEqual(author, '[email protected]')
146-
os.environ["VC_MAILADDR"] = ''
145+
self.assertEqual(author, 'geeko')
146+
os.environ["VC_REALNAME"] = ''
147147

148148
def test_changes_get_chga_default(self):
149149
'''Test if getting default changesauthor if running inside OBS'''
@@ -153,7 +153,34 @@ def test_changes_get_chga_default(self):
153153
chg = Changes()
154154
author = chg.get_changesauthor(self.cli)
155155
os.environ['HOME'] = home
156-
self.assertEqual(author, 'obs-service-tar-scm@invalid')
156+
self.assertEqual(author, 'geeko')
157+
os.environ['OBS_SERVICE_DAEMON'] = "0"
158+
159+
def test_changes_get_chge_args(self):
160+
'''Test if getting changesemail from cli args works'''
161+
chg = Changes()
162+
cli = copy.copy(self.cli)
163+
cli.changesemail = '[email protected]'
164+
email = chg.get_changesemail(cli)
165+
self.assertEqual(email, '[email protected]')
166+
167+
def test_changes_get_chge_oscrc(self):
168+
'''Test if getting changesemail from osc works'''
169+
os.environ["VC_MAILADDR"] = '[email protected]'
170+
chg = Changes()
171+
email = chg.get_changesemail(self.cli)
172+
self.assertEqual(email, '[email protected]')
173+
os.environ["VC_MAILADDR"] = ''
174+
175+
def test_changes_get_chge_default(self):
176+
'''Test if getting default changesemail if running inside OBS'''
177+
os.environ['OBS_SERVICE_DAEMON'] = "1"
178+
home = os.environ['HOME']
179+
os.environ['HOME'] = '/nir/va/na'
180+
chg = Changes()
181+
email = chg.get_changesemail(self.cli)
182+
os.environ['HOME'] = home
183+
self.assertEqual(email, 'obs-service-tar-scm@invalid')
157184
os.environ['OBS_SERVICE_DAEMON'] = "0"
158185

159186
def test_git_repoc_hash_wo_subdir(self):

0 commit comments

Comments
 (0)