This commit is contained in:
James 2019-11-10 15:02:44 +00:00
parent 5352a3ea37
commit 3fcd44e9ce
9 changed files with 142 additions and 130 deletions

View File

@ -1,16 +1,17 @@
#!/usr/bin/php #!/usr/bin/php
<?php <?php
/****************** /*
* close ALL issues * get ALL open issues
******************/ * 1. close thm
*/
// load the config, create the connection and load the repo sepcified on the cmd line args // load the config, create the connection and load the repo sepcified on the cmd line args
// we will then have $client and $repo available // we will then have $client and $repo available
require('setup.php'); require 'setup.php';
// close all issues // close all issues
// we use getIssues() here rather than forIssues() (see https://git.jhodges.co.uk/jhodges/GiteaBot/issues/1) // we use getIssues() here rather than forIssues() (see https://git.jhodges.co.uk/jhodges/GiteaBot/issues/1)
foreach($repo->getIssues() as $issue){ foreach ($repo->getIssues() as $issue) {
$issue->state='closed'; $issue->state = 'closed';
$issue->save(); $issue->save();
} }

View File

@ -1,27 +1,27 @@
#!/usr/bin/php #!/usr/bin/php
<?php <?php
/**************************************************** /*
* This will loop through a list of label names * This will loop through a list of label names
* If the label exists on the repo, delete the label * If the label exists on the repo, delete the label
****************************************************/ */
// load the config, create the connection and load the repo sepcified on the cmd line args // load the config, create the connection and load the repo sepcified on the cmd line args
// we will then have $client and $repo available // we will then have $client and $repo available
require('setup.php'); require 'setup.php';
//define the repeat terms //define the repeat terms
$names=[ $names = [
'janurary','feburary','march','april','may','june','july','august','september','october','november','december', 'janurary', 'feburary', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december',
'monday','tuesday','wednesday','thursday','friday','saturday','sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday',
'daily','weekly','fortnightly','monthly','yearly' 'daily', 'weekly', 'fortnightly', 'monthly', 'yearly',
]; ];
//loop through the names //loop through the names
foreach($names as $name){ foreach ($names as $name) {
//see if label exists? //see if label exists?
$label=$repo->getLabelByName($name); $label = $repo->getLabelByName($name);
if($label){ //it exists if ($label) { //it exists
//delete it //delete it
$repo->deleteLabel($label); $repo->deleteLabel($label);
} }
} }

View File

