Git::getWrapper()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
/** Created by PhpStorm,  User: jonphipps,  Date: 2018-02-15,  Time: 4:19 PM */
4
5
namespace App\Services\Publish;
6
7
use App\Jobs\GenerateRdf;
8
use App\Models\Access\User\User;
9
use App\Models\Project;
10
use App\Models\Release;
11
use GitWrapper\GitException;
12
use GitWrapper\GitWorkingCopy;
13
use GitWrapper\GitWrapper;
14
use Illuminate\Support\Facades\Storage;
15
16
class Git
17
{
18
    /**
19
     * @param \App\Models\Project $project
20
     * @param                     $message
21
     * @param string              $disk
22
     *
23
     * @return void
24
     * @throws GitException
25
     */
26
    public static function commitDir(Project $project, $message, $disk = GenerateRdf::REPO_ROOT): void
27
    {
28
        $projectPath = self::getProjectPath($project->id);
29
        $dir         = Storage::disk($disk)->path($projectPath);
30
31
        /** @var GitWrapper $wrapper */
32
        $wrapper = static::getWrapper();
33
        $git     = $wrapper->workingCopy($dir);
34
35
        if ($git->hasChanges()) {
36
            $git->add('.');
37
            $git->commit($message);
38
        }
39
    }
40
41
    /**
42
     * @param \App\Models\Project          $project
43
     * @param string                       $disk
44
     * @param \App\Models\Access\User\User $user
45
     *
46
     * @return void
47
     */
48
    public static function initDir(Project $project, $disk = GenerateRdf::REPO_ROOT, User $user): void
49
    {
50
        $projectPath = self::getProjectPath($project->id);
51
        $repo        = self::getProjectRepo($project,$user);
52
        $dir         = Storage::disk($disk)->path($projectPath);
53
        /** @var GitWrapper $wrapper */
54
        $wrapper = static::getWrapper();
55
56
        //this is what happens when the working directory exists (published), but has never been synced with a github remote
57
        if (Storage::disk($disk)->exists($projectPath) && $repo) {
58
            $git = $wrapper->workingCopy($dir);
59
            if (empty($git->getRemotes())) {
60
                //todo: this is much too heavy-handed. There may be previously published vocabs that aren't being published now
61
                //delete the dir
62
                Storage::disk($disk)->deleteDir($projectPath);
0 ignored issues
show
Bug introduced by
The method deleteDir() does not exist on Illuminate\Contracts\Filesystem\Filesystem. Did you maybe mean delete()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
                Storage::disk($disk)->/** @scrutinizer ignore-call */ deleteDir($projectPath);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
            } else {
64
                $git->pull();
65
            }
66
        }
67
        if (! Storage::disk($disk)->exists($projectPath)) {
68
            if ($repo) {
69
                $git = $wrapper->cloneRepository($repo, $dir);
70
                if (! $git->isCloned()) {
71
                    throw new GitException($git);
72
                }
73
            } else {
74
                Storage::disk($disk)->createDir($projectPath);
0 ignored issues
show
Bug introduced by
The method createDir() does not exist on Illuminate\Contracts\Filesystem\Filesystem. It seems like you code against a sub-type of said class. However, the method does not exist in Illuminate\Contracts\Filesystem\Cloud. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
                Storage::disk($disk)->/** @scrutinizer ignore-call */ createDir($projectPath);
Loading history...
75
                $wrapper->init($dir);
76
            }
77
        }
78
    }
79
80
    public static function getProjectPath(int $projectId): string
81
    {
82
        return GenerateRdf::PROJECT_ROOT . "/{$projectId}/";
83
    }
84
85
    public static function getProjectRepo(Project $project, User $user): ?string
86
    {
87
        $repo   = $project->repo;
88
        $access = '';
89
90
        if ($repo) {
91
            if ($user->githubToken) {
92
                $token    = $user->githubToken;
93
                $nickname = $user->nickname;
94
                $access   = "{$nickname}:{$token}@";
95
            }
96
97
            return "https://{$access}github.com/{$repo}.git";
98
        }
99
100
        return null;
101
    }
102
103
    /**
104
     * @return GitWrapper
105
     * @throws GitException
106
     */
107
    public static function getWrapper(): GitWrapper
108
    {
109
        try {
110
            $wrapper = new GitWrapper();
111
        } catch (GitException $e) {
112
            //we couldn't find the default and have to use the config
113
            $wrapper = new GitWrapper(config('app.git_executable'));
114
        }
115
116
        //$wrapper->setEnvVar('GIT_SSH_COMMAND', 'ssh -o StrictHostKeyChecking=no');
117
        return $wrapper;
118
    }
119
120
    /**
121
     * @param \App\Models\Project $project
122
     * @param                     $tag
123
     * @param string              $disk
124
     *
125
     * @return void
126
     */
127
    public static function tagDir(Project $project, $tag, $disk = GenerateRdf::REPO_ROOT)
128
    {
129
        $projectPath = self::getProjectPath($project->id);
130
        $dir         = Storage::disk($disk)->path($projectPath);
131
132
        /** @var GitWrapper $wrapper */
133
        $wrapper = static::getWrapper();
134
        $git     = $wrapper->workingCopy($dir);
135
136
        try {
137
            $git->tag($tag, '-f');
138
        } catch (GitException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
139
        }
140
    }
141
142
    /**
143
     * @param \App\Models\Release          $release
144
     * @param string                       $disk
145
     * @param \App\Models\Access\User\User $user
146
     *
147
     * @return void
148
     * @throws \GitWrapper\GitException
149
     */
150
    public static function updateRemote(Release $release, $disk = GenerateRdf::REPO_ROOT, User $user): void
151
    {
152
        $projectId   = $release->project_id;
153
        $tag         = $release->tag_name;
0 ignored issues
show
Unused Code introduced by
The assignment to $tag is dead and can be removed.
Loading history...
154
        $projectPath = self::getProjectPath($projectId);
155
        $dir         = Storage::disk($disk)->path($projectPath);
156
157
        /** @var GitWrapper $wrapper */
158
        $wrapper = static::getWrapper();
159
        $git     = $wrapper->workingCopy($dir);
160
        $git->config('user.name', $user->nickname);
161
        $git->config('user.email', $user->email);
162
163
        try {
164
            if ($git->hasRemote('origin')) {
165
                self::pushToGitHub($git);
166
            }
167
        } catch (GitException $e) {
168
            if (str_contains($e->getMessage(), 'fatal: No such remote')) {
169
                $repo = $release->project->repo;
170
                if ($repo) {
171
                    $url = 'https://github.com/' . $repo . '.git';
172
                    $git->addRemote('origin', $url);
173
                    self::pushToGitHub($git);
174
                }
175
            }
176
        }
177
    }
178
179
    /**
180
     * @param $git
181
     *
182
     * @throws \GitWrapper\GitException
183
     */
184
    private static function pushToGitHub(GitWorkingCopy $git): void
185
    {
186
        $git->push('origin', 'master');
187
    }
188
}
189