Sergiusz Bazanski | f157b4d | 2020-04-10 17:39:43 +0200 | [diff] [blame] | 1 | package source |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | |
| 9 | dvpb "code.hackerspace.pl/hscloud/devtools/depotview/proto" |
| 10 | ) |
| 11 | |
| 12 | type DepotViewSourceProvider struct { |
| 13 | stub dvpb.DepotViewClient |
| 14 | } |
| 15 | |
| 16 | func NewDepotView(stub dvpb.DepotViewClient) SourceProvider { |
| 17 | return &DepotViewSourceProvider{ |
| 18 | stub: stub, |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | func changeRef(ref string) int64 { |
| 23 | ref = strings.ToLower(ref) |
| 24 | if !strings.HasPrefix(ref, "change/") && !strings.HasPrefix(ref, "cr/") { |
| 25 | return 0 |
| 26 | } |
| 27 | n, err := strconv.ParseInt(strings.SplitN(ref, "/", 2)[1], 10, 64) |
| 28 | if err != nil { |
| 29 | return 0 |
| 30 | } |
| 31 | |
| 32 | return n |
| 33 | } |
| 34 | |
| 35 | func (s *DepotViewSourceProvider) Source(ctx context.Context, ref string) (Source, error) { |
| 36 | var hash string |
| 37 | n := changeRef(ref) |
| 38 | if n != 0 { |
| 39 | res, err := s.stub.ResolveGerritChange(ctx, &dvpb.ResolveGerritChangeRequest{Change: n}) |
| 40 | if err != nil { |
| 41 | return nil, err |
| 42 | } |
| 43 | hash = res.Hash |
| 44 | } else { |
| 45 | res, err := s.stub.Resolve(ctx, &dvpb.ResolveRequest{Ref: ref}) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | hash = res.Hash |
| 50 | } |
| 51 | |
| 52 | if hash == "" { |
| 53 | return nil, nil |
| 54 | } |
| 55 | |
| 56 | return &depotViewSource{ |
| 57 | stub: s.stub, |
| 58 | hash: hash, |
| 59 | change: n, |
| 60 | }, nil |
| 61 | } |
| 62 | |
| 63 | type depotViewSource struct { |
| 64 | stub dvpb.DepotViewClient |
| 65 | hash string |
| 66 | change int64 |
| 67 | } |
| 68 | |
| 69 | func (s *depotViewSource) IsFile(ctx context.Context, path string) (bool, error) { |
| 70 | res, err := s.stub.Stat(ctx, &dvpb.StatRequest{ |
| 71 | Hash: s.hash, |
| 72 | Path: path, |
| 73 | }) |
| 74 | if err != nil { |
| 75 | return false, err |
| 76 | } |
| 77 | return res.Type == dvpb.StatResponse_TYPE_FILE, nil |
| 78 | } |
| 79 | |
| 80 | func (s *depotViewSource) IsDirectory(ctx context.Context, path string) (bool, error) { |
| 81 | res, err := s.stub.Stat(ctx, &dvpb.StatRequest{ |
| 82 | Hash: s.hash, |
| 83 | Path: path, |
| 84 | }) |
| 85 | if err != nil { |
| 86 | return false, err |
| 87 | } |
| 88 | return res.Type == dvpb.StatResponse_TYPE_DIRECTORY, nil |
| 89 | } |
| 90 | |
| 91 | func (s *depotViewSource) ReadFile(ctx context.Context, path string) ([]byte, error) { |
| 92 | var data []byte |
| 93 | srv, err := s.stub.Read(ctx, &dvpb.ReadRequest{ |
| 94 | Hash: s.hash, |
| 95 | Path: path, |
| 96 | }) |
| 97 | if err != nil { |
| 98 | return nil, err |
| 99 | } |
| 100 | for { |
| 101 | res, err := srv.Recv() |
| 102 | if err != nil { |
| 103 | return nil, err |
| 104 | } |
| 105 | if len(res.Data) == 0 { |
| 106 | break |
| 107 | } |
| 108 | data = append(data, res.Data...) |
| 109 | } |
| 110 | return data, nil |
| 111 | } |
| 112 | |
| 113 | func (s *depotViewSource) WebLinks(fpath string) []WebLink { |
| 114 | gitURL := fmt.Sprintf(FlagGitwebURLPattern, s.hash, fpath) |
| 115 | links := []WebLink{ |
Serge Bazanski | bc0d3cb | 2021-03-06 20:49:00 +0000 | [diff] [blame] | 116 | WebLink{Kind: "source", LinkLabel: s.hash[:16], LinkURL: gitURL}, |
Sergiusz Bazanski | f157b4d | 2020-04-10 17:39:43 +0200 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | if s.change != 0 { |
| 120 | gerritLabel := fmt.Sprintf("change %d", s.change) |
| 121 | gerritLink := fmt.Sprintf("https://gerrit.hackerspace.pl/%d", s.change) |
| 122 | links = append(links, WebLink{Kind: "gerrit", LinkLabel: gerritLabel, LinkURL: gerritLink}) |
| 123 | } |
| 124 | |
| 125 | return links |
| 126 | } |