Skip to content

Instantly share code, notes, and snippets.

@TABETA
Last active March 10, 2018 23:24
Show Gist options
  • Save TABETA/adfc926d2ef3cf4c624985dd60797d09 to your computer and use it in GitHub Desktop.
Save TABETA/adfc926d2ef3cf4c624985dd60797d09 to your computer and use it in GitHub Desktop.
JsonConverterに依存。
'左から順番に ID project summary issueType EpicName assignee 初期見積 残余見積 先行ID JIRAID 先行JIRAID と並んでいる前提
Option Explicit
Sub 課題作成()
Dim j As jira
Set j = New jira
Dim row As Long: row = 2
With ThisWorkbook.Sheets(1)
Do While .Cells(row, 1).Value <> ""
If .Cells(row, 10).Value = "" Then
Dim i As Long: i = 2
Dim project: project = .Cells(row, i).Value: i = i + 1
Dim summary: summary = .Cells(row, i).Value: i = i + 1
Dim issueType: issueType = .Cells(row, i).Value: i = i + 1
Dim epicName: epicName = .Cells(row, i).Value: i = i + 1
Dim assignee: assignee = .Cells(row, i).Value: i = i + 1
Dim jiraID: jiraID = j.createIssue(project, summary, issueType, epicName, assignee, 0, 0)
.Cells(row, 10).Value = jiraID
Else
End If
row = row + 1
Loop
End With
End Sub
Sub 課題リンク()
Dim j As jira
Set j = New jira
Dim row As Long: row = 2
With ThisWorkbook.Sheets(1)
Do While .Cells(row, 1).Value <> ""
Dim inward: inward = .Cells(row, 11).Value
Dim outward: outward = .Cells(row, 10).Value
If inward <> "" And outward <> "" Then
Call j.linkIssue(inward, outward)
Else
End If
row = row + 1
Loop
End With
End Sub
Option Explicit
Private JiraService As New MSXML2.XMLHTTP60
Private JiraAuth As New MSXML2.XMLHTTP60
Private sCookie
Const sJIRAUserID = ""
Const sJIRAPass = ""
Const url = "https://<name>.atlassian.net/rest/"
Private Sub Class_Initialize()
Dim sErg
With JiraAuth
.Open "POST", url & "auth/1/session", False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Accept", "application/json"
.setRequestHeader "X-Atlassian-Token", "no-check"
.send "{""username"" : """ & sJIRAUserID & """, ""password"" : """ & sJIRAPass & """}"
sErg = .responseText
If .Status = "200" Then
sCookie = "JSESSIONID=" & Mid(sErg, 42, 32) & "; Path=/Jira"
Else
MsgBox "Login failed"
End If
End With
End Sub
Public Function createIssue(project, summary, issueType, epicName, assignee, 初期見積, 残余見積)
Dim sAux As Variant
Dim sStatus As Variant
Dim sRestAntwort
Dim sProject: sProject = """project"" : { ""key"" : """ & project & """ }"
Dim sSummary: sSummary = """summary"" : """ & summary & """"
Dim sIssueType: sIssueType = """issuetype"" : { ""name"" : """ & issueType & """ }"
Dim sEpicName: sEpicName = """customfield_10019"":""" & epicName & """"
Dim sAssignee: sAssignee = """assignee"" : { ""name"" : """ & assignee & """ }"
Dim sData As Variant
If issueType = "Epic" Then
sData = " { ""fields"" : { " & sProject & ", " & sSummary & ", " & sIssueType & ", " & sEpicName & ", " & sAssignee & " } } "
Else
sData = " { ""fields"" : { " & sProject & ", " & sSummary & ", " & sIssueType & ", " & sAssignee & " } } "
End If
With JiraService
.Open "POST", url & "api/2/issue/", False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Accept", "application/json"
.setRequestHeader "X-Atlassian-Token", "no-check"
.setRequestHeader "Set-Cookie", sCookie
.send sData
sRestAntwort = .responseText
sStatus = .Status & " | " & .statusText
If .Status = "201" Then
sAux = Replace(sRestAntwort, "{", "")
sAux = Replace(sAux, "}", "")
sAux = Split(sAux, ",")
sAux = sAux(1)
sAux = Split(sAux, ":")
sAux = sAux(1)
sAux = Replace(sAux, """", "")
createIssue = sAux
Else
MsgBox "Create issue failed:" & sStatus
End If
End With
End Function
Public Sub linkIssue(inward, outward)
Dim sStatus As Variant
Dim sRestAntwort
Dim sData: sData = "{""type"": {""name"": ""Blocks""},""inwardIssue"": {""key"": """ & inward & """},""outwardIssue"": {""key"": """ & outward & """}}"
With JiraService
.Open "POST", url & "api/2/issueLink", False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Accept", "application/json"
.setRequestHeader "X-Atlassian-Token", "no-check"
.setRequestHeader "Set-Cookie", sCookie
.send sData
sRestAntwort = .responseText
sStatus = .Status & " | " & .statusText
If .Status = "201" Then
Else
MsgBox "Link issue failed:" & sStatus
End If
End With
End Sub
Public Function getIssue(ByVal issueKey) As JiraIssue
Dim sRestAntwort
With JiraService
.Open "GET", url & "api/2/issue/" & issueKey, False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Accept", "application/json"
.setRequestHeader "X-Atlassian-Token", "no-check"
.setRequestHeader "Set-Cookie", sCookie
.send
sRestAntwort = .responseText
End With
Dim Json As Object
Set Json = JsonConverter.ParseJson(sRestAntwort)
Dim ji As JiraIssue
ji.Summary = Json("fields")("summary")
ji.Assignee = Json("fields")("assignee")("name")
Dim LinkedIssues
For Each LinkedIssues In Json("fields")("issuelinks")
If Not IsEmpty(LinkedIssues("inwardIssue")) Then
If IsEmpty(ji.Predecessors) Then
ji.Predecessors = Array(LinkedIssues("inwardIssue")("key"))
Else
Dim i As Integer
i = UBound(ji.Predecessors)
ReDim Preserve ji.Predecessors(i + 1)
ji.Predecessors(i + 1) = LinkedIssues("inwardIssue")("key")
End If
End If
Next
ji.OriginalEstimate = Json("fields")("timetracking")("originalEstimate")
ji.RemainingEstimate = Json("fields")("timetracking")("remainingEstimate")
ji.TimeSpent = Json("fields")("timetracking")("timeSpent")
ji.Started = Json("fields")("started")
ji.ResolutionDate = IIf(IsNull(Json("fields")("resolutiondate")), "", Json("fields")("resolutiondate"))
getIssue = ji
End Function
Private Sub Class_Terminate()
With JiraAuth
.Open "DELETE", url & "auth/1/session", False
.send
End With
End Sub
Public Type JiraIssue
Id As String
Summary As String
Assignee As String
Predecessors As Variant
OriginalEstimate As String
RemainingEstimate As String
TimeSpent As String
Started As String
ResolutionDate As String
End Type
Sub test()
Dim prj As Project
Set prj = ThisProject
Dim tsk As Task
Dim pTsk As Task
Dim r As Resource
Dim j As JIRA
Set j = New JIRA
For Each tsk In prj.Tasks
Dim ji As JiraIssue
If tsk Is Nothing Then
ElseIf tsk.Text1 = "" Then
Else
ji = j.getIssue(tsk.Text1)
tsk.Name = ji.Summary
If Right(ji.Summary, 1) = ")" Then
Dim assigneeName As String
assigneeName = Mid(ji.Summary, InStrRev(ji.Summary, "(") + 1)
tsk.ResourceNames = Left(assigneeName, Len(assigneeName) - 1)
Else
tsk.ResourceNames = ji.Assignee
End If
Dim t As Task
If Not IsEmpty(ji.Predecessors) Then
Dim i As Integer
For i = LBound(ji.Predecessors) To UBound(ji.Predecessors)
For Each t In prj.Tasks
If t.Text1 = ji.Predecessors(i) Then
ji.Predecessors(i) = t.Id
Exit For
End If
Next
Next
tsk.Predecessors = Join(ji.Predecessors, ",")
End If
tsk.BaselineWork = ji.OriginalEstimate
If Not tsk.Summary Then
tsk.ActualStart = Left(ji.Started, 10)
tsk.ActualFinish = Left(ji.ResolutionDate, 10)
End If
End If
Next
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment