Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
renovate
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
GitHub Mirror
Renovate Bot
renovate
Commits
ba7f6b6e
Commit
ba7f6b6e
authored
7 years ago
by
Troy Coutu
Committed by
Rhys Arkins
7 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Add support for v4 of Gitlab API (#221)
Closes #220 * add gitlab api v4 support * switch to projects/owned route
parent
d5a15d63
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lib/api/gitlab.js
+63
-15
63 additions, 15 deletions
lib/api/gitlab.js
with
63 additions
and
15 deletions
lib/api/gitlab.js
+
63
−
15
View file @
ba7f6b6e
...
...
@@ -64,6 +64,15 @@ async function initRepo(repoName, token, endpoint) {
if
(
endpoint
)
{
process
.
env
.
GITLAB_ENDPOINT
=
endpoint
;
}
try
{
logger
.
debug
(
`Determining Gitlab API version`
);
// projects/owned route deprecated in v4
await
glGot
(
`projects/owned`
);
config
.
apiVersion
=
'
v3
'
;
}
catch
(
err
)
{
config
.
apiVersion
=
'
v4
'
;
}
logger
.
debug
(
`Detected Gitlab API
${
config
.
apiVersion
}
`
);
config
.
repoName
=
repoName
.
replace
(
'
/
'
,
'
%2F
'
);
try
{
const
res
=
await
glGot
(
`projects/
${
config
.
repoName
}
`
);
...
...
@@ -275,9 +284,14 @@ async function mergePr(pr) {
// Generic File operations
async
function
getFile
(
filePath
,
branchName
=
config
.
defaultBranch
)
{
const
res
=
await
glGot
(
`projects/
${
config
.
repoName
}
/repository/files?file_path=
${
filePath
}
&ref=
${
branchName
}
`
);
// Gitlab API v3 support
let
url
;
if
(
config
.
apiVersion
===
'
v3
'
)
{
url
=
`projects/
${
config
.
repoName
}
/repository/files?file_path=
${
filePath
}
&ref=
${
branchName
}
`
;
}
else
{
url
=
`projects/
${
config
.
repoName
}
/repository/files/
${
filePath
}
?ref=
${
branchName
}
`
;
}
const
res
=
await
glGot
(
url
);
return
res
.
body
.
content
;
}
...
...
@@ -310,27 +324,53 @@ async function getFileJson(filePath, branchName) {
}
async
function
createFile
(
branchName
,
filePath
,
fileContents
,
message
)
{
await
glGot
.
post
(
`projects/
${
config
.
repoName
}
/repository/files`
,
{
body
:
{
// Gitlab API v3 support
let
url
;
const
opts
=
{};
if
(
config
.
apiVersion
===
'
v3
'
)
{
url
=
`projects/
${
config
.
repoName
}
/repository/files`
;
opts
.
body
=
{
file_path
:
filePath
,
branch_name
:
branchName
,
commit_message
:
message
,
encoding
:
'
base64
'
,
content
:
new
Buffer
(
fileContents
).
toString
(
'
base64
'
),
},
});
};
}
else
{
url
=
`projects/
${
config
.
repoName
}
/repository/files/
${
filePath
}
`
;
opts
.
body
=
{
branch
:
branchName
,
commit_message
:
message
,
encoding
:
'
base64
'
,
content
:
new
Buffer
(
fileContents
).
toString
(
'
base64
'
),
};
}
await
glGot
.
post
(
url
,
opts
);
}
async
function
updateFile
(
branchName
,
filePath
,
fileContents
,
message
)
{
await
glGot
.
put
(
`projects/
${
config
.
repoName
}
/repository/files`
,
{
body
:
{
// Gitlab API v3 support
let
url
;
const
opts
=
{};
if
(
config
.
apiVersion
===
'
v3
'
)
{
url
=
`projects/
${
config
.
repoName
}
/repository/files`
;
opts
.
body
=
{
file_path
:
filePath
,
branch_name
:
branchName
,
commit_message
:
message
,
encoding
:
'
base64
'
,
content
:
new
Buffer
(
fileContents
).
toString
(
'
base64
'
),
},
});
};
}
else
{
url
=
`projects/
${
config
.
repoName
}
/repository/files/
${
filePath
}
`
;
opts
.
body
=
{
branch
:
branchName
,
commit_message
:
message
,
encoding
:
'
base64
'
,
content
:
new
Buffer
(
fileContents
).
toString
(
'
base64
'
),
};
}
await
glGot
.
put
(
url
,
opts
);
}
// Add a new commit, create branch if not existing
...
...
@@ -368,10 +408,18 @@ async function commitFilesToBranch(
// Creates a new branch with provided commit
async
function
createBranch
(
branchName
,
ref
=
config
.
defaultBranch
)
{
await
glGot
.
post
(
`projects/
${
config
.
repoName
}
/repository/branches`
,
{
body
:
{
// Gitlab API v3 support
const
opts
=
{};
if
(
config
.
apiVersion
===
'
v3
'
)
{
opts
.
body
=
{
branch_name
:
branchName
,
ref
,
},
});
};
}
else
{
opts
.
body
=
{
branch
:
branchName
,
ref
,
};
}
await
glGot
.
post
(
`projects/
${
config
.
repoName
}
/repository/branches`
,
opts
);
}
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