Skip to content

Commit

Permalink
update #34
Browse files Browse the repository at this point in the history
- Added option to add query only specific repository for a module instead of the whole organization
  • Loading branch information
oyeaussie committed Aug 23, 2024
1 parent b24b7e2 commit 1267fe6
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@
'componentId' : componentId,
'sectionId' : sectionId,
'fieldId' : 'repo_url',
'fieldLabel' : 'API Repo Url',
'fieldLabel' : 'Org/User/Repo Url',
'fieldType' : 'input',
'fieldHelp' : true,
'fieldHelpTooltipContent' : 'API Repo URL',
'fieldHelpTooltipContent' : 'Org/User/Repo Url',
'fieldRequired' : true,
'fieldBazScan' : true,
'fieldBazPostOnCreate' : true,
Expand All @@ -140,10 +140,10 @@
'componentId' : componentId,
'sectionId' : sectionId,
'fieldId' : 'branch',
'fieldLabel' : 'API Repo Branch',
'fieldLabel' : 'Repo Branch',
'fieldType' : 'input',
'fieldHelp' : true,
'fieldHelpTooltipContent' : 'API Repo Branch',
'fieldHelpTooltipContent' : 'Repo Branch',
'fieldRequired' : true,
'fieldBazScan' : true,
'fieldBazPostOnCreate' : true,
Expand Down Expand Up @@ -350,8 +350,8 @@
'{{componentId}}-{{sectionId}}-description' : { },
'{{componentId}}-{{sectionId}}-api_url' : { },
'{{componentId}}-{{sectionId}}-org_user' : { },
'{{componentId}}-{{sectionId}}-branch' : { },
'{{componentId}}-{{sectionId}}-repo_url' : { },
'{{componentId}}-{{sectionId}}-branch' : { },
'{{componentId}}-{{sectionId}}-auth_type' : {
afterInit : function() {
$('#{{componentId}}-{{sectionId}}-auth_type').click(function() {
Expand Down Expand Up @@ -379,8 +379,10 @@
'{{componentId}}-{{sectionId}}-name' : 'required',
'{{componentId}}-{{sectionId}}-category' : 'required',
'{{componentId}}-{{sectionId}}-location' : 'required',
'{{componentId}}-{{sectionId}}-api_url' : 'required',
'{{componentId}}-{{sectionId}}-org_user' : 'required',
'{{componentId}}-{{sectionId}}-repo_url' : 'required',
'{{componentId}}-{{sectionId}}-site_url' : 'required',
'{{componentId}}-{{sectionId}}-branch' : 'required',
'{{componentId}}-{{sectionId}}-username' : 'required',
'{{componentId}}-{{sectionId}}-password' : 'required',
'{{componentId}}-{{sectionId}}-access_token' : 'required',
Expand All @@ -390,8 +392,10 @@
'{{componentId}}-{{sectionId}}-name' : 'Please enter api name',
'{{componentId}}-{{sectionId}}-category' : 'Please enter api category',
'{{componentId}}-{{sectionId}}-location' : 'Please enter api location',
'{{componentId}}-{{sectionId}}-api_url' : 'Please enter repo url',
'{{componentId}}-{{sectionId}}-org_user' : 'Please enter org name or username',
'{{componentId}}-{{sectionId}}-repo_url' : 'Please enter repo url',
'{{componentId}}-{{sectionId}}-site_url' : 'Please enter site url',
'{{componentId}}-{{sectionId}}-branch' : 'Please enter branch (main/dev)',
'{{componentId}}-{{sectionId}}-username' : 'Please enter username',
'{{componentId}}-{{sectionId}}-password' : 'Please enter password',
'{{componentId}}-{{sectionId}}-access_token' : 'Please enter api access token',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
'fieldLabel' : 'Org/User',
'fieldType' : 'input',
'fieldHelp' : true,
'fieldHelpTooltipContent' : 'Org/User name',
'fieldHelpTooltipContent' : 'Org/User name. Note: For modules, ',
'fieldRequired' : true,
'fieldBazScan' : true,
'fieldBazPostOnCreate' : true,
Expand All @@ -118,10 +118,10 @@
'componentId' : componentId,
'sectionId' : sectionId,
'fieldId' : 'repo_url',
'fieldLabel' : 'API Repo Url',
'fieldLabel' : 'Org/User/Repo Url',
'fieldType' : 'input',
'fieldHelp' : true,
'fieldHelpTooltipContent' : 'API Repo URL',
'fieldHelpTooltipContent' : 'Org/User/Repo Url',
'fieldRequired' : true,
'fieldBazScan' : true,
'fieldBazPostOnCreate' : true,
Expand All @@ -140,10 +140,10 @@
'componentId' : componentId,
'sectionId' : sectionId,
'fieldId' : 'branch',
'fieldLabel' : 'API Repo Branch',
'fieldLabel' : 'Repo Branch',
'fieldType' : 'input',
'fieldHelp' : true,
'fieldHelpTooltipContent' : 'API Repo Branch',
'fieldHelpTooltipContent' : 'Repo Branch',
'fieldRequired' : true,
'fieldBazScan' : true,
'fieldBazPostOnCreate' : true,
Expand Down Expand Up @@ -350,8 +350,8 @@
'{{componentId}}-{{sectionId}}-description' : { },
'{{componentId}}-{{sectionId}}-api_url' : { },
'{{componentId}}-{{sectionId}}-org_user' : { },
'{{componentId}}-{{sectionId}}-branch' : { },
'{{componentId}}-{{sectionId}}-repo_url' : { },
'{{componentId}}-{{sectionId}}-branch' : { },
'{{componentId}}-{{sectionId}}-auth_type' : {
afterInit : function() {
$('#{{componentId}}-{{sectionId}}-auth_type').click(function() {
Expand Down Expand Up @@ -379,8 +379,10 @@
'{{componentId}}-{{sectionId}}-name' : 'required',
'{{componentId}}-{{sectionId}}-category' : 'required',
'{{componentId}}-{{sectionId}}-location' : 'required',
'{{componentId}}-{{sectionId}}-api_url' : 'required',
'{{componentId}}-{{sectionId}}-org_user' : 'required',
'{{componentId}}-{{sectionId}}-repo_url' : 'required',
'{{componentId}}-{{sectionId}}-site_url' : 'required',
'{{componentId}}-{{sectionId}}-branch' : 'required',
'{{componentId}}-{{sectionId}}-username' : 'required',
'{{componentId}}-{{sectionId}}-password' : 'required',
'{{componentId}}-{{sectionId}}-access_token' : 'required',
Expand All @@ -390,8 +392,10 @@
'{{componentId}}-{{sectionId}}-name' : 'Please enter api name',
'{{componentId}}-{{sectionId}}-category' : 'Please enter api category',
'{{componentId}}-{{sectionId}}-location' : 'Please enter api location',
'{{componentId}}-{{sectionId}}-api_url' : 'Please enter repo url',
'{{componentId}}-{{sectionId}}-org_user' : 'Please enter org name or username',
'{{componentId}}-{{sectionId}}-repo_url' : 'Please enter repo url',
'{{componentId}}-{{sectionId}}-site_url' : 'Please enter site url',
'{{componentId}}-{{sectionId}}-branch' : 'Please enter branch (main/dev)',
'{{componentId}}-{{sectionId}}-username' : 'Please enter username',
'{{componentId}}-{{sectionId}}-password' : 'Please enter password',
'{{componentId}}-{{sectionId}}-access_token' : 'Please enter api access token',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public function register($db, $ff)
$modulesRepo =
[
'api_url' => 'https://api.github.com',
'org_user' => 'sp-framework',
'repo_url' => 'https://github.com/sp-framework/sp-modules',
'org_user' => 'sp-modules',
'repo_url' => 'https://github.com/sp-modules/',
'branch' => 'main',
'auth_type' => 'autho',
'authorization' => ''
Expand Down
113 changes: 87 additions & 26 deletions system/Base/Providers/ModulesServiceProvider/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,28 +473,45 @@ public function syncRemoteWithLocal($data)

protected function getRemoteModules()
{
if (strtolower($this->apiClientConfig['provider']) === 'gitea') {
$collection = 'OrganizationApi';
$method = 'orgListRepos';
$args = [$this->apiClientConfig['org_user']];
} else if (strtolower($this->apiClientConfig['provider']) === 'github') {
$collection = 'ReposApi';
$method = 'reposListForOrg';
$args = [$this->apiClientConfig['org_user']];
}
$this->apiClientConfig['repo_url'] = rtrim($this->apiClientConfig['repo_url'], '/');
//Get one or core repo details, no need to loop through all repos in the org.
if (str_ends_with($this->apiClientConfig['repo_url'], '/core')) {
return $this->getRemoteModule('core');
} else if (!str_ends_with($this->apiClientConfig['repo_url'], '/' . $this->apiClientConfig['org_user'])) {
$repoUrlArr = explode('/', $this->apiClientConfig['repo_url']);

try {
$modulesArr = $this->apiClient->useMethod($collection, $method, $args)->getResponse(true);
} catch (\throwable | ClientException $e) {
$this->addResponse($e->getMessage(), 1);
$remoteModule = $this->getRemoteModule($this->helper->last($repoUrlArr));

if ($e->getCode() === 401) {
$this->addResponse('API Authentication failed.', 1);
} else if (str_contains($e->getMessage(), 'Connection timed out')) {
$this->addResponse('Error connecting to the repository.', 1);
if (!$remoteModule) {
return false;
}

return false;
$modulesArr = [$remoteModule];
} else {
//Process all repositories in the org as repourl and org name is the same
if (strtolower($this->apiClientConfig['provider']) === 'gitea') {
$collection = 'OrganizationApi';
$method = 'orgListRepos';
$args = [$this->apiClientConfig['org_user']];
} else if (strtolower($this->apiClientConfig['provider']) === 'github') {
$collection = 'ReposApi';
$method = 'reposListForOrg';
$args = [$this->apiClientConfig['org_user']];
}

try {
$modulesArr = $this->apiClient->useMethod($collection, $method, $args)->getResponse(true);
} catch (\throwable | ClientException $e) {
$this->addResponse($e->getMessage(), 1);

if ($e->getCode() === 401) {
$this->addResponse('API Authentication failed.', 1);
} else if (str_contains($e->getMessage(), 'Connection timed out')) {
$this->addResponse('Error connecting to the repository.', 1);
}

return false;
}
}

if ($modulesArr) {
Expand All @@ -516,13 +533,6 @@ protected function getRemoteModules()

$names = explode('-', $module['name']);

if ($names && count($names) === 1 && $names[0] === 'core') {
$this->remoteModules['packages'] = [];
$this->remoteModules['packages'] = [$module];

return true;
}

if (strtolower($this->apiClientConfig['provider']) === 'github') {//Github does not have release_counter set
$collection = 'ReposApi';
$method = 'reposListReleases';
Expand Down Expand Up @@ -573,6 +583,57 @@ protected function getRemoteModules()
return false;
}

protected function getRemoteModule($moduleName)
{
if (strtolower($this->apiClientConfig['provider']) === 'gitea') {
$collection = 'RepositoryApi';
$method = 'repoGet';
$args = [$this->apiClientConfig['org_user']];
} else if (strtolower($this->apiClientConfig['provider']) === 'github') {
$collection = 'ReposApi';
$method = 'reposGet';
$args = [$this->apiClientConfig['org_user'], $moduleName];
}

try {
$module = $this->apiClient->useMethod($collection, $method, $args)->getResponse(true);
} catch (\throwable | ClientException $e) {
$this->addResponse($e->getMessage(), 1);

if ($e->getCode() === 401) {
$this->addResponse('API Authentication failed.', 1);
} else if (str_contains($e->getMessage(), 'Connection timed out')) {
$this->addResponse('Error connecting to the repository.', 1);
}

return false;
}

if ($moduleName !== 'core') {
return $module;
}

if ($this->apiClientConfig['sync'] &&
isset($this->apiClientConfig['sync']['modules']['last_sync']) &&
$this->apiClientConfig['sync']['modules']['last_sync'] !== ''
) {
$lastSync = \Carbon\Carbon::parse($this->apiClientConfig['sync']['modules']['last_sync']);
}

if (isset($lastSync) && isset($module['updated_at']) && $module['updated_at'] !== '') {
$updatedAt = \Carbon\Carbon::parse($module['updated_at']);

if ($lastSync->greaterThan($updatedAt)) {//Only process if its updated.
return true;
}
}

$this->remoteModules['packages'] = [];
$this->remoteModules['packages'] = [$module];

return true;
}

protected function getRemoteModuleJson($moduleType, $module, $onlyJson = false)
{
if ($moduleType === 'apptypes') {
Expand Down Expand Up @@ -633,7 +694,7 @@ protected function getRemoteModuleJson($moduleType, $module, $onlyJson = false)
$e->getMessage()
);

//We dont so anything here with respect to return. If the json file is not there, we consider the module to be unavailable
//We dont do anything here with respect to return. If the json file is not there, we consider the module to be unavailable
}

return true;
Expand Down

0 comments on commit 1267fe6

Please sign in to comment.