BacklogAPI有効活用 -各担当者へ個別レポート-

目的

便利なバックログですが最近タスクがたまり気味のため
各タスクオーナに個別詳細レポートを送信する

やってみた事

各担当者へ納期順に個別レポートをメールする

送信されるレポートメールサンプル

担当者のメールアドレスに自分が担当分の下記レポートメールが送信され
代表者のメールアドレスには全ての担当者分がまとまって送信されます。

期限日:2014/06/24
[PRJ_NAME-95] 課題名
状態:処理済み / 担当者:XXX

2014/06/23 14:13:57
最新コメントの先頭100文字
https://space-name.backlog.jp/view/PRJ_NAME-95
...

プログラム本体

動作確認環境

  • Python 2.6.6
  • backlog lib (python)
    https://code.google.com/p/backloglib/

設定ファイル

プログラム本体と同一ディレクトリに配置の上
ファイル名を config.ini として下さい

1行目 プロジェクト名
2行目 代表者メールアドレス

ソースコード

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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#coding:utf-8
 
#####
## FileName:report_mail.py
## Author:Ryo Tanaka
## Version:0.3
## Date:2014/06/26
## Usage:./report_mail.py
## Summary:担当者別にBACKLOGのプロジェクトサマリをメール送信します
#####
 
__author__ = 'tanaka'
import datetime
import backloglib
import smtplib
from email.MIMEText import MIMEText
from email.Header import Header
from email.Utils import formatdate
 
config_file = "config.ini"
 
spacename = "スペースネームを記載 xxxx.backlog.jp の部分です"
username = "API用ユーザネームを記載 APIリクエストに使うユーザ名"
password = "API用ユーザパスワードを記載 APIリクエストに使うユーザのパスワード"
 
smtpsvr = "メール送信サーバアドレス"
port = 25
from_addr = "メール送信元アドレスを記載"
 
whole_report = ""
separator = "--------------------------------------------------------------------"
 
#
# load_config 設定ファイル読み込み
# @param cnf 設定ファイル名
#
def load_config(cnf):
    global line
    try:
        f = open(cnf, 'r')
        line = f.readlines()
        f.close()
    except:
        print cnf + "の読み込みに失敗しました"
        exit(1)
 
#
# create_message MIMETextを作成
# @param from_addr 送信元アドレス
# @param to_addr 送信先アドレス
# @param subject 件名
# @param body メール本文
# @param encoding 文字エンコーディング
# @return msg MIMEText
#
def create_message(from_addr, to_addr, subject, body, encoding):
    msg = MIMEText(body, 'plain', encoding)
    msg['Subject'] = Header(subject, encoding)
    msg['From'] = from_addr
    msg['To'] = to_addr
    msg['Date'] = formatdate(localtime=True)
    msg['X-Priority'] = '1'
    return msg
 
#
# send_mail メール送信
# @param from_addr 送信元アドレス
# @param to_addr 送信先アドレス
# @param msg 送信するMIMEText
#
def send_mail(from_addr, to_addr, msg):
    smtp = smtplib.SMTP(smtpsvr, port)
    smtp.sendmail(from_addr, [to_addr], msg.as_string())
    smtp.close()
 
#
# cut_comment コメント整形
# @param come コメント
# @return come 整形したコメント
#
def cut_comment(come):
    come = come[0:100]
    come = come.replace('\n', '')
    come = come.replace('\r', '')
    come = come.replace('\r\n', '')
    return come
 
#
# make_report 報告レポート作成
# @param due 期限日
# @param key 課題キー
# @param summary 課題の概要
# @param assigner 担当者
# @param url 課題URL
# @param time 時刻
# @param comment コメント
# @return rep 構築されたレポート
#
def make_report(due, key, summary, status, assigner, url, time, comment):
    global separator
    rep = u"期限日:" + due + "\n"  + \
        "[" + key + "] " + summary + "\n" + \
        u"状態:" + status + u" / 担当者:" + assigner + "\n" + "\n" + \
        time + "\n" + \
        comment + "\n" + \
        url + "\n" + "\n" + \
        separator + "\n"
    return rep
 
#
# convert_time 時刻整形
# @param str 日付文字列(YYYYmmddHHMMSS)
# @return t_str 整形済み日付文字列(YYYY/mm/dd HH:MM:SS)
#
def convert_time(str):
    time = datetime.datetime.strptime(str, "%Y%m%d%H%M%S")
    t_str = time.strftime("%Y/%m/%d %H:%M:%S")
    return t_str
 
#
# convert_date 時刻整形
# @param str 日付文字列(YYYYmmdd)
# @return t_str 整形済み日付文字列(YYYY/mm/dd)
#
def convert_date(str):
    time = datetime.datetime.strptime(str, "%Y%m%d")
    t_str = time.strftime("%Y/%m/%d")
    return t_str
 
#
# set_blank 空白埋め
#
def set_blank():
    global comment
    global time_str
    comment = ""
    time_str = ""
 
# #### #
# MAIN #
# #### #
if __name__ == "__main__" :
    load_config(config_file)
    projects = line[0].strip()
    chief_addr = line[1].strip()
 
    backlog = backloglib.Backlog(spacename, username, password)
    backlog_admin = backloglib.BacklogAdmin(spacename, username, password)
 
    project = backlog.get_project(projects)
    allusers = backlog_admin.get_users()
 
    report_subject = "【BACKLOG残タスク】" + project.key + "プロジェクトで未完了のタスクを報告します"
 
    mail_addr = [0] * (len(allusers) - 1)
 
    for cnt in range(len(allusers) - 1):
        mail_addr[cnt] = allusers[cnt].mail_address
 
    for prj_index in range(len(allusers) - 1):
        issues = backlog.find_issue({
            'projectId':project.id,
            'statusId':[1,2,3],
            'assignerId':allusers[prj_index].id,
            'sort':"DUE_DATE",
            'order':True
        })
 
        if len(issues) < 1:
            continue
 
        report = [0] * (len(issues) - 1)
 
        for issue_index in range(len(issues) - 1):
            issue = backlog.get_issue(issues[issue_index].id)
            comments = backlog.get_comments(issues[issue_index].id)
 
            if len(comments) < 1:
                set_blank()
            else:
                try:
                    comment = comments[-1].content # 最新のコメントを取得
                    time_str = convert_time(comments[-1].created_on) # 時刻を整形
                except:
                    set_blank()
 
            comment = cut_comment(comment)
 
            if len(comment) < 1:
                comment = u"コメントが存在しませんでした"
 
            if len(issue.due_date) < 1:
                due_date = u"なし"
            else:
                due_date = convert_date(issue.due_date)
 
            rep_section = make_report(due_date, issue.key, issue.summary, issue.status.name, issue.assigner.name, issue.url, time_str, comment)
            report[issue_index] = rep_section.encode('utf-8')
            report_msg = "".join(map(str, report)) # report配列を結合
            issue_index += 1
 
        try:
            to_addr = mail_addr[prj_index]
            body_msg = create_message(from_addr, to_addr, report_subject, report_msg, 'utf-8')
            whole_report += report_msg
            send_mail(from_addr, to_addr, body_msg)
        except:
            prj_index += 1
            continue
 
        prj_index += 1
 
whole_msg = create_message(from_addr, chief_addr, report_subject, whole_report, 'utf-8')
send_mail(from_addr, chief_addr, whole_msg)
exit(0)

投稿者プロフィール

takashi
Japan AWS Ambassadors 2023, 2024
開発会社での ASP型WEBサービス企画 / 開発 / サーバ運用 を経て
2010年よりスカイアーチネットワークスに在籍しております

機械化/効率化/システム構築を軸に人に喜んで頂ける物作りが大好きです。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA


Time limit is exhausted. Please reload CAPTCHA.