{"id":419,"date":"2024-11-02T10:28:01","date_gmt":"2024-11-02T01:28:01","guid":{"rendered":"https:\/\/syrup-factory.com\/memo\/?p=419"},"modified":"2024-11-02T10:52:30","modified_gmt":"2024-11-02T01:52:30","slug":"toggl%e2%86%92redmine%e6%99%82%e9%96%93%e3%83%87%e3%83%bc%e3%82%bf%e5%8f%8d%e6%98%a0%e3%83%84%e3%83%bc%e3%83%ab","status":"publish","type":"post","link":"https:\/\/syrup-factory.com\/memo\/archives\/419","title":{"rendered":"toggl\u2192Redmine\u6642\u9593\u30c7\u30fc\u30bf\u53cd\u6620\u30c4\u30fc\u30eb"},"content":{"rendered":"\n<p>toggl\u3067\u52e4\u6020\u7ba1\u7406\u3001Redmine\u306e\u30c1\u30b1\u30c3\u30c8\u306b\u4f5c\u696d\u6642\u9593\u3092\u4ed8\u3051\u308b\u904b\u7528\u6642\u3001\u4e21\u8005\u306e\u5408\u8a08\u306f\u4e00\u81f4\u3059\u308b\u306f\u305a\u3060\u304c\u3001\u624b\u5165\u529b\u904b\u7528\u3067\u306f\u9762\u5012\u3002<\/p>\n\n\n\n<p>toggl\u30c7\u30fc\u30bf\u306bRedmine\u306e\u30c1\u30b1\u30c3\u30c8\u60c5\u5831\u3092\u6301\u305f\u305b\u3066\u3001Redmine\u3078\u6642\u9593\u30c7\u30fc\u30bf\u3092\u4ed8\u3051\u3089\u308c\u305f\u3089\u5fc5\u305a\u4e00\u81f4\u3057\u3066\u624b\u9593\u304c\u7701\u3051\u3066\u8d85\u4fbf\u5229\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u5404API\u3092\u547c\u3073\u51fa\u3057\u3001\u51e6\u7406\u3059\u308b\u30c4\u30fc\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<h2>\u30d5\u30ed\u30fc<\/h2>\n\n\n\n<h3>\u524d\u63d0<\/h3>\n\n\n\n<p>toggl\u306edescription\u306b\u3001\u6240\u5b9a\u306e\u5f62\u5f0f\u3067Redmine\u306e\u30c1\u30b1\u30c3\u30c8\u756a\u53f7\u3092\u66f8\u304f<\/p>\n\n\n\n<p>\uff08\u4f8b\uff09\u9ec4\u8272\u90e8\u5206\u3002\u30c1\u30b1\u30c3\u30c8\u756a\u53f7 12533<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"223\" src=\"https:\/\/syrup-factory.com\/memo\/wp-content\/uploads\/2024\/11\/image-1-1024x223.png\" alt=\"\" class=\"wp-image-420\" srcset=\"https:\/\/syrup-factory.com\/memo\/wp-content\/uploads\/2024\/11\/image-1-1024x223.png 1024w, https:\/\/syrup-factory.com\/memo\/wp-content\/uploads\/2024\/11\/image-1-300x65.png 300w, https:\/\/syrup-factory.com\/memo\/wp-content\/uploads\/2024\/11\/image-1-768x167.png 768w, https:\/\/syrup-factory.com\/memo\/wp-content\/uploads\/2024\/11\/image-1.png 1046w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h3>\u30b3\u30fc\u30c9<\/h3>\n\n\n\n<p class=\"has-small-font-size\">\u203b\u7d30\u304b\u3044\u30a8\u30e9\u30fc\u51e6\u7406\u306f\u7701\u3044\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"has-small-font-size\">\u203b\u5b9f\u969b\u306e\u30b3\u30fc\u30c9\u304b\u3089\u629c\u7c8b\u3057\u3066\u3044\u308b\u305f\u3081\u3001\u4fee\u6b63\u30df\u30b9\u304c\u3042\u308b\uff08\u3053\u306e\u307e\u307e\u3067\u306f\u52d5\u304b\u306a\u3044\uff09\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u3042\u304f\u307e\u3067\u3056\u3063\u3068\u3057\u305f\u6d41\u308c\u306e\u53c2\u8003\u3068\u3057\u3066\u898b\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n\n\n\n<h4>toggl API\u3092\u5229\u7528\u3057\u3066\u30c7\u30fc\u30bf\u53d6\u5f97\u3059\u308b\u30e1\u30bd\u30c3\u30c9<\/h4>\n\n\n\n<p>ToggleRepository.php<\/p>\n\n\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\n    \/**\n     * Togggl\u306e\u4f5c\u696d\u8a18\u9332\u3092\u53d6\u5f97\u3059\u308b\n     *\n     * @param string $apiToken\n     * @param string $startDate=&#039;&#039;\n     * @param string $endDate=&#039;&#039;\n     * @return mixed\n     *\n     *\/\n    public function getEntries(\n        string $apiToken,\n        string $startDate=&#039;&#039;,\n        string $endDate=&#039;&#039;\n    ): mixed\n    {\n        $togglUri = &#039;https:\/\/api.track.toggl.com\/api\/v9\/me\/time_entries&#039;;  \/\/ \u5b9f\u969b\u306fconfig\u7b49\u306b\u8a2d\u5b9a\u3057\u3066\u304a\u304f\n        $options = [&#039;auth&#039; =&gt; [$apiToken, &#039;api_token&#039;]];  \/\/ \u53d6\u5f97\u5bfe\u8c61\u306e\u30e6\u30fc\u30b6\u306eapi token\u3092\u30bb\u30c3\u30c8\u3059\u308b\n\n        $query = http_build_query([\n            &#039;start_date&#039; =&gt; $startDate,\n            &#039;end_date&#039; =&gt; $endDate,\n        ]);\n        $entryRequest = (new \\GuzzleHttp\\Client())-&gt;request(&#039;GET&#039;, &quot;{$togglUri}?{$query}&quot;, $options);\n\n        $res = $entryRequest-&gt;getBody()-&gt;getContents();\n        return json_decode($res, true);\n    }\n\n<\/pre>\n\n\n<h4>Redmine API\u3092\u5229\u7528\u3057\u3066\u30c7\u30fc\u30bf\u767b\u9332\u3059\u308b\u30e1\u30bd\u30c3\u30c9<\/h4>\n\n\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\n    \/**\n     * Redmine\u306b\u6642\u9593\u30a8\u30f3\u30c8\u30ea\u3092\u767b\u9332\u3059\u308b\n     *\n     * @param array $xmlValues\n     * @return bool\n     *\n     *\/\n    public function addTimeEntry(string $apiToken, array $xmlValues) : bool\n    {\n        $xml = new \\SimpleXMLElement(&#039;&lt;time_entry&gt;&lt;\/time_entry&gt;&#039;);\n        foreach ($xmlValues as $key =&gt; $value) {\n            $xml-&gt;addChild($key, (string)$value);\n        }\n\n        \/\/Redmine \u306b\u4f5c\u696d\u6642\u9593\u3092\u767b\u9332\u3059\u308b\n        $redmineUri = self::REDMINE_URL . &quot;time_entries.xml?key={$apiToken}&quot;;\n        $request = (new \\GuzzleHttp\\Client())-&gt;request(&#039;POST&#039;, $redmineUri, [\n                &#039;headers&#039; =&gt; [&#039;Content-Type&#039; =&gt; &#039;text\/xml&#039;,],\n                &#039;body&#039; =&gt; $xml-&gt;asXML()\n        ]);\n\n        return true;\n    }\n<\/pre>\n\n\n<h4>\u30e1\u30a4\u30f3\u51e6\u7406<\/h4>\n\n\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\n    \/**\n     * main\u51e6\u7406\n     * @return array\n     *\/\n    private function exec() :array\n    {\n        $this-&gt;togglApiToken = &#039;xxxxxxxxxxxxx&#039; \/\/ \u53d6\u5f97\u5bfe\u8c61\u306e\u30e6\u30fc\u30b6\u306eapi token\n        $this-&gt;redmineApiKey = &#039;ZZZZZZZZZZZZZ&#039;; \/\/ \u5bfe\u8c61\u30e6\u30fc\u30b6\u306eRedmine\u306eAPI\u30ad\u30fc\n        $this-&gt;redmineUserId = 0; \/\/ \u5bfe\u8c61\u30e6\u30fc\u30b6\u306eRedmine\u306e\u30e6\u30fc\u30b6ID\n\n        \/\/ toggl : \u4f5c\u696d\u8a18\u9332\u3092\u53d6\u5f97\u3059\u308b\n        $entries = $this-&gt;toggleRepository-&gt;getEntries(\n            $this-&gt;togglApiToken,\n            $this-&gt;dateTime-&gt;setTime(00, 00, 00)-&gt;format(&#039;c&#039;),\n            $this-&gt;dateTimeEnd-&gt;setTime(23, 59, 59)-&gt;format(&#039;c&#039;)\n        );\n\n        \/\/ toggl \u306e\u4f5c\u696d\u5185\u5bb9\u304b\u3089 Redmine\u7528\u306e\u4f5c\u696d\u6642\u9593\u30c7\u30fc\u30bf\u3092\u4f5c\u6210\u3057\u767b\u9332\u3059\u308b\n        return $this-&gt;makeRecord($entries);\n    }\n<\/pre>\n\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\n    \/**\n     * Toggl \u306e\u4f5c\u696d\u5185\u5bb9\u304b\u3089 Redmine\u7528\u306e\u4f5c\u696d\u6642\u9593\u30c7\u30fc\u30bf\u3092\u4f5c\u6210\u3057\u767b\u9332\u3059\u308b\n     *\n     * @param array $entries\n     * @param array $redmineTimeRecs\n     * @return array : \u5b9f\u884c\u7d50\u679c\n     *\n     *\/\n    private function makeRecord(array $entries, array $redmineTimeRecs) : array\n    {\n        foreach ($entries as $entry) {\n            $description = &#039;&#039;;\n\n            \/\/ \u4f5c\u696d\u6642\u9593\u306e\u6642\u9593\n            \/\/ \u958b\u59cb\u30fb\u7d42\u4e86\u306e\u5dee\u5206\n            $start = new \\DateTime($entry[&#039;start&#039;]);\n            $start-&gt;setTimezone($this-&gt;timezone);\n\n            if (!isset($entry[&#039;stop&#039;])) {\n                $this-&gt;results-&gt;addIssueSkipNowExec($start, $description, &#039;timer \u5b9f\u884c\u4e2d\u3067\u3059&#039;);\n                continue;\n            }\n\n            $stop = new \\DateTime($entry[&#039;stop&#039;]);\n            $stop-&gt;setTimezone($this-&gt;timezone);\n\n            $diff = $stop-&gt;diff($start);\n            $min = round(($diff-&gt;i \/ 60), 3);\n            $diffHours = $diff-&gt;h + $min;\n\n            \/\/ Redmine \u3067\u30bf\u30a4\u30de\u30fc\u3092\u6253\u3063\u305f\u3068\u304d\u306e\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u306b\u6cbf\u3063\u3066` #1234 `\u306e\u6587\u5b57\u5217\u3092\u30c1\u30b1\u30c3\u30c8ID\u3068\u3057\u3066\u691c\u7d22\n            preg_match(&#039;\/\\#([0-9]+)\/&#039;, $description, $preg_match_results);\n\n            \/\/Redmine \u306b\u4f5c\u696d\u6642\u9593\u3092\u767b\u9332\u3059\u308b\n            $ret = $this-&gt;addToRedmine($issueId, $entry, $start, $diffHours, $description);\n            if ($ret) {\n                \/\/ \u6210\u529f\u6642\u51e6\u7406\n            } else {\n                \/\/ \u30a8\u30e9\u30fc\u6642\u51e6\u7406\n            }\n        }\n\n        return [\n            \/\/ \u7d50\u679c\n        ];\n    }\n<\/pre>\n\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\n    \/**\n     * Redmine \u306b\u4f5c\u696d\u6642\u9593\u3092\u767b\u9332\u3059\u308b\n     *\n     * @param int $issueId\n     * @param array $entry\n     * @param Datetime $start\n     * @param float $diffHours\n     * @param string $description\n     * @return bool\n     *\n     *\/\n    private function addToRedmine (\n        int $issueId,\n        array $entry,\n        Datetime $start,\n        float $diffHours,\n        string $description\n    ) : bool\n    {\n        $xmlValues = [\n            &#039;issue_id&#039; =&gt; $issueId,\n            &#039;spent_on&#039; =&gt; $start-&gt;format(&#039;Y-m-d&#039;),\n            &#039;hours&#039; =&gt; $diffHours,\n            &#039;activity_id&#039; =&gt; 8,  \/\/ $entry[&#039;tags&#039;]\u304b\u3089\u30bf\u30b0\u304c\u53d6\u5f97\u3067\u304d\u308b\u306e\u3067\u3001\u5fc5\u8981\u306a\u3089Redmine\u306e\u30bf\u30b0\u306eID\u3068\u5bfe\u5fdc\u3055\u305b\u3066\u30bb\u30c3\u30c8\u3059\u308b\n            &#039;user_id&#039; =&gt; $this-&gt;redmineUserId, \/\/ \u5bfe\u8c61\u30e6\u30fc\u30b6\u306eRedmine\u306e\u30e6\u30fc\u30b6ID\n            &#039;comments&#039; =&gt; $description,\n        ];\n\n        return $this-&gt;redmineRepository-&gt;addTimeEntry($this-&gt;redmineApiKey, $xmlValues);\n    }\n<\/pre>\n\n\n<h3>\u5b9f\u884c\u7d50\u679c<\/h3>\n\n\n\n<p>Redmine\u3001\u8d64\u90e8\u5206\u304c\u8ffd\u52a0\u3055\u308c\u3066\u3044\u308b\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"375\" src=\"https:\/\/syrup-factory.com\/memo\/wp-content\/uploads\/2024\/11\/image-3-1024x375.png\" alt=\"\" class=\"wp-image-426\" srcset=\"https:\/\/syrup-factory.com\/memo\/wp-content\/uploads\/2024\/11\/image-3-1024x375.png 1024w, https:\/\/syrup-factory.com\/memo\/wp-content\/uploads\/2024\/11\/image-3-300x110.png 300w, https:\/\/syrup-factory.com\/memo\/wp-content\/uploads\/2024\/11\/image-3-768x281.png 768w, https:\/\/syrup-factory.com\/memo\/wp-content\/uploads\/2024\/11\/image-3.png 1046w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3>Note<\/h3>\n\n\n\n<p>\u5b9f\u969b\u306b\u306f\u3001\u4ee5\u4e0b\u306e\u51e6\u7406\u306b\u5bfe\u5fdc\u3057\u3066\u3044\u307e\u3059<\/p>\n\n\n\n<p>\u30fb\u3059\u3067\u306bredmine\u306b\u6642\u9593\u767b\u9332\u6e08\u307f\u304b\uff1f\u91cd\u8907\u30c1\u30a7\u30c3\u30af<br>\u30fbredmine\u306b\u8a72\u5f53\u756a\u53f7\u306e\u30c1\u30b1\u30c3\u30c8\u304c\u3042\u308b\u304b\uff1f\u30c1\u30a7\u30c3\u30af<br>\u30fb\u8a72\u5f53\u756a\u53f7\u306e\u30c1\u30b1\u30c3\u30c8\u306b\u305d\u306e\u30e6\u30fc\u30b6\u304c\u6a29\u9650\u3092\u6301\u3063\u3066\u3044\u308b\u304b\uff1f\u30c1\u30a7\u30c3\u30af<\/p>\n","protected":false},"excerpt":{"rendered":"<p>toggl\u3067\u52e4\u6020\u7ba1\u7406\u3001Redmine\u306e\u30c1\u30b1\u30c3\u30c8\u306b\u4f5c\u696d\u6642\u9593\u3092\u4ed8\u3051\u308b\u904b\u7528\u6642\u3001\u4e21\u8005\u306e\u5408\u8a08\u306f\u4e00\u81f4\u3059\u308b\u306f\u305a\u3060\u304c &#8230;<\/p>\n<p> <a class=\"continue-reading-link\" href=\"https:\/\/syrup-factory.com\/memo\/archives\/419\"><span>Continue reading<\/span><i class=\"crycon-right-dir\"><\/i><\/a> <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,5],"tags":[],"_links":{"self":[{"href":"https:\/\/syrup-factory.com\/memo\/wp-json\/wp\/v2\/posts\/419"}],"collection":[{"href":"https:\/\/syrup-factory.com\/memo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/syrup-factory.com\/memo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/syrup-factory.com\/memo\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/syrup-factory.com\/memo\/wp-json\/wp\/v2\/comments?post=419"}],"version-history":[{"count":10,"href":"https:\/\/syrup-factory.com\/memo\/wp-json\/wp\/v2\/posts\/419\/revisions"}],"predecessor-version":[{"id":440,"href":"https:\/\/syrup-factory.com\/memo\/wp-json\/wp\/v2\/posts\/419\/revisions\/440"}],"wp:attachment":[{"href":"https:\/\/syrup-factory.com\/memo\/wp-json\/wp\/v2\/media?parent=419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/syrup-factory.com\/memo\/wp-json\/wp\/v2\/categories?post=419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/syrup-factory.com\/memo\/wp-json\/wp\/v2\/tags?post=419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}