|
| 1 | +import boto3 |
| 2 | +import json |
| 3 | +from botocore.vendored import requests |
| 4 | + |
| 5 | +def add_priority(issue, priority): |
| 6 | + fields = issue["fields"] |
| 7 | + fields["priority"] = {"name": priority} |
| 8 | + |
| 9 | +def add_assignee(issue, assignee): |
| 10 | + fields = issue["fields"] |
| 11 | + fields["assignee"] = {"name": assignee} |
| 12 | + |
| 13 | +def add_due_date(issue, due_date): |
| 14 | + fields = issue["fields"] |
| 15 | + fields["duedate"] = due_date |
| 16 | + |
| 17 | +def handler(event, context): |
| 18 | + |
| 19 | + client = boto3.client("ssm") |
| 20 | + |
| 21 | + ssm_parameter_name = event["SSMParameterName"].strip() |
| 22 | + secret = client.get_parameter(Name=ssm_parameter_name, WithDecryption=True)['Parameter']['Value'] |
| 23 | + |
| 24 | + username = event["JiraUsername"].strip() |
| 25 | + url = event["JiraURL"].strip() |
| 26 | + |
| 27 | + issue = { |
| 28 | + "fields": { |
| 29 | + "summary": event["IssueSummary"].strip(), |
| 30 | + "project": { |
| 31 | + "key": event["ProjectKey"].strip() |
| 32 | + }, |
| 33 | + "description": event["IssueDescription"].strip(), |
| 34 | + "issuetype": { |
| 35 | + "name": event["IssueTypeName"].strip() |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + priority = event["PriorityName"].strip() |
| 41 | + if priority: |
| 42 | + add_priority(issue, priority) |
| 43 | + |
| 44 | + assignee = event["AssigneeName"].strip() |
| 45 | + |
| 46 | + if assignee: |
| 47 | + add_assignee(issue, assignee) |
| 48 | + |
| 49 | + due_date = event["DueDate"].strip() |
| 50 | + if due_date: |
| 51 | + add_due_date(issue, due_date) |
| 52 | + |
| 53 | + data = json.dumps(issue) |
| 54 | + |
| 55 | + headers = {'Content-Type':'application/json'} |
| 56 | + |
| 57 | + response = requests.post('{0}/rest/api/2/issue/'.format(url), |
| 58 | + headers=headers, |
| 59 | + data=data, |
| 60 | + auth=(username, secret)) |
| 61 | + |
| 62 | + if not response.ok: |
| 63 | + raise Exception("Received error with status code " + str(response.status_code) + " from Jira") |
| 64 | + else: |
| 65 | + issue_key = (response.json()["key"]) |
| 66 | + return {"IssueKey" : issue_key} |
0 commit comments