Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Commit

Permalink
Fixed build restart and cancel mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
amitkhairnar44 committed Dec 8, 2020
1 parent 405fa33 commit 8c2282d
Show file tree
Hide file tree
Showing 9 changed files with 361 additions and 101 deletions.
35 changes: 18 additions & 17 deletions lib/api/travis_ci_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,12 @@ class TravisCIApi {

NetworkUtil _netUtil = NetworkUtil();

Future<Map> cancelBuild(String id, CancelToken cancelToken) async {
//print(ApiUrls.jobUrl + id + '/restart');
print(ApiUrls.buildUrl + '/' + id + '/cancel');
var res = await _netUtil.post(
ApiUrls.buildUrl + '/' + id + '/cancel', cancelToken,
headers: headers);
print(res);
return res;
}

//{@type: pending, build: {@type: build, @href: /build/735270426, @representation: minimal, id: 735270426, number: 26, state: canceled, duration: 5, event_type: push, previous_state: null, pull_request_title: null, pull_request_number: null, started_at: 2020-10-18T05:26:24Z, finished_at: 2020-10-18T05:26:29Z, private: false, priority: false}, state_change: restart, resource_type: build}
Future<Map> getBuildLog(String id, CancelToken cancelToken) async {
var res = await _netUtil.get(ApiUrls.jobUrl + id + '/log', cancelToken,
headers: headers);
return res;
}

//{@type: pending, build: {@type: build, @href: /build/735270426, @representation: minimal, id: 735270426, number: 26, state: started, duration: null, event_type: push, previous_state: null, pull_request_title: null, pull_request_number: null, started_at: 2020-10-18T05:26:24Z, finished_at: null, private: false, priority: false}, state_change: cancel, resource_type: build}
Future<String> getBuildLogAsTxt(String id, CancelToken cancelToken) async {
var res = await _netUtil.get(ApiUrls.jobUrl + id + '/log.txt', cancelToken,
headers: headers);
Expand Down Expand Up @@ -74,16 +62,29 @@ class TravisCIApi {
return User.fromJson(res);
}

Future<Map> restartBuild(String id, CancelToken cancelToken) async {
Future<Map> restartCancelBuild(
String id, bool isRestart, CancelToken cancelToken) async {
//print(ApiUrls.jobUrl + id + '/restart');
print(ApiUrls.buildUrl + '/' + id + '/restart');
//print(ApiUrls.buildUrl + '/' + id + (isRestart ? '/restart' : '/cancel'));
var res = await _netUtil.post(
ApiUrls.buildUrl + '/' + id + '/restart', cancelToken,
headers: headers);
print(res);
ApiUrls.buildUrl + '/' + id + (isRestart ? '/restart' : '/cancel'),
cancelToken,
headers: headers,
);
//print(res);
return res;
}

// Future<Map> cancelBuild(String id, CancelToken cancelToken) async {
// //print(ApiUrls.jobUrl + id + '/restart');
// print(ApiUrls.buildUrl + '/' + id + '/cancel');
// var res = await _netUtil.post(
// ApiUrls.buildUrl + '/' + id + '/cancel', cancelToken,
// headers: headers);
// print(res);
// return res;
// }

Future<RepositoriesModel> starUnStarRepo(
String id, bool isStar, CancelToken cancelToken) async {
var res = await _netUtil.post(
Expand Down
114 changes: 66 additions & 48 deletions lib/fragments/my_builds.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import 'package:feather_icons_flutter/feather_icons_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter_pagewise/flutter_pagewise.dart';
import 'package:timeago/timeago.dart' as timeago;
import 'package:travis_ci/pages/build_details.dart';

import '../api/travis_ci_api.dart';
import '../models/build_model.dart';
import '../pages/build_details.dart';
import '../utils/get_icon.dart';
import '../utils/get_state_color.dart';
import '../utils/icon_fonts.dart';
Expand Down Expand Up @@ -92,7 +92,12 @@ class _MyBuildsState extends State<MyBuilds> {
width: 4.0,
),
Text(
"#${buildsModel.number} ${buildsModel.state.toString().split('.').last}",
[
"#${buildsModel.number}",
buildsModel.state != null
? buildsModel.state.toString().split('.').last
: 'received'
].join(" "),
style: TextStyle(
color: GetStateColor.getStateColor(
buildsModel.state),
Expand Down Expand Up @@ -171,8 +176,10 @@ class _MyBuildsState extends State<MyBuilds> {
width: 8.0,
),
Text(
"${buildsModel.duration ~/ 60} min "
"${buildsModel.duration % 60} sec",
buildsModel.duration != null
? "${buildsModel.duration ~/ 60} min "
"${buildsModel.duration % 60} sec"
: '-',
style: TextStyle(fontWeight: FontWeight.w500),
)
],
Expand All @@ -182,50 +189,50 @@ class _MyBuildsState extends State<MyBuilds> {
SizedBox(
height: 16.0,
),
InkWell(
child: Container(
padding: const EdgeInsets.only(
right: 8.0, left: 4.0, top: 4.0, bottom: 4.0),
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(20.0)),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
Icons.refresh,
color: Colors.teal,
size: 16.0,
),
SizedBox(
width: 8.0,
),
Text(
"Restart build",
style: TextStyle(
color: Colors.teal,
fontSize: 12.0,
fontWeight: FontWeight.w500),
)
],
),
),
onTap: () {
if (!_restarted) {
TravisCIApi()
.restartBuild(buildsModel.id.toString(), CancelToken());
setState(() {
_restarted = true;
});
} else {
TravisCIApi()
.cancelBuild(buildsModel.id.toString(), CancelToken());
setState(() {
_restarted = false;
});
}
},
),
// InkWell(
// child: Container(
// padding: const EdgeInsets.only(
// right: 8.0, left: 4.0, top: 4.0, bottom: 4.0),
// decoration: BoxDecoration(
// color: Colors.grey[200],
// borderRadius: BorderRadius.circular(20.0)),
// child: Row(
// mainAxisSize: MainAxisSize.min,
// children: [
// Icon(
// Icons.refresh,
// color: Colors.teal,
// size: 16.0,
// ),
// SizedBox(
// width: 8.0,
// ),
// Text(
// "Restart build",
// style: TextStyle(
// color: Colors.teal,
// fontSize: 12.0,
// fontWeight: FontWeight.w500),
// )
// ],
// ),
// ),
// onTap: () {
// // if (!_restarted) {
// // TravisCIApi()
// // .restartBuild(buildsModel.id.toString(), CancelToken());
// // setState(() {
// // _restarted = true;
// // });
// // } else {
// // TravisCIApi()
// // .cancelBuild(buildsModel.id.toString(), CancelToken());
// // setState(() {
// // _restarted = false;
// // });
// // }
// },
// ),
],
),
leading: Icon(TravisLogos.source_repository),
Expand All @@ -236,6 +243,17 @@ class _MyBuildsState extends State<MyBuilds> {
builder: (_) => BuildDetails(
buildData: buildsModel,
showAppbar: true,
onChanged: (value) {
Navigator.of(context).pop();
setState(() {
_pageWiseLoadController = PagewiseLoadController(
pageSize: 10,
pageFuture: (pageIndex) {
return TravisCIApi.getMyBuilds(
(pageIndex * 10), 10, CancelToken());
});
});
},
));
},
),
Expand Down
66 changes: 46 additions & 20 deletions lib/pages/build_details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@
* Created by Amit Khairnar on 11/10/2020.
*/

