์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ™œ์„ฑํ™” ํ•ด์ฃผ์„ธ์š”

p085 Send Messages to Gitlab MR Assignees to Be Noticed Every 2 Hours

 ·   ·  โ˜• 1 min read

์‘์šฉ๋ถ€ํ„ฐ ๋งํ•˜๋ฉด

  • redmine, jira์— ๋‚ด๊ฐ€ ๋‹ด๋‹น์ด ๋œ ํ‹ฐ์ผ“์ด ์˜ฌ๋ผ์™”์„ ๋•Œ,
  • gitlab, github, bitbucket, jetbrains space ์— MR์ด ์™”์„ ๋•Œ
  • ํ†ต์ง€๋Š” ๋ฉ”์ผ, chatwork, slack, line, kakao ๋“ฑ๋“ฑ API๋ฅผ ์ง€์›ํ•˜๋Š” ์–ด๋Š๊ฒƒ์ด๋“ 

๋‚ด์šฉ์€ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. scheduled job์œผ๋กœ ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ๊ฐ OS์— ์žˆ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
์–ธ์–ด๋„ ์—ฌ๋Ÿฌ ์–ธ์–ด๋กœ ๊ตฌ์„ฑํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค๋งŒ, ํ˜„์žฌ ์œˆ๋„์šฐ์ฆˆ VM๊ฐ€ ์—…๋ฌด์ƒ ์ง€๊ธ‰๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ powershell๋กœ ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

assignee.ps1

 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
[CmdletBinding()]
param(
  [string]
  $gitlab_token = "my_gitlab_token",

  [string]
  $chatwork_token = "my_chatwork_token",

  [string]
  $chatwork_roomid = "my_room_id",

  [string]
  $cache = "c:\dc\.notified"
)

. c:\dc\users.ps1

# get merge_requests, opened, and assigned to someone
$groupid = 1234
$Uri = "https://gitlab.tkim.info/api/v4/groups/$groupid/merge_requests?state=opened&assginee_id=Any"
try {
  $merge_requests = Invoke-RestMethod `
    -Uri $Uri `
    -Method Get `
    -Headers @{ "PRIVATE-TOKEN" = $gitlab_token }
}
catch {
  throw $_
}
$merge_requests = $merge_requests | Where-Object { $_.assignee }

# get notified list
$notified = Get-Content -Path $cache | ConvertFrom-Json
if ($null -eq $notified) {
  $notified = @()
}

# get merge_requests, which is not notified yet
$new_merge_requests = $merge_requests | Where-Object { $_.id -notin $notified }
if (-not $new_merge_requests) {
  exit 0
}

# build messages
$messages = [Users].GetEnumNames() | ForEach-Object {
  $id = [int]([Users]::$_)
  return @{
    assignee       = $_
    merge_requests = ($new_merge_requests | Where-Object { $_.assignee.id -eq $id }).web_url
  }
}

$body = ""
$messages | ForEach-Object {
  if ($_.merge_requests) {
    $id = [int]([ChatworkUsers]::($_.assignee))
    $body += "[To:$id] Please review these merge requests:`r`n"
    $_.merge_requests | ForEach-Object {
      $body += "  $_`r`n"
    }
    $body += "`r`n"
  }
}

# send
if ($body) {
  $Uri = "https://api.chatwork.com/v2/rooms/$($chatwork_roomid)/messages"
  try {
    Invoke-RestMethod `
      -Uri $Uri `
      -Method Post `
      -Headers @{ "X-ChatworkToken" = $chatwork_token } `
      -Body @{"body" = $body }
  }
  catch {
    throw $_
  }

  # append notified
  Set-Content $cache ($new_merge_requests.id + $notified | ConvertTo-Json -Compress)
}

๋‹ค์Œ์˜ ์ด๋ฏธ์ง€๋Š”, ๊ทธ์ € ๋‚จ์•„์žˆ๋Š” MR์ด ์—†๋‹ค๋Š” ํ™”๋ฉด์ž…๋‹ˆ๋‹ค. :-)

p085_gitlab_no_mr_exists.png

Ref

  • None
๊ณต์œ ํ•˜๊ธฐ

tkim
๊ธ€์“ด์ด
tkim
Software Engineer