json on SwiftUI with Xcode 11.3 - swiftui

I'm new to Swift
need help
I am supposed to output data with json in the app.
if the date = date then it should be output in the same date.
import SwiftUI
struct ContentView: View {
var salatTimes: [SalatTime] = []
//timer
static let taskDateFormat: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .medium
//formatter.timeStyle = .medium
return formatter
}()
var dueDate = Date()
//datum vergleich
#State private var salatT = taskDateFormat
//ende Timer
var body: some View {
NavigationView {
VStack {
Text("Datum: \(dueDate, formatter: Self.taskDateFormat)")
List(salatTimes) { item in
Image(item.imageName)
.colorInvert()
Text(item.nameSalat)
Spacer()
Text(item.timeSalat)
.padding()
}.padding()
}
}.navigationViewStyle(StackNavigationViewStyle())
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(salatTimes: SalatApp)
}
}
/**********/
struct SalatTime: Identifiable, Decodable {
var id = UUID()
var nameSalat: String
var timeSalat: String
var imageName: String { return nameSalat }
}
/************/
json data
{
"times": [
{
"date": "2020-01-02",
"fajr": "4:58 AM",
"sunrise": "6:19 AM",
"dhuhr": "11:37 AM",
"asr": "2:35 PM",
"maghrib": "4:55 PM",
"isha": "6:25 PM"
},
{
"date": "2020-02-02",
"fajr": "4:59 AM",
"sunrise": "6:17 AM",
"dhuhr": "11:47 AM",
"asr": "2:56 PM",
"maghrib": "5:18 PM",
"isha": "6:48 PM"
},
{
"date": "2020-03-02",
"fajr": "4:41 AM",
"sunrise": "5:57 AM",
"dhuhr": "11:46 AM",
"asr": "3:07 PM",
"maghrib": "5:36 PM",
"isha": "7:06 PM"
},
{
"date": "2020-04-02",
"fajr": "4:08 AM",
"sunrise": "5:25 AM",
"dhuhr": "11:38 AM",
"asr": "3:07 PM",
"maghrib": "5:51 PM",
"isha": "7:21 PM"
},
{
"date": "2020-05-02",
"fajr": "3:36 AM",
"sunrise": "4:58 AM",
"dhuhr": "11:31 AM",
"asr": "3:00 PM",
"maghrib": "6:05 PM",
"isha": "7:35 PM"
},
{
"date": "2020-06-02",
"fajr": "3:15 AM",
"sunrise": "4:43 AM",
"dhuhr": "11:32 AM",
"asr": "2:56 PM",
"maghrib": "6:20 PM",
"isha": "7:50 PM"
},
{
"date": "2020-07-02",
"fajr": "3:18 AM",
"sunrise": "4:47 AM",
"dhuhr": "11:38 AM",
"asr": "3:01 PM",
"maghrib": "6:28 PM",
"isha": "7:58 PM"
},
{
"date": "2020-08-02",
"fajr": "3:37 AM",
"sunrise": "5:01 AM",
"dhuhr": "11:40 AM",
"asr": "3:07 PM",
"maghrib": "6:19 PM",
"isha": "7:49 PM"
},
{
"date": "2020-09-02",
"fajr": "3:56 AM",
"sunrise": "5:15 AM",
"dhuhr": "11:34 AM",
"asr": "3:03 PM",
"maghrib": "5:53 PM",
"isha": "7:23 PM"
},
{
"date": "2020-10-02",
"fajr": "4:10 AM",
"sunrise": "5:26 AM",
"dhuhr": "11:23 AM",
"asr": "2:47 PM",
"maghrib": "5:20 PM",
"isha": "6:50 PM"
},
{
"date": "2020-11-02",
"fajr": "4:24 AM",
"sunrise": "5:42 AM",
"dhuhr": "11:17 AM",
"asr": "2:29 PM",
"maghrib": "4:53 PM",
"isha": "6:23 PM"
},
{
"date": "2020-12-02",
"fajr": "4:42 AM",
"sunrise": "6:03 AM",
"dhuhr": "11:23 AM",
"asr": "2:23 PM",
"maghrib": "4:43 PM",
"isha": "6:13 PM"
},
{
"date": "2020-13-02",
"fajr": "4:58 AM",
"sunrise": "6:19 AM",
"dhuhr": "11:37 AM",
"asr": "2:35 PM",
"maghrib": "4:55 PM",
"isha": "6:25 PM"
},
{
"date": "2020-14-02",
"fajr": "4:58 AM",
"sunrise": "6:19 AM",
"dhuhr": "11:37 AM",
"asr": "2:35 PM",
"maghrib": "4:55 PM",
"isha": "6:25 PM"
},
{
"date": "2020-15-02",
"fajr": "4:58 AM",
"sunrise": "6:19 AM",
"dhuhr": "11:37 AM",
"asr": "2:35 PM",
"maghrib": "4:55 PM",
"isha": "6:25 PM"
},
{
"date": "2020-16-02",
"fajr": "4:58 AM",
"sunrise": "6:19 AM",
"dhuhr": "11:37 AM",
"asr": "2:35 PM",
"maghrib": "4:55 PM",
"isha": "6:25 PM"
},
{
"date": "2020-17-02",
"fajr": "4:58 AM",
"sunrise": "6:19 AM",
"dhuhr": "11:37 AM",
"asr": "2:35 PM",
"maghrib": "4:55 PM",
"isha": "6:25 PM"
},
{
"date": "2020-18-02",
"fajr": "4:58 AM",
"sunrise": "6:19 AM",
"dhuhr": "11:37 AM",
"asr": "2:35 PM",
"maghrib": "4:55 PM",
"isha": "6:25 PM"
},
{
"date": "2020-19-02",
"fajr": "4:58 AM",
"sunrise": "6:19 AM",
"dhuhr": "11:37 AM",
"asr": "2:35 PM",
"maghrib": "4:55 PM",
"isha": "6:25 PM"
},
]
}
please help
thank you

