Sergiusz Bazanski | f157b4d | 2020-04-10 17:39:43 +0200 | [diff] [blame] | 1 | package service |
| 2 | |
| 3 | import ( |
| 4 | "strconv" |
| 5 | "strings" |
| 6 | ) |
| 7 | |
| 8 | type gerritMeta struct { |
| 9 | patchSet int64 |
| 10 | changeId string |
| 11 | commit string |
| 12 | } |
| 13 | |
| 14 | // parseGerritMetadata takes a NoteDB metadata entry and extracts info from it. |
| 15 | func parseGerritMetadata(messages []string) *gerritMeta { |
| 16 | meta := &gerritMeta{} |
| 17 | |
| 18 | for _, message := range messages { |
| 19 | for _, line := range strings.Split(message, "\n") { |
| 20 | line = strings.TrimSpace(line) |
| 21 | if len(line) == 0 { |
| 22 | continue |
| 23 | } |
| 24 | |
| 25 | parts := strings.SplitN(line, ":", 2) |
| 26 | if len(parts) < 2 { |
| 27 | continue |
| 28 | } |
| 29 | k, v := parts[0], strings.TrimSpace(parts[1]) |
| 30 | |
| 31 | switch k { |
| 32 | case "Patch-set": |
| 33 | n, err := strconv.ParseInt(v, 10, 64) |
| 34 | if err != nil { |
| 35 | continue |
| 36 | } |
| 37 | meta.patchSet = n |
| 38 | case "Change-id": |
| 39 | meta.changeId = v |
| 40 | case "Commit": |
| 41 | meta.commit = v |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | if meta.patchSet == 0 { |
| 47 | return nil |
| 48 | } |
| 49 | if meta.changeId == "" { |
| 50 | return nil |
| 51 | } |
| 52 | if meta.commit == "" { |
| 53 | return nil |
| 54 | } |
| 55 | return meta |
| 56 | } |