9 from datetime import datetime
10 from collections import defaultdict
11 from collections import OrderedDict
13 #################################
17 github_auth_username = os.environ['ICINGA_GITHUB_AUTH_USERNAME']
19 print "ERROR: Environment variable 'ICINGA_GITHUB_AUTH_USERNAME' is not set."
23 github_auth_token = os.environ['ICINGA_GITHUB_AUTH_TOKEN']
25 print "ERROR: Environment variable 'ICINGA_GITHUB_AUTH_TOKEN' is not set."
29 project_name = os.environ['ICINGA_GITHUB_PROJECT']
31 print "ERROR: Environment variable 'ICINGA_GITHUB_PROJECT' is not set."
34 #################################
37 changelog_file = "CHANGELOG.md" # TODO: config param
40 # Keep this in sync with GitHub labels.
42 "high-priority", "low-priority",
44 "needs-feedback", "question", "duplicate", "invalid", "wontfix",
45 "backported", "build-fix"
48 # Selectively show and collect specific categories
50 # (category, list of case sensitive matching labels)
51 # The order is important!
52 # Keep this in sync with GitHub labels.
53 categories = OrderedDict(
55 ("Enhancement", ["enhancement"]),
56 ("Bug", ["bug", "crash"]),
58 ("Documentation", ["Documentation"]),
59 ("Support", ["code-quality", "Tests", "Packages", "Installation"])
63 #################################
66 def write_changelog(line):
67 clfp.write(line + "\n")
73 def fetch_github_resources(uri, params = {}):
76 url = 'https://api.github.com/repos/' + project_name + uri + "?per_page=100" # 100 is the maximum
79 log(2, "Requesting URL: " + url)
80 resp = requests.get(url, auth=(github_auth_username, github_auth_token), params=params)
82 resp.raise_for_status()
83 except requests.exceptions.HTTPError as e:
91 resources.extend(data)
93 # fetch the next page from headers, do not count pages
94 # http://engineering.hackerearth.com/2014/08/21/python-requests-module/
95 if "next" in resp.links:
96 url = resp.links['next']['url']
97 log(2, "Found next link for Github pagination: " + url)
99 break # no link found, we are done
100 log(2, "No more pages to fetch, stop.")
104 def issue_type(issue):
105 issue_labels = [label["name"] for label in issue["labels"]]
107 # start with the least important first (e.g. "Support", "Documentation", "Bug", "Enhancement" as order)
108 for category in reversed(categories):
109 labels = categories[category]
112 if label in issue_labels:
117 def escape_markdown(text):
118 #tmp = text.replace('&', '&').replace('<', '<').replace('>', '>')
120 tmp.replace('\\', '\\\\')
122 return re.sub("([<>*_()\[\]#])", r"\\\1", tmp)
124 def format_labels(issue):
125 labels = filter(lambda label: label not in ignored_labels, [label["name"] for label in issue["labels"]])
127 # Mark PRs as custom label
128 if "pull_request" in issue:
132 return " (" + ", ".join(labels) + ")"
136 def format_title(title):
139 issue_title = str(title.encode('ascii', 'ignore').encode('utf-8'))
141 log(1, "Error: Cannot convert " + title + " to UTF-8")
143 # Remove dev.icinga.com tag
144 issue_title = re.sub('\[dev\.icinga\.com #\d+\] ', '', issue_title)
146 #log(1, "Issue title: " + issue_title + "Type: " + str(type(issue_title)))
148 return escape_markdown(issue_title)
150 #################################
154 issues = defaultdict(lambda: defaultdict(list))
156 log(1, "Fetching data from GitHub API for project " + project_name)
159 tickets = fetch_github_resources("/issues", { "state": "all" })
160 except requests.exceptions.HTTPError as e:
161 log(1, "ERROR " + str(e.response.status_code) + ": " + e.response.text)
165 clfp = open(changelog_file, "w+")
167 with open('tickets.pickle', 'wb') as fp:
168 pickle.dump(tickets, fp)
170 with open('tickets.pickle', 'rb') as fp:
171 cached_issues = pickle.load(fp)
173 for issue in cached_issues: #fetch_github_resources("/issues", { "state": "all" }):
174 milestone = issue["milestone"]
179 ms_title = milestone["title"]
181 if not re.match('^\d+\.\d+\.\d+$', ms_title):
184 if ms_title.split(".")[0] != "2":
187 milestones[ms_title] = milestone
189 ms_tickets = issues[ms_title][issue_type(issue)]
190 ms_tickets.append(issue)
192 # TODO: Generic header based on project_name
193 write_changelog("# Icinga 2.x CHANGELOG")
196 for milestone in sorted(milestones.values(), key=lambda ms: (ms["due_on"], ms["title"]), reverse=True):
197 if milestone["state"] != "closed":
200 if milestone["due_on"] == None:
201 print "Milestone", milestone["title"], "does not have a due date."
204 ms_due_on = datetime.strptime(milestone["due_on"], "%Y-%m-%dT%H:%M:%SZ")
206 write_changelog("## %s (%s)" % (milestone["title"], ms_due_on.strftime("%Y-%m-%d")))
209 ms_description = milestone["description"]
210 ms_description = re.sub('\r\n', '\n', ms_description)
212 if len(ms_description) > 0:
213 write_changelog("### Notes\n\n" + ms_description + "\n") # Don't escape anything, we take care on Github for valid Markdown
215 for category, labels in categories.iteritems():
217 ms_issues = issues[milestone["title"]][category]
221 if len(ms_issues) == 0:
224 write_changelog("### " + category)
227 for issue in ms_issues:
228 write_changelog("* [#" + str(issue["number"]) + "](https://github.com/" + project_name
229 + "/issues/" + str(issue["number"]) + ")" + format_labels(issue) + ": " + format_title(issue["title"]))
234 log(1, "Finished writing " + changelog_file)