closetodo.pp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. program closetodo;
  2. {$mode objfpc}{$H+}
  3. uses
  4. {$IFDEF UNIX}
  5. cthreads,
  6. {$ENDIF}
  7. Classes, SysUtils, CustApp, fpjson, gitlabclient,opensslsockets, jsonparser ;
  8. type
  9. { TCloseTodoApplication }
  10. TCloseTodoApplication = class(TCustomApplication)
  11. private
  12. FConfig : TGitlabConfig;
  13. FClient : TGitLabClient;
  14. FIDS : TStrings;
  15. FIssueState: String;
  16. FQuiet : Boolean;
  17. FNoWrite : Boolean;
  18. FToDoAction : String;
  19. procedure CloseTodo(aID: int64);
  20. procedure DoClientLog(Sender: TObject; const aMessage: string);
  21. procedure DoResource(Sender: TObject; aPage, aIndex, aCount: Integer; aObject: TJSONObject; aContinue: Boolean);
  22. protected
  23. procedure DoRun; override;
  24. public
  25. constructor Create(TheOwner: TComponent); override;
  26. destructor Destroy; override;
  27. procedure Usage(const aError : String); virtual;
  28. Property NoWrite : Boolean Read FNoWrite Write FNoWrite;
  29. Property Quiet : Boolean Read FQuiet Write FQuiet;
  30. Property ToDoAction : String Read FToDoAction Write FToDoAction;
  31. Property IssueState : String Read FIssueState Write FIssueState;
  32. end;
  33. { TCloseTodoApplication }
  34. procedure TCloseTodoApplication.DoResource(Sender: TObject; aPage, aIndex,
  35. aCount: Integer; aObject: TJSONObject; aContinue: Boolean);
  36. Var
  37. aData : TJSONData;
  38. Msg,aState : String;
  39. aBugID,aBugIID,aProjectID : Int64;
  40. begin
  41. Msg:=Format('[Page %d [%d/%d]: ',[aPage,aIndex,aCount]);
  42. aProjectID:=0;
  43. aBugID:=0;
  44. aBugIID:=0;
  45. aState:='';
  46. aData:=aObject.FindPath('target.state');
  47. if Assigned(aData) then
  48. aState:=aData.AsString;
  49. aData:=aObject.FindPath('project.id');
  50. if Assigned(aData) then
  51. aProjectID:=aData.AsInt64;
  52. aData:=aObject.FindPath('target.id');
  53. if Assigned(aData) then
  54. aBugID:=aData.AsInt64;
  55. aData:=aObject.FindPath('target.iid');
  56. if Assigned(aData) then
  57. aBugIID:=aData.Asint64;
  58. DoClientLog(Self,Msg+Format('Project: %d, bug: %d, bug iid: %d, state : %s',[aProjectID,aBugID,aBugIID,aState]));
  59. if (IssueState='any') or SameText(aState,IssueState) then
  60. begin
  61. if (FConfig.ProjectID=0) or (aProjectID=FConfig.ProjectID) then
  62. FIDS.Add(IntToStr(aObject.Get('id',Int64(0))));
  63. end
  64. end;
  65. procedure TCloseTodoApplication.CloseTodo(aID : int64);
  66. Var
  67. aResource : String;
  68. begin
  69. if (aID=-1) then
  70. exit;
  71. aResource:=Format('todos/%d/mark_as_done',[aID]);
  72. Writeln('Posting ',aResource);
  73. if not NoWrite then
  74. FClient.CreateResource(aResource,Nil);
  75. end;
  76. procedure TCloseTodoApplication.DoClientLog(Sender: TObject;
  77. const aMessage: string);
  78. begin
  79. if not FQuiet then
  80. Writeln(aMessage);
  81. end;
  82. procedure TCloseTodoApplication.DoRun;
  83. var
  84. ErrorMsg: String;
  85. ListFN,ConfigFN : String;
  86. I : Integer;
  87. begin
  88. Terminate;
  89. ErrorMsg:=CheckOptions('hc:l:qna:s:', ['help','config:','list:','quiet','no-write','action:','state:']);
  90. if (ErrorMsg<>'') or HasOption('h','help') then
  91. begin
  92. Usage(ErrorMsg);
  93. Exit;
  94. end;
  95. FQuiet:=HasOption('q','quiet');
  96. FNoWrite:=HasOption('n','no-write');
  97. ConfigFN:=GetOptionValue('c','config');
  98. FToDoAction:=GetOptionValue('a','action');
  99. if FToDoAction='' then
  100. FToDoAction:='assigned';
  101. FIssueState:=GetOptionValue('s','state');
  102. if FIssueState='' then
  103. FIssueState:='closed';
  104. if ConfigFN='' then
  105. begin
  106. Usage('Need gitlab config file');
  107. Exit;
  108. end;
  109. if not FileExists(ConfigFN)then
  110. begin
  111. Usage('Gitlab config file "'+ConfigFN+'" does not exist');
  112. Exit;
  113. end;
  114. FConfig.LoadFromFile(ConfigFN,'');
  115. FClient.Config:=FConfig;
  116. ListFN:=GetOptionValue('l','list');
  117. if FileExists(ListFN)then
  118. FIDS.LoadFromFile(ListFN)
  119. else
  120. begin
  121. FClient.ForEachResource('todos',['action',FTodoAction,'state','pending'],@DoResource);
  122. if ListFN<>'' then
  123. FIDS.SaveToFile(ListFN);
  124. end;
  125. For I:=0 to FIDS.Count-1 do
  126. CloseTodo(StrToInt64Def(FIDS[i],-1));
  127. Writeln(Format('Closed %d todos',[FIDS.Count]));
  128. // stop program loop
  129. Terminate;
  130. end;
  131. constructor TCloseTodoApplication.Create(TheOwner: TComponent);
  132. begin
  133. inherited Create(TheOwner);
  134. FConfig.Reset;
  135. FClient:=TGitLabClient.Create;
  136. FClient.OnLog:=@DoClientLog;
  137. StopOnException:=True;
  138. FIDS:=TStringList.Create;
  139. end;
  140. destructor TCloseTodoApplication.Destroy;
  141. begin
  142. FreeAndNil(FIDS);
  143. FreeAndNil(FClient);
  144. inherited Destroy;
  145. end;
  146. procedure TCloseTodoApplication.Usage(const aError: String);
  147. begin
  148. if (aError<>'') then
  149. Writeln('Error : ',aError);
  150. Writeln('Usage: ', ExeName, ' [options]');
  151. Writeln('Where [Options] is one or more of:');
  152. Writeln('-a --action=TYPE Action of todo: default is "assigned". Other possibilities include:');
  153. Writeln(' mentioned, build_failed, marked, approval_required, unmergeable, directly_addressed or merge_train_removed.');
  154. Writeln('-c --config=FILE Config file');
  155. Writeln('-h --help This help');
  156. Writeln('-l --list=FILE if file exists, read todo IDS from list. If file does not exist, write file after querying gitlab');
  157. Writeln('-n --no-write Do not actaully change the TODO item');
  158. Writeln('-q --quiet less messages');
  159. Writeln('-s --state=STATE State of issue coupled to TODO. default is "closed". If set to "any" all issues will be marked.');
  160. end;
  161. var
  162. Application: TCloseTodoApplication;
  163. begin
  164. Application:=TCloseTodoApplication.Create(nil);
  165. Application.Title:='Close Todos Application';
  166. Application.Run;
  167. Application.Free;
  168. end.