Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
renovate
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SmartDataLab
public
applications
renovate
Commits
ca9ed19b
Unverified
Commit
ca9ed19b
authored
Apr 26, 2021
by
Sergei Zharinov
Committed by
GitHub
Apr 26, 2021
Browse files
Options
Downloads
Patches
Plain Diff
refactor(maven): Extract util functions from datasource index (#9727)
parent
65c3992e
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
lib/datasource/maven/index.ts
+8
-107
8 additions, 107 deletions
lib/datasource/maven/index.ts
lib/datasource/maven/types.ts
+13
-0
13 additions, 0 deletions
lib/datasource/maven/types.ts
lib/datasource/maven/util.ts
+97
-0
97 additions, 0 deletions
lib/datasource/maven/util.ts
with
118 additions
and
107 deletions
lib/datasource/maven/index.ts
+
8
−
107
View file @
ca9ed19b
import
url
from
'
url
'
;
import
url
from
'
url
'
;
import
fs
from
'
fs-extra
'
;
import
pAll
from
'
p-all
'
;
import
pAll
from
'
p-all
'
;
import
{
XmlDocument
}
from
'
xmldoc
'
;
import
{
XmlDocument
}
from
'
xmldoc
'
;
import
{
logger
}
from
'
../../logger
'
;
import
{
logger
}
from
'
../../logger
'
;
...
@@ -9,7 +8,14 @@ import * as mavenVersioning from '../../versioning/maven';
...
@@ -9,7 +8,14 @@ import * as mavenVersioning from '../../versioning/maven';
import
{
compare
}
from
'
../../versioning/maven/compare
'
;
import
{
compare
}
from
'
../../versioning/maven/compare
'
;
import
type
{
GetReleasesConfig
,
Release
,
ReleaseResult
}
from
'
../types
'
;
import
type
{
GetReleasesConfig
,
Release
,
ReleaseResult
}
from
'
../types
'
;
import
{
MAVEN_REPO
}
from
'
./common
'
;
import
{
MAVEN_REPO
}
from
'
./common
'
;
import
{
downloadHttpProtocol
,
isHttpResourceExists
}
from
'
./util
'
;
import
type
{
MavenDependency
}
from
'
./types
'
;
import
{
downloadMavenXml
,
getDependencyInfo
,
getDependencyParts
,
getMavenUrl
,
isHttpResourceExists
,
}
from
'
./util
'
;
export
{
id
}
from
'
./common
'
;
export
{
id
}
from
'
./common
'
;
...
@@ -18,93 +24,6 @@ export const defaultRegistryUrls = [MAVEN_REPO];
...
@@ -18,93 +24,6 @@ export const defaultRegistryUrls = [MAVEN_REPO];
export
const
defaultVersioning
=
mavenVersioning
.
id
;
export
const
defaultVersioning
=
mavenVersioning
.
id
;
export
const
registryStrategy
=
'
merge
'
;
export
const
registryStrategy
=
'
merge
'
;
function
containsPlaceholder
(
str
:
string
):
boolean
{
return
/
\$
{.*
?
}/g
.
test
(
str
);
}
async
function
downloadFileProtocol
(
pkgUrl
:
url
.
URL
):
Promise
<
string
|
null
>
{
const
pkgPath
=
pkgUrl
.
toString
().
replace
(
'
file://
'
,
''
);
if
(
!
(
await
fs
.
exists
(
pkgPath
)))
{
return
null
;
}
return
fs
.
readFile
(
pkgPath
,
'
utf8
'
);
}
function
getMavenUrl
(
dependency
:
MavenDependency
,
repoUrl
:
string
,
path
:
string
):
url
.
URL
|
null
{
return
new
url
.
URL
(
`
${
dependency
.
dependencyUrl
}
/
${
path
}
`
,
repoUrl
);
}
interface
MavenXml
{
authorization
?:
boolean
;
xml
?:
XmlDocument
;
}
async
function
downloadMavenXml
(
pkgUrl
:
url
.
URL
|
null
):
Promise
<
MavenXml
|
null
>
{
/* istanbul ignore if */
if
(
!
pkgUrl
)
{
return
{};
}
let
rawContent
:
string
;
let
authorization
:
boolean
;
switch
(
pkgUrl
.
protocol
)
{
case
'
file:
'
:
rawContent
=
await
downloadFileProtocol
(
pkgUrl
);
break
;
case
'
http:
'
:
case
'
https:
'
:
({
authorization
,
body
:
rawContent
}
=
await
downloadHttpProtocol
(
pkgUrl
));
break
;
case
'
s3:
'
:
logger
.
debug
(
'
Skipping s3 dependency
'
);
return
{};
default
:
logger
.
debug
({
url
:
pkgUrl
.
toString
()
},
`Unsupported Maven protocol`
);
return
{};
}
if
(
!
rawContent
)
{
logger
.
debug
(
`Content is not found for Maven url:
${
pkgUrl
.
toString
()}
`
);
return
{};
}
return
{
authorization
,
xml
:
new
XmlDocument
(
rawContent
)
};
}
async
function
getDependencyInfo
(
dependency
:
MavenDependency
,
repoUrl
:
string
,
version
:
string
):
Promise
<
Partial
<
ReleaseResult
>>
{
const
result
:
Partial
<
ReleaseResult
>
=
{};
const
path
=
`
${
version
}
/
${
dependency
.
name
}
-
${
version
}
.pom`
;
const
pomUrl
=
getMavenUrl
(
dependency
,
repoUrl
,
path
);
const
{
xml
:
pomContent
}
=
await
downloadMavenXml
(
pomUrl
);
if
(
!
pomContent
)
{
return
result
;
}
const
homepage
=
pomContent
.
valueWithPath
(
'
url
'
);
if
(
homepage
&&
!
containsPlaceholder
(
homepage
))
{
result
.
homepage
=
homepage
;
}
const
sourceUrl
=
pomContent
.
valueWithPath
(
'
scm.url
'
);
if
(
sourceUrl
&&
!
containsPlaceholder
(
sourceUrl
))
{
result
.
sourceUrl
=
sourceUrl
.
replace
(
/^scm:/
,
''
);
}
return
result
;
}
function
isStableVersion
(
x
:
string
):
boolean
{
function
isStableVersion
(
x
:
string
):
boolean
{
return
mavenVersion
.
isStable
(
x
);
return
mavenVersion
.
isStable
(
x
);
}
}
...
@@ -121,24 +40,6 @@ function getLatestStableVersion(releases: Release[]): string | null {
...
@@ -121,24 +40,6 @@ function getLatestStableVersion(releases: Release[]): string | null {
return
null
;
return
null
;
}
}
interface
MavenDependency
{
display
:
string
;
group
?:
string
;
name
?:
string
;
dependencyUrl
:
string
;
}
function
getDependencyParts
(
lookupName
:
string
):
MavenDependency
{
const
[
group
,
name
]
=
lookupName
.
split
(
'
:
'
);
const
dependencyUrl
=
`
${
group
.
replace
(
/
\.
/g
,
'
/
'
)}
/
${
name
}
`
;
return
{
display
:
lookupName
,
group
,
name
,
dependencyUrl
,
};
}
function
extractVersions
(
metadata
:
XmlDocument
):
string
[]
{
function
extractVersions
(
metadata
:
XmlDocument
):
string
[]
{
const
versions
=
metadata
.
descendantWithPath
(
'
versioning.versions
'
);
const
versions
=
metadata
.
descendantWithPath
(
'
versioning.versions
'
);
const
elements
=
versions
?.
childrenNamed
(
'
version
'
);
const
elements
=
versions
?.
childrenNamed
(
'
version
'
);
...
...
This diff is collapsed.
Click to expand it.
lib/datasource/maven/types.ts
0 → 100644
+
13
−
0
View file @
ca9ed19b
import
type
{
XmlDocument
}
from
'
xmldoc
'
;
export
interface
MavenDependency
{
display
:
string
;
group
?:
string
;
name
?:
string
;
dependencyUrl
:
string
;
}
export
interface
MavenXml
{
authorization
?:
boolean
;
xml
?:
XmlDocument
;
}
This diff is collapsed.
Click to expand it.
lib/datasource/maven/util.ts
+
97
−
0
View file @
ca9ed19b
import
url
from
'
url
'
;
import
url
from
'
url
'
;
import
fs
from
'
fs-extra
'
;
import
{
XmlDocument
}
from
'
xmldoc
'
;
import
{
HOST_DISABLED
}
from
'
../../constants/error-messages
'
;
import
{
HOST_DISABLED
}
from
'
../../constants/error-messages
'
;
import
{
logger
}
from
'
../../logger
'
;
import
{
logger
}
from
'
../../logger
'
;
import
{
ExternalHostError
}
from
'
../../types/errors/external-host-error
'
;
import
{
ExternalHostError
}
from
'
../../types/errors/external-host-error
'
;
import
{
Http
,
HttpResponse
}
from
'
../../util/http
'
;
import
{
Http
,
HttpResponse
}
from
'
../../util/http
'
;
import
type
{
ReleaseResult
}
from
'
../types
'
;
import
{
MAVEN_REPO
,
id
}
from
'
./common
'
;
import
{
MAVEN_REPO
,
id
}
from
'
./common
'
;
import
type
{
MavenDependency
,
MavenXml
}
from
'
./types
'
;
const
http
:
Record
<
string
,
Http
>
=
{};
const
http
:
Record
<
string
,
Http
>
=
{};
...
@@ -96,6 +100,14 @@ export async function downloadHttpProtocol(
...
@@ -96,6 +100,14 @@ export async function downloadHttpProtocol(
}
}
}
}
async
function
downloadFileProtocol
(
pkgUrl
:
url
.
URL
):
Promise
<
string
|
null
>
{
const
pkgPath
=
pkgUrl
.
toString
().
replace
(
'
file://
'
,
''
);
if
(
!
(
await
fs
.
exists
(
pkgPath
)))
{
return
null
;
}
return
fs
.
readFile
(
pkgPath
,
'
utf8
'
);
}
export
async
function
isHttpResourceExists
(
export
async
function
isHttpResourceExists
(
pkgUrl
:
url
.
URL
|
string
,
pkgUrl
:
url
.
URL
|
string
,
hostType
=
id
hostType
=
id
...
@@ -119,3 +131,88 @@ export async function isHttpResourceExists(
...
@@ -119,3 +131,88 @@ export async function isHttpResourceExists(
return
null
;
return
null
;
}
}
}
}
function
containsPlaceholder
(
str
:
string
):
boolean
{
return
/
\$
{.*
?
}/g
.
test
(
str
);
}
export
function
getMavenUrl
(
dependency
:
MavenDependency
,
repoUrl
:
string
,
path
:
string
):
url
.
URL
|
null
{
return
new
url
.
URL
(
`
${
dependency
.
dependencyUrl
}
/
${
path
}
`
,
repoUrl
);
}
export
async
function
downloadMavenXml
(
pkgUrl
:
url
.
URL
|
null
):
Promise
<
MavenXml
|
null
>
{
/* istanbul ignore if */
if
(
!
pkgUrl
)
{
return
{};
}
let
rawContent
:
string
;
let
authorization
:
boolean
;
switch
(
pkgUrl
.
protocol
)
{
case
'
file:
'
:
rawContent
=
await
downloadFileProtocol
(
pkgUrl
);
break
;
case
'
http:
'
:
case
'
https:
'
:
({
authorization
,
body
:
rawContent
}
=
await
downloadHttpProtocol
(
pkgUrl
));
break
;
case
'
s3:
'
:
logger
.
debug
(
'
Skipping s3 dependency
'
);
return
{};
default
:
logger
.
debug
({
url
:
pkgUrl
.
toString
()
},
`Unsupported Maven protocol`
);
return
{};
}
if
(
!
rawContent
)
{
logger
.
debug
(
`Content is not found for Maven url:
${
pkgUrl
.
toString
()}
`
);
return
{};
}
return
{
authorization
,
xml
:
new
XmlDocument
(
rawContent
)
};
}
export
async
function
getDependencyInfo
(
dependency
:
MavenDependency
,
repoUrl
:
string
,
version
:
string
):
Promise
<
Partial
<
ReleaseResult
>>
{
const
result
:
Partial
<
ReleaseResult
>
=
{};
const
path
=
`
${
version
}
/
${
dependency
.
name
}
-
${
version
}
.pom`
;
const
pomUrl
=
getMavenUrl
(
dependency
,
repoUrl
,
path
);
const
{
xml
:
pomContent
}
=
await
downloadMavenXml
(
pomUrl
);
if
(
!
pomContent
)
{
return
result
;
}
const
homepage
=
pomContent
.
valueWithPath
(
'
url
'
);
if
(
homepage
&&
!
containsPlaceholder
(
homepage
))
{
result
.
homepage
=
homepage
;
}
const
sourceUrl
=
pomContent
.
valueWithPath
(
'
scm.url
'
);
if
(
sourceUrl
&&
!
containsPlaceholder
(
sourceUrl
))
{
result
.
sourceUrl
=
sourceUrl
.
replace
(
/^scm:/
,
''
);
}
return
result
;
}
export
function
getDependencyParts
(
lookupName
:
string
):
MavenDependency
{
const
[
group
,
name
]
=
lookupName
.
split
(
'
:
'
);
const
dependencyUrl
=
`
${
group
.
replace
(
/
\.
/g
,
'
/
'
)}
/
${
name
}
`
;
return
{
display
:
lookupName
,
group
,
name
,
dependencyUrl
,
};
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment