0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

JIRAの期限の近いタスクをチャットワークに通知したい。GoogleActionScriptで。

Last updated at Posted at 2025-01-03

JIRAとチャットワークを連携したい。

期日が近いJIRA課題をリマインダーしたい。ただ、JIRA上の自動化で設定してもうまくいかない。
ずっと課題だった。。なぜメールが飛んでくれないのか。

長期休みに入ったので、別の方法で実装を考えてみた。
chatGPTに相談したら、前やったGmail-Chatwork連携と同じようにGAS連携でなんとかなりそうだった。
ChatworkとGmailを連携して、請求をラクにしたい
試したら出来たので共有。

ログイン用のJIRA APIの取得方法や、チャットワークTOKENの取得方法は適宜chatGPTに聞くなりして調べて欲しい。
JQLは例として、期日が設定されていて、5日以内のタスクをひっかける場合の設定例。
あとはGASのトリガーでscheduledTask()を希望の日時に走らせるだけ。
一応、月・金の9時台に設定して運用してみる。

これで、リマインダーが実装できた。

GAS上のコード。
// JIRA APIの設定
var JIRA_BASE_URL = "https://xxx123456.atlassian.net";
var JIRA_EMAIL = "pg@xxx123456.co.jp"; // JIRAログイン用のメールアドレス
var JIRA_API_TOKEN = "xxxxxxx"; // JIRAのログイン用APIトークン
var JQL = "resolution = EMPTY AND due <= 5d ORDER BY updated DESC"; // 実行するJQLクエリ

// Chatworkの設定
var CW_TOKEN = 'zzzzzzz';
var ROOM_ID = 'yyyyyyy'; // 通知先ルーム

// JIRAの課題を取得
function fetchJiraIssues() {
    var url = JIRA_BASE_URL + "/rest/api/3/search?jql=" + encodeURIComponent(JQL) + "&fields=summary,assignee,duedate,updated,comment";
    var options = {
        method: "get",
        headers: {
            "Authorization": "Basic " + Utilities.base64Encode(JIRA_EMAIL + ":" + JIRA_API_TOKEN),
            "Accept": "application/json"
        }
    };

    var response = UrlFetchApp.fetch(url, options);
    var data = JSON.parse(response.getContentText());

    if (data.issues && data.issues.length > 0) {
        var message = "[info][title]JIRA課題通知[/title]";
        data.issues.forEach(function(issue) {
            var issueUrl = JIRA_BASE_URL + "/browse/" + issue.key; // JIRAインスタンスのURLを設定
            var latestComment = "コメントなし";

            if (issue.fields.comment && issue.fields.comment.comments) {
                var comments = issue.fields.comment.comments;
                if (Array.isArray(comments) && comments.length > 0) {
                    // 最新のコメントデータ
                    var latestCommentObj = comments[comments.length - 1];
                    // 最新コメントの本文を組み立て
                    latestComment = extractCommentBody(latestCommentObj.body);
                }
                //Logger.log(JSON.stringify(comments))
            }

            message += "概要: " + issue.fields.summary + "\n";
            message += "期限: " + issue.fields.duedate + "\n";
            message += "更新日: " + issue.fields.updated + "\n";
            message += "担当者: " + (issue.fields.assignee ? issue.fields.assignee.displayName : "未設定") + "\n";
            message += "リンク: " + issueUrl + "\n";
            message += "最近コメ: " + latestComment + "\n";
            message += "---\n";
            message += "\n";
        });
        message += "[/info]";
        sendMessageToChatwork(message);
    } else {
        Logger.log("JIRA課題はありません。");
    }
}

// コメント本文を抽出する関数
function extractCommentBody(body) {
    var commentText = "";
    if (body && body.content) {
        body.content.forEach(function(contentBlock) {
            if (contentBlock.type === "paragraph" && contentBlock.content) {
                contentBlock.content.forEach(function(textBlock) {
                    if (textBlock.type === "text") {
                        commentText += textBlock.text;
                    } else if (textBlock.type === "hardBreak") {
                        commentText += "\n";
                    }
                });
            }
        });
    }
    return commentText.trim();
}

// Chatworkにメッセージを送信
function sendMessageToChatwork(message) {
    var url = "https://api.chatwork.com/v2/rooms/" + ROOM_ID + "/messages";
    var options = {
        headers: { "X-ChatWorkToken": CW_TOKEN },
        method: "post",
        payload: { body: message }
    };
    UrlFetchApp.fetch(url, options);
}

// 定期実行タスク
function scheduledTask() {
    fetchJiraIssues();
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?