Add more metadata to request.

This commit is contained in:
James Pace 2024-01-04 19:55:42 -05:00
parent 65e44e748b
commit 48c5cfa89a
2 changed files with 13 additions and 7 deletions

View File

@ -24,6 +24,9 @@ which are checked at start up.
branches of concern, for specific branch use '^refs/heads/branch$' branches of concern, for specific branch use '^refs/heads/branch$'
* `J7S_LOOPTIME` - Default: 1.0 minutes between checks of the state of * `J7S_LOOPTIME` - Default: 1.0 minutes between checks of the state of
of the repo of the repo
* `J7S_ORG_NAME` - Default: "" the name of the org the repo is in in gitea.
Is addeded to the sent request.
* `J7S_REPO_NAME` - Default: "" the repo name which will be added to the * `J7S_REPO_NAME` - Default: "" the repo name which will be added to the
data posted to the event. Normally the thing right before .git in the clone data posted to the event. Normally the thing right before .git in the clone
url. url.
* `J7S_JOB_NAME` = Default: "" name for this trigger. Added to the sent request.

View File

@ -25,9 +25,9 @@ import logging
def main(): def main():
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
logging.debug('Starting up....') logging.debug('Starting up....')
(repo, url, data_file, pattern, loop_time, repo_name) = get_config() (repo, url, data_file, pattern, loop_time, repo_name, org_name, job_name) = get_config()
def job(): def job():
loop(repo, pattern, url, data_file, repo_name) loop(repo, pattern, url, data_file, repo_name, org_name, job_name)
schedule.every(loop_time).minutes.do(job) schedule.every(loop_time).minutes.do(job)
logging.debug('Doing initial run.') logging.debug('Doing initial run.')
schedule.run_all() schedule.run_all()
@ -36,14 +36,14 @@ def main():
schedule.run_pending() schedule.run_pending()
time.sleep(1) time.sleep(1)
def loop(repo, pattern, request_url, data_file, repo_name): def loop(repo, pattern, request_url, data_file, repo_name, org_name, job_name):
logging.debug('Looking at {} for {}.'.format(repo, pattern)) logging.debug('Looking at {} for {}.'.format(repo, pattern))
all_branches = git_ls(repo) all_branches = git_ls(repo)
logging.debug('All branches: {}'.format(all_branches)) logging.debug('All branches: {}'.format(all_branches))
concerned_branches = find_concerned_branches(all_branches, pattern) concerned_branches = find_concerned_branches(all_branches, pattern)
logging.debug('Concerned branches for {}: {}'.format(pattern, concerned_branches)) logging.debug('Concerned branches for {}: {}'.format(pattern, concerned_branches))
last_time = load_last_time(data_file) last_time = load_last_time(data_file)
compare_branches(last_time, concerned_branches, request_url, repo_name) compare_branches(last_time, concerned_branches, request_url, repo_name, org_name, job_name)
save_this_time(data_file, concerned_branches) save_this_time(data_file, concerned_branches)
def git_ls(repo): def git_ls(repo):
@ -82,13 +82,13 @@ def save_this_time(file_name, this_time):
with open(file_name, 'w') as f: with open(file_name, 'w') as f:
json.dump(this_time, f) json.dump(this_time, f)
def compare_branches(last_time, this_time, url, repo_name): def compare_branches(last_time, this_time, url, repo_name, org_name, job_name):
for branch in this_time.keys(): for branch in this_time.keys():
my_hash = this_time[branch] my_hash = this_time[branch]
last_hash = last_time.get(branch, None) last_hash = last_time.get(branch, None)
if my_hash != last_hash: if my_hash != last_hash:
logging.debug('Branch {} has new hash.'.format(branch)) logging.debug('Branch {} has new hash.'.format(branch))
r = requests.post(url, json={"sha": my_hash, "repo_name": repo_name}) r = requests.post(url, json={"sha": my_hash, "repo_name": repo_name, "org_name": org_name, "job_name": job_name})
if r.status_code == 200 or r.status_code == 202: if r.status_code == 200 or r.status_code == 202:
logging.debug("Request ok!") logging.debug("Request ok!")
else: else:
@ -101,8 +101,11 @@ def get_config():
raise Exception('Missing J7S_REPO OR J7S_URL.') raise Exception('Missing J7S_REPO OR J7S_URL.')
# the part right before .git in gitea clone url # the part right before .git in gitea clone url
repo_name = os.environ.get('J7S_REPO_NAME', "") repo_name = os.environ.get('J7S_REPO_NAME', "")
# the gitea org the repo lives in.
org_name = os.environ.get('J7S_ORG_NAME', "")
job_name = os.environ.get('J7S_JOB_NAME', "")
data_file = os.environ.get('J7S_HISTORY_FILE', '/tmp/branch-history.json') data_file = os.environ.get('J7S_HISTORY_FILE', '/tmp/branch-history.json')
pattern = os.environ.get('J7S_PATTERN', '^HEAD$') pattern = os.environ.get('J7S_PATTERN', '^HEAD$')
loop_time = float(os.environ.get('J7S_LOOPTIME', 1.0)) loop_time = float(os.environ.get('J7S_LOOPTIME', 1.0))
return (repo, url, data_file, pattern, loop_time, repo_name) return (repo, url, data_file, pattern, loop_time, repo_name, org_name, job_name)