-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsanitize.py
executable file
·130 lines (108 loc) · 4.8 KB
/
sanitize.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2011 Alf Lervåg. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY ALF LERVÅG ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL ALF LERVÅG OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be interpreted
# as representing official policies, either expressed or implied, of
# Alf Lervåg.
import os
import sys
import datetime
import calendar
from collections import defaultdict
from suds.client import Client
import ConfigParser
from ct.apis import BaseAPI
config = ConfigParser.ConfigParser()
user_cfg = os.path.expanduser('~/.sanitizer.cfg')
default_cfg = os.path.join(
sys.prefix,
'share/ct-jira-sanitizer/config.ini.sample')
config.read([default_cfg, user_cfg])
def dates_in_month(year, month):
_, n_days = calendar.monthrange(year, month)
return [datetime.date(year, month, d) for d in range(1, n_days + 1)]
def get_jira_activities():
soap = Client('https://jira.bouvet.no/rpc/soap/jirasoapservice-v2?wsdl')
custom_field_id = config.get("jira", "custom_field_id")
username = config.get("jira-login", "username")
password = config.get("jira-login", "password")
auth = soap.service.login(username, password)
query = """
project in projectsWhereUserHasPermission("Work On Issues") AND
cf[%s] is not empty
""" % custom_field_id
activities = defaultdict(lambda: defaultdict(lambda: 0))
issues = soap.service.getIssuesFromJqlSearch(auth, query, 1000)
for issue in issues:
ctref = None
for customField in issue.customFieldValues:
if customField.customfieldId == "customfield_%s" % custom_field_id:
ctref = int(customField.values[0])
if not ctref:
msg = "This should never happen! Assumptions are wrong!"
raise SystemError(msg)
worklogs = soap.service.getWorklogs(auth, issue.key)
for worklog in worklogs:
day = worklog.startDate.date()
hours = worklog.timeSpentInSeconds / 3600.0
activities[ctref][day] += hours
return activities
def get_ct_activities(year, month):
ct = BaseAPI(config.get("server", "url"))
ct.login(config.get("login", "username"), config.get("login", "password"))
ct_activities = defaultdict(lambda: defaultdict(lambda: 0))
for activity in ct.get_month(args.year, args.month):
ctref, _, _ = activity.project_id.partition(",")
ctref = int(ctref)
if not ctref in jira_activities:
continue
date = activity.date
hours = activity.duration
ct_activities[ctref][date] += hours
return ct_activities
if __name__ == "__main__":
import argparse
now = datetime.datetime.now()
parser = argparse.ArgumentParser()
parser.add_argument('-m', dest='month', type=int, default=now.month,
help='The month to sanitize hours from, defaults to current month')
parser.add_argument('-y', dest='year', type=int, default=now.year,
help='The year to sanitize hours from, defaults to current year')
args = parser.parse_args()
jira_activities = get_jira_activities()
ct_activities = get_ct_activities(args.year, args.month)
ctrefs = set(ct_activities.keys() + jira_activities.keys())
print "Discrepency: id, date, jira, ct"
for ctref in ctrefs:
for day in dates_in_month(args.year, args.month):
ct_hours = ct_activities[ctref][day]
jira_hours = jira_activities[ctref][day]
if jira_hours != ct_hours:
print "Discrepency: %s, %s, %04s, %04s" % (
ctref, day, jira_hours, ct_hours)