-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathLocalPullRequest.php
More file actions
147 lines (117 loc) · 4.54 KB
/
Copy pathLocalPullRequest.php
File metadata and controls
147 lines (117 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
declare(strict_types=1);
namespace Danger\Struct\Local;
use Danger\Exception\CouldNotGetFileContentException;
use Danger\Struct\CommentCollection;
use Danger\Struct\Commit;
use Danger\Struct\CommitCollection;
use Danger\Struct\File;
use Danger\Struct\FileCollection;
use Danger\Struct\PullRequest;
use Symfony\Component\Process\Process;
class LocalPullRequest extends PullRequest
{
/**
* @var CommitCollection<Commit>|null
*/
private ?CommitCollection $commits = null;
/**
* @var FileCollection<File>|null
*/
private ?FileCollection $files = null;
public function __construct(private string $repo, private string $local, private string $target)
{
$this->projectIdentifier = $this->local;
$this->id = $this->local;
$this->body = '';
$commits = $this->getCommits();
if ($commits->count() > 0) {
$firstCommit = $commits->first();
\assert($firstCommit !== null);
$this->title = $firstCommit->message;
$this->createdAt = $firstCommit->createdAt;
$this->updatedAt = $firstCommit->createdAt;
} else {
$this->title = 'empty';
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
}
}
public function getCommits(): CommitCollection
{
if ($this->commits !== null) {
return $this->commits;
}
$process = new Process([
'git',
'log',
'--pretty=format:{%n "commit": "%H",%n "abbreviated_commit": "%h",%n "tree": "%T",%n "abbreviated_tree": "%t",%n "parent": "%P",%n "abbreviated_parent": "%p",%n "refs": "%D",%n "encoding": "%e",%n "subject": "%s",%n "sanitized_subject_line": "%f",%n "body": "%b",%n "commit_notes": "%N",%n "verification_flag": "%G?",%n "signer": "%GS",%n "signer_key": "%GK",%n "author": {%n "name": "%aN",%n "email": "%aE",%n "date": "%aD"%n },%n "commiter": {%n "name": "%cN",%n "email": "%cE",%n "date": "%cD"%n }%n},',
$this->target . '..' . $this->local,
], $this->repo);
$process->mustRun();
$commits = new CommitCollection();
/** @var array{commit: string, author: array{name: string, email: string, date: string}, subject: string}[] $gitOutput */
$gitOutput = json_decode('[' . mb_substr($process->getOutput(), 0, -1) . ']', true);
foreach ($gitOutput as $commit) {
$commitObj = new Commit();
$commitObj->sha = $commit['commit'];
$commitObj->author = $commit['author']['name'];
$commitObj->authorEmail = $commit['author']['email'];
$commitObj->message = $commit['subject'];
$commitObj->createdAt = new \DateTime($commit['author']['date']);
$commits->add($commitObj);
}
return $this->commits = $commits;
}
public function getFile(string $fileName): File
{
return new LocalFile($this->repo . '/' . $fileName);
}
public function getFiles(): FileCollection
{
if ($this->files !== null) {
return $this->files;
}
$process = new Process([
'git',
'diff',
$this->target . '..' . $this->local,
'--name-status',
], $this->repo);
$process->mustRun();
$files = new FileCollection();
foreach (explode(\PHP_EOL, $process->getOutput()) as $line) {
if ($line === '') {
continue;
}
$status = $line[0];
$file = mb_trim(mb_substr($line, 1));
$element = new LocalFile($this->repo . '/' . $file);
$element->name = $file;
$element->additions = 0;
$element->changes = 0;
$element->deletions = 0;
if ($status === 'A') {
$element->status = File::STATUS_ADDED;
} elseif ($status === 'M') {
$element->status = File::STATUS_MODIFIED;
} else {
$element->status = File::STATUS_REMOVED;
}
$files->set($element->name, $element);
}
return $this->files = $files;
}
public function getComments(): CommentCollection
{
return new CommentCollection();
}
public function getFileContent(string $path): string
{
$file = $this->repo . '/' . $path;
if (!file_exists($file)) {
throw new CouldNotGetFileContentException($path);
}
return (string) file_get_contents($file);
}
}