-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgit_helper.py
111 lines (91 loc) · 3.81 KB
/
git_helper.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
from plumbum.cmd import git
from pathlib import PurePath
from os import path,environ
import json
from scripts.utils import store_var,get_specs, get_logger
logger = get_logger()
class GitHelper:
@staticmethod
def commit_hash() -> str:
return git["rev-parse", "HEAD"]().strip()
@staticmethod
def commit_hash_tag() -> str:
return GitHelper.commit_hash()
@staticmethod
def commit_hash_tag_shortened() -> str:
return GitHelper.commit_hash()[:7]
@staticmethod
def commit_message() -> str:
return git["log", -1, "--pretty=%B"]().strip()
@staticmethod
def commit_changed_files() -> list:
print(f"Github ref name: {environ['GITHUB_REF_NAME']}")
print(f"Changed files: { git['diff', 'HEAD^', 'HEAD', '--name-only']().split() }")
return git['diff', "HEAD^", "HEAD", '--name-only']().split()
@staticmethod
def get_branch_name() -> str:
name = environ['GITHUB_REF_NAME']
if 'merge' in name:
# it will be sth like 71/merge in a PR action run
# we use GITHUB_HEAD_REF instead
# see https://stackoverflow.com/a/58035262
name = environ['GITHUB_HEAD_REF']
print(f"Github ref (branch) name: {name}")
return name
def get_changed_images():
changed_images = set()
changed_files = GitHelper.commit_changed_files()
# read all image name
spec = get_specs('images/spec.yml')
images = list(spec['images'].keys())
# read all build tags
with open('images/change_ignore.json','r') as ftp:
tags = json.load(ftp)
# use commit message to force full rebuild
if("full rebuild" in GitHelper.commit_message().lower()):
logger.info("Triggering full rebuild based on commit message.")
changed_images.update(images)
return list(changed_images)
# if the commit is in main, do a full rebuild, as the stable tag action needs 4 images to make stable.
elif(environ['GITHUB_REF_NAME'] == 'main'):
logger.info("Triggering full rebuild based on being in the main branch.")
changed_images.update(images)
return list(changed_images)
for file in changed_files:
fp = PurePath(file)
logger.debug(f"Detecting changed file: {file}")
# need to be under images and must be a folder
if fp.parts[0] == 'images':
image_ref = fp.parts[1]
if image_ref in tags['BuildAll'] or environ['GITHUB_REF_NAME'] == 'main':
changed_images.update(images)
# included all images so break and proceed as all images needs to be built
break
if image_ref not in changed_images and image_ref not in tags['ChangeIgnore']:
logger.info(f"Changed file {file} belongs to {image_ref}. Will rebuild.")
changed_images.add(image_ref)
return list(changed_images)
def save_changed_images():
"""
side-effects:
"""
images = get_changed_images()
print('Images changed:', images)
store_var('IMAGES_CHANGED', images)
def save_git_info():
for fp, func in {
'GIT_HASH': GitHelper.commit_hash_tag,
'GIT_HASH_SHORT': GitHelper.commit_hash_tag_shortened,
'GIT_MESSAGE': GitHelper.commit_message,
'GIT_CHANGED_FILES': GitHelper.commit_changed_files
}.items():
store_var(fp, func())
if __name__ == "__main__":
print("Git hash:", GitHelper.commit_hash())
print("Git hash:", GitHelper.commit_hash_tag())
print("Git hash shortened:", GitHelper.commit_hash_tag_shortened())
print("Git message:", GitHelper.commit_message())
print("Git changed files:", GitHelper.commit_changed_files())
print('Git changed images', get_changed_images())