Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSOE-726: Migrate auto-resolve-done-jira-issue and create-jira-issues-for-community-activities to use API Key Manager for Jira #301

Closed
wants to merge 15 commits into from
Closed

This file was deleted.

63 changes: 63 additions & 0 deletions .github/actions/auto-resolve-done-jira-issue/Update-JiraIssue.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
param($Repository, $PullRequestNumber, $IsDone, $IsResolve)

# We need to fetch the PR details using the CLI (see https://cli.github.com/manual/gh_pr_view) as opposed to just using
# the context, because the title may have changed (by the user or the add-jira-issue-code-to-pull-request action) after
# the start of the run and that wouldn't be present in it.
$title = gh pr view $PullRequestNumber --repo $Repository --json title --template '{{.title}}'
$issueKey = Get-JiraIssueKeyFromPullRequestTitle $title

$transition = $IsDone ? 'Done' : ($IsResolve ? 'Resolve' : $null)

if ($null -eq $transition)
{
Write-Error 'Unknown Jira issue transition was selected.'
exit
}

$headers = @{
'apikey' = $Env:JIRA_API_KEY
'Content-Type' = 'application/json'
'Accept' = 'application/json'
}

$body = @{
options = @{
method = 'GET'
headers = @{
'Content-Type' = 'application/json'
'Accept' = 'application/json'
}
}
url = "/rest/api/3/issue/$issueKey/transitions"
} | ConvertTo-Json

$response = Invoke-RestMethod -Uri $Env:JIRA_ENDPOINT_URL -Method Get -Headers $headers -Body $body

$availableTransition = $response | Select-Object -ExpandProperty transitions | Where-Object { $_.name -eq $transition }

if ($null -ne $availableTransition)
{
Write-Output "Transition exists. $($availableTransition.id)"

$body = @{
options = @{
method = 'POST'
headers = @{
'Content-Type' = 'application/json'
'Accept' = 'application/json'
}
body = @{
transition = @{
id = $availableTransition.id
}
}
}
url = "/rest/api/3/issue/$issueKey/transitions"
} | ConvertTo-Json -Depth 3

$response = Invoke-RestMethod -Uri $Env:JIRA_ENDPOINT_URL -Method Post -Headers $headers -Body $body
}
else
{
Write-Warning "The ""$transition"" transition is not available for the issue."
}
25 changes: 3 additions & 22 deletions .github/actions/auto-resolve-done-jira-issue/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ runs:
label1: "resolve-jira-issue-if-checks-succeed"
label2: "merge-and-resolve-jira-issue-if-checks-succeed"

- name: Initialize Jira Issue Parameters
id: initialize-jira-issue-parameters
- name: Transition Jira Issue
if: steps.check-done.outputs.contains-label == 'true' || steps.check-resolve.outputs.contains-label == 'true'
shell: pwsh
run: |
Expand All @@ -39,30 +38,12 @@ runs:
IsDone = '${{ steps.check-done.outputs.contains-label }}' -eq 'True'
IsResolve = '${{ steps.check-resolve.outputs.contains-label }}' -eq 'True'
}
Initialize-IssueParameters @parameters

- name: Login to Jira
if: steps.initialize-jira-issue-parameters.outputs.can-transition == 'true'
# v3
uses: atlassian/gajira-login@ca13f8850ea309cf44a6e4e0c49d9aa48ac3ca4c
env:
JIRA_BASE_URL: ${{ env.JIRA_BASE_URL }}
JIRA_USER_EMAIL: ${{ env.JIRA_USER_EMAIL }}
JIRA_API_TOKEN: ${{ env.JIRA_API_TOKEN }}

- name: Transition Jira Issue
if: steps.initialize-jira-issue-parameters.outputs.can-transition == 'true'
# v3
uses: atlassian/gajira-transition@4749176faf14633954d72af7a44d7f2af01cc92b
with:
issue: ${{ steps.initialize-jira-issue-parameters.outputs.key }}
transition: ${{ steps.initialize-jira-issue-parameters.outputs.transition }}
Update-JiraIssue @parameters

- name: Remove Label
if: steps.initialize-jira-issue-parameters.outputs.can-transition == 'true'
# v2.0.0
uses: buildsville/add-remove-label@eeae411a9be2e173f2420e1644514edbecc4e835
with:
token: ${{ env.GITHUB_TOKEN }}
labels: resolve-jira-issue-if-checks-succeed, done-jira-issue-if-checks-succeed
labels: merge-and-resolve-jira-issue-if-checks-succeed, resolve-jira-issue-if-checks-succeed, done-jira-issue-if-checks-succeed
type: remove
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
param
(
$Summary,
$Description,
$Type,
$IssueComponent,
$LinkUrl,
$LinkTitle
)

