-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixVersionConfluence.py
131 lines (100 loc) · 5.15 KB
/
fixVersionConfluence.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from atlassian import Confluence
from airflow.models import Variable
from airflow.decorators import dag, task
from airflow.operators.python import PythonOperator
from airflow.providers.atlassian.jira.hooks.jira import JiraHook
from datetime import datetime
def fix_version():
# Jira Configuration
jira_url = Variable.get("JIRA_URL")
jira_username = Variable.get("JIRA_USERNAME")
jira_api_token = Variable.get("JIRA_API_TOKEN")
jira_project_key = Variable.get("JIRA_PROJECT_KEY")
custom_field_id = Variable.get("CUSTOM_FIELD_ID")
# Confluence Configuration
confluence_url = Variable.get("CONFLUENCE_URL")
confluence_username = Variable.get("CONFLUENCE_USERNAME")
confluence_api_token = Variable.get("JIRA_API_TOKEN")
confluence_page_id = Variable.get("CONFLUENCE_PAGE_ID")
# Initialize Jira
# jira = jc.JIRA(server=jira_url, basic_auth=(jira_username, jira_api_token))
jira = JiraHook(jira_conn_id='jira-user').get_conn()
# Query Jira for issues updated since August 10, 2023
query = f'project = {jira_project_key} AND issuetype = Features AND fixVersion CHANGED AFTER "2023-08-10" AND key != PMZ-721'
issues = jira.jql(query, fields="summary,status,fixVersions,created,components,customfield_13085")
# Calculate date seven days ago
seven_days_ago = ('August 10, 2023')
# Initialize a list to store extracted results
results = []
# Iterate through the fetched issues
for issue in issues.get('issues'):
issue_key = issue.get('key')
summary = issue.get('fields').get('summary')
created_date = issue.get('fields').get('created')[:10]
components = issue.get('fields').get('components')
# Fetch issue changelogs
changelogs = jira.issue(issue_key, expand='changelog').get('changelog').get('histories')
# Initialize variables to store previous values
prev_fix_versions = []
# Extract change details
for change in changelogs:
for item in change.get('items'):
if item.get('field') == 'Fix Version':
# Check if the 'fromString' attribute exists in the item and it's not None
# if hasattr(item, 'fromString') and item.fromString is not None:
if item.get('fromString') is not None:
prev_fix_versions.append(item.get('fromString'))
# Get the most recent Fix Version value from the changelog
most_recent_prev_fix_version = prev_fix_versions[-1] if prev_fix_versions else None
# Get the current "fixVersions" associated with the Jira issue
current_fix_versions = [fix_version.get('name') for fix_version in
issue.get('fields').get('fixVersions')] if issue.get('fields').get(
'fixVersions') else []
# Convert components objects to a list of component names
component_names = ', '.join([component.get('name') for component in components]) if components else ''
# Access the custom field 'customfield_13085' (User Picker - Single User)
engman_value = issue.get('fields').get(custom_field_id)
# Check if there are changes to the fix version
if most_recent_prev_fix_version is not None:
# Add the relevant information to the results list
results.append({
'key': issue_key,
'summary': summary,
'status': issue.get('fields').get('status').get('name'),
'created_date': created_date,
'previous_fix_versions': most_recent_prev_fix_version,
'current_fix_versions': ', '.join(current_fix_versions),
'components': component_names,
'engman': engman_value.get('displayName') if engman_value else '', # Get the display name of the user
})
# Initialize Confluence
confluence = Confluence(
url=confluence_url,
username=confluence_username,
password=confluence_api_token)
# Construct the content for the Confluence page
page_content = f"Updated Issue Fix Versions Since {seven_days_ago}\n\n"
# Add a table with headers
page_content += "|| Issue Key || Summary || Status || Created Date || Previous Fix Versions || Current Fix Versions || Component || Engineering Manager ||\n"
# Add rows to the table with issue details
for result in results:
row = (
f"| {result['key']} | {result['summary']} | {result['status']} | "
f"{result['created_date']} | {result['previous_fix_versions']} | {result['current_fix_versions']} | {result['components']} | {result['engman']} |\n"
)
page_content += row
# Update the Confluence page
confluence.update_page(
page_id=confluence_page_id,
title=f"Updated Issue Fix Versions Since August 11, 2023",
body=page_content,
representation='wiki'
)
@dag(dag_id='Jira-FixVersionConfluence', schedule_interval=None, start_date=datetime(2023, 9, 1), catchup=False)
def base_function():
execute_fix_version = PythonOperator(
task_id='ExecuteCode',
python_callable=fix_version
)
execute_fix_version
base_function()