diff --git a/assets/pipeline-test2.yaml b/assets/pipeline-test2.yaml index ee15870..0afa70f 100644 --- a/assets/pipeline-test2.yaml +++ b/assets/pipeline-test2.yaml @@ -12,6 +12,7 @@ pipeline: id: main workflow: - exe: read + - exe: branch - exe: param-modify - exe: write @@ -35,6 +36,15 @@ commands: to: setValue: <@property.content> +### List up branches +- command: GitBranch + id: branch + param: + path: /Users/toki/works/lgup-mcs-aos + startDay: 20 + setBranches: <@property.value> + +### Modify param - command: JenkinsParameterModify id: param-modify param: diff --git a/lib/oto/commands/command.dart b/lib/oto/commands/command.dart index 8d1359d..0d113b4 100644 --- a/lib/oto/commands/command.dart +++ b/lib/oto/commands/command.dart @@ -101,6 +101,7 @@ enum CommandType { GitReset, GitStashPush, GitStashApply, + GitBranch, GitHub, GitHubPullRequestCreate, GitHubPullRequestList, @@ -177,6 +178,7 @@ abstract class Command { CommandType.GitReset: () => GitReset(), CommandType.GitStashPush: () => GitStashPush(), CommandType.GitStashApply: () => GitStashApply(), + CommandType.GitBranch: () => GitBranch(), CommandType.GitHub: () => GitHub(), CommandType.GitHubPullRequestCreate: () => GitHubPullRequestCreate(), CommandType.GitHubPullRequestList: () => GitHubPullRequestList(), diff --git a/lib/oto/commands/git/git.dart b/lib/oto/commands/git/git.dart index f6578eb..0930fa8 100644 --- a/lib/oto/commands/git/git.dart +++ b/lib/oto/commands/git/git.dart @@ -1,3 +1,6 @@ +import 'dart:convert'; +import 'dart:io'; + import 'package:dart_framework/utils/system_util.dart'; import 'package:oto_cli/oto/application.dart'; import 'package:oto_cli/oto/commands/command.dart'; @@ -220,3 +223,40 @@ class GitStashApply extends Command { return await completeProcess(process, command, passExitCodes: [1]); } } + +//무시가가능한 에러 무시 +class GitBranch extends Command { + @override + Future execute(DataCommand command) async { + var data = DataGitBranch.fromJson(getParam(command)); + + var shell = StringBuffer('cd ${data.path}'); + shell.write( + ' $and git for-each-ref --format=\'{"ref": "%(refname:short)", "sha": "%(objectname)", "date": "%(committerdate:iso8601)"}\' refs/remotes | jq -s . > branches.json'); + + var process = + await ProcessExecutor.start(shell, logHandler: Application.logWithType); + + var file = File('${data.path}/branches.json'); + var content = file.readAsStringSync(); + final List decoded = jsonDecode(content); + final branches = decoded.map((e) => BranchInfo.fromJson(e)).toList(); + var list = []; + var fixedBranch = ['main', 'master', 'develop']; + var startDate = + DateTime.now().subtract(Duration(days: data.startDay!)).millisecond; + for (var branch in branches) { + var branchName = branch.ref!.replaceFirst('origin/', ''); + if (DateTime.parse(branch.date!).millisecond > startDate) { + list.add(branchName); + } else { + if (fixedBranch.contains(branchName)) { + list.add(branchName); + } + } + } + setProperty(data.setBranches!, list, showPrint: true); + + return await completeProcess(process, command); + } +} diff --git a/lib/oto/data/command_data.dart b/lib/oto/data/command_data.dart index 847683b..297f3a6 100644 --- a/lib/oto/data/command_data.dart +++ b/lib/oto/data/command_data.dart @@ -721,6 +721,35 @@ class DataGitCommit extends DataParam { Map toJson() => _$DataGitCommitToJson(this); } +//GitBranch +@JsonSerializable() +class DataGitBranch extends DataParam { + DataGitBranch(); + + late String? path; + late int? startDay; + late String? setBranches; + + factory DataGitBranch.fromJson(Map json) => + _$DataGitBranchFromJson(json); + @override + Map toJson() => _$DataGitBranchToJson(this); +} + +@JsonSerializable() +class BranchInfo { + BranchInfo(); + + late String? ref; + late String? sha; + late String? date; + + factory BranchInfo.fromJson(Map json) => + _$BranchInfoFromJson(json); + @override + Map toJson() => _$BranchInfoToJson(this); +} + //GitPush @JsonSerializable() class DataGitPush extends DataParam { diff --git a/lib/oto/data/command_data.g.dart b/lib/oto/data/command_data.g.dart index 61465ed..9b9fd9a 100644 --- a/lib/oto/data/command_data.g.dart +++ b/lib/oto/data/command_data.g.dart @@ -106,6 +106,7 @@ const _$CommandTypeEnumMap = { CommandType.GitReset: 'GitReset', CommandType.GitStashPush: 'GitStashPush', CommandType.GitStashApply: 'GitStashApply', + CommandType.GitBranch: 'GitBranch', CommandType.GitHub: 'GitHub', CommandType.GitHubPullRequestCreate: 'GitHubPullRequestCreate', CommandType.GitHubPullRequestList: 'GitHubPullRequestList', @@ -1278,6 +1279,43 @@ Map _$DataGitCommitToJson(DataGitCommit instance) => 'message': instance.message, }; +DataGitBranch _$DataGitBranchFromJson(Map json) => + DataGitBranch() + ..workspace = json['workspace'] as String? + ..passExitCodes = (json['passExitCodes'] as List?) + ?.map((e) => (e as num).toInt()) + .toList() + ..setResult = json['setResult'] as String? + ..setExitCode = json['setExitCode'] as String? + ..showPrint = json['showPrint'] as bool? + ..path = json['path'] as String? + ..startDay = (json['startDay'] as num?)?.toInt() + ..setBranches = json['setBranches'] as String?; + +Map _$DataGitBranchToJson(DataGitBranch instance) => + { + 'workspace': instance.workspace, + 'passExitCodes': instance.passExitCodes, + 'setResult': instance.setResult, + 'setExitCode': instance.setExitCode, + 'showPrint': instance.showPrint, + 'path': instance.path, + 'startDay': instance.startDay, + 'setBranches': instance.setBranches, + }; + +BranchInfo _$BranchInfoFromJson(Map json) => BranchInfo() + ..ref = json['ref'] as String? + ..sha = json['sha'] as String? + ..date = json['date'] as String?; + +Map _$BranchInfoToJson(BranchInfo instance) => + { + 'ref': instance.ref, + 'sha': instance.sha, + 'date': instance.date, + }; + DataGitPush _$DataGitPushFromJson(Map json) => DataGitPush() ..workspace = json['workspace'] as String? ..passExitCodes = (json['passExitCodes'] as List?)