@ -1,28 +1,28 @@
#!/usr/bin/php #!/usr/bin/php
<?php <?php
/************************************************** /*
* this will process all issues titled "delete me": * this will process all issues titled "delete me":
* 1. remove all labels * 1. remove all labels
* 2. close the issue * 2. close the issue
* 3. change the title to "deleted" * 3. change the title to "deleted"
**************************************************/ */
// load the config, create the connection and load the repo sepcified on the cmd line args // load the config, create the connection and load the repo sepcified on the cmd line args
// we will then have $client and $repo available // we will then have $client and $repo available
require('setup.php'); require 'setup.php';
// loop through all matching issues // loop through all matching issues
// we use getIssues() here rather than forIssues() (see https://git.jhodges.co.uk/jhodges/GiteaBot/issues/1) // we use getIssues() here rather than forIssues() (see https://git.jhodges.co.uk/jhodges/GiteaBot/issues/1)
foreach($repo->getIssues(['q'=>'delete me']) as $issue){ foreach ($repo->getIssues(['q' => 'delete me']) as $issue) {
// check for exact match // check for exact match
if($issue->title=='delete me'){ if ('delete me' == $issue->title) {
// remove all labels // remove all labels
foreach($issue->labels as $label){ foreach ($issue->labels as $label) {
$issue->removeLabel($label); $issue->removeLabel($label);
}
//change status and title.
$issue->state = 'closed';
$issue->title = 'deleted';
$issue->save();
} }
//change status and title. }
$issue->state='closed';
$issue->title='deleted';
$issue->save();
}
};

View File

@ -1,24 +1,24 @@
#!/usr/bin/php #!/usr/bin/php
<?php <?php
/************************************************************** /*
* This will delete all comments that match a specific keyword * This will delete all comments that match a specific keyword
* 1. Process all issues that match "NAG!": * 1. Process all issues that match "NAG!":
* 2. Delete any comments that body exactly match "NAG!" * 2. Delete any comments that body exactly match "NAG!"
**************************************************************/ */
// load the config, create the connection and load the repo sepcified on the cmd line args // load the config, create the connection and load the repo sepcified on the cmd line args
// we will then have $client and $repo available // we will then have $client and $repo available
require('setup.php'); require 'setup.php';
// loop through the issues // loop through the issues
// we use getIssues() here rather than forIssues() (see https://git.jhodges.co.uk/jhodges/GiteaBot/issues/1) // we use getIssues() here rather than forIssues() (see https://git.jhodges.co.uk/jhodges/GiteaBot/issues/1)
foreach($repo->getIssues(['q'=>'NAG!']) as $issue){ foreach ($repo->getIssues(['q' => 'NAG!']) as $issue) {
// loop through the current issues comments // loop through the current issues comments
foreach($issue->getComments() as $comment){ foreach ($issue->getComments() as $comment) {
// if the comment body matches // if the comment body matches
if($comment->body=='NAG!'){ if ('NAG!' == $comment->body) {
// delete the commend // delete the commend
$repo->deleteComment($comment); $repo->deleteComment($comment);
}
} }
}
} }

View File

@ -1,30 +1,30 @@
#!/usr/bin/php #!/usr/bin/php
<?php <?php
/******************************************************* /*
* This will loop through all issues and add some custom * This will loop through all issues and add some custom
* text to the issue body. It will use a placeholder so * text to the issue body. It will use a placeholder so
* that it can be automatically updated without replacing * that it can be automatically updated without replacing
* the whole issue body * the whole issue body
********************************************************/ */
// load the config, create the connection and load the repo sepcified on the cmd line args // load the config, create the connection and load the repo sepcified on the cmd line args
// we will then have $client and $repo available // we will then have $client and $repo available
require('setup.php'); require 'setup.php';
//this is the function to generate the custom text //this is the function to generate the custom text
$text=function($issue){ $text = function ($issue) {
return "\n```plain\n".$issue->number."\n".$issue->title."\ntexting\n".time()."\n```\n"; return "\n```plain\n".$issue->number."\n".$issue->title."\ntexting\n".time()."\n```\n";
}; };
//callback function to add/replace the tag with our text //callback function to add/replace the tag with our text
$tmp=function($issue) use ($text){ $tmp = function ($issue) use ($text) {
// add a tag to the body if to doesn't exist // add a tag to the body if to doesn't exist
if( strpos($issue->body,'<!--BOT-->')===false ){ if (false === strpos($issue->body, '<!--BOT-->')) {
$issue->body.='<hr/><!--BOT--><!--ENDBOT-->'; $issue->body .= '<hr/><!--BOT--><!--ENDBOT-->';
} }
//replace the tag contents with the result from our $text function //replace the tag contents with the result from our $text function
$issue->body=preg_replace("#<!--BOT-->.*?<!--ENDBOT-->#s",'<!--BOT-->'.$text($issue).'<!--ENDBOT-->',$issue->body); $issue->body = preg_replace('#<!--BOT-->.*?<!--ENDBOT-->#s', '<!--BOT-->'.$text($issue).'<!--ENDBOT-->', $issue->body);
$issue->save(); $issue->save();
}; };
//process ALL issues //process ALL issues

View File

@ -1,40 +1,50 @@
#!/usr/bin/php #!/usr/bin/php
<?php <?php
/*************************************************************************************************************** /*
* 1. Adds a 'no-milestone' label to any issues with no milestone * 1. Adds a 'no-milestone' label to any issues with no milestone
* 2. Removes the 'no-milestone' label if present on an issue with a milestone * 2. Removes the 'no-milestone' label if present on an issue with a milestone
* 3. Adds a 'no-label' label to any issues with no labels (excluding 'no-label' and 'no-milestone') * 3. Adds a 'no-label' label to any issues with no labels (excluding 'no-label' and 'no-milestone')
* 4. Removes the 'no-label' label if present on an issue with a label (excluding 'no-label' and 'no-milestone') * 4. Removes the 'no-label' label if present on an issue with a label (excluding 'no-label' and 'no-milestone')
****************************************************************************************************************/ */
// load the config, create the connection and load the repo sepcified on the cmd line args // load the config, create the connection and load the repo sepcified on the cmd line args
// we will then have $client and $repo available // we will then have $client and $repo available
require('setup.php'); require 'setup.php';
//get the labels of interest //get the labels of interest
$nolabelLabel=$repo->getLabelByName('no-label'); $nolabelLabel = $repo->getLabelByName('no-label');
if(!$nolabelLabel) die ("Can't find 'no-label' label in repo\n"); if (!$nolabelLabel) {
die("Can't find 'no-label' label in repo\n");
}
$nomilestoneLabel=$repo->getLabelByName('no-milestone'); $nomilestoneLabel = $repo->getLabelByName('no-milestone');
if(!$nomilestoneLabel) die ("Can't find 'no-milestone' label in repo\n"); if (!$nomilestoneLabel) {
die("Can't find 'no-milestone' label in repo\n");
}
//define the function to process each issue //define the function to process each issue
$callback=function($issue) use ($nolabelLabel,$nomilestoneLabel){ $callback = function ($issue) use ($nolabelLabel,$nomilestoneLabel) {
// do the no-label thing // do the no-label thing
$labelCount=count($issue->labels); $labelCount = count($issue->labels);
if($issue->hasLabel($nolabelLabel)) $labelCount--; //dont count the no-label label if ($issue->hasLabel($nolabelLabel)) {
if($nomilestoneLabel && $issue->hasLabel($nomilestoneLabel)) $labelCount--; //dont count the no-milestone label --$labelCount;
if($labelCount==0 && !$issue->hasLabel($nolabelLabel) ){ }
$issue->addLabel( $nolabelLabel ); //dont count the no-label label
}elseif( $labelCount>0 && $issue->hasLabel($nolabelLabel) ){ if ($nomilestoneLabel && $issue->hasLabel($nomilestoneLabel)) {
$issue->removeLabel( $nolabelLabel ); --$labelCount;
} }
//do the no-milestone thing //dont count the no-milestone label
if( !$issue->milestone && !$issue->hasLabel($nomilestoneLabel) ){ if (0 == $labelCount && !$issue->hasLabel($nolabelLabel)) {
$issue->addLabel( $nomilestoneLabel ); $issue->addLabel($nolabelLabel);
}elseif( $issue->milestone && $issue->hasLabel($nomilestoneLabel) ){ } elseif ($labelCount > 0 && $issue->hasLabel($nolabelLabel)) {
$issue->removeLabel( $nomilestoneLabel ); $issue->removeLabel($nolabelLabel);
} }
//do the no-milestone thing
if (!$issue->milestone && !$issue->hasLabel($nomilestoneLabel)) {
$issue->addLabel($nomilestoneLabel);
} elseif ($issue->milestone && $issue->hasLabel($nomilestoneLabel)) {
$issue->removeLabel($nomilestoneLabel);
}
}; };
//loop through all issues and call the callback //loop through all issues and call the callback