$headers = @{
'apikey' = $Env:JIRA_API_KEY
'Content-Type' = 'application/json'
'Accept' = 'application/json'
}

function CreateIssue {
$body = @{
options = @{
method = 'POST'
headers = @{
'Content-Type' = 'application/json'
'Accept' = 'application/json'
}
body = @{
fields = @{
project = @{
key = $Env:JIRA_PROJECT_KEY
}
summary = $Summary
description = @{
type = 'doc'
version = 1
content = @(
@{
type = 'paragraph'
content = @(
@{
type = 'text'
text = $Description
}
)
}
)
}
issuetype = @{
name = $Type
}
labels = @('created-from-github')
}
}
}
url = "/rest/api/3/issue"
}

if (-not [string]::IsNullOrWhiteSpace($IssueComponent)) {
$body.options.body.fields += @{
components = @(@{
name = $IssueComponent
})
}
}

$body = $body | ConvertTo-Json -Depth 9

$response = Invoke-RestMethod -Uri $Env:JIRA_ENDPOINT_URL -Method Post -Headers $headers -Body $body
Write-Information "Jira issue created with the key $($response.key)." -InformationAction Continue
$response.key
}

function AddLink {
param($issueKey)

$body = @{
options = @{
method = 'POST'
headers = @{
'Content-Type' = 'application/json'
'Accept' = 'application/json'
}
body = @{
object = @{
url = $LinkUrl
title = $LinkTitle
}
}
}
url = "/rest/api/3/issue/$issueKey/remotelink"
} | ConvertTo-Json -Depth 3

Invoke-RestMethod -Uri $Env:JIRA_ENDPOINT_URL -Method Post -Headers $headers -Body $body
}

function GetIssueUrl {
param($issueKey)

$body = @{
options = @{
method = 'GET'
headers = @{
'Content-Type' = 'application/json'
'Accept' = 'application/json'
}
}
url = "/rest/api/3/serverInfo"
} | ConvertTo-Json

$response = Invoke-RestMethod -Uri $Env:JIRA_ENDPOINT_URL -Method Get -Headers $headers -Body $body

"$($response.baseUrl)/browse/$issueKey"
}

$issueKey = CreateIssue
AddLink $issueKey
Set-GitHubOutput 'issue-key' $issueKey
Set-GitHubOutput 'issue-url' (GetIssueUrl $issueKey)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ param
$SuffixIssueTitles,
$DiscussionJiraIssueDescription,
$IssueJiraIssueDescription,
$PullReqestJiraIssueDescription
$PullRequestJiraIssueDescription
)