import 'package:dio/dio.dart';
import 'package:feather_icons_flutter/feather_icons_flutter.dart';
import 'package:flutter/material.dart';
import 'package:timeago/timeago.dart' as timeago;

import '../api/travis_ci_api.dart';
import '../enum/build_status.dart';
import '../models/build_model.dart';
import '../utils/get_icon.dart';
import '../utils/get_state_color.dart';
import '../utils/open_url.dart';
import '../widgets/restart_cancel_button.dart';
import 'show_logs.dart';
import 'user_data_widget.dart';

class BuildDetails extends StatelessWidget {
final BuildsModel buildData;
final bool showAppbar;
final ValueChanged<bool> onChanged;

const BuildDetails({Key key, this.buildData, this.showAppbar})
const BuildDetails({Key key, this.buildData, this.showAppbar, this.onChanged})
: super(key: key);
@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -229,35 +230,60 @@ class BuildDetails extends StatelessWidget {
id: buildData.createdBy.id.toString(),
),
),
// Align(
// alignment: Alignment.centerRight,
// child: Padding(
// padding: const EdgeInsets.all(8.0),
// child: ActionChip(
// avatar: Icon(
// Icons.refresh,
// color: Colors.teal,
// ),
// label: Text(
// 'Restart build',
// style: TextStyle(color: Colors.teal),
// ),
// backgroundColor: Colors.white30,
// shape:
// StadiumBorder(side: BorderSide(color: Colors.grey[200])),
// onPressed: () {
// //TravisCIApi().restartBuild(buildData.jobs.first.id.toString(), CancelToken());
// TravisCIApi()
// .restartBuild(buildData.id.toString(), CancelToken());
// }),
// ),
// ),
Align(
alignment: Alignment.centerRight,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: ActionChip(
avatar: Icon(
Icons.refresh,
color: Colors.teal,
),
label: Text(
'Restart build',
style: TextStyle(color: Colors.teal),
),
backgroundColor: Colors.white30,
shape:
StadiumBorder(side: BorderSide(color: Colors.grey[200])),
onPressed: () {
//TravisCIApi().restartBuild(buildData.jobs.first.id.toString(), CancelToken());
TravisCIApi().restartBuild(buildData.id.toString(), CancelToken());
}),
child: RestartCancelBuildButton(
buildId: buildData.id.toString(),
onChanged: onChanged,
isRestart: buildData.state == BuildState.passed ||
buildData.state == BuildState.failed ||
buildData.state == BuildState.errored ||
buildData.state == BuildState.canceled,
),
),
),
Divider(),
Padding(
padding:
const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
child: Text(
"Jobs",
style: TextStyle(fontSize: 16.0, fontWeight: FontWeight.w600),
),
),
ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: buildData.jobs.length,
itemBuilder: (_, index) => ListTile(
title: Text("View build log"),
title: Text(buildData.jobs.length == 1
? 'View build log'
: "#${buildData.jobs[index].id} log"),
leading: Icon(FeatherIcons.fileText),
trailing: Icon(Icons.keyboard_arrow_right),
onTap: () {
Expand Down
4 changes: 4 additions & 0 deletions lib/pages/details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ class _DetailsState extends State<Details> with SingleTickerProviderStateMixin {
? BuildDetails(
buildData: _buildsStore.builds.first,
showAppbar: false,
onChanged: (value) {
_buildsStore.getBuilds(
widget.repositoriesModel.id.toString(), CancelToken());
},
)
: Center(
child: Text("No builds for this repository"),
Expand Down
15 changes: 0 additions & 15 deletions lib/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,6 @@ class HomePageState extends State<HomePage> {
});
},
),
ListTile(
leading: Icon(FeatherIcons.briefcase),
title: Text("Jobs"),
selected: _index == 3,
onTap: () {
Navigator.of(context).pop();
setState(() {
_index = 3;
_title = "Jobs";
_selectedWidget = Center(
child: Text("Yet to be implemented \u{1f605}"),
);
});
},
),
],
),
),
Expand Down
Loading

0 comments on commit 8c2282d

Please sign in to comment.