View File

@ -1,17 +1,17 @@
<?php <?php
require(__DIR__.'/../vendor/autoload.php');
require __DIR__.'/../vendor/autoload.php';
use JHodges\GiteaBot\Client; use JHodges\GiteaBot\Client;
$config=require(__DIR__.'/config.php'); $config = require __DIR__.'/config.php';
//pass cmd line args //pass cmd line args
if($argc<3){ if ($argc < 3) {
die(basename(__FILE__)." <USER> <REPO> [debug]\n"); die(basename(__FILE__)." <USER> <REPO> [debug]\n");
} }
$debug = ($argc==4 && $argv[3]=='debug'); $debug = (4 == $argc && 'debug' == $argv[3]);
//open connection and repo //open connection and repo
$client=new Client($config['url'],$config['user'],$config['pass'],$debug); $client = new Client($config['url'], $config['user'], $config['pass'], $debug);
$repo=$client->getRepo($argv[1],$argv[2]); $repo = $client->getRepo($argv[1], $argv[2]);

View File

@ -2,22 +2,22 @@
<?php <?php
// load the config, create the connection and load the repo sepcified on the cmd line args // load the config, create the connection and load the repo sepcified on the cmd line args
// we will then have $client and $repo available // we will then have $client and $repo available
require('setup.php'); require 'setup.php';
//get the test label or create if not exist //get the test label or create if not exist
if(!$label=$repo->getLabelByName('Test')){ if (!$label = $repo->getLabelByName('Test')) {
$label=$repo->createLabel(['name'=>'Test','color'=>'#336699']); $label = $repo->createLabel(['name' => 'Test', 'color' => '#336699']);
} }
//create a test issue //create a test issue
$issue=$repo->createIssue([ $issue = $repo->createIssue([
'title'=>'Test', 'title' => 'Test',
'body'=>'Just testing' 'body' => 'Just testing',
]); ]);
$issue->addLabel($label); $issue->addLabel($label);
$issue->addComment('Test comment'); $issue->addComment('Test comment');
$issue->save(); $issue->save();
//change the issue body //change the issue body
$issue->body.=' (Edited!)'; $issue->body .= ' (Edited!)';
$issue->save(); $issue->save();

View File

@ -1,58 +1,59 @@
#!/usr/bin/php #!/usr/bin/php
<?php <?php
/******************************************************* /*
* Creats a csv report of times spend on issues for * Creats a csv report of times spend on issues for
* a specific month of a specific user. * a specific month of a specific user.
********************************************************/ */
// load the config, create the connection and load the repo sepcified on the cmd line args // load the config, create the connection and load the repo sepcified on the cmd line args
// we will then have $client and $repo available // we will then have $client and $repo available
require('setup.php'); require 'setup.php';
// Build a crazy id to number lookup table // Build a crazy id to number lookup table
// https://github.com/go-gitea/gitea/issues/8513 // https://github.com/go-gitea/gitea/issues/8513
foreach($repo->getIssues() as $issue){ foreach ($repo->getIssues() as $issue) {
$issueNumbers[$issue->id]=$issue->number; $issueNumbers[$issue->id] = $issue->number;
} }
foreach($repo->getPulls() as $pull){ foreach ($repo->getPulls() as $pull) {
$issue=$repo->getIssue($pull->number); $issue = $repo->getIssue($pull->number);
$issueNumbers[$issue->id]=$issue->number; $issueNumbers[$issue->id] = $issue->number;
} }
foreach($repo->getIssues(['state'=>'closed']) as $issue){ foreach ($repo->getIssues(['state' => 'closed']) as $issue) {
$issueNumbers[$issue->id]=$issue->number; $issueNumbers[$issue->id] = $issue->number;
} }
foreach($repo->getPulls(['state'=>'closed']) as $pull){ foreach ($repo->getPulls(['state' => 'closed']) as $pull) {
$issue=$repo->getIssue($pull->number); $issue = $repo->getIssue($pull->number);
$issueNumbers[$issue->id]=$issue->number; $issueNumbers[$issue->id] = $issue->number;
} }
$name='jhodges'; $name = 'jhodges';
$date='2019-10'; $date = '2019-10';
$sums=[]; $sums = [];
foreach($repo->getTimesByUsername($name) as $time){ foreach ($repo->getTimesByUsername($name) as $time) {
if($date==date('Y-m',strtotime($time->created))){ if ($date == date('Y-m', strtotime($time->created))) {
$number=$issueNumbers[$time->issue_id]; $number = $issueNumbers[$time->issue_id];
if(!isset($sums[$number])){ if (!isset($sums[$number])) {
$issue=$repo->getIssue($number); $issue = $repo->getIssue($number);
$sums[$number]=[ $sums[$number] = [
'id'=>$number, 'id' => $number,
'name'=>$issue->title, 'name' => $issue->title,
'time'=>0 'time' => 0,
]; ];
}
$sums[$number]['time'] += $time->time;
} }
$sums[$number]['time']+=$time->time;
}
} }
function format_time($t){ function format_time($t)
return sprintf('%02d:%02d:%02d', floor($t/3600), ($t/60)%60, $t%60); {
return sprintf('%02d:%02d:%02d', floor($t / 3600), ($t / 60) % 60, $t % 60);
} }
$f=fopen('report.csv','w'); $f = fopen('report.csv', 'w');
fputcsv($f,['id','title','time']); fputcsv($f, ['id', 'title', 'time']);
foreach($sums as $id=>$data){ foreach ($sums as $id => $data) {
$data['time']=format_time($data['time']); $data['time'] = format_time($data['time']);
fputcsv($f,$data); fputcsv($f, $data);
} }
fclose($f); fclose($f);
echo "Saved as report.csv\n"; echo "Saved as report.csv\n";