Skip to content

Commit 6e55f6c

Browse files
authored
Merge pull request #333 from online-judge-tools/remove-gitignore
Remove gitignore
2 parents d15e449 + 3d80612 commit 6e55f6c

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

onlinejudge_verify/main.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,50 @@ 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+
# checkout the target branch
216+
branch = os.environ['GITHUB_REF'][len('refs/heads/'):]
217+
logger.info('$ git checkout %s', branch)
218+
subprocess.check_call(['git', 'checkout', branch])
219+
220+
# check if .verify-helper/.gitignore exists
221+
gitignore_path = pathlib.Path('.verify-helper', '.gitignore')
222+
gitignore_checked_in = (subprocess.run(['git', 'ls-files', '--error-unmatch', str(gitignore_path)]).returncode == 0)
223+
if not gitignore_checked_in:
224+
return
225+
logger.warning('file %s exists in this Git repository. It should not be checked in.', str(gitignore_path))
226+
227+
# read config
228+
logger.info('use GITHUB_TOKEN') # NOTE: don't use GH_PAT here, because it may cause infinite loops with triggering GitHub Actions itself
229+
url = 'https://{}:{}@github.com/{}.git'.format(os.environ['GITHUB_ACTOR'], os.environ['GITHUB_TOKEN'], os.environ['GITHUB_REPOSITORY'])
230+
logger.info('GITHUB_ACTOR = %s', os.environ['GITHUB_ACTOR'])
231+
logger.info('GITHUB_REPOSITORY = %s', os.environ['GITHUB_REPOSITORY'])
232+
233+
# remove .verify-helper/.gitignore
234+
subprocess.check_call(['git', 'config', '--global', 'user.name', 'GitHub'])
235+
subprocess.check_call(['git', 'config', '--global', 'user.email', '[email protected]'])
236+
logger.info('$ git rm --cached %s', str(gitignore_path))
237+
subprocess.check_call(['git', 'rm', '--cached', str(gitignore_path)])
238+
message = '[auto-verifier] remove .verify-helper/.gitignore (see https://github.com/online-judge-tools/verification-helper/issues/332)'
239+
logger.info('$ git commit -m ...')
240+
subprocess.check_call(['git', 'commit', '-m', message])
241+
logger.info('$ git push ... HEAD')
242+
subprocess.check_call(['git', 'push', url, 'HEAD'])
243+
244+
except Exception:
245+
logger.exception('something wrong in _delete_gitignore(). ignored.')
246+
247+
204248
def main(args: Optional[List[str]] = None) -> None:
205249
# configure logging
206250
log_format = '%(log_color)s%(levelname)s%(reset)s:%(name)s:%(message)s'
@@ -220,6 +264,7 @@ def main(args: Optional[List[str]] = None) -> None:
220264
onlinejudge_verify.marker.get_verification_marker(jobs=parsed.jobs)
221265

222266
if parsed.subcommand == 'all':
267+
_delete_gitignore()
223268
generate_gitignore()
224269
summary = subcommand_run(paths=[], timeout=parsed.timeout, tle=parsed.tle, jobs=parsed.jobs)
225270
subcommand_docs(jobs=parsed.jobs)
@@ -228,13 +273,15 @@ def main(args: Optional[List[str]] = None) -> None:
228273
sys.exit(1)
229274

230275
elif parsed.subcommand == 'run':
276+
_delete_gitignore()
231277
generate_gitignore()
232278
summary = subcommand_run(paths=parsed.path, timeout=parsed.timeout, tle=parsed.tle, jobs=parsed.jobs)
233279
summary.show()
234280
if not summary.succeeded():
235281
sys.exit(1)
236282

237283
elif parsed.subcommand == 'docs':
284+
_delete_gitignore()
238285
generate_gitignore()
239286
subcommand_docs(jobs=parsed.jobs)
240287

0 commit comments

Comments
 (0)