$context = [string]::IsNullOrEmpty($IssueComponent) ? $GitHub.repository : $IssueComponent
Expand Down Expand Up @@ -47,7 +47,7 @@ switch ($GitHub.event_name)
'pull_request_target'
{
$summary = "Review `"$($GitHub.event.pull_request.title)`"$titleSuffix"
$description = $PullReqestJiraIssueDescription
$description = $PullRequestJiraIssueDescription
$linkUrl = $GitHub.event.pull_request.html_url
$linkTitle = 'GitHub pull request'
}
Expand All @@ -67,8 +67,10 @@ if ($null -eq $type)
$type = 'Task'
}

Set-GitHubOutput 'summary' $summary
Set-GitHubOutput 'json-description' $($description | ConvertTo-Json)
Set-GitHubOutput 'type' $type
Set-GitHubOutput 'link-url' $linkUrl
Set-GitHubOutput 'link-title' $linkTitle
[PSCustomObject]@{
Summary = $summary
Description = $description
Type = $type
LinkUrl = $linkUrl
LinkTitle = $linkTitle
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,9 @@ runs:

Set-GitHubOutput "is-issue-pr" $output

- name: Login to Jira
if: steps.issue-related-pr.outputs.is-issue-pr == 'False'
# v3
uses: atlassian/gajira-login@ca13f8850ea309cf44a6e4e0c49d9aa48ac3ca4c
env:
JIRA_BASE_URL: ${{ env.JIRA_BASE_URL }}
JIRA_USER_EMAIL: ${{ env.JIRA_USER_EMAIL }}
JIRA_API_TOKEN: ${{ env.JIRA_API_TOKEN }}

- name: Initialize Jira Issue Templates
if: steps.issue-related-pr.outputs.is-issue-pr == 'False'
shell: pwsh
run: Initialize-IssueTemplates

- name: Initialize Jira Issue Details
- name: Create Jira Issue
if: steps.issue-related-pr.outputs.is-issue-pr == 'False'
id: issue-details
id: create-issue
env:
# Necessary to pass this via an environment variable, because when assigned directly to a variable in the
# script, it can cause escaping issues with backtick characters (like a "`ulong..." causing "The Unicode escape
Expand All @@ -72,62 +58,27 @@ runs:

$templates = Initialize-IssueTemplates

Initialize-IssueDetails $github `
"${{ inputs.issue-component }}" `
$([System.Convert]::ToBoolean("${{ inputs.suffix-issue-titles }}")) `
$templates["DISCUSSION_JIRA_ISSUE_DESCRIPTION"] `
$templates["ISSUE_JIRA_ISSUE_DESCRIPTION"] `
$templates["PULL_REQUEST_JIRA_ISSUE_DESCRIPTION"]

$fields = @"
{"labels": ["created-from-github"]
"@

if ("${{ inputs.issue-component }}" -ne "")
{
$fields += ', "components": [{"name": "${{ inputs.issue-component }}"}]'
$params = @{
GitHub = $github
IssueComponent = '${{ inputs.issue-component }}'
SuffixIssueTitles = [System.Convert]::ToBoolean('${{ inputs.suffix-issue-titles }}')
DiscussionJiraIssueDescription = $templates['DISCUSSION_JIRA_ISSUE_DESCRIPTION']
IssueJiraIssueDescription = $templates['ISSUE_JIRA_ISSUE_DESCRIPTION']
PullRequestJiraIssueDescription = $templates['PULL_REQUEST_JIRA_ISSUE_DESCRIPTION']
}

$fields += '}'
$issueDetails = Initialize-IssueDetails @params

Set-GitHubOutput "fields" $fields

- name: Create Jira Issue
if: steps.issue-related-pr.outputs.is-issue-pr == 'False'
id: create-issue
# v3
uses: atlassian/gajira-create@1ff0b6bd115a780592b47bfbb63fc4629132e6ec
with:
project: ${{ env.JIRA_PROJECT_KEY }}
issuetype: ${{ steps.issue-details.outputs.type }}
summary: ${{ steps.issue-details.outputs.summary }}
description: ${{ fromJSON(steps.issue-details.outputs.json-description) }}
fields: ${{ steps.issue-details.outputs.fields }}

- name: Add Remote Link to Jira Issue
if: steps.issue-related-pr.outputs.is-issue-pr == 'False'
shell: pwsh
run: |
$linkProperties = @{
object = @{
url = '${{ steps.issue-details.outputs.link-url }}'
title = '${{ steps.issue-details.outputs.link-title }}'
}
}
$authorizationBytes = [Text.Encoding]::ASCII.GetBytes('${{ env.JIRA_USER_EMAIL }}:${{ env.JIRA_API_TOKEN }}')
$authorizationBytesBase64 = [Convert]::ToBase64String($authorizationBytes)
$requestParameters = @{
Uri = '${{ env.JIRA_BASE_URL }}/rest/api/3/issue/${{ steps.create-issue.outputs.issue }}/remotelink'
Method = 'POST'
ContentType = 'application/json'
Headers = @{
Accept = 'application/json'
Authorization = "Basic $authorizationBytesBase64"
}
Body = ConvertTo-Json($linkProperties)
$params = @{
Summary = $issueDetails.Summary
Description = $issueDetails.Description
Type = $issueDetails.Type
IssueComponent = '${{ inputs.issue-component }}'
LinkUrl = $issueDetails.LinkUrl
LinkTitle = $issueDetails.LinkTitle
}

Invoke-WebRequest @requestParameters
Add-JiraIssue @params

- name: Update GitHub Issue
if: steps.issue-related-pr.outputs.is-issue-pr == 'False' && github.event.issue
Expand All @@ -137,6 +88,6 @@ runs:
actions: "update-issue"
token: ${{ env.GITHUB_TOKEN }}
issue-number: ${{ github.event.issue.number }}
title: ${{ github.event.issue.title }} (${{ steps.create-issue.outputs.issue }})
body: "${{ github.event.issue.body }}\n\n[Jira issue](${{ env.JIRA_BASE_URL }}/browse/${{ steps.create-issue.outputs.issue }})"
title: ${{ github.event.issue.title }} (${{ steps.create-issue.outputs.issue-key }})
body: "${{ github.event.issue.body }}\n\n[Jira issue](${{ steps.create-issue.outputs.issue-url }})"
update-mode: "replace"
Loading