Skip to content

Commit 6073003

Browse files
committed
Better loop detection
1 parent 5bb0acb commit 6073003

File tree

2 files changed

+52
-22
lines changed

2 files changed

+52
-22
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LABEL maintainer="jrandiny <[email protected]>"
55
ENV DEBIAN_FRONTEND=noninteractive
66

77
RUN apt update
8-
RUN apt install -y reprepro gpg python3 python3-git python3-gnupg expect
8+
RUN apt install -y reprepro gpg python3 python3-git python3-gnupg expect python3-debian
99

1010
COPY entrypoint.py /entrypoint.py
1111
COPY key.py /key.py

entrypoint.py

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import gnupg
55
import git
66
import shutil
7+
import re
8+
import json
9+
from debian.debfile import DebFile
710
from key import detectPublicKey, importPrivateKey
811

912
debug = os.environ.get('INPUT_DEBUG', False)
@@ -17,30 +20,32 @@
1720
logging.info('-- Parsing input --')
1821

1922
github_token = os.environ.get('INPUT_GITHUB_TOKEN')
20-
arch = os.environ.get('INPUT_ARCH')
21-
version = os.environ.get('INPUT_VERSION')
22-
deb_file = os.environ.get('INPUT_FILE')
23-
deb_file_version = os.environ.get('INPUT_FILE_TARGET_VERSION')
23+
supported_arch = os.environ.get('INPUT_ARCH')
24+
supported_version = os.environ.get('INPUT_VERSION')
25+
deb_file_path = os.environ.get('INPUT_FILE')
26+
deb_file_target_version = os.environ.get('INPUT_FILE_TARGET_VERSION')
2427
github_repo = os.environ.get('GITHUB_REPOSITORY')
2528

2629
gh_branch = os.environ.get('INPUT_PAGE_BRANCH', 'gh-pages')
2730
apt_folder = os.environ.get('INPUT_REPO_FOLDER', 'repo')
2831

29-
if None in (github_token, arch, version, deb_file):
32+
if None in (github_token, supported_arch, supported_version, deb_file_path):
3033
logging.error('Required key is missing')
3134
sys.exit(1)
3235

33-
arch_list = arch.strip().split('\n')
34-
version_list = version.strip().split('\n')
35-
deb_file_list = deb_file.strip().split('\n')
36-
deb_file_version_list = deb_file_version.strip().split('\n')
36+
supported_arch_list = supported_arch.strip().split('\n')
37+
supported_version_list = supported_version.strip().split('\n')
38+
deb_file_list = deb_file_path.strip().split('\n')
39+
deb_file_version_list = deb_file_target_version.strip().split('\n')
3740

38-
logging.debug(arch_list)
39-
logging.debug(version_list)
41+
logging.debug(supported_arch_list)
42+
logging.debug(supported_version_list)
4043
logging.debug(deb_file_list)
4144
logging.debug(deb_file_version_list)
4245

43-
if any((target_version not in version_list) for target_version in deb_file_version_list):
46+
if any(
47+
(target_version not in supported_version_list) for target_version in deb_file_version_list
48+
):
4449
logging.error('File version target is not listed in repo supported version list')
4550
sys.exit(1)
4651

@@ -49,8 +54,8 @@
4954
key_passphrase = os.environ.get('INPUT_KEY_PASSPHRASE')
5055

5156
logging.debug(github_token)
52-
logging.debug(arch_list)
53-
logging.debug(version_list)
57+
logging.debug(supported_arch_list)
58+
logging.debug(supported_version_list)
5459

5560
logging.info('-- Done parsing input --')
5661

@@ -70,9 +75,32 @@
7075
branch=gh_branch,
7176
)
7277

73-
if git_repo.head.commit.message[:12] == '[apt-action]':
74-
logging.info('Loop detected, exiting')
75-
sys.exit(0)
78+
# Generate metadata
79+
deb_file_handle = DebFile(filename=deb_file_path)
80+
deb_file_control = deb_file_handle.debcontrol()
81+
82+
current_metadata = {
83+
'format_version': 1,
84+
'sw_version': deb_file_control['Version'],
85+
'sw_architecture': deb_file_control['Architecture'],
86+
'linux_version': deb_file_target_version
87+
}
88+
89+
current_metadata_str = json.dumps(current_metadata)
90+
logging.debug('Metadata {}'.format(current_metadata_str))
91+
92+
# Get metadata
93+
all_commit = git_repo.iter_commits()
94+
all_apt_action_commit = filter(lambda x: (x.message[:12] == '[apt-action]'), all_commit)
95+
apt_action_metadata_str = map(lambda x: re.findall('apt-action-metadata({.+})$', x.message))
96+
apt_action_metadata = map(lambda x: json.loads(x[0]))
97+
98+
logging.debug(apt_action_metadata)
99+
100+
for check_metadata in apt_action_metadata:
101+
if (check_metadata == current_metadata):
102+
logging.error('Loop detected, exiting')
103+
sys.exit(1)
76104

77105
logging.info('-- Done cloning current Github page --')
78106

@@ -102,11 +130,11 @@
102130

103131
logging.debug('Creating repo config')
104132

105-
with open(os.path.join(apt_conf_dir, 'distributions'), "w") as distributions_file:
106-
for codename in version_list:
133+
with open(os.path.join(apt_conf_dir, 'distributions'), 'w') as distributions_file:
134+
for codename in supported_version_list:
107135
distributions_file.write('Description: {}\n'.format(github_repo))
108136
distributions_file.write('Codename: {}\n'.format(codename))
109-
distributions_file.write('Architectures: {}\n'.format(' '.join(arch_list)))
137+
distributions_file.write('Architectures: {}\n'.format(' '.join(supported_arch_list)))
110138
distributions_file.write('Components: main\n')
111139
distributions_file.write('SignWith: {}\n'.format(private_key_id))
112140
distributions_file.write('\n\n')
@@ -144,7 +172,9 @@
144172
)
145173

146174
git_repo.git.add('*')
147-
git_repo.index.commit('[apt-action] Update apt repo')
175+
git_repo.index.commit(
176+
'[apt-action] Update apt repo\n\n\napt-action-metadata{}'.format(current_metadata_str)
177+
)
148178
origin = git_repo.remote()
149179
origin.push()
150180

0 commit comments

Comments
 (0)