~aleteoryx/muditaos

ref: a56e8dbe8e95ca9bea2eae7a5039ffcd876f3331 muditaos/tools/check_commit_messages.py -rwxr-xr-x 2.0 KiB
a56e8dbe — Lefucjusz [MOS-361] Fix improper fonts in navbar 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
'''
Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
'''

import os
from git import Repo
import re


def get_pull_request_commits():
  github_base_ref = os.environ['GITHUB_BASE_REF']
  github_head_ref = os.environ['GITHUB_HEAD_REF']
  try:
    is_fork = ("true" in os.environ['fork'])
  except:
    is_fork = False

  if is_fork:
    print("Checking fork against original repo!")
    pr_from_sha = os.environ['pr_from_sha']
    pr_to_sha = os.environ['pr_to_sha']
    return Repo('.').iter_commits(rev=f'{pr_to_sha}..{pr_from_sha}')

  return Repo('.').iter_commits(rev=f'origin/{github_base_ref}..origin/{github_head_ref}')


def validate_commit(commit):
  errors = []

  lines = commit.message.split('\n')

  if len(lines) < 3:
    errors.append(f'[{commit.hexsha}] commit message should be composed of at least 3 lines: a subject, an empty line and a body')
    return errors

  line_length_limit = 72
  if any(len(line) > line_length_limit for line in lines):
    errors.append(f'[{commit.hexsha}] maximum allowed line length is {line_length_limit}')

  subject = lines[0]
  empty_line = lines[1]
  body = ''.join(lines[2:]).strip()

  subject_format = r'^(\[(EGD|BH|CP|MOS)-\d+\])+ [A-Z].+[^.]$'
  if not re.match(subject_format, subject):
    errors.append(f'[{commit.hexsha}] invalid subject "{subject}", should match format "{subject_format}"')

  if empty_line:
    errors.append(f'[{commit.hexsha}] the second line in a commit message should be empty')

  if not body:
    errors.append(f"[{commit.hexsha}] message body can't be empty")

  return errors


def main():
  errors = []

  for commit in get_pull_request_commits():
    errors.extend(validate_commit(commit))

  for error in errors:
    print(error)

  if errors:
    print('See https://github.com/mudita/MuditaOS/blob/master/doc/development_workflow.md#commit-changes for the commit message format specification')
    exit(1)


if __name__ == '__main__':
  main()