Skip to content

Commit 68e3d16

Browse files
[patch] remove pipeline name from the messages
1 parent 7b559fe commit 68e3d16

2 files changed

Lines changed: 41 additions & 98 deletions

File tree

bin/mas-devops-notify-slack

Lines changed: 28 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -97,82 +97,53 @@ def notifyProvisionRoks(channels: list[str], rc: int, additionalMsg: str | None
9797
return response.data.get("ok", False)
9898

9999

100-
def notifyPipelineStart(channels: list[str], pipelineName: str, instanceId: str | None = None) -> bool:
100+
def notifyPipelineStart(channels: list[str], instanceId: str | None = None) -> dict | None:
101101
"""Send Slack notification about pipeline start and create thread."""
102102
namespace = f"mas-{instanceId}-pipelines"
103-
pipelineRunName = os.getenv("PIPELINERUN_NAME", "")
104103
if instanceId is None or instanceId == "":
105104
print("instanceId must be set")
106105
sys.exit(1)
107-
if namespace == "" or pipelineRunName == "":
108-
print("PIPELINE_NAMESPACE and PIPELINERUN_NAME env vars must be set")
109-
sys.exit(1)
110-
106+
111107
# Check if thread already exists
112-
threadInfo = SlackUtil.getThreadConfigMap(namespace, pipelineRunName)
108+
threadInfo = SlackUtil.getThreadConfigMap(namespace, instanceId)
113109
if threadInfo is not None:
114110
print("Pipeline start notification already sent")
115-
return True
116-
111+
return threadInfo
112+
117113
# Send pipeline started message
118114
toolchainLink = _getToolchainLink()
119115
instanceInfo = f"\nInstance ID: `{instanceId}`" if instanceId else ""
120116
message = [
121-
SlackUtil.buildHeader(f"🚀 MAS {pipelineName.title()} Pipeline Started"),
122-
SlackUtil.buildSection(f"Pipeline Run: `{pipelineRunName}`{instanceInfo}\n{toolchainLink}")
117+
SlackUtil.buildHeader("🚀 MAS Pipeline Started"),
118+
SlackUtil.buildSection(f"Pipeline Run: {instanceInfo}\n{toolchainLink}")
123119
]
124120
response = SlackUtil.postMessageBlocks(channels[0], message)
125121
if response.data.get("ok", False):
126122
threadId = response["ts"]
127123
channelId = response["channel"]
128124
# Store thread information in ConfigMap
129-
SlackUtil.createThreadConfigMap(namespace, pipelineRunName, channelId, threadId, pipelineName)
125+
SlackUtil.createThreadConfigMap(namespace, channelId, threadId, instanceId)
130126
return True
131127
else:
132128
print("Failed to send pipeline start Slack message")
133129
return False
134130

135131

136-
def notifyAnsibleStart(channels: list[str], taskName: str, pipelineName: str, instanceId: str | None = None) -> bool:
132+
def notifyAnsibleStart(channels: list[str], taskName: str, instanceId: str | None = None) -> bool:
137133
"""Send Slack notification about Ansible task start."""
138134
namespace = f"mas-{instanceId}-pipelines"
139-
pipelineRunName = os.getenv("PIPELINERUN_NAME", "")
140135
if instanceId is None or instanceId == "":
141136
print("instanceId must be set")
142137
sys.exit(1)
143-
if namespace == "" or pipelineRunName == "":
144-
print("PIPELINE_NAMESPACE and PIPELINERUN_NAME env vars must be set")
145-
sys.exit(1)
146-
138+
147139
# Get thread information, create if doesn't exist
148-
threadInfo = SlackUtil.getThreadConfigMap(namespace, pipelineRunName)
140+
threadInfo = SlackUtil.getThreadConfigMap(namespace, instanceId)
149141
if threadInfo is None:
150142
print("No thread found - creating pipeline start notification")
151-
# Send pipeline started message
152-
toolchainLink = _getToolchainLink()
153-
instanceInfo = f"\nInstance ID: `{instanceId}`" if instanceId else ""
154-
message = [
155-
SlackUtil.buildHeader(f"🚀 MAS {pipelineName.title()} Pipeline Started"),
156-
SlackUtil.buildSection(f"Pipeline Run: `{pipelineRunName}`{instanceInfo}\n{toolchainLink}")
157-
]
158-
response = SlackUtil.postMessageBlocks(channels[0], message)
159-
if response.data.get("ok", False):
160-
threadId = response["ts"]
161-
channelId = response["channel"]
162-
# Store thread information in ConfigMap
163-
SlackUtil.createThreadConfigMap(namespace, pipelineRunName, channelId, threadId, pipelineName)
164-
threadInfo = {
165-
"threadId": threadId,
166-
"channelId": channelId,
167-
"pipelineName": pipelineName
168-
}
169-
else:
170-
print("Failed to send pipeline start Slack message")
171-
return False
172-
143+
notifyPipelineStart(channels, instanceId)
173144
threadId = threadInfo.get("threadId")
174145
channelId = threadInfo.get("channelId")
175-
146+
176147
# Send task start message as thread reply
177148
taskMessage = [
178149
SlackUtil.buildSection(f"⏳ **{taskName}** - Started")
@@ -183,43 +154,18 @@ def notifyAnsibleStart(channels: list[str], taskName: str, pipelineName: str, in
183154
return response.data.get("ok", False)
184155

185156

186-
def notifyAnsibleComplete(channels: list[str], rc: int, taskName: str, pipelineName: str, instanceId: str | None = None) -> bool:
157+
def notifyAnsibleComplete(channels: list[str], rc: int, taskName: str, instanceId: str | None = None) -> bool:
187158
"""Send Slack notification about Ansible task completion status."""
188159
namespace = f"mas-{instanceId}-pipelines"
189-
pipelineRunName = os.getenv("PIPELINERUN_NAME", "")
190160
if instanceId is None or instanceId == "":
191161
print("instanceId must be set")
192162
sys.exit(1)
193-
if namespace == "" or pipelineRunName == "":
194-
print("PIPELINE_NAMESPACE and PIPELINERUN_NAME env vars must be set")
195-
sys.exit(1)
196-
163+
197164
# Get thread information, create if doesn't exist
198-
threadInfo = SlackUtil.getThreadConfigMap(namespace, pipelineRunName)
165+
threadInfo = SlackUtil.getThreadConfigMap(namespace, instanceId)
199166
if threadInfo is None:
200167
print("No thread found - creating pipeline start notification")
201-
# Send pipeline started message
202-
toolchainLink = _getToolchainLink()
203-
instanceInfo = f"\nInstance ID: `{instanceId}`" if instanceId else ""
204-
message = [
205-
SlackUtil.buildHeader(f"🚀 MAS {pipelineName.title()} Pipeline Started"),
206-
SlackUtil.buildSection(f"Pipeline Run: `{pipelineRunName}`{instanceInfo}\n{toolchainLink}")
207-
]
208-
response = SlackUtil.postMessageBlocks(channels[0], message)
209-
if response.data.get("ok", False):
210-
threadId = response["ts"]
211-
channelId = response["channel"]
212-
# Store thread information in ConfigMap
213-
SlackUtil.createThreadConfigMap(namespace, pipelineRunName, channelId, threadId, pipelineName)
214-
threadInfo = {
215-
"threadId": threadId,
216-
"channelId": channelId,
217-
"pipelineName": pipelineName
218-
}
219-
else:
220-
print("Failed to send pipeline start Slack message")
221-
return False
222-
168+
notifyPipelineStart(channels, instanceId)
223169
# Send task completion message as thread reply
224170
threadId = threadInfo.get("threadId")
225171
channelId = threadInfo.get("channelId")
@@ -240,15 +186,14 @@ def notifyAnsibleComplete(channels: list[str], rc: int, taskName: str, pipelineN
240186
return response.data.get("ok", False)
241187

242188

243-
def notifyPipelineComplete(channels: list[str], rc: int, pipelineName: str, instanceId: str | None = None) -> bool:
189+
def notifyPipelineComplete(channels: list[str], rc: int, instanceId: str | None = None) -> bool:
244190
"""Send Slack notification about pipeline completion and cleanup ConfigMap."""
245191
namespace = f"mas-{instanceId}-pipelines"
246-
pipelineRunName = os.getenv("PIPELINERUN_NAME", "")
247-
if namespace == "" or pipelineRunName == "":
248-
print("PIPELINE_NAMESPACE and PIPELINERUN_NAME env vars must be set")
192+
if instanceId is None or instanceId == "":
193+
print("instanceId must be set")
249194
sys.exit(1)
250195
# Get thread information
251-
threadInfo = SlackUtil.getThreadConfigMap(namespace, pipelineRunName)
196+
threadInfo = SlackUtil.getThreadConfigMap(namespace, instanceId)
252197
if threadInfo is None:
253198
print("No thread information found - pipeline may not have started properly")
254199
return False
@@ -281,12 +226,12 @@ def notifyPipelineComplete(channels: list[str], rc: int, pipelineName: str, inst
281226
status = "Failed"
282227
additionalInfo = f"\nPipeline failed with return code: `{rc}`"
283228
message = [
284-
SlackUtil.buildHeader(f"{emoji} MAS {pipelineName.title()} Pipeline {status}"),
285-
SlackUtil.buildSection(f"Pipeline Run: `{pipelineRunName}`{instanceInfo}{durationText}{additionalInfo}")
229+
SlackUtil.buildHeader(f"{emoji} MAS Pipeline {status}"),
230+
SlackUtil.buildSection(f"Pipeline Run: {instanceInfo}{durationText}{additionalInfo}")
286231
]
287232
response = SlackUtil.postMessageBlocks(channelId, message, threadId)
288233
# Clean up ConfigMap
289-
SlackUtil.deleteThreadConfigMap(namespace, pipelineRunName)
234+
SlackUtil.deleteThreadConfigMap(namespace, instanceId)
290235
if isinstance(response, list):
291236
return all([res.data.get("ok", False) for res in response])
292237
return response.data.get("ok", False)
@@ -310,7 +255,6 @@ if __name__ == "__main__":
310255
parser.add_argument("--rc", required=True, type=int)
311256
parser.add_argument("--msg", required=False, default=None)
312257
parser.add_argument("--task-name", required=False, default="")
313-
parser.add_argument("--pipeline-name", required=False, default="")
314258
parser.add_argument("--instance-id", required=False, default=None)
315259

316260
args, unknown = parser.parse_known_args()
@@ -320,10 +264,10 @@ if __name__ == "__main__":
320264
elif args.action == "ocp-provision-roks":
321265
notifyProvisionRoks(channelList, args.rc, args.msg)
322266
elif args.action == "pipeline-start":
323-
notifyPipelineStart(channelList, args.pipeline_name, args.instance_id)
267+
notifyPipelineStart(channelList, args.instance_id)
324268
elif args.action == "ansible-start":
325-
notifyAnsibleStart(channelList, args.task_name, args.pipeline_name, args.instance_id)
269+
notifyAnsibleStart(channelList, args.task_name, args.instance_id)
326270
elif args.action == "ansible-complete":
327-
notifyAnsibleComplete(channelList, args.rc, args.task_name, args.pipeline_name, args.instance_id)
271+
notifyAnsibleComplete(channelList, args.rc, args.task_name, args.instance_id)
328272
elif args.action == "pipeline-complete":
329-
notifyPipelineComplete(channelList, args.rc, args.pipeline_name, args.instance_id)
273+
notifyPipelineComplete(channelList, args.rc, args.instance_id)

src/mas/devops/slack.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from slack_sdk import WebClient
1515
from slack_sdk.web.slack_response import SlackResponse
1616
from kubernetes import client, config
17-
from datetime import datetime
17+
from datetime import datetime, timezone
1818
import logging
1919

2020
logger = logging.getLogger(__name__)
@@ -271,16 +271,15 @@ def buildDivider(cls) -> dict:
271271
Returns:
272272
dict: Slack block kit divider element
273273
"""
274-
def createThreadConfigMap(cls, namespace: str, pipelineRunName: str, channelId: str, threadId: str, pipelineName: str) -> bool:
274+
def createThreadConfigMap(cls, namespace: str, channelId: str, threadId: str, instanceId: str) -> bool:
275275
"""
276276
Create a ConfigMap to store Slack thread information for a pipeline run.
277277
278278
Parameters:
279279
namespace (str): Kubernetes namespace for the ConfigMap
280-
pipelineRunName (str): Unique identifier for the pipeline run
281280
channelId (str): Slack channel ID where the thread was created
282281
threadId (str): Slack thread timestamp
283-
pipelineName (str): Name of the pipeline (install/update/upgrade/uninstall)
282+
instanceId (str): Name of the Mas Instance ID
284283
285284
Returns:
286285
bool: True if ConfigMap was created successfully, False otherwise
@@ -292,7 +291,7 @@ def createThreadConfigMap(cls, namespace: str, pipelineRunName: str, channelId:
292291
except Exception:
293292
config.load_kube_config()
294293
v1 = client.CoreV1Api()
295-
configmap_name = f"slack-thread-{pipelineRunName}"
294+
configmap_name = f"slack-thread-{instanceId}"
296295
configmap = client.V1ConfigMap(
297296
metadata=client.V1ObjectMeta(
298297
name=configmap_name,
@@ -301,8 +300,8 @@ def createThreadConfigMap(cls, namespace: str, pipelineRunName: str, channelId:
301300
data={
302301
"threadId": threadId,
303302
"channelId": channelId,
304-
"pipelineName": pipelineName,
305-
"startTime": datetime.utcnow().isoformat() + "Z"
303+
"instanceId": instanceId,
304+
"startTime": datetime.now(timezone.utc)
306305
}
307306
)
308307
v1.create_namespaced_config_map(namespace=namespace, body=configmap)
@@ -312,13 +311,13 @@ def createThreadConfigMap(cls, namespace: str, pipelineRunName: str, channelId:
312311
logger.error(f"Failed to create ConfigMap: {e}")
313312
return False
314313

315-
def getThreadConfigMap(cls, namespace: str, pipelineRunName: str) -> dict | None:
314+
def getThreadConfigMap(cls, namespace: str, instanceId: str) -> dict | None:
316315
"""
317316
Retrieve Slack thread information from a ConfigMap.
318317
319318
Parameters:
320319
namespace (str): Kubernetes namespace containing the ConfigMap
321-
pipelineRunName (str): Unique identifier for the pipeline run
320+
instanceId (str): Unique identifier for the pipeline run
322321
323322
Returns:
324323
dict | None: Dictionary containing threadId, channelId, pipelineName, and startTime, or None if not found
@@ -330,21 +329,21 @@ def getThreadConfigMap(cls, namespace: str, pipelineRunName: str) -> dict | None
330329
except Exception:
331330
config.load_kube_config()
332331
v1 = client.CoreV1Api()
333-
configmap_name = f"slack-thread-{pipelineRunName}"
332+
configmap_name = f"slack-thread-{instanceId}"
334333
configmap = v1.read_namespaced_config_map(name=configmap_name, namespace=namespace)
335334
logger.debug(f"Retrieved ConfigMap {configmap_name} from namespace {namespace}")
336335
return configmap.data
337336
except client.exceptions.ApiException as e:
338337
if e.status == 404:
339-
logger.debug(f"ConfigMap slack-thread-{pipelineRunName} not found in namespace {namespace}")
338+
logger.debug(f"ConfigMap slack-thread-{instanceId} not found in namespace {namespace}")
340339
else:
341340
logger.error(f"Failed to retrieve ConfigMap: {e}")
342341
return None
343342
except Exception as e:
344343
logger.error(f"Failed to retrieve ConfigMap: {e}")
345344
return None
346345

347-
def deleteThreadConfigMap(cls, namespace: str, pipelineRunName: str) -> bool:
346+
def deleteThreadConfigMap(cls, namespace: str, instanceId: str) -> bool:
348347
"""
349348
Delete the ConfigMap containing Slack thread information.
350349
@@ -363,13 +362,13 @@ def deleteThreadConfigMap(cls, namespace: str, pipelineRunName: str) -> bool:
363362
config.load_kube_config()
364363

365364
v1 = client.CoreV1Api()
366-
configmap_name = f"slack-thread-{pipelineRunName}"
365+
configmap_name = f"slack-thread-{instanceId}"
367366
v1.delete_namespaced_config_map(name=configmap_name, namespace=namespace)
368367
logger.info(f"Deleted ConfigMap {configmap_name} from namespace {namespace}")
369368
return True
370369
except client.exceptions.ApiException as e:
371370
if e.status == 404:
372-
logger.warning(f"ConfigMap slack-thread-{pipelineRunName} not found in namespace {namespace}")
371+
logger.warning(f"ConfigMap slack-thread-{instanceId} not found in namespace {namespace}")
373372
else:
374373
logger.error(f"Failed to delete ConfigMap: {e}")
375374
return False

0 commit comments

Comments
 (0)