9 from datetime import datetime
10 from collections import defaultdict
13 github_auth_username = os.environ['ICINGA_GITHUB_AUTH_USERNAME']
15 print "ERROR: Environment variable 'ICINGA_GITHUB_AUTH_USERNAME' is not set."
19 github_auth_token = os.environ['ICINGA_GITHUB_AUTH_TOKEN']
21 print "ERROR: Environment variable 'ICINGA_GITHUB_AUTH_TOKEN' is not set."
25 project_name = os.environ['ICINGA_GITHUB_PROJECT']
27 print "ERROR: Environment variable 'ICINGA_GITHUB_PROJECT' is not set."
30 changelog_file = "CHANGELOG.md" # TODO: config param
33 #################################
36 def write_changelog(line):
37 clfp.write(line + "\n")
43 def fetch_github_resources(uri, params = {}):
46 url = 'https://api.github.com/repos/' + project_name + uri + "?per_page=100" # 100 is the maximum
49 log(2, "Requesting URL: " + url)
50 resp = requests.get(url, auth=(github_auth_username, github_auth_token), params=params)
52 resp.raise_for_status()
61 resources.extend(data)
63 # fetch the next page from headers, do not count pages
64 # http://engineering.hackerearth.com/2014/08/21/python-requests-module/
65 if "next" in resp.links:
66 url = resp.links['next']['url']
67 log(2, "Found next link for Github pagination: " + url)
69 break # no link found, we are done
70 log(2, "No more pages to fetch, stop.")
74 def issue_type(issue):
75 if "bug" in [label["name"] for label in issue["labels"]]:
77 elif "enhancement" in [label["name"] for label in issue["labels"]]:
82 def escape_markdown(text):
83 #tmp = text.replace('&', '&').replace('<', '<').replace('>', '>')
85 tmp.replace('\\', '\\\\')
87 return re.sub("([<>*_()\[\]#])", r"\\\1", tmp)
89 def format_labels(issue):
90 labels = filter(lambda label: label not in ["high", "low", "bug", "enhancement", "feedback", "question"], [label["name"] for label in issue["labels"]])
93 return " (" + ", ".join(labels) + ")"
97 def format_title(title):
100 issue_title = str(title.encode('ascii', 'ignore').encode('utf-8'))
102 log(1, "Error: Cannot convert " + title + " to UTF-8")
104 # Remove dev.icinga.com tag
105 issue_title = re.sub('\[dev\.icinga\.com #\d+\] ', '', issue_title)
107 #log(1, "Issue title: " + issue_title + "Type: " + str(type(issue_title)))
109 return escape_markdown(issue_title)
111 #################################
115 issues = defaultdict(lambda: defaultdict(list))
117 log(1, "Fetching data from GitHub API for " + project_name)
119 clfp = open(changelog_file, "w+")
121 with open('tickets.pickle', 'wb') as fp:
122 pickle.dump(fetch_github_resources("/issues", { "state": "all" }), fp)
124 with open('tickets.pickle', 'rb') as fp:
125 cached_issues = pickle.load(fp)
127 for issue in cached_issues: #fetch_github_resources("/issues", { "state": "all" }):
128 milestone = issue["milestone"]
133 ms_title = milestone["title"]
135 if not re.match('^\d+\.\d+\.\d+$', ms_title):
138 if ms_title.split(".")[0] != "2":
141 milestones[ms_title] = milestone
143 ms_tickets = issues[ms_title][issue_type(issue)]
144 ms_tickets.append(issue)
146 write_changelog("# Icinga 2.x CHANGELOG")
149 for milestone in sorted(milestones.values(), key=lambda ms: (ms["due_on"], ms["title"]), reverse=True):
150 if milestone["state"] != "closed":
153 if milestone["due_on"] == None:
154 print "Milestone", milestone["title"], "does not have a due date."
157 ms_due_on = datetime.strptime(milestone["due_on"], "%Y-%m-%dT%H:%M:%SZ")
159 write_changelog("## %s (%s)" % (milestone["title"], ms_due_on.strftime("%Y-%m-%d")))
162 ms_description = milestone["description"]
163 ms_description = re.sub('\r\n', '\n', ms_description)
165 if len(ms_description) > 0:
166 write_changelog("### Notes\n\n" + ms_description + "\n") # Don't escape anything, we take care on Github for valid Markdown
168 for category in ["Enhancement", "Bug", "Support"]:
170 ms_issues = issues[milestone["title"]][category]
174 if len(ms_issues) == 0:
177 write_changelog("### " + category)
180 for issue in ms_issues:
181 write_changelog("* [#" + str(issue["number"]) + "](https://github.com/" + project_name
182 + "/issues/" + str(issue["number"]) + ")" + format_labels(issue) + ": " + format_title(issue["title"]))
187 log(1, "Finished writing " + changelog_file)