aboutsummaryrefslogtreecommitdiff
path: root/Timeline/ClientApp/src/app/todo-list-page/todo-list.service.ts
blob: 17ded67b69d2a0f3f81c8646c8b8f7aeb35f2170 (plain)
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
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { switchMap, concatMap, map, toArray } from 'rxjs/operators';

export interface AzureDevOpsAccessInfo {
  username: string;
  personalAccessToken: string;
  organization: string;
  project: string;
}

export interface WiqlWorkItemResult {
  id: number;
  url: string;
}

export interface WiqlResult {
  workItems: WiqlWorkItemResult[];
}

export interface WorkItemResult {
  id: number;
  fields: { [name: string]: any };
}

export interface WorkItemTypeResult {
  icon: {
    url: string
  };
}

export interface WorkItem {
  id: number;
  title: string;
  closed: boolean;
  detailUrl: string;
  iconUrl: string;
}

@Injectable({
  providedIn: 'root'
})
export class TodoListService {

  public static titleFieldName = 'System.Title';
  public static stateFieldName = 'System.State';
  public static typeFieldName = 'System.WorkItemType';

  constructor(private client: HttpClient) { }

  private getAzureDevOpsAccessInfo(): Observable<AzureDevOpsAccessInfo> {
    return this.client.get<AzureDevOpsAccessInfo>('/api/TodoPage/AzureDevOpsAccessInfo');
  }

  private getItemIconUrl(baseUrl: string, headers: HttpHeaders, type: string): Observable<string> {
    return this.client.get<WorkItemTypeResult>(`${baseUrl}_apis/wit/workitemtypes/${encodeURIComponent(type)}?api-version=5.0`, {
      headers: headers
    }).pipe(
      map(result => result.icon.url)
    );
  }

  getWorkItemList(): Observable<WorkItem[]> {
    return this.getAzureDevOpsAccessInfo().pipe(
      switchMap(
        accessInfo => {
          const baseUrl = `https://dev.azure.com/${accessInfo.organization}/${accessInfo.project}/`;
          const headers = new HttpHeaders({
            'Accept': 'application/json',
            'Authorization': `Basic ${btoa(accessInfo.username + ':' + accessInfo.personalAccessToken)}`
          });
          return this.client.post<WiqlResult>(
            `${baseUrl}_apis/wit/wiql?api-version=5.0`, {
              query: 'SELECT [System.Id] FROM workitems WHERE [System.TeamProject] = @project'
            }, { headers: headers }).pipe(
              switchMap(result => result.workItems),
              concatMap(result => this.client.get<WorkItemResult>(result.url, { headers: headers })),
              concatMap(result => this.getItemIconUrl(baseUrl, headers, result.fields[TodoListService.typeFieldName]).pipe(
                map(iconResult => <WorkItem>{
                id: result.id,
                title: <string>result.fields[TodoListService.titleFieldName],
                closed: ((<string>result.fields[TodoListService.stateFieldName]).toLowerCase() === 'closed'),
                detailUrl: `${baseUrl}_workitems/edit/${result.id}/`,
                iconUrl: iconResult
              }))),
              toArray()
            );
        }
      )
    );
  }
}