Skip to content

Commit 9a40e91

Browse files
committed
feat(status-badges): status badges
1 parent 866d959 commit 9a40e91

File tree

12 files changed

+60
-20
lines changed

12 files changed

+60
-20
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# abstruse
66

7-
[![AbstruseCI](https://abstruse.bleenco.io/api/repositories/badge/1)](https://abstruse.bleenco.io/repo/1)
7+
[![AbstruseCI](https://abstruse.bleenco.io/badge/1)](https://abstruse.bleenco.io/repo/1)
88

99
Run Continuous Integration (CI) on your own servers.
1010

src/api/server-routes.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,13 @@ export function repositoryRoutes(): express.Router {
138138
}).catch(err => res.status(200).json({ status: false }));
139139
});
140140

141-
router.get('/badge/:id', (req: express.Request, res: express.Response) => {
141+
return router;
142+
}
143+
144+
export function badgeRoutes(): express.Router {
145+
const router = express.Router();
146+
147+
router.get('/:id', (req: express.Request, res: express.Response) => {
142148
getRepositoryBadge(req.params.id).then(status => {
143149
let background = null;
144150
if (status === 'failing') {

src/api/server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export class ExpressServer implements IExpressServer {
3232
app.use('/api/repositories', routes.repositoryRoutes());
3333
app.use('/api/builds', routes.buildRoutes());
3434
app.use('/api/jobs', routes.jobRoutes());
35+
app.use('/badge', routes.badgeRoutes());
3536
app.use(routes.webRoutes());
3637
app.listen(this.config.port, () => {
3738
observer.next(`Server running on port ${this.config.port}`);

src/app/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { SocketServiceProvider } from './services/socket.service';
1111
import { AuthGuardProvider, AuthGuard } from './services/auth-guard.service';
1212
import { AuthServiceProvider } from './services/auth.service';
1313
import { EqualValidator } from './directives/equal-validator.directive';
14+
import { SafeHtmlPipe } from './pipes/safe-html.pipe';
1415
import { AppComponent } from './app.component';
1516
import { AppSetupComponent } from './components/app-setup';
1617
import { AppTerminalComponent } from './components/app-terminal';
@@ -39,7 +40,8 @@ import { AppTeamComponent } from './components/app-team';
3940
AppJobComponent,
4041
AppSettingsComponent,
4142
AppTeamComponent,
42-
EqualValidator
43+
EqualValidator,
44+
SafeHtmlPipe
4345
],
4446
imports: [
4547
BrowserModule,

src/app/components/app-repositories/app-repositories.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ <h1>Repositories</h1>
6565
<span>{{ repo.default_branch }}</span>
6666
</div>
6767
<div class="column is-2 justify-end">
68-
<img [src]="repo.status_badge">
68+
<span *ngIf="repo.status_badge" [innerHTML]="repo.status_badge | safeHtml" class="status-badge"></span>
6969
</div>
7070
</div>
7171
</div>

src/app/components/app-repositories/app-repositories.component.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class AppRepositoriesComponent implements OnInit {
2121
loading: boolean;
2222
repository: Repository;
2323
userData: any;
24-
repositories: string[];
24+
repositories: any[];
2525
dropdowns: boolean[];
2626
buildTriggered: boolean;
2727
url: string;
@@ -52,10 +52,15 @@ export class AppRepositoriesComponent implements OnInit {
5252
fetch(keyword = ''): void {
5353
this.loading = true;
5454
this.apiService.getRepositories(this.userData.id, keyword).subscribe(event => {
55-
this.repositories = event.map(repo => {
56-
repo.status_badge = this.url + '/api/repositories/badge/' + repo.id;
57-
return repo;
55+
this.repositories = event;
56+
this.repositories.forEach((repo: any, i) => {
57+
this.apiService.getBadge(repo.id).subscribe(badge => {
58+
if (badge.ok) {
59+
this.repositories[i].status_badge = badge._body;
60+
}
61+
});
5862
});
63+
5964
this.loading = false;
6065
});
6166
}

src/app/components/app-repository/app-repository.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<div class="nav-left">
66
<span class="nav-item">
77
<h1>{{ repo?.full_name }}</h1>
8-
<span><img [src]="statusBadge" class="badge-img"></span>
8+
<span *ngIf="statusBadge" [innerHTML]="statusBadge | safeHtml" class="status-badge"></span>
99
</span>
1010
</div>
1111
<div class="nav-center"></div>

src/app/components/app-repository/app-repository.component.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ export class AppRepositoryComponent implements OnInit, OnDestroy {
2424
private socketService: SocketService,
2525
private api: ApiService,
2626
private config: ConfigService
27-
) { }
27+
) {
28+
this.loading = true;
29+
}
2830

2931
ngOnInit() {
3032
this.url = this.config.url;
@@ -35,6 +37,7 @@ export class AppRepositoryComponent implements OnInit, OnDestroy {
3537
this.router.navigate(['repositories']);
3638
} else {
3739
this.fetch();
40+
this.fetchBadge();
3841
}
3942
});
4043

@@ -82,9 +85,6 @@ export class AppRepositoryComponent implements OnInit, OnDestroy {
8285
}
8386

8487
this.repo.builds[index].jobs[jobIndex].status = status;
85-
86-
this.statusBadge = '';
87-
this.statusBadge = this.url + '/api/repositories/badge/' + this.id;
8888
}
8989
}
9090
});
@@ -99,12 +99,18 @@ export class AppRepositoryComponent implements OnInit, OnDestroy {
9999
this.repo = event;
100100
this.loading = false;
101101
this.updateJobs();
102-
this.statusBadge = '';
103-
this.statusBadge = this.url + '/api/repositories/badge/' + this.id;
104102
setInterval(() => this.updateJobs(), 1000);
105103
});
106104
}
107105

106+
fetchBadge(): void {
107+
this.api.getBadge(parseInt(this.id, 10)).subscribe(event => {
108+
if (event.ok) {
109+
this.statusBadge = event._body.replace(/ \r/g, '').trim();
110+
}
111+
});
112+
}
113+
108114
updateJobs(): void {
109115
let currentTime = new Date().getTime() - this.socketService.timeSyncDiff;
110116

src/app/pipes/safe-html.pipe.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Pipe, PipeTransform } from '@angular/core';
2+
import { DomSanitizer } from '@angular/platform-browser';
3+
4+
@Pipe({ name: 'safeHtml'})
5+
export class SafeHtmlPipe implements PipeTransform {
6+
constructor(private sanitizer: DomSanitizer) { }
7+
8+
transform(value) {
9+
return this.sanitizer.bypassSecurityTrustHtml(value);
10+
}
11+
}

src/app/services/api.service.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ import { Observable } from 'rxjs/Observable';
55
@Injectable()
66
export class ApiService {
77
url: string;
8+
loc: Location;
9+
port: string;
810

911
constructor(private http: Http) {
10-
let loc: Location = window.location;
11-
let port: string = loc.port === '8000' ? ':6500' : `:${loc.port}`; // dev mode
12-
this.url = `${loc.protocol}//${loc.hostname}${port}/api`;
12+
this.loc = window.location;
13+
this.port = this.loc.port === '8000' ? ':6500' : `:${this.loc.port}`; // dev mode
14+
this.url = `${this.loc.protocol}//${this.loc.hostname}${this.port}/api`;
15+
}
16+
17+
getBadge(id: number): Observable<any> {
18+
return this.http.get(`${this.loc.protocol}//${this.loc.hostname}${this.port}/badge/${id}`);
1319
}
1420

1521
getBuilds(limit: number, offset: number): Observable<any> {

0 commit comments

Comments
 (0)