/
action.py
108 lines (92 loc) · 3.58 KB
/
action.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
# (C) Datadog, Inc. 2018
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
import os
import json
from datadog import initialize, api
import requests
(
EVENT_PATH_ENV_VAR,
TRELLO_LIST_ID,
TRELLO_KEY_ENV_VAR,
TRELLO_TOKEN_ENV_VAR,
DD_API_KEY_ENV_VAR
) = ENV_VARS = (
'GITHUB_EVENT_PATH',
'TRELLO_LIST_ID',
'TRELLO_KEY',
'TRELLO_TOKEN',
'DD_API_KEY',
)
TRELLO_API_URL = "https://api.trello.com/1/cards"
SUCCESS = "Success"
FAILED = "Failed"
# Make sure all environment variables are present
def validate_env_vars():
msg = ''
for var in ENV_VARS:
if var not in os.environ:
msg += f"Missing a required environment variable {var}. Cannot create card\n"
if msg:
emit_dd_event(FAILED, msg)
raise Exception(msg)
# Emit an event to Datadog based on whether or not we could create the card
def emit_dd_event(status, msg):
options = {
'api_key': os.environ['DD_API_KEY'],
}
initialize(**options)
title = f"Trello Card creation {status}"
text = msg
tags = ['team:agent-integrations', 'application:trello_github_action']
api.Event.create(title=title, text=text, tags=tags)
# Retrieve the github environment from the docker runtime Github Actions provides
# https://developer.github.com/actions/creating-github-actions/accessing-the-runtime-environment/
def get_github_event():
event_file_path = os.environ[EVENT_PATH_ENV_VAR]
try:
with open(event_file_path, "r") as f:
pull_request_event = f.read()
return json.loads(pull_request_event)
except (IOError, ValueError) as e:
emit_dd_event(FAILED, f"Unable to create Trello card: {e}")
raise
# Create the Trello card on the board specified by the environment variable
# https://developers.trello.com/reference/#cards-2
def create_trello_card(pull_request_event, pr_url):
querystring = {
"idList": os.environ[TRELLO_LIST_ID],
"keepFromSource":"all",
"name": pull_request_event.get('title'),
"desc": f"PR URL: {pr_url}\n\n{pull_request_event.get('body', '')[:5000]}",
"key": os.environ[TRELLO_KEY_ENV_VAR],
"token": os.environ[TRELLO_TOKEN_ENV_VAR]
}
response = requests.post(TRELLO_API_URL, params=querystring)
response.raise_for_status()
# Use the github labels from the PR to determine if we should create a Trello card
# True if any of the labels on the PR starts with changelog and isn't `no-changelog`
# AND the PR is merged
def should_create_card(pull_request_event):
pr_includes_changes = False
pr_is_merged = False
for label in pull_request_event.get('pull_request').get('labels', []):
label = label.get('name', '')
if label.startswith('changelog/') and label != 'changelog/no-changelog':
pr_includes_changes = True
if pull_request_event.get('pull_request', {}).get('merged') and pull_request_event.get('action') == 'closed':
pr_is_merged = True
return pr_includes_changes and pr_is_merged
if __name__ == "__main__":
validate_env_vars()
pull_request_event = get_github_event()
pr_url = pull_request_event.get('pull_request', {}).get('html_url')
if should_create_card(pull_request_event):
try:
create_trello_card(pull_request_event.get('pull_request'), pr_url)
except Exception as e:
emit_dd_event(FAILED, f"Couldn't submit card to Trello API: {e}")
else:
emit_dd_event(SUCCESS, f"Succesfully created Trello card for PR {pr_url}")
else:
print(f"Not creating a card for Pull Request {pr_url}")