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

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

interface WiqlResult {
  workItems: WiqlWorkItemResult[];
}

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

export interface WorkItem {
  id: number;
  title: string;
}

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

  private username = 'crupest';
  private organization = 'crupest-web';
  private project = 'Timeline';
  private fieldId = 'System.Title';


  constructor(private client: HttpClient) { }

  private getAzureDevOpsPat(): Observable<string> {
    return this.client.get('/api/TodoList/AzureDevOpsPat', {
      headers: {
        'Accept': 'text/plain'
      },
      responseType: 'text'
    });
  }

  getWorkItemList(): Observable<WorkItem[]> {
    return this.getAzureDevOpsPat().pipe(
      switchMap(
        pat => {
          const headers = new HttpHeaders({
            'Accept': 'application/json',
            'Authorization': `Basic ${btoa(this.username + ':' + pat)}`
          });
          return this.client.post<WiqlResult>(
            `https://dev.azure.com/${this.organization}/${this.project}/_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 })),
              map(result => <WorkItem>{ id: result.id, title: result.fields[this.fieldId] }),
              toArray()
            );
        }
      )
    );
  }
}