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
4e2e5390
Commit
4e2e5390
authored
Jan 7, 2017
by
Rhys Arkins
Browse files
Options
Downloads
Patches
Plain Diff
Refactor PR update
parent
052b8aa1
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/github.js
+6
-9
6 additions, 9 deletions
src/github.js
src/index.js
+31
-32
31 additions, 32 deletions
src/index.js
with
37 additions
and
41 deletions
src/github.js
+
6
−
9
View file @
4e2e5390
...
...
@@ -67,17 +67,14 @@ module.exports = {
return
JSON
.
parse
(
new
Buffer
(
res
.
body
.
content
,
'
base64
'
).
toString
());
});
},
getPr
No
:
function
(
branchName
,
state
=
'
open
'
)
{
return
ghGot
(
`repos/
${
config
.
repoName
}
/pulls?state=
${
state
}
&base=
${
config
.
baseBranch
}
&head=
${
config
.
userName
}
:
${
branchName
}
`
,
{
getPr
:
function
(
branchName
)
{
return
ghGot
(
`repos/
${
config
.
repoName
}
/pulls?state=
open
&base=
${
config
.
baseBranch
}
&head=
${
config
.
userName
}
:
${
branchName
}
`
,
{
token
:
config
.
token
,
}).
then
(
res
=>
{
let
prNo
=
0
;
res
.
body
.
forEach
(
function
(
result
)
{
if
(
result
.
state
===
'
open
'
&&
result
.
head
.
label
===
`
${
config
.
userName
}
:
${
branchName
}
`
)
{
prNo
=
result
.
number
;
if
(
res
.
body
.
length
)
{
return
res
.
body
[
0
];
}
});
return
prNo
;
return
null
;
});
},
writeFile
:
function
(
branchName
,
oldFileSHA
,
filePath
,
fileContents
,
message
)
{
...
...
This diff is collapsed.
Click to expand it.
src/index.js
+
31
−
32
View file @
4e2e5390
...
...
@@ -59,41 +59,45 @@ function updateDependency(depType, depName, currentVersion, nextVersion) {
}
else
{
prTitle
=
config
.
templates
.
prTitleMinor
({
depType
,
depName
,
currentVersion
,
nextVersion
,
nextVersionMajor
});
}
const
prBody
=
config
.
templates
.
prBody
({
depName
,
currentVersion
,
nextVersion
});
const
commitMessage
=
config
.
templates
.
commitMessage
({
depName
,
currentVersion
,
nextVersion
});
// Check if same PR already existed and skip if so
return
github
.
checkForClosedPr
(
branchName
,
prTitle
).
then
((
prExisted
)
=>
{
if
(
prExisted
)
{
console
.
log
(
`
${
depName
}
: Skipping due to existing PR found.`
);
return
;
}
const
prBody
=
config
.
templates
.
prBody
({
depName
,
currentVersion
,
nextVersion
});
return
github
.
createBranch
(
branchName
).
catch
(
error
=>
{
// Save an API call by attempting to create branch without checking for existence first
return
github
.
createBranch
(
branchName
)
.
catch
(
error
=>
{
// Check in case it's because the branch already existed
if
(
error
.
response
.
body
.
message
!==
'
Reference already exists
'
)
{
// In this case it means we really do have a problem and can't continue
console
.
log
(
'
Error creating branch:
'
+
branchName
);
console
.
log
(
error
.
response
.
body
);
}
}).
then
(
res
=>
{
if
(
config
.
verbose
)
{
console
.
log
(
`Branch exists (
${
branchName
}
), now writing file`
);
console
.
log
(
'
Response body:
'
+
error
.
response
.
body
);
throw
error
;
}
}).
then
(()
=>
{
// Retrieve the package.json from this renovate branch
return
github
.
getFile
(
packageFile
,
branchName
).
then
(
res
=>
{
const
oldFile
SHA
=
res
.
body
.
sha
;
const
current
SHA
=
res
.
body
.
sha
;
let
currentFileContent
=
JSON
.
parse
(
new
Buffer
(
res
.
body
.
content
,
'
base64
'
).
toString
());
if
(
currentFileContent
[
depType
][
depName
]
!==
nextVersion
)
{
// Branch is new, or needs version updated
if
(
config
.
verbose
)
{
console
.
log
(
`Updating
${
depName
}
to
${
nextVersion
}
in branch
${
branchName
}
`
);
}
currentFileContent
[
depType
][
depName
]
=
nextVersion
;
const
newPackageString
=
JSON
.
stringify
(
currentFileContent
,
null
,
2
)
+
'
\n
'
;
var
commitMessage
=
config
.
templates
.
commitMessage
({
depName
,
currentVersion
,
nextVersion
});
return
github
.
writeFile
(
branchName
,
oldFileSHA
,
packageFile
,
newPackageString
,
commitMessage
)
const
newPackageContents
=
JSON
.
stringify
(
currentFileContent
,
null
,
2
)
+
'
\n
'
;
return
github
.
writeFile
(
branchName
,
currentSHA
,
packageFile
,
newPackageContents
,
commitMessage
)
.
then
(()
=>
{
return
createOrUpdatePullRequest
(
branchName
,
prTitle
,
prBody
);
})
.
catch
(
err
=>
{
console
.
error
(
'
Error writing new package file for
'
+
depName
);
console
.
log
(
err
);
});
}
else
{
if
(
config
.
verbose
)
{
console
.
log
(
`
${
depName
}
was already up-to-date in branch
${
branchName
}
`
);
}
// File was up to date. Ensure PR
return
createOrUpdatePullRequest
(
branchName
,
prTitle
,
prBody
);
}
...
...
@@ -106,21 +110,16 @@ function updateDependency(depType, depName, currentVersion, nextVersion) {
}
function
createOrUpdatePullRequest
(
branchName
,
prTitle
,
prBody
)
{
return
github
.
getPrNo
(
branchName
).
then
(
prNo
=>
{
if
(
prNo
)
{
// PR already exists - update it
// Note: PR might be unchanged, so no log message
return
github
.
updatePr
(
prNo
,
prTitle
,
prBody
)
.
catch
(
err
=>
{
console
.
error
(
'
Error: Failed to update Pull Request:
'
+
prTitle
);
console
.
log
(
err
);
});
return
github
.
getPr
(
branchName
).
then
(
pr
=>
{
if
(
pr
)
{
if
(
pr
.
title
===
prTitle
&&
pr
.
body
===
prBody
)
{
if
(
config
.
verbose
)
{
console
.
log
(
'
PR already up-to-date
'
);
}
}
else
{
console
.
log
(
`Updating PR #
${
pr
.
number
}
`
);
return
github
.
updatePr
(
pr
.
number
,
prTitle
,
prBody
);
}
}
return
github
.
createPr
(
branchName
,
prTitle
,
prBody
).
then
(
res
=>
{
console
.
log
(
'
Created Pull Request:
'
+
prTitle
);
}).
catch
(
err
=>
{
console
.
error
(
'
Error: Failed to create Pull Request:
'
+
prTitle
);
console
.
log
(
err
);
});
});
}
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
register
or
sign in
to comment