Skip to content

chore(deps): update microsoft to v10 (major) #86

chore(deps): update microsoft to v10 (major)

chore(deps): update microsoft to v10 (major) #86

name: Jira
on:
pull_request:
paths-ignore: ["*.md"]
workflow_call:
inputs:
branch_name:
description: 'The branch name to use for Jira (defaults to the triggering branch)'
type: string
required: false
default: ${{ github.head_ref || github.ref_name }}
is_CI:
description: 'Whether the workflow runs in a test mode (skips certain steps)'
type: boolean
required: false
default: false
jobs:
main:
runs-on: [idp]
permissions:
id-token: write
contents: read
pull-requests: write
steps:
- id: get_client_id
env:
INFRA_CONFIG: ${{ vars.INFRA_CONFIG }}
AZURE_CLIENT_ID: ${{ vars.AZURE_CLIENT_ID }}
shell: pwsh
run: |
if ($env:AZURE_CLIENT_ID) {
$ClientId = $env:AZURE_CLIENT_ID
} elseif ($env:INFRA_CONFIG) {
$ClientId = (ConvertFrom-Json $env:INFRA_CONFIG).repo_identity_client_id
} else {
Write-Error "Neither AZURE_CLIENT_ID nor INFRA_CONFIG is set. Cannot determine client id."
exit 1
}
echo "client_id=$ClientId"
"client_id=$ClientId" >> $env:GITHUB_OUTPUT
- name: Get JiraApiToken secret
id: get_jira_api_secret
uses: workleap/wl-reusable-workflows/retrieve-managed-secret@main
with:
azure-client-id: ${{ steps.get_client_id.outputs.client_id }}
azure-tenant-id: ${{ vars.AZURE_TENANT_ID }}
azure-subscription-id: ${{ vars.WORKLEAP_GLOBAL_KEYVAULT_SUBSCRIPTION_ID }}
keyvault-name: ${{ vars.WORKLEAP_GLOBAL_KEYVAULT_NAME }}
secret-name: "JiraApiToken"
- name: Get JiraUsername secret
id: get_jira_username_secret
uses: workleap/wl-reusable-workflows/retrieve-managed-secret@main
with:
azure-client-id: ${{ steps.get_client_id.outputs.client_id }}
azure-tenant-id: ${{ vars.AZURE_TENANT_ID }}
azure-subscription-id: ${{ vars.WORKLEAP_GLOBAL_KEYVAULT_SUBSCRIPTION_ID }}
keyvault-name: ${{ vars.WORKLEAP_GLOBAL_KEYVAULT_NAME }}
secret-name: "JiraUsername"
- name: Get branch name
id: branch_name
shell: pwsh
run: |
$BranchName = "${{ inputs.branch_name || github.head_ref || github.ref_name }}"
Write-Host "Branch name: $BranchName"
"branch_name=$BranchName" >> $env:GITHUB_OUTPUT
- name: Extract Jira Issue
id: extract_jira
uses: workleap/wl-reusable-workflows/extract-jira-issue@main
with:
branch_name: ${{ steps.branch_name.outputs.branch_name }}
- name: Check Jira Story
id: check_jira
shell: pwsh
env:
BranchName: "${{steps.branch_name.outputs.branch_name}}"
BranchPattern: "${{steps.extract_jira.outputs.pattern}}"
JiraMatches: "${{steps.extract_jira.outputs.matches}}"
JiraIssueKeys: "${{steps.extract_jira.outputs.jira_issue_matches}}"
run: |
Install-Module JiraPS -Scope CurrentUser -Force
if ("$env:BranchName" -like "*renovate/*" -eq $True)
{
Write-Host "Skipping, renovate branch detected"
return
}
if ("$env:BranchName" -like "copilot/*" -eq $True)
{
Write-Host "Skipping, copilot branch detected"
return
}
Set-JiraConfigServer -Server "${{ vars.JIRA_URL }}"
if("$env:JiraMatches" -eq "false")
{
throw "Branch name '$env:BranchName' doesn't respect the required pattern $env:BranchPattern. A valid branch name example would be: feature/PRJ-123"
}
$PWord = ConvertTo-SecureString -String "${{ steps.get_jira_api_secret.outputs.secret }}" -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "${{ steps.get_jira_username_secret.outputs.secret }}", $PWord
$issueKeys = $env:JiraIssueKeys | ConvertFrom-Json
$found = $false
foreach ($key in $issueKeys) {
$key = $key.Trim()
Write-Host "Retrieving Jira issue with number $key"
try {
Get-JiraIssue -Key $key -Credential $Credential -ErrorAction Stop | out-null
Write-Host "Successfully retrieved Jira issue $key"
$found = $true
"jira_issue=$key" >> $env:GITHUB_OUTPUT
break
}
catch {
Write-Host "Failed to retrieve Jira issue $key : $_"
}
}
if (-not $found) {
throw "No valid Jira issue found for any of the keys: $env:JiraIssueKeys"
}
- name: Add Jira issue link
shell: pwsh
env:
BranchName: "${{ steps.branch_name.outputs.branch_name }}"
BranchPattern: "${{ steps.extract_jira.outputs.pattern }}"
JiraMatches: "${{ steps.extract_jira.outputs.matches }}"
JiraIssue: "${{ steps.check_jira.outputs.jira_issue }}"
run: |
# We don't want the Jira link to come from the test workflow since we're using a fixed branch name
if ("${{ inputs.is_CI }}" -eq $true) {
Write-Host 'Running in test mode, skipping adding Jira link to pull request description.'
exit 0
}
if (-not "${{ github.event.pull_request.number }}") {
Write-Host 'No pull request context. Skipping adding Jira link to pull request description.'
exit 0
}
Write-Host 'Adding JIRA link to pull request description'
$repo = "${{ github.repository }}"
$prNumber = "${{ github.event.pull_request.number }}"
# Not all valid branch names will match the pattern (e.g. renovate branches)
if("$env:JiraMatches" -eq "true") {
$jiraLinkUrl = "https://workleap.atlassian.net/browse/$env:JiraIssue"
$jiraLinkDescription = "Jira issue link: [$env:JiraIssue]($jiraLinkUrl)"
$token = "${{ github.token }}"
$headers = @{
Authorization = "Bearer $token"
"Accept" = "application/vnd.github.v3+json"
}
$prUrl = "https://api.github.com/repos/$repo/pulls/$prNumber"
$pr = Invoke-RestMethod -Uri $prUrl -Headers $headers
if ($null -ne $pr.body -and ($pr.body -match "$jiraLinkUrl")) {
$newBody = $pr.body
} else {
$newBody = "$jiraLinkDescription`n`n$($pr.body)"
}
if ($newBody -ne $pr.body) {
$body = @{ body = $newBody } | ConvertTo-Json
Invoke-RestMethod -Uri $prUrl -Headers $headers -Method Patch -Body $body
Write-Host 'Successfully added JIRA link to pull request description'
} else {
Write-Host 'JIRA link already present in pull request description'
}
} else {
Write-Host "Branch name '$env:BranchName' does not match the Jira pattern $env:BranchPattern. Skipping JIRA link insertion."
}