@@ -23,35 +23,39 @@ class GitLabResponseParser {
2323 var status = PipelineStatus ( activity: . other)
2424 status. webUrl = latest [ " web_url " ] as? String
2525 if let pipelineStatus = latest [ " status " ] as? String {
26- status. activity = activityForString ( pipelineStatus)
26+ status. activity = GitLabResponseParser . activityForString ( pipelineStatus)
2727 }
2828
2929 if status. activity == . building {
30- status. currentBuild = build ( pipeline: latest)
30+ status. currentBuild = GitLabResponseParser . build ( pipeline: latest)
3131 if let completed = pipelineList. first ( where: isCompletedSuccessful ( pipeline: ) ) ??
32- pipelineList. first ( where: isCompleted ( pipeline: ) ) {
33- status. lastBuild = build ( pipeline: completed)
32+ pipelineList. first ( where: GitLabResponseParser . isCompleted ( pipeline: ) ) {
33+ status. lastBuild = GitLabResponseParser . build ( pipeline: completed)
3434 }
3535 } else {
36- status. lastBuild = build ( pipeline: latest)
36+ status. lastBuild = GitLabResponseParser . build ( pipeline: latest)
3737 }
3838
3939 return status
4040 }
4141
42- private func build( pipeline: Dictionary < String , Any > ) -> Build {
42+ fileprivate static func build( pipeline: Dictionary < String , Any > ) -> Build {
4343 let status = pipeline [ " status " ] as? String
4444 var build = Build ( result: resultForString ( status) )
4545
46- if let pipelineId = pipeline [ " iid " ] as? Int {
47- build. label = String ( pipelineId)
46+ if let pipelineId = pipeline [ " id " ] as? Int {
47+ build. id = String ( pipelineId)
48+ }
49+
50+ if let pipelineIid = pipeline [ " iid " ] as? Int {
51+ build. label = String ( pipelineIid)
4852 }
4953
50- // TODO: AI generated code - rework to get actual run start if possible
5154 if let createdAt = pipeline [ " created_at " ] as? String , let createdAtDate = dateForString ( createdAt) {
5255 build. timestamp = createdAtDate
53- if let updatedAt = pipeline [ " updated_at " ] as? String , let updatedAtDate = dateForString ( updatedAt) {
54- build. duration = updatedAtDate. timeIntervalSince ( createdAtDate)
56+ // This is only present when called with a response with the pipeline detail
57+ if let duration = pipeline [ " duration " ] as? Int {
58+ build. duration = Double ( duration)
5559 }
5660 }
5761
@@ -68,30 +72,34 @@ class GitLabResponseParser {
6872 build. message = messageParts. joined ( separator: " \u{22EE} " )
6973 }
7074
71- // GitLab doesn't include user info directly in pipeline API response
72- // We would need to make an additional API call to get user details
73- // For now, we'll leave user and avatar empty
75+ // This is only present when called with a response with the pipeline detail
76+ if let user = pipeline [ " user " ] as? Dictionary < String , Any > {
77+ build. user = user [ " name " ] as? String
78+ if let avatar = user [ " avatar_url " ] as? String {
79+ build. avatar = URL ( string: avatar)
80+ }
81+ }
7482
7583 return build
7684 }
7785
78- private func isCompleted( pipeline: Dictionary < String , Any > ) -> Bool {
79- return activityForString ( pipeline [ " status " ] as? String ) == . sleeping
86+ private static func isCompleted( pipeline: Dictionary < String , Any > ) -> Bool {
87+ return GitLabResponseParser . activityForString ( pipeline [ " status " ] as? String ) == . sleeping
8088 }
8189
8290 private func isCompletedSuccessful( pipeline: Dictionary < String , Any > ) -> Bool {
83- return resultForString ( pipeline [ " status " ] as? String ) == . success
91+ return GitLabResponseParser . resultForString ( pipeline [ " status " ] as? String ) == . success
8492 }
8593
86- func activityForString( _ string: String ? ) -> PipelineStatus . Activity {
94+ static func activityForString( _ string: String ? ) -> PipelineStatus . Activity {
8795 switch string {
8896 case " running " , " pending " : return . building
8997 case " success " , " failed " , " canceled " , " skipped " , " manual " , " scheduled " : return . sleeping
9098 default : return . other
9199 }
92100 }
93101
94- func resultForString( _ string: String ? ) -> BuildResult {
102+ static func resultForString( _ string: String ? ) -> BuildResult {
95103 switch string {
96104 case " success " : return . success
97105 case " failed " : return . failure
@@ -100,9 +108,28 @@ class GitLabResponseParser {
100108 }
101109 }
102110
103- func dateForString( _ string: String ) -> Date ? {
111+ static func dateForString( _ string: String ) -> Date ? {
104112 let formatter = ISO8601DateFormatter ( )
105113 formatter. formatOptions = [ . withInternetDateTime, . withFractionalSeconds]
106114 return formatter. date ( from: string)
107115 }
108116}
117+
118+
119+ class GitLabDetailResponseParser {
120+
121+ var pipelineDetail : Dictionary < String , Any > = [ : ]
122+
123+ func parseResponse( _ data: Data ) throws {
124+ if let response = try JSONSerialization . jsonObject ( with: data, options: [ ] ) as? Dictionary < String , Any > {
125+ pipelineDetail = response
126+ } else {
127+ pipelineDetail = [ : ]
128+ }
129+ }
130+
131+ func build( ) -> Build {
132+ GitLabResponseParser . build ( pipeline: pipelineDetail)
133+ }
134+
135+ }
0 commit comments