forked from LeanKit/LeanKit.API.Client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeanKitQuery.cs
More file actions
120 lines (109 loc) · 3.38 KB
/
LeanKitQuery.cs
File metadata and controls
120 lines (109 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//------------------------------------------------------------------------------
// <copyright company="LeanKit Inc.">
// Copyright (c) LeanKit Inc. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using LeanKit.API.Client.Library;
using LeanKit.API.Client.Library.TransferObjects;
using LeanKitCmdQuery.Mappings;
using PowerArgs;
using ServiceStack.Text;
namespace LeanKitCmdQuery
{
public class LeanKitQuery
{
private readonly QueryArgs _args;
public LeanKitQuery(QueryArgs queryArgs)
{
_args = queryArgs;
}
private void WriteInfo(string output)
{
if (_args.Csv || _args.Json) return;
Console.WriteLine(output);
}
private void ValidateQuery()
{
if (_args.Boards) return;
if (_args.Board > 0) return;
throw new ArgException("At least one query parameter is required.");
}
public void RunQuery()
{
ValidateQuery();
var leanKitAuth = new LeanKitAccountAuth
{
Hostname = _args.Host,
Username = _args.User,
Password = _args.Password
};
// For internal development testing
if (_args.Host.Equals("kanban-cibuild", StringComparison.InvariantCultureIgnoreCase))
leanKitAuth.UrlTemplateOverride = "http://{0}.localkanban.com";
WriteInfo("Connecting to LeanKit account...");
var api = new LeanKitClientFactory().Create(leanKitAuth);
if (_args.Boards)
{
WriteInfo("Getting all boards...");
var boards = api.GetBoards();
var boardList = boards.Select(Mapper.Map<BoardLiteView>);
var output = _args.Csv ? boardList.ToCsv() : _args.Json ? boardList.ToJson() : boardList.Dump();
Console.WriteLine(output);
}
else if (_args.Board > 0)
{
WriteInfo(string.Format("Getting board [{0}]...", _args.Board));
var board = api.GetBoard(_args.Board);
if (_args.Lanes || _args.Lane > 0 || _args.Cards)
{
// Get lanes
var boardLanes = new List<Lane>();
if (_args.Lane > 0)
{
WriteInfo(string.Format("Getting lane [{0}]...", _args.Lane));
boardLanes.AddRange(board.AllLanes().Where(lane => lane.Id == _args.Lane));
}
else
{
if (_args.IncludeBacklog) boardLanes.AddRange(board.Backlog);
boardLanes.AddRange(board.Lanes);
if (_args.IncludeArchive) boardLanes.AddRange(board.Archive);
}
if (_args.Cards)
{
WriteInfo("Getting cards...");
var cards = new List<MappedCardView>();
foreach (var lane in boardLanes)
{
cards.AddRange(lane.Cards.Select(Mapper.Map<MappedCardView>));
}
// Archived cards is a separate API call
if (_args.IncludeArchive)
{
var archivedCards = api.GetArchiveCards(_args.Board);
cards.AddRange(archivedCards.Select(Mapper.Map<MappedCardView>));
}
var output = _args.Csv ? cards.ToCsv() : _args.Json ? cards.ToJson() : cards.Dump();
Console.WriteLine(output);
}
else
{
var lanes = boardLanes.Select(Mapper.Map<LaneView>);
var output = _args.Csv ? lanes.ToCsv() : _args.Json ? lanes.ToJson() : lanes.Dump();
Console.WriteLine(output);
}
}
else
{
var boardView = Mapper.Map<BoardView>(board);
var output = _args.Csv ? boardView.ToCsv() : _args.Json ? boardView.ToJson() : boardView.Dump();
Console.WriteLine(output);
}
}
}
}
}