Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: initial github release version checker #4792

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:flutter/material.dart';

import 'package:appflowy/generated/flowy_svgs.g.dart';
import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';
Expand All @@ -7,7 +9,6 @@ import 'package:appflowy_popover/appflowy_popover.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flowy_infra/theme_extension.dart';
import 'package:flowy_infra_ui/flowy_infra_ui.dart';
import 'package:flutter/material.dart';
import 'package:styled_widget/styled_widget.dart';

const optionActionColorDefaultColor = 'appflowy_theme_default_color';
Expand Down Expand Up @@ -142,10 +143,7 @@ enum OptionDepthType {
class DividerOptionAction extends CustomActionCell {
@override
Widget buildWithContext(BuildContext context) {
return const Divider(
height: 1.0,
thickness: 1.0,
);
return const Divider(height: 1.0, thickness: 1.0);
}
}

Expand Down
3 changes: 3 additions & 0 deletions frontend/appflowy_flutter/lib/startup/deps_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import 'package:appflowy/workspace/application/settings/prelude.dart';
import 'package:appflowy/workspace/application/sidebar/rename_view/rename_view_bloc.dart';
import 'package:appflowy/workspace/application/tabs/tabs_bloc.dart';
import 'package:appflowy/workspace/application/user/prelude.dart';
import 'package:appflowy/workspace/application/version_checker/version_checker_bloc.dart';
import 'package:appflowy/workspace/application/view/prelude.dart';
import 'package:appflowy/workspace/application/workspace/prelude.dart';
import 'package:appflowy/workspace/presentation/home/menu/menu_shared_state.dart';
Expand Down Expand Up @@ -200,6 +201,8 @@ void _resolveHomeDeps(GetIt getIt) {
getIt.registerSingleton<ReminderBloc>(ReminderBloc());

getIt.registerSingleton<RenameViewBloc>(RenameViewBloc(PopoverController()));

getIt.registerSingleton<VersionCheckerBloc>(VersionCheckerBloc());
}

void _resolveFolderDeps(GetIt getIt) {
Expand Down
5 changes: 3 additions & 2 deletions frontend/appflowy_flutter/lib/startup/startup.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import 'dart:async';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'package:appflowy/env/cloud_env.dart';
import 'package:appflowy/startup/tasks/feature_flag_task.dart';
import 'package:appflowy/workspace/application/settings/prelude.dart';
import 'package:appflowy_backend/appflowy_backend.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:package_info_plus/package_info_plus.dart';

Expand Down
5 changes: 5 additions & 0 deletions frontend/appflowy_flutter/lib/startup/tasks/app_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:appflowy/workspace/application/notifications/notification_servic
import 'package:appflowy/workspace/application/settings/appearance/appearance_cubit.dart';
import 'package:appflowy/workspace/application/settings/notifications/notification_settings_cubit.dart';
import 'package:appflowy/workspace/application/sidebar/rename_view/rename_view_bloc.dart';
import 'package:appflowy/workspace/application/version_checker/version_checker_bloc.dart';
import 'package:appflowy/workspace/application/view/view_ext.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
Expand Down Expand Up @@ -136,6 +137,10 @@ class _ApplicationWidgetState extends State<ApplicationWidget> {
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider<VersionCheckerBloc>.value(
value: getIt<VersionCheckerBloc>()
..add(const VersionCheckerEvent.checkLatestRelease()),
),
BlocProvider<AppearanceSettingsCubit>(
create: (_) => AppearanceSettingsCubit(
widget.appearanceSetting,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// ignore_for_file: invalid_annotation_target

import 'dart:convert';

import 'package:appflowy_backend/log.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:http/http.dart' as http;

part 'github_service.freezed.dart';
part 'github_service.g.dart';

abstract class IGitHubService {
/// Checks for the latest release on GitHub.
///
/// Returns [GitHubReleaseInfo] if the request is successful, otherwise returns null.
Future<GitHubReleaseInfo?> checkLatestGitHubRelease();

/// Disposes of any resources used by the service.
///
void dispose();
}

@freezed
class GitHubReleaseInfo with _$GitHubReleaseInfo {
const GitHubReleaseInfo._();

const factory GitHubReleaseInfo({
@JsonKey(name: 'tag_name') required String tagName,
required String name,
@JsonKey(name: 'body') required String changelog,
@JsonKey(name: 'html_url') required String htmlUrl,
@JsonKey(name: 'created_at') required String createdAt,
@JsonKey(name: 'published_at') required String publishedAt,
}) = _GitHubReleaseInfo;

factory GitHubReleaseInfo.fromJson(Map<String, dynamic> json) =>
_$GitHubReleaseInfoFromJson(json);
}

class GitHubService implements IGitHubService {
GitHubService();

static const String _gitHubVersionHeader = 'X-GitHub-Api-Version';
static const String _gitHubVersion = '2022-11-28';

static const String _baseUrl = 'https://api.github.com';
static const String _repo = '/repos/AppFlowy-IO/AppFlowy';

static const String _latestReleaseEndpoint = '/releases/latest';

final http.Client _client = http.Client();
Xazin marked this conversation as resolved.
Show resolved Hide resolved
Uri _uri = Uri.parse(_baseUrl);

@override
Future<GitHubReleaseInfo?> checkLatestGitHubRelease() async {
_uri = _uri.replace(path: '$_repo$_latestReleaseEndpoint');
try {
final response = await _client.get(
_uri,
headers: {
_gitHubVersionHeader: _gitHubVersion,
},
);

if (response.statusCode == 200) {
final Map<String, dynamic> body =
jsonDecode(response.body) as Map<String, dynamic>;
return GitHubReleaseInfo.fromJson(body);
} else if (response.statusCode == 403) {
Xazin marked this conversation as resolved.
Show resolved Hide resolved
// For unauthenticated usage, the rate limit is 60 requests per hour.
//
// https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28
Log.warn(
'checkLatestGitHubRelease failed: GitHub API rate limit exceeded',
);
}
} on http.ClientException catch (e, s) {
Log.error(
'checkLatestGitHubRelease failed: ${e.message}\n$s',
);
} on TypeError catch (e) {
Log.error(
'checkLatestGitHubRelease failed mapping GitHubReleaseInfo\n${e.stackTrace}',
);
}

return null;
}

@override
void dispose() {
_client.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import 'package:flutter/foundation.dart';

import 'package:appflowy/user/application/github/github_service.dart';
import 'package:bloc/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:package_info_plus/package_info_plus.dart';

part 'version_checker_bloc.freezed.dart';

class VersionCheckerBloc
extends Bloc<VersionCheckerEvent, VersionCheckerState> {
VersionCheckerBloc() : super(const VersionCheckerState()) {
_gitHubService = GitHubService();

on<VersionCheckerEvent>((event, emit) async {
await event.when(
checkLatestRelease: () async {
final releaseInfo = await _gitHubService.checkLatestGitHubRelease();
final currentInfo = await PackageInfo.fromPlatform();

if (releaseInfo == null) {
// We "fail" silently here because we don't want to bother users without network connection
return emit(
state.copyWith(
appName: currentInfo.appName,
currentVersion: currentInfo.version,
isLoading: false,
isUpdateAvailable: false,
),
);
} else {
emit(
state.copyWith(
appName: currentInfo.appName,
currentVersion: currentInfo.version,
latestVersion: releaseInfo.tagName,
isUpdateAvailable: currentInfo.version != releaseInfo.tagName,
isLoading: false,
changelog: releaseInfo.changelog,
downloadLink: releaseInfo.htmlUrl,
),
);
}
},
);
});
}

late final IGitHubService _gitHubService;

@override
Future<void> close() async {
_gitHubService.dispose();
return super.close();
}
}

@freezed
class VersionCheckerEvent with _$VersionCheckerEvent {
const factory VersionCheckerEvent.checkLatestRelease() = _CheckLatestRelease;
}

@freezed
class VersionCheckerState with _$VersionCheckerState {
const factory VersionCheckerState({
@Default(null) String? appName,
@Default(null) String? currentVersion,
@Default(null) String? latestVersion,
@Default(false) bool isUpdateAvailable,
@Default(true) bool isLoading,
@Default(null) String? changelog,
@Default(null) String? downloadLink,
}) = _VersionCheckerState;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:flutter/material.dart';

import 'package:appflowy/generated/flowy_svgs.g.dart';
import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/workspace/application/user/user_workspace_bloc.dart';
Expand All @@ -7,7 +9,6 @@ import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
import 'package:appflowy_popover/appflowy_popover.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flowy_infra_ui/flowy_infra_ui.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

enum WorkspaceMoreAction {
Expand Down
Loading
Loading