ci_get_mr_info.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env python
  2. #
  3. # internal use only for CI
  4. # get latest MR information by source branch
  5. #
  6. # Copyright 2020 Espressif Systems (Shanghai) PTE LTD
  7. #
  8. # Licensed under the Apache License, Version 2.0 (the "License");
  9. # you may not use this file except in compliance with the License.
  10. # You may obtain a copy of the License at
  11. #
  12. # http://www.apache.org/licenses/LICENSE-2.0
  13. #
  14. # Unless required by applicable law or agreed to in writing, software
  15. # distributed under the License is distributed on an "AS IS" BASIS,
  16. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. # See the License for the specific language governing permissions and
  18. # limitations under the License.
  19. #
  20. import argparse
  21. import os
  22. import subprocess
  23. from gitlab_api import Gitlab
  24. try:
  25. from typing import Any, Union
  26. except ImportError:
  27. # Only used for type annotations
  28. pass
  29. def _get_mr_obj(source_branch): # type: (str) -> Union[Gitlab, None]
  30. if not source_branch:
  31. return None
  32. gl = Gitlab(os.getenv('CI_PROJECT_ID', 'espressif/esp-idf'))
  33. if not gl.project:
  34. return None
  35. mrs = gl.project.mergerequests.list(state='opened', source_branch=source_branch)
  36. if mrs:
  37. return mrs[0] # one source branch can only have one opened MR at one moment
  38. else:
  39. return None
  40. def get_mr_iid(source_branch): # type: (str) -> str
  41. mr = _get_mr_obj(source_branch)
  42. if not mr:
  43. return ''
  44. else:
  45. return str(mr.iid)
  46. def get_mr_changed_files(source_branch): # type: (str) -> Any
  47. mr = _get_mr_obj(source_branch)
  48. if not mr:
  49. return ''
  50. return subprocess.check_output(['git', 'diff', '--name-only',
  51. 'origin/{}...origin/{}'.format(mr.target_branch, source_branch)]).decode('utf8')
  52. def get_mr_commits(source_branch): # type: (str) -> str
  53. mr = _get_mr_obj(source_branch)
  54. if not mr:
  55. return ''
  56. return '\n'.join([commit.id for commit in mr.commits()])
  57. if __name__ == '__main__':
  58. parser = argparse.ArgumentParser(description='Get the latest merge request info by pipeline')
  59. actions = parser.add_subparsers(dest='action', help='info type')
  60. common_args = argparse.ArgumentParser(add_help=False)
  61. common_args.add_argument('src_branch', nargs='?', help='source branch')
  62. actions.add_parser('id', parents=[common_args])
  63. actions.add_parser('files', parents=[common_args])
  64. actions.add_parser('commits', parents=[common_args])
  65. args = parser.parse_args()
  66. if args.action == 'id':
  67. print(get_mr_iid(args.src_branch))
  68. elif args.action == 'files':
  69. print(get_mr_changed_files(args.src_branch))
  70. elif args.action == 'commits':
  71. print(get_mr_commits(args.src_branch))
  72. else:
  73. raise NotImplementedError('not possible to get here')