You are starting this the wrong way round.
You are trying to go directly to display, when your real problem is to parse the text in your JSON document and interpret it correctly as dates.
First thing you do is to create a model object that represents one of the array elements in your JSON data, importantly in the way that is most suitable for your application. Not in the way it is found in the JSON document. Then you write code that initialises the model object with one array element of the JSON data.
Once that is working, and thoroughly tested, then you can start with your UI. The date conversion functions should be part of your model object.
For extra points, ask yourself if your code will work in summer in Murmansk. Murmansk is a bit north of the arctic circle. Which means sunset can be at 0:05am for example.

hello people i found the following solution with the help of some friends. I post my code here and hope that I can help someone with this solution.
first part:
extension Formatter {
static let month: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "MM"
return formatter
}()
static let day: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "dd"
return formatter
}()
static let amPM: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "a"
return formatter
}()
}
extension Date {
var month: String { return Formatter.month.string(from: self) }
var day: String { return Formatter.day.string(from: self) }
var amPM: String { return Formatter.amPM.string(from: self) }
}
second part:
import SwiftUI
struct ContentView: View {
let prayTimes = Bundle.main.decode("time.json")
var body: some View {
GeometryReader { geometry in
ZStack {
Image("background")
.resizable()
.aspectRatio(geometry.size, contentMode: .fill)
.edgesIgnoringSafeArea(.all)
ZStack{
timeView(self.prayTimes).padding()
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct ourView: View {
var item: PrayTime
var body: some View {
VStack{
HStack{
Text("Fajr").font(.body)
Spacer()
Text("\(item.fajr)")
Image(systemName: "speaker").padding()
}.padding()
HStack{
Text("Sunrise").font(.body)
Spacer()
Text(item.sunrise)
Image(systemName: "circle").padding()
}.padding()
HStack{
Text("Dhuhr").font(.body)
Spacer()
Text(item.dhuhr)
Image(systemName: "speaker").padding()
}.padding()
HStack{
Text("Asr").font(.body)
Spacer()
Text(item.asr)
Image(systemName: "speaker").padding()
}.padding()
HStack{
Text("Maghrib").font(.body)
Spacer()
Text(item.maghrib)
Image(systemName: "speaker").padding()
}.padding()
HStack{
Text("Isha").font(.body)
Spacer()
Text(item.isha)
Image(systemName: "speaker").padding()
}.padding()
}.foregroundColor(Color.white).background(Rectangle().opacity(0.5))
}
}
struct Countries: Decodable {
let name: String
let capital: String
}
func timeView(_ items: [PrayTime]) -> AnyView {
let date = Date()
let day = date.day + "-" + date.month
for item in items{
if (item.id == day) {
return(AnyView(ourView(item: item)))
}
}
return AnyView(Text("Nothing Found. Please update to resolve."))
}

Related

how to customise codebuild/codepipeline notifications for slack?

What I am trying to achieve :- When a codebuild stage( action) is is successful, I want to send a notification to slack. The real catch is in the message I want to send the codebuild build logs console link.
In my current workflow when codebuild build is successful it sends default event to sns -> which triggers lambda -> slack
I am pretty sure there must be a way to receive codebuild build logs console link. Since URL for console link for every build is dynamic it cant be set static in my opinion.
Also:- Since this is not possible in default event, I was thinking to create a custom event in lambda ( after getting triggered from sns ) and add the console link and then pass on to slack.Is this the right approach?
You can get log group link in two way.
First one is on Codebuild page you can create "Create notification rule" under "Notify" menu. Then you need to select only "Succeeded" on Build State of "Events that trigger notifications" then only two option to forward this message SNS or AWS Chatbot.
When you choose SNS, attach a Lambda as subscription to this SNS.On lambda function you will get a message like
message = json.loads(event.Records[0].SNS.Message) then get log from message message.detail.additional-information.logs.deep-link
{
"Records": [
{
"EventSource": "aws:sns",
"EventVersion": "1.0",
"EventSubscriptionArn": "arn:aws:sns:us-east-1: 3595xxx02761:codestar-notifications-test: 3648ad36-735e-4ce7-83ad-f5340d503c5d",
"Sns": {
"Type": "Notification",
"MessageId": "df6e2eed-651e-5e21-b24b-72d5c7cdb684",
"TopicArn": "arn:aws:sns:us-east-1: 3595xxx02761:codestar-notifications-test",
"Subject": None,
"Message": "{"account": "3595xxx02761",
"detailType": "CodeBuild Build State Change",
"region": "us-east-1",
"source": "aws.codebuild",
"time": "2023-01-11T11:42:06Z",
"notificationRuleArn": "arn:aws:codestar-notifications:us-east-1:3595xxx02761:notificationrule/e0b422a57a890c96aad913a2d9b0ca0d2f845223",
"detail": {
"build-status": "SUCCEEDED",
"project-name": "test-build",
"build-id": "arn:aws:codebuild:us-east-1:3595xxx02761:build/test-build:88cfdd6f-0028-4bd6-ae43-ce44fcf5bbc2",
"additional-information": {
"cache": {
"type": "NO_CACHE"
},
"build-number": 1.0,
"timeout-in-minutes": 60.0,
"build-complete": true,
"initiator": "root",
"build-start-time": "Jan 11, 2023 11:41:22 AM",
"source": {
"git-clone-depth": 1.0,
"type": "NO_SOURCE"
},
"artifact": {
"location": ""
},
"environment": {
"image": "aws/codebuild/amazonlinux2-x86_64-standard:4.0",
"privileged-mode": false,
"image-pull-credentials-type": "CODEBUILD",
"compute-type": "BUILD_GENERAL1_SMALL",
"type": "LINUX_CONTAINER",
"environment-variables": []
},
"logs": {
"group-name": "/aws/codebuild/test-build",
"stream-name": "88cfdd6f-0028-4bd6-ae43-ce44fcf5bbc2",
"deep-link": "https://console.aws.amazon.com/cloudwatch/home?region=us-east-1#logEvent:group=/aws/codebuild/test-build;stream=88cfdd6f-0028-4bd6-ae43-ce44fcf5bbc2"
},
"phases": [
{
"phase-context": [],
"start-time": "Jan 11, 2023 11:41:22 AM",
"end-time": "Jan 11, 2023 11:41:22 AM",
"duration-in-seconds": 0.0,
"phase-type": "SUBMITTED",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [],
"start-time": "Jan 11, 2023 11:41:22 AM",
"end-time": "Jan 11, 2023 11:41:24 AM",
"duration-in-seconds": 1.0,
"phase-type": "QUEUED",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [
": "
],
"start-time": "Jan 11, 2023 11:41:24 AM",
"end-time": "Jan 11, 2023 11:41:57 AM",
"duration-in-seconds": 33.0,
"phase-type": "PROVISIONING",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [
": "
],
"start-time": "Jan 11, 2023 11:41:57 AM",
"end-time": "Jan 11, 2023 11:42:00 AM",
"duration-in-seconds": 2.0,
"phase-type": "DOWNLOAD_SOURCE",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [
": "
],
"start-time": "Jan 11, 2023 11:42:00 AM",
"end-time": "Jan 11, 2023 11:42:00 AM",
"duration-in-seconds": 0.0,
"phase-type": "INSTALL",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [
": "
],
"start-time": "Jan 11, 2023 11:42:00 AM",
"end-time": "Jan 11, 2023 11:42:00 AM",
"duration-in-seconds": 0.0,
"phase-type": "PRE_BUILD",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [
": "
],
"start-time": "Jan 11, 2023 11:42:00 AM",
"end-time": "Jan 11, 2023 11:42:00 AM",
"duration-in-seconds": 0.0,
"phase-type": "BUILD",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [
": "
],
"start-time": "Jan 11, 2023 11:42:00 AM",
"end-time": "Jan 11, 2023 11:42:00 AM",
"duration-in-seconds": 0.0,
"phase-type": "POST_BUILD",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [
": "
],
"start-time": "Jan 11, 2023 11:42:00 AM",
"end-time": "Jan 11, 2023 11:42:00 AM",
"duration-in-seconds": 0.0,
"phase-type": "UPLOAD_ARTIFACTS",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [
": "
],
"start-time": "Jan 11, 2023 11:42:00 AM",
"end-time": "Jan 11, 2023 11:42:04 AM",
"duration-in-seconds": 4.0,
"phase-type": "FINALIZING",
"phase-status": "SUCCEEDED"
},
{
"start-time": "Jan 11, 2023 11:42:04 AM",
"phase-type": "COMPLETED"
}
],
"queued-timeout-in-minutes": 480.0
},
"current-phase": "COMPLETED",
"current-phase-context": "[: ]",
"version": "1"
},
"resources": [
"arn:aws:codebuild:us-east-1:3595xxx02761:build/test-build:88cfdd6f-0028-4bd6-ae43-ce44fcf5bbc2"
],
"additionalAttributes": {}
}", "Timestamp": "2023-01-11T11: 42: 11.284Z", "SignatureVersion": "1", "Signature": "QyI4CAlszBYE8MJLy+Wwxpn/Ie4kFYigaaRzPEAz4v6vFDE7x7TjaG7yWSiLNGFnBXLpg6Cl3/B70eTLJKmNv/5adXOsDz0Pw53iAcXumLsgtSuPoCpG3zISPvgR1nLBfZia7fv0w02C6d9LrjQq5v8jRZi/GJUt5frigL4Tfq3Q+wAFkmjl31GfxjcSTc0X7ujO1t+/qRIcVaa+XXOhLcDTWG9Q0qWMh0jyf5/126d3CUSRhlM6v9OzqZ4w5a/qEkq+XMaH2ElDdF9rEN+D+GayqltA4hkxwERsbvNyHmG7LBthWChezxfohJ88/O3nIvCsyv1IX9MNgyeWcOxHnA==", "SigningCertUrl": "https: //sns.us-east-1.amazonaws.com/SimpleNotificationService-56e67fcb41f6fec09b0196692625d385.pem", "UnsubscribeUrl": "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:3595xxx02761:codestar-notifications-test:3648ad36-735e-4ce7-83ad-f5340d503c5d", "MessageAttributes": {}}}]}
Other option using EventBridge Rule, using an event pattern like
{
"source": ["aws.codebuild"],
"detail-type": ["CodeBuild Build State Change"],
"detail": {
"build-status": ["SUCCEEDED"]
}
}
your answer on the event field event.detail.additional-information.logs.deep-link is link for log stream.
{
"version": "0",
"id": "1bdc2d34-2b5a-4de9-8738-bd38ef2de265",
"detail-type": "CodeBuild Build State Change",
"source": "aws.codebuild",
"account": "3595xxx02761",
"time": "2023-01-11T11:42:06Z",
"region": "us-east-1",
"resources": ["arn:aws:codebuild:us-east-1:3595xxx02761:build/test-build:88cfdd6f-0028-4bd6-ae43-ce44fcf5bbc2"],
"detail": {
"build-status": "SUCCEEDED",
"project-name": "test-build",
"build-id": "arn:aws:codebuild:us-east-1:3595xxx02761:build/test-build:88cfdd6f-0028-4bd6-ae43-ce44fcf5bbc2",
"additional-information": {
"cache": {"type": "NO_CACHE"},
"build-number": 1.0,
"timeout-in-minutes": 60.0,
"build-complete": True,
"initiator": "root",
"build-start-time": "Jan 11, 2023 11:41:22 AM",
"source": {"buildspec": "version: 0.2\n\nphases:\n build:\n commands:\n - echo "Hello world"\n", "git-clone-depth": 1.0, "type": "NO_SOURCE"},
"artifact": {"location": ""},
"environment": {"image": "aws/codebuild/amazonlinux2-x86_64-standard:4.0", "privileged-mode": False, "image-pull-credentials-type": "CODEBUILD", "compute-type": "BUILD_GENERAL1_SMALL", "type": "LINUX_CONTAINER", "environment-variables": []},
"logs": {"group-name": "/aws/codebuild/test-build", "stream-name": "88cfdd6f-0028-4bd6-ae43-ce44fcf5bbc2", "deep-link": "https://console.aws.amazon.com/cloudwatch/home?region=us-east-1#logEvent:group=/aws/codebuild/test-build;stream=88cfdd6f-0028-4bd6-ae43-ce44fcf5bbc2"},
"phases": [{"phase-context": [], "start-time": "Jan 11, 2023 11:41:22 AM", "end-time": "Jan 11, 2023 11:41:22 AM", "duration-in-seconds": 0.0, "phase-type": "SUBMITTED", "phase-status": "SUCCEEDED"}, {"phase-context": [], "start-time": "Jan 11, 2023 11:41:22 AM", "end-time": "Jan 11, 2023 11:41:24 AM", "duration-in-seconds": 1.0, "phase-type": "QUEUED", "phase-status": "SUCCEEDED"}, {"phase-context": [": "], "start-time": "Jan 11, 2023 11:41:24 AM", "end-time": "Jan 11, 2023 11:41:57 AM", "duration-in-seconds": 33.0, "phase-type": "PROVISIONING", "phase-status": "SUCCEEDED"}, {"phase-context": [": "], "start-time": "Jan 11, 2023 11:41:57 AM", "end-time": "Jan 11, 2023 11:42:00 AM", "duration-in-seconds": 2.0, "phase-type": "DOWNLOAD_SOURCE", "phase-status": "SUCCEEDED"}, {"phase-context": [": "], "start-time": "Jan 11, 2023 11:42:00 AM", "end-time": "Jan 11, 2023 11:42:00 AM", "duration-in-seconds": 0.0, "phase-type": "INSTALL", "phase-status": "SUCCEEDED"}, {"phase-context": [": "], "start-time": "Jan 11, 2023 11:42:00 AM", "end-time": "Jan 11, 2023 11:42:00 AM", "duration-in-seconds": 0.0, "phase-type": "PRE_BUILD", "phase-status": "SUCCEEDED"}, {"phase-context": [": "], "start-time": "Jan 11, 2023 11:42:00 AM", "end-time": "Jan 11, 2023 11:42:00 AM", "duration-in-seconds": 0.0, "phase-type": "BUILD", "phase-status": "SUCCEEDED"}, {"phase-context": [": "], "start-time": "Jan 11, 2023 11:42:00 AM", "end-time": "Jan 11, 2023 11:42:00 AM", "duration-in-seconds": 0.0, "phase-type": "POST_BUILD", "phase-status": "SUCCEEDED"}, {"phase-context": [": "], "start-time": "Jan 11, 2023 11:42:00 AM", "end-time": "Jan 11, 2023 11:42:00 AM", "duration-in-seconds": 0.0, "phase-type": "UPLOAD_ARTIFACTS", "phase-status": "SUCCEEDED"}, {"phase-context": [": "], "start-time": "Jan 11, 2023 11:42:00 AM", "end-time": "Jan 11, 2023 11:42:04 AM", "duration-in-seconds": 4.0, "phase-type": "FINALIZING", "phase-status": "SUCCEEDED"}, {"start-time": "Jan 11, 2023 11:42:04 AM", "phase-type": "COMPLETED"}],
"queued-timeout-in-minutes": 480.0
},
"current-phase": "COMPLETED",
"current-phase-context": "[: ]",
"version": "1"
}
}

AWS EventBridge: Get branch name and last commit author for AWS CodeBuild event rule

We're using CodeBuild and have implemented our own email build notification system (as seems to be required currently).
We use the same CodeBuild project to build all of our branches for the same Bitbucket repository and we use an EventBridge rule that triggers if that CodeBuild project fails. It then calls a Lambda function to issue a notification via a SNS topic.
We can get some basic information in the event passed to Lambda from EventBridge, such as the build number, project and status. But we need to include the branch the build was triggered for since it is confusing when these emails arrive and it's unclear what branch the error was on. Additionally, having the author of the commit triggering the build would be helpful to just email them rather than the more general notification we're currently doing.
Unfortunately neither the branch nor author seem to be in the event. Is there a way to include them or fetch them?
Our EventBridge event pattern is
{
"detail": {
"build-status": ["FAILED"],
"project-name": ["api"]
},
"detail-type": ["CodeBuild Build State Change"],
"source": ["aws.codebuild"]
}
Our Lambda is
const AWS = require("aws-sdk");
const snsTopicArn = process.env.TOPIC_ARN;
exports.handler = (event, context, callback) => {
const status = event.detail["build-status"];
const project = event.detail["project-name"];
const buildLabel = event.detail["additional-information"]["build-number"];
const account = event.account;
const region = event.region;
// determine the link to the build
const buildFullId = event.detail["build-id"];
const buildIdParts = buildFullId.split(":");
const buildId = buildIdParts[buildIdParts.length - 1];
const buildLink = `https://${region}.console.aws.amazon.com/codesuite/codebuild/${account}/projects/${project}/build/${project}%3A${buildId}/?region=${region}`;
const subject = `${project} build ${buildLabel} ${status}`;
const message = `${subject}\n\n${buildLink}`;
const sns = new AWS.SNS();
sns.publish(
{
TopicArn: snsTopicArn,
Message: message,
Subject: subject,
},
function (error, data) {
if (error) console.log(error, error.stack);
callback(error, data);
}
);
};
And we're getting events like this passed to our Lambda:
{
"version": "0",
"id": "...",
"detail-type": "CodeBuild Build State Change",
"source": "aws.codebuild",
"account": "...",
"time": "2022-08-24T09:01:14Z",
"region": "eu-west-2",
"resources": [
"arn:aws:codebuild:eu-west-2:...:build/demo-release-deployment:..."
],
"detail": {
"build-status": "FAILED",
"project-name": "demo-release-deployment",
"build-id": "arn:aws:codebuild:eu-west-2:...:build/demo-release-deployment:...",
"additional-information": {
"cache": {
"type": "NO_CACHE"
},
"build-number": 18,
"timeout-in-minutes": 10,
"build-complete": true,
"initiator": "...",
"build-start-time": "Aug 24, 2022 8:57:39 AM",
"source": {
"report-build-status": true,
"buildspec": "",
"location": "...#bitbucket.org/.../build-test.git>https://...#bitbucket.org/.../build-test.git",
"git-clone-depth": 1,
"type": "BITBUCKET",
"git-submodules-config": {
"fetch-submodules": true
}
},
"artifact": {
"location": ""
},
"environment": {
"image": "aws/codebuild/amazonlinux2-x86_64-standard:4.0",
"privileged-mode": false,
"image-pull-credentials-type": "CODEBUILD",
"compute-type": "BUILD_GENERAL1_SMALL",
"type": "LINUX_CONTAINER",
"environment-variables": []
},
"logs": {
"group-name": "demo-deployment",
"stream-name": "demo-deployment/...",
"deep-link": "https://console.aws.amazon.com/cloudwatch/home?region=eu-west-2#logEvent:group=demo-deployment;stream=demo-deployment/..."
},
"phases": [
{
"phase-context": [],
"start-time": "Aug 24, 2022 8:57:39 AM",
"end-time": "Aug 24, 2022 8:57:39 AM",
"duration-in-seconds": 0,
"phase-type": "SUBMITTED",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [],
"start-time": "Aug 24, 2022 8:57:39 AM",
"end-time": "Aug 24, 2022 8:58:51 AM",
"duration-in-seconds": 72,
"phase-type": "QUEUED",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [": "],
"start-time": "Aug 24, 2022 8:58:51 AM",
"end-time": "Aug 24, 2022 9:00:59 AM",
"duration-in-seconds": 127,
"phase-type": "PROVISIONING",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [": "],
"start-time": "Aug 24, 2022 9:00:59 AM",
"end-time": "Aug 24, 2022 9:01:06 AM",
"duration-in-seconds": 6,
"phase-type": "DOWNLOAD_SOURCE",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [": "],
"start-time": "Aug 24, 2022 9:01:06 AM",
"end-time": "Aug 24, 2022 9:01:06 AM",
"duration-in-seconds": 0,
"phase-type": "INSTALL",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [": "],
"start-time": "Aug 24, 2022 9:01:06 AM",
"end-time": "Aug 24, 2022 9:01:06 AM",
"duration-in-seconds": 0,
"phase-type": "PRE_BUILD",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [
"COMMAND_EXECUTION_ERROR: Error while executing command: npm publish. Reason: exit status 1"
],
"start-time": "Aug 24, 2022 9:01:06 AM",
"end-time": "Aug 24, 2022 9:01:11 AM",
"duration-in-seconds": 4,
"phase-type": "BUILD",
"phase-status": "FAILED"
},
{
"phase-context": [": "],
"start-time": "Aug 24, 2022 9:01:11 AM",
"end-time": "Aug 24, 2022 9:01:11 AM",
"duration-in-seconds": 0,
"phase-type": "POST_BUILD",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [": "],
"start-time": "Aug 24, 2022 9:01:11 AM",
"end-time": "Aug 24, 2022 9:01:11 AM",
"duration-in-seconds": 0,
"phase-type": "UPLOAD_ARTIFACTS",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [": "],
"start-time": "Aug 24, 2022 9:01:11 AM",
"end-time": "Aug 24, 2022 9:01:13 AM",
"duration-in-seconds": 2,
"phase-type": "FINALIZING",
"phase-status": "SUCCEEDED"
},
{
"start-time": "Aug 24, 2022 9:01:13 AM",
"phase-type": "COMPLETED"
}
],
"queued-timeout-in-minutes": 480
},
"current-phase": "COMPLETED",
"current-phase-context": "[: ]",
"version": "1"
}
}
I had the same task. I tried to find any solution. I tried to implement the following script: https://github.com/thii/aws-codebuild-extras. But there is a problem with moving environment variables to lambda.
So finally I solved it using the following steps:
I print author email in buildSpec.yml file:
pre_build:
commands:
- AUTHOR_EMAIL="$(git log -1 --pretty=%ae)"
- echo "CODEBUILD_GIT_AUTHOR_EMAIL=$AUTHOR_EMAIL"
I get all logs from CloudWatch in my lambda handler and find string by pattern ^CODEBUILD_GIT_AUTHOR_EMAIL=(.*)$
I send message to this email using AWS SES inside my lambda handler
Very bad solution, but it works. I can't cost more time for this task

Is it possible to match this event with an AWS EventBridge pattern?

I'm trying to create an EventBridge Event Pattern that matches a CodeBuild state change only when a specific string is present in "phase-context" of one of the objects within the "phases" array.
I've been unable to find an example of how to match a single objects property within an array of objects. Is it possible? I know I could look to match a CodeBuild phases change event instead but want to avoid that if possible.
I have marked in the string I want to match in the example CodeBuild state change event below:
{
"version": "0",
"id": "c030038d-8c4d-6141-9545-00ff7b7153EX",
"detail-type": "CodeBuild Build State Change",
"source": "aws.codebuild",
"account": "123456789012",
"time": "2017-09-01T16:14:28Z",
"region": "us-west-2",
"resources":[
"arn:aws:codebuild:us-west-2:123456789012:build/my-sample-project:8745a7a9-c340-456a-9166-edf953571bEX"
],
"detail":{
"build-status": "SUCCEEDED",
"project-name": "my-sample-project",
"build-id": "arn:aws:codebuild:us-west-2:123456789012:build/my-sample-project:8745a7a9-c340-456a-9166-edf953571bEX",
"additional-information": {
"artifact": {
"md5sum": "da9c44c8a9a3cd4b443126e823168fEX",
"sha256sum": "6ccc2ae1df9d155ba83c597051611c42d60e09c6329dcb14a312cecc0a8e39EX",
"location": "arn:aws:s3:::codebuild-123456789012-output-bucket/my-output-artifact.zip"
},
"environment": {
"image": "aws/codebuild/standard:4.0",
"privileged-mode": false,
"compute-type": "BUILD_GENERAL1_SMALL",
"type": "LINUX_CONTAINER",
"environment-variables": []
},
"timeout-in-minutes": 60,
"build-complete": true,
"initiator": "MyCodeBuildDemoUser",
"build-start-time": "Sep 1, 2017 4:12:29 PM",
"source": {
"location": "codebuild-123456789012-input-bucket/my-input-artifact.zip",
"type": "S3"
},
"logs": {
"group-name": "/aws/codebuild/my-sample-project",
"stream-name": "8745a7a9-c340-456a-9166-edf953571bEX",
"deep-link": "https://console.aws.amazon.com/cloudwatch/home?region=us-west-2#logEvent:group=/aws/codebuild/my-sample-project;stream=8745a7a9-c340-456a-9166-edf953571bEX"
},
"phases": [
{
"phase-context": [],
"start-time": "Sep 1, 2017 4:12:29 PM",
"end-time": "Sep 1, 2017 4:12:29 PM",
"duration-in-seconds": 0,
"phase-type": "SUBMITTED",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [],
"start-time": "Sep 1, 2017 4:12:29 PM",
"end-time": "Sep 1, 2017 4:13:05 PM",
"duration-in-seconds": 36,
"phase-type": "PROVISIONING",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [],
"start-time": "Sep 1, 2017 4:13:05 PM",
"end-time": "Sep 1, 2017 4:13:10 PM",
"duration-in-seconds": 4,
"phase-type": "DOWNLOAD_SOURCE",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [],
"start-time": "Sep 1, 2017 4:13:10 PM",
"end-time": "Sep 1, 2017 4:13:10 PM",
"duration-in-seconds": 0,
"phase-type": "INSTALL",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [],
"start-time": "Sep 1, 2017 4:13:10 PM",
"end-time": "Sep 1, 2017 4:13:10 PM",
"duration-in-seconds": 0,
"phase-type": "PRE_BUILD",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [
"something here" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< THIS STRING
],
"start-time": "Sep 1, 2017 4:13:10 PM",
"end-time": "Sep 1, 2017 4:14:21 PM",
"duration-in-seconds": 70,
"phase-type": "BUILD",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [],
"start-time": "Sep 1, 2017 4:14:21 PM",
"end-time": "Sep 1, 2017 4:14:21 PM",
"duration-in-seconds": 0,
"phase-type": "POST_BUILD",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [],
"start-time": "Sep 1, 2017 4:14:21 PM",
"end-time": "Sep 1, 2017 4:14:21 PM",
"duration-in-seconds": 0,
"phase-type": "UPLOAD_ARTIFACTS",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [],
"start-time": "Sep 1, 2017 4:14:21 PM",
"end-time": "Sep 1, 2017 4:14:26 PM",
"duration-in-seconds": 4,
"phase-type": "FINALIZING",
"phase-status": "SUCCEEDED"
},
{
"start-time": "Sep 1, 2017 4:14:26 PM",
"phase-type": "COMPLETED"
}
]
},
"current-phase": "COMPLETED",
"current-phase-context": "[]",
"version": "1"
}
}
EventBridge supports value matches within array (https://docs.aws.amazon.com/eventbridge/latest/userguide/arrays-in-eventbridge-event-patterns.html). I'm not certain but AFAIK it doesn't support patterns within an array. To be sure, I'd recommend trying out the filters specified in https://docs.aws.amazon.com/eventbridge/latest/userguide/content-filtering-with-event-patterns.html within a rule pattern.
If this fails, you would need to setup a lambda (or fargate / ec2) to transform the input before calling put-events again.

Set variable inside if/else statement?

I'm pretty new to applescript. I'm trying to change what a variable is set to based on an if condition. The user chooses a time and depending on what time they choose, the variable 'time' changes. I get the error "A end of line can’t go after this “"”." referring to the quotations following "0", but I need these numbers to be set as string values. Not sure what I'm missing here so any help is appreciated.
property time : "12"
choose from list {"12 am", "1 am", "2 am", "3 am", "4 am", "5 am", "6 am", "7 am", "8 am", "9 am", "10 am", "11 am", "12 pm", "1 pm", "2 pm", "3 pm", "4 pm", "5 pm", "6 pm", "7 pm", "8 pm", "9 pm", "10 pm", "11 pm"} with title "Time Selection" with prompt "What time would you like?" OK button name "This Time" cancel button name "Cancel" default items {"12 am"}
if answer is equal to "12 am" then
set time equal to "0"
else if answer is equal to "1 am" then
set time equal to "1"
end if
There are many issues:
time is a reserved word. Don't use it as a variable.
set ... equal to is wrong syntax, you have to write set ... to.
answer is not related to the result of choose from list.
And even if the first three issues are resolved, choose from list returns a list.
property myTime : "12"
set answer to choose from list {"12 am", "1 am", "2 am", "3 am", "4 am", "5 am", "6 am", "7 am", "8 am", "9 am", "10 am", "11 am", "12 pm", "1 pm", "2 pm", "3 pm", "4 pm", "5 pm", "6 pm", "7 pm", "8 pm", "9 pm", "10 pm", "11 pm"} with title "Time Selection" with prompt "What time would you like?" OK button name "This Time" cancel button name "Cancel" default items {"12 am"}
if answer is false then return -- catch if nothing is selected
set answer to item 1 of answer -- flatten the list
if answer is equal to "12 am" then
set myTime to "0"
else if answer is equal to "1 am" then
set myTime to "1"
end if
1) you should not use "time" as variable name. it is a reserved word in Applescript. for instance, choose "myTime" as variable name.
2) the variable "answer" is not defined in your script. The result of the « choose from list » is in the default variable "text returned". This variable can also return "false" if user clicks on the cancel button instead of choosing in the list. for clarity of the script, better to formally assign a variable
Then script becomes :
set myResponse to choose from list {"12 am", "1 am", "2 am", "3 am", "4 am", "5 am", "6 am", "7 am", "8 am", "9 am", "10 am", "11 am", "12 pm", "1 pm", "2 pm", "3 pm", "4 pm", "5 pm", "6 pm", "7 pm", "8 pm", "9 pm", "10 pm", "11 pm"} with title "Time Selection" with prompt "What time would you like?" OK button name "This Time" cancel button name "Cancel" default items {"12 am"}
set UserChoice to item 1 of myResponse
set myTime to "" -- goods practice to initialise a variable
if UserChoice is "12 am" then
set myTime to "0"
else if UserChoice is "1 am" then
set myTime to "1"
end if
log myTime

EmberJS error while processing route

I am grabbing json data from a service that I cannot change. I am trying to load this data into an Ember.ArrayController.
Error I am dealing with:
Error while processing route: profiles Assertion Failed: ArrayProxy expects an Array or Ember.ArrayProxy, but you passed object
data format looks like this
var testData =
{
"CustomerProfilesResult": [
{ "DOB": "10\/23\/1969 12:00:00 AM", "DateEnrolled": "7\/10\/2014 12:00:00 AM", "FirstName": "Rob", "LastName": "Weiner", "ProfileId": 1 },
{ "DOB": "10\/23\/1979 12:00:00 AM", "DateEnrolled": "10\/3\/2014 12:00:00 AM", "FirstName": "Repub", "LastName": "Smitty", "ProfileId": 1 },
{ "DOB": "10\/23\/1978 12:00:00 AM", "DateEnrolled": "10\/17\/2014 12:00:00 AM", "FirstName": "Democrat", "LastName": "Johnson", "ProfileId": 1 },
{ "DOB": "10\/23\/1996 12:00:00 AM", "DateEnrolled": "10\/18\/2014 12:00:00 AM", "FirstName": "Itchy", "LastName": "Digger", "ProfileId": 1 }]
};
This should be simple... but Ember only wants it when its not wrapped in the CustomerProfilesResult. I've tried returning
return Ember.$.getJSON(getProfiles)
.success(function(data){
return data;
}).error(function(){
alert('error happened... should have caught this.')
});
and
return Ember.$.getJSON(getProfiles)
.success(function(data){
return data.CustomerProfilesResult;
}).error(function(){
alert('error happened... should have caught this.')
});
both of which fail with
but during testing I have returned
return testData.CustomerProfilesResult;
just fine so its confusing. I know its something stupid I'm missing... help if you can.
success doesn't care about the value you return to it, so it isn't returned to the model hook when the promise is resolved. The original json data is sent to both the success call and the then portion of the promise.
return Ember.$.getJSON(getProfiles)
.then(function(data){
return data.CustomerProfilesResult;
});
Example: http://emberjs.jsbin.com/hafaj/edit