Skip to content

Commit e846815

Browse files
committed
Remove .verify-helper/.gitignore if exists in users' git repository
1 parent d15e449 commit e846815

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

onlinejudge_verify/main.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,45 @@ def generate_gitignore() -> None:
201201
fh.write(data)
202202

203203

204+
# TODO: remove this function when affected people disappears. You can see the list of such people at https://github.com/search?q=%22timestamps.local.json%22+language%3Agitignore&type=Code
205+
def _delete_gitignore() -> None:
206+
"""A workaround for the issue https://github.com/online-judge-tools/verification-helper/issues/332
207+
"""
208+
209+
try:
210+
# check if it's on GitHub Action
211+
should_push = 'GITHUB_ACTION' in os.environ and 'GITHUB_TOKEN' in os.environ and os.environ.get('GITHUB_REF', '').startswith('refs/heads/')
212+
if not should_push:
213+
return
214+
215+
# check if .verify-helper/.gitignore exists
216+
gitignore_path = pathlib.Path('.verify-helper', '.gitignore')
217+
gitignore_checked_in = (subprocess.run(['git', 'ls-files', '--error-unmatch', str(gitignore_path)]).returncode == 0)
218+
if not gitignore_checked_in:
219+
return
220+
logger.warning('file %s exists in this Git repository. It should not be checked in.', str(gitignore_path))
221+
222+
# read config
223+
logger.info('use GITHUB_TOKEN') # NOTE: don't use GH_PAT here, because it may cause infinite loops with triggering GitHub Actions itself
224+
url = 'https://{}:{}@github.com/{}.git'.format(os.environ['GITHUB_ACTOR'], os.environ['GITHUB_TOKEN'], os.environ['GITHUB_REPOSITORY'])
225+
logger.info('GITHUB_ACTOR = %s', os.environ['GITHUB_ACTOR'])
226+
logger.info('GITHUB_REPOSITORY = %s', os.environ['GITHUB_REPOSITORY'])
227+
228+
# remove .verify-helper/.gitignore
229+
subprocess.check_call(['git', 'config', '--global', 'user.name', 'GitHub'])
230+
subprocess.check_call(['git', 'config', '--global', 'user.email', '[email protected]'])
231+
logger.info('$ git rm --cached %s', str(gitignore_path))
232+
subprocess.check_call(['git', 'rm', '--cached', str(gitignore_path)])
233+
message = '[auto-verifier] remove .verify-helper/.gitignore (see https://github.com/online-judge-tools/verification-helper/issues/332)'
234+
logger.info('$ git commit -m ...')
235+
subprocess.check_call(['git', 'commit', '-m', message])
236+
logger.info('$ git push ... HEAD')
237+
subprocess.check_call(['git', 'push', url, 'HEAD'])
238+
239+
except Exception:
240+
logger.exception('something wrong in _delete_gitignore(). ignored.')
241+
242+
204243
def main(args: Optional[List[str]] = None) -> None:
205244
# configure logging
206245
log_format = '%(log_color)s%(levelname)s%(reset)s:%(name)s:%(message)s'
@@ -220,6 +259,7 @@ def main(args: Optional[List[str]] = None) -> None:
220259
onlinejudge_verify.marker.get_verification_marker(jobs=parsed.jobs)
221260

222261
if parsed.subcommand == 'all':
262+
_delete_gitignore()
223263
generate_gitignore()
224264
summary = subcommand_run(paths=[], timeout=parsed.timeout, tle=parsed.tle, jobs=parsed.jobs)
225265
subcommand_docs(jobs=parsed.jobs)
@@ -228,13 +268,15 @@ def main(args: Optional[List[str]] = None) -> None:
228268
sys.exit(1)
229269

230270
elif parsed.subcommand == 'run':
271+
_delete_gitignore()
231272
generate_gitignore()
232273
summary = subcommand_run(paths=parsed.path, timeout=parsed.timeout, tle=parsed.tle, jobs=parsed.jobs)
233274
summary.show()
234275
if not summary.succeeded():
235276
sys.exit(1)
236277

237278
elif parsed.subcommand == 'docs':
279+
_delete_gitignore()
238280
generate_gitignore()
239281
subcommand_docs(jobs=parsed.jobs)
240282

0 commit comments

Comments
 (0)