forked from untangle/ngfw_pkgtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchangelog.py
executable file
·192 lines (160 loc) · 6.2 KB
/
changelog.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#! /usr/bin/env python3
import argparse, datetime, git, logging, os, re, sys
import os.path as osp
# FIXME/TODO
# - use static SSH config ?
#
## constants
PROJECT = "NGFW"
BASE_DIR = osp.join("/tmp", os.getenv('USER'))
REMOTE_TPL = "[email protected]:untangle/{}_{}.git".format(PROJECT.lower(),'{}')
BRANCH_TPL = "origin/release-{}"
REPOSITORIES = ("src", "pkgs", "hades-pkgs", "isotools-jessie", "kernels") #, "isotools-stretch")
JIRA_FILTER = re.compile(r'{}-\d+'.format(PROJECT))
CHANGELOG_FILTER = re.compile(r'@changelog')
CHANGELOG_EXCLUDE_FILTER = re.compile(r'@exclude')
## CL options
parser = argparse.ArgumentParser(description='''List changelog entries
between tags across multiple repositories.
It can also optionally create and push additional tags, of the form
X.Y.Z-YYYYmmddTHHMM-(promotion|sync)''')
def fullVersion(o):
if len(o.split('.')) != 3:
raise argparse.ArgumentTypeError("Not a valid full version (x.y.z)")
else:
return o
parser.add_argument('--log-level', dest='logLevel',
choices=['debug', 'info', 'warning'],
default='warning',
help='level at which to log')
parser.add_argument('--create-tags', dest='createTags',
action='store_true',
default=False,
help='create new tags (default=no tag creation)')
parser.add_argument('--version', dest='version',
action='store',
required=True,
default=None,
metavar="VERSION",
type=fullVersion,
help='the version on which to base the diff. It needs to be of the form x.y.z, that means including the bugfix revision')
mode = parser.add_mutually_exclusive_group(required=True)
mode.add_argument('--tag-type', dest='tagType', action='store',
choices=('promotion','sync'),
default=None,
metavar="TAG-TYPE",
help='tag type')
mode.add_argument('--manual-boundaries', dest='manualBoundaries', nargs=2,
default=None,
metavar="TAG_N",
help='specify 2 arbitrary tags to diff between, instead of using <latest-type>..HEAD')
## functions
def formatCommit(commit, repo, tickets = None):
s = "{} [{}] {}".format(str(commit)[0:7], repo, commit.summary)
if not tickets:
return s
else:
return "{} ({})".format(s, ", ".join(tickets))
def generateTag(version, tagType):
ts = datetime.datetime.now().strftime('%Y%m%dT%H')
return "{}-{}-{}".format(version, ts, tagType)
def updateRepo(name):
d = osp.join(BASE_DIR, name)
repoUrl = REMOTE_TPL.format(name)
logging.info("looking at {}".format(repoUrl))
if osp.isdir(d):
logging.info("using existing {} ".format(d))
r = git.Repo(d)
o = r.remote('origin')
o.fetch()
else:
logging.info("cloning from remote into {} ".format(d))
r = git.Repo.clone_from(repoUrl, d)
o = r.remote('origin')
return r, o
def findMostRecentTag(repo, version, tagType):
# filter tags by type first
tags = [ t for t in repo.tags if t.name.find(tagType) >= 0]
# let's see if some of those are about the current version
versionTags = [ t for t in tags if t.name.find(version) >= 0 ]
if versionTags:
tags = versionTags
tags = sorted(tags, key = lambda x: x.name)
logging.info("found tags: {}".format(tags))
if not tags:
logging.error("no tags found, aborting")
sys.exit(2)
old = tags[-1]
logging.info("most recent tag: {}".format(old.name))
return old
def listCommits(repo, old, new):
sl = "{}...{}".format(old, new)
logging.info("running git log {}".format(sl))
yield from repo.iter_commits(sl)
def filterCommit(commit):
tickets = JIRA_FILTER.findall(commit.message)
cl = CHANGELOG_FILTER.search(commit.message)
exclude = CHANGELOG_EXCLUDE_FILTER.search(commit.message)
if (tickets or cl) and not exclude:
# only attach those tickets that are not directly mentioned in
# the subject
tickets = [ t for t in tickets if commit.summary.find(t) < 0 ]
return commit, tickets
else:
return None, None
def sortCommitListByDateAuthored(l):
return sorted(l, key = lambda x: x[0].authored_date)
def formatCommitList(l, sep = '\n'):
return sep.join([formatCommit(*x) for x in l])
## main
args = parser.parse_args()
# logging
logging.getLogger().setLevel(getattr(logging, args.logLevel.upper()))
console = logging.StreamHandler(sys.stderr)
formatter = logging.Formatter('[%(asctime)s] changelog: %(levelname)-7s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
# go
logging.info("started with {}".format(" ".join(sys.argv[1:])))
# derive remote branch name from version
majorMinor = '.'.join(args.version.split(".")[0:2]) # FIXME
if not args.manualBoundaries:
new = BRANCH_TPL.format(majorMinor)
else:
old, new = args.manualBoundaries
# to store final results
changelogCommits = []
allCommits = []
# create tag name and message anyway
tagName = generateTag(args.version, args.tagType)
tagMsg = "Automated tag creation: version={}, branch={}".format(args.version, new)
# create tmp dir
if not osp.isdir(BASE_DIR):
os.makedirs(BASE_DIR)
# iterate over repositories
for name in REPOSITORIES:
repo, origin = updateRepo(name)
if not args.manualBoundaries:
old = findMostRecentTag(repo, args.version, args.tagType).name
# origin/release-X.Y may not have been created already (promoting
# from "current" for instance); in that case, use origin/master
# instead
try:
repo.commit(new)
except git.exc.BadName:
new = "origin/master"
for commit in listCommits(repo, old, new):
allCommits.append((commit, name, None))
clCommit, tickets = filterCommit(commit)
if clCommit:
changelogCommits.append((commit, name, tickets))
if args.createTags:
logging.info("about to create tag {}".format(tagName))
t = repo.create_tag(tagName, ref=new, message=tagMsg)
origin.push(t)
allCommits = sortCommitListByDateAuthored(allCommits)
changelogCommits = sortCommitListByDateAuthored(changelogCommits)
logging.debug("all commits:\n {}".format(formatCommitList(allCommits,"\n ")))
logging.info("done")
if changelogCommits:
print(formatCommitList(changelogCommits))