PowerShell でRedmineのチケットのステータスが 終了 したフォルダを別のフォルダに移動する

前回の内容に依存する処理になる。

kameyatakefumi.hatenablog.com

前回の内容では、ローカルに Redmine のチケットNo.と同じフォルダを作成していた。
どんどんフォルダができると可視性が悪くなるので、ステータスが 終了 したチケットのフォルダを別のフォルダに移動させる事にした。

前提条件

  • Windows 10 Professional
  • $PSVersionTable PSVersion=5.1.15063.1387
  • ファイルを選択し右クリック → PowerShell で実行
  • 別のフォルダ = finishedフォルダ

create_redmine_folder.ps1

$apiAccessKey = "{APIアクセスキー}"
$finishedFolder = "$PSScriptRoot\finished"

if (!(Test-Path -Path $finishedFolder)) {
    New-Item $finishedFolder -ItemType Directory
}

$folders = Get-ChildItem -Path $PSScriptRoot -Directory
foreach($folder in $folders) {

    if (0 -ne $folder.Name.LastIndexOf("#")) {
        continue
    }
    
    $ticketId = $folder.Name.remove(0, 1)
    $ticketUrl = "http://{ドメイン名}/issues/$ticketId"
    
    $response = Invoke-RestMethod -Uri "$ticketUrl.json?key=$apiAccessKey"

    if ("終了" -ne $response.issue.status.name) {
        continue
    }

    Move-Item -Path $folder.FullName -Destination $finishedFolder
    
    Write-Host "$($response.issue.status.name) #$ticketId $($response.issue.subject)"
}

Pause

PowerShellInvoke-RestMethod は、かなり便利。
Jsonを取得し、なおかつオブジェクトで扱えるようになる。
最高。