]> granicus.if.org Git - icinga2/blob - pick.py
Replace BOOST_FOREACH with range-based for loops
[icinga2] / pick.py
1 #!/usr/bin/env python
2 # Icinga 2
3 # Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/)
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software Foundation
17 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 import urllib2, json, sys, string, subprocess, re, os
20 from argparse import ArgumentParser
21 from git import Repo
22 from tempfile import NamedTemporaryFile
23
24 DESCRIPTION="cherry-pick commits for releases"
25 VERSION="1.0.0"
26 ISSUE_URL= "https://dev.icinga.org/issues/"
27 ISSUE_PROJECT="i2"
28
29 arg_parser = ArgumentParser(description= "%s (Version: %s)" % (DESCRIPTION, VERSION))
30 arg_parser.add_argument('-V', '--version', required=True, type=str, help="define version to query")
31 arg_parser.add_argument('-p', '--project', type=str, help="project")
32
33 args = arg_parser.parse_args(sys.argv[1:])
34
35 version_name = args.version
36
37 if args.project:
38     ISSUE_PROJECT=args.project
39
40 rsp = urllib2.urlopen("https://dev.icinga.org/projects/%s/versions.json" % (ISSUE_PROJECT))
41 versions_data = json.loads(rsp.read())
42
43 version_id = None
44
45 for version in versions_data["versions"]:
46     if version["name"] == version_name:
47         version_id = version["id"]
48         break
49
50 if version_id == None:
51     print "Version '%s' not found." % (version_name)
52     sys.exit(1)
53
54 offset = 0
55
56 issues = set()
57
58 while True:
59     # We could filter using &cf_13=1, however this doesn't currently work because the custom field isn't set
60     # for some of the older tickets:
61     rsp = urllib2.urlopen("https://dev.icinga.org/projects/%s/issues.json?offset=%d&status_id=closed&fixed_version_id=%d" % (ISSUE_PROJECT, offset, version_id))
62     issues_data = json.loads(rsp.read())
63     issues_count = len(issues_data["issues"])
64     offset = offset + issues_count
65
66     if issues_count == 0:
67         break
68
69     for issue in issues_data["issues"]:
70         ignore_issue = False
71
72         if "custom_fields" in issue:
73             for field in issue["custom_fields"]:
74                 if field["id"] == 12 and "value" in field and field["value"] != "Not yet backported":
75                     ignore_issue = True
76                     break
77
78             if ignore_issue:
79                 continue
80
81         issues.add(issue["id"])
82
83 repo = Repo(".")
84
85 repo.git.fetch()
86
87 new_branch = repo.create_head("auto-merged-" + version_name, "origin/support/" + ".".join(version_name.split(".")[:-1]))
88 new_branch.checkout()
89
90 commits = reversed(list(repo.iter_commits(rev="origin/master")))
91
92 tmpEditor = NamedTemporaryFile()
93
94 tmpEditor.write("#!/bin/sh\ncat > $1 <<COMMITS\n")
95
96 for commit in commits:
97     ids = set([int(id) for id in re.findall('#(?P<id>\d+)', commit.message)])
98
99     if not ids.intersection(issues):
100         continue
101
102     tmpEditor.write("pick %s\n" % commit.hexsha)
103
104 tmpEditor.write("COMMITS")
105 tmpEditor.flush()
106
107 os.chmod(tmpEditor.name, 0700)
108
109 env = os.environ.copy()
110 env["EDITOR"] = tmpEditor.name
111
112 subprocess.call(["git", "rebase", "-i", "HEAD"], env=env)
113
114 sys.exit(0)