Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
佳 邓
Gitlab4j Api
Commits
775e147a
Commit
775e147a
authored
Oct 09, 2019
by
Greg Messner
Browse files
Completed implementation of the Badges API for projects (#447).
parent
a9bcd7fe
Changes
3
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/gitlab4j/api/ProjectApi.java
View file @
775e147a
...
@@ -2947,42 +2947,122 @@ public class ProjectApi extends AbstractApi implements Constants {
...
@@ -2947,42 +2947,122 @@ public class ProjectApi extends AbstractApi implements Constants {
post
(
Response
.
Status
.
OK
,
(
Form
)
null
,
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"housekeeping"
);
post
(
Response
.
Status
.
OK
,
(
Form
)
null
,
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"housekeeping"
);
}
}
/**
/**
* Add a badge to a project.
* Gets a list of a project’s badges and its group badges.
*
*
* <pre>
* <pre><code>GitLab Endpoint: GET /projects/:id/badges</code></pre>
* <code>GitLab Endpoint: POST /projects/:id/badges</code>
*
* </pre>
* @param projectIdOrPath the project in the form of an Integer(ID),
*
* String(path), or Project instance
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @throws GitLabApiException if any exception occurs
* @param badge the badge to add
*/
* @throws GitLabApiException if any exception occurs
public
List
<
Badge
>
getBadges
(
Object
projectIdOrPath
)
throws
GitLabApiException
{
*/
Response
response
=
get
(
Response
.
Status
.
OK
,
null
,
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"badges"
);
public
void
addBadge
(
Object
projectIdOrPath
,
Badge
badge
)
throws
GitLabApiException
{
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Badge
>>()
{}));
Form
formData
=
new
Form
();
}
formData
.
param
(
"id"
,
String
.
valueOf
(
getProjectIdOrPath
(
projectIdOrPath
)));
formData
.
param
(
"link_url"
,
badge
.
getLinkUrl
());
formData
.
param
(
"image_url"
,
badge
.
getImageUrl
());
post
(
Response
.
Status
.
OK
,
formData
,
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"badges"
);
}
/**
/**
* List all badges of a project <br>
* Gets a badge of a project.
* Gets a list of a project’s badges and its group badges..
*
*
* <pre><code>GitLab Endpoint: GET /projects/:id/badges/:badge_id</code></pre>
* <pre>
*
* <code>GitLab Endpoint: GET /projects/:id/badges</code>
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* </pre>
* @param badgeId the ID of the badge to get
*
* @return a Badge instance for the specified project/badge ID pair
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @throws GitLabApiException if any exception occurs
* @throws GitLabApiException if any exception occurs
*/
*/
public
Badge
getBadge
(
Object
projectIdOrPath
,
Integer
badgeId
)
throws
GitLabApiException
{
public
List
<
Badge
>
getAllBadges
(
Object
projectIdOrPath
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
null
,
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"badges"
,
badgeId
);
try
(
Response
response
=
get
(
Response
.
Status
.
OK
,
null
,
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"badges"
))
{
return
(
response
.
readEntity
(
Badge
.
class
));
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Badge
>>()
{
}));
}
}
/**
* Get an Optional instance with the value for the specified badge.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/badges/:badge_id</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param badgeId the ID of the badge to get
* @return an Optional instance with the specified badge as the value
* @throws GitLabApiException if any exception occurs
*/
public
Optional
<
Badge
>
getOptionalBadge
(
Object
projectIdOrPath
,
Integer
badgeId
)
{
try
{
return
(
Optional
.
ofNullable
(
getBadge
(
projectIdOrPath
,
badgeId
)));
}
catch
(
GitLabApiException
glae
)
{
return
(
GitLabApi
.
createOptionalFromException
(
glae
));
}
}
}
/**
* Add a badge to a project.
*
* <pre><code>GitLab Endpoint: POST /projects/:id/badges</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param linkUrl the URL of the badge link
* @param imageUrl the URL of the image link
* @return a Badge instance for the added badge
* @throws GitLabApiException if any exception occurs
*/
public
Badge
addBadge
(
Object
projectIdOrPath
,
String
linkUrl
,
String
imageUrl
)
throws
GitLabApiException
{
GitLabApiForm
formData
=
new
GitLabApiForm
()
.
withParam
(
"link_url"
,
linkUrl
,
true
)
.
withParam
(
"image_url"
,
imageUrl
,
true
);
Response
response
=
post
(
Response
.
Status
.
OK
,
formData
,
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"badges"
);
return
(
response
.
readEntity
(
Badge
.
class
));
}
/**
* Edit a badge of a project.
*
* <pre><code>GitLab Endpoint: PUT /projects/:id/badges</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param badgeId the ID of the badge to get
* @param linkUrl the URL of the badge link
* @param imageUrl the URL of the image link
* @return a Badge instance for the editted badge
* @throws GitLabApiException if any exception occurs
*/
public
Badge
editBadge
(
Object
projectIdOrPath
,
Integer
badgeId
,
String
linkUrl
,
String
imageUrl
)
throws
GitLabApiException
{
GitLabApiForm
formData
=
new
GitLabApiForm
()
.
withParam
(
"link_url"
,
linkUrl
,
false
)
.
withParam
(
"image_url"
,
imageUrl
,
false
);
Response
response
=
putWithFormData
(
Response
.
Status
.
OK
,
formData
,
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"badges"
,
badgeId
);
return
(
response
.
readEntity
(
Badge
.
class
));
}
/**
* Remove a badge from a project.
*
* <pre><code>GitLab Endpoint: DELETE /projects/:id/badges/:badge_id</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param badgeId the ID of the badge to remove
* @return a Badge instance for the specified project/badge ID pair
* @throws GitLabApiException if any exception occurs
*/
public
void
removeBadge
(
Object
projectIdOrPath
,
Integer
badgeId
)
throws
GitLabApiException
{
delete
(
Response
.
Status
.
NO_CONTENT
,
null
,
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"badges"
,
badgeId
);
}
/**
* Returns how the link_url and image_url final URLs would be after resolving the placeholder interpolation.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/badges/render</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param linkUrl the URL of the badge link
* @param imageUrl the URL of the image link
* @return a Badge instance for the rendered badge
* @throws GitLabApiException if any exception occurs
*/
public
Badge
previewBadge
(
Object
projectIdOrPath
,
String
linkUrl
,
String
imageUrl
)
throws
GitLabApiException
{
GitLabApiForm
formData
=
new
GitLabApiForm
()
.
withParam
(
"link_url"
,
linkUrl
,
true
)
.
withParam
(
"image_url"
,
imageUrl
,
true
);
Response
response
=
get
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"badges"
,
"render"
);
return
(
response
.
readEntity
(
Badge
.
class
));
}
}
}
src/main/java/org/gitlab4j/api/models/Badge.java
View file @
775e147a
package
org.gitlab4j.api.models
;
package
org.gitlab4j.api.models
;
import
org.gitlab4j.api.utils.JacksonJson
;
import
org.gitlab4j.api.utils.JacksonJson
;
import
org.gitlab4j.api.utils.JacksonJsonEnumHelper
;
import
com.fasterxml.jackson.annotation.JsonCreator
;
import
com.fasterxml.jackson.annotation.JsonValue
;
public
class
Badge
{
public
class
Badge
{
public
enum
BadgeKind
{
PROJECT
,
GROUP
;
private
static
JacksonJsonEnumHelper
<
BadgeKind
>
enumHelper
=
new
JacksonJsonEnumHelper
<>(
BadgeKind
.
class
);
@JsonCreator
public
static
BadgeKind
forValue
(
String
value
)
{
return
enumHelper
.
forValue
(
value
);
}
@JsonValue
public
String
toValue
()
{
return
(
enumHelper
.
toString
(
this
));
}
@Override
public
String
toString
()
{
return
(
enumHelper
.
toString
(
this
));
}
}
private
Integer
id
;
private
Integer
id
;
private
String
linkUrl
;
private
String
linkUrl
;
private
String
imageUrl
;
private
String
imageUrl
;
private
String
renderedLinkUrl
;
private
String
renderedLinkUrl
;
private
String
renderedImageUrl
;
private
String
renderedImageUrl
;
private
String
kind
;
private
BadgeKind
kind
;
/**
* @return The ID or URL-encoded path of the project owned by the authenticated user
*/
public
Integer
getId
()
{
public
Integer
getId
()
{
return
id
;
return
id
;
}
}
/**
* @param id The ID or URL-encoded path of the project owned by the authenticated user
*/
public
void
setId
(
Integer
id
)
{
public
void
setId
(
Integer
id
)
{
this
.
id
=
id
;
this
.
id
=
id
;
}
}
/**
* @return URL of the badge link
*/
public
String
getLinkUrl
()
{
public
String
getLinkUrl
()
{
return
linkUrl
;
return
linkUrl
;
}
}
/**
* @param linkUrl URL of the badge link
*/
public
void
setLinkUrl
(
String
linkUrl
)
{
public
void
setLinkUrl
(
String
linkUrl
)
{
this
.
linkUrl
=
linkUrl
;
this
.
linkUrl
=
linkUrl
;
}
}
/**
* @return URL of the badge image
*/
public
String
getImageUrl
()
{
public
String
getImageUrl
()
{
return
imageUrl
;
return
imageUrl
;
}
}
/**
* @param imageUrl URL of the badge image
*/
public
void
setImageUrl
(
String
imageUrl
)
{
public
void
setImageUrl
(
String
imageUrl
)
{
this
.
imageUrl
=
imageUrl
;
this
.
imageUrl
=
imageUrl
;
}
}
public
String
getRenderedImageUrl
()
{
public
String
getRenderedImageUrl
()
{
return
renderedImageUrl
;
return
renderedImageUrl
;
}
}
public
void
setRenderedImageUrl
(
String
renderedImageUrl
)
{
public
void
setRenderedImageUrl
(
String
renderedImageUrl
)
{
this
.
renderedImageUrl
=
renderedImageUrl
;
this
.
renderedImageUrl
=
renderedImageUrl
;
}
}
public
String
getRenderedLinkUrl
()
{
public
String
getRenderedLinkUrl
()
{
return
renderedLinkUrl
;
return
renderedLinkUrl
;
}
}
public
void
setRenderedLinkUrl
(
String
renderedLinkUrl
)
{
public
void
setRenderedLinkUrl
(
String
renderedLinkUrl
)
{
this
.
renderedLinkUrl
=
renderedLinkUrl
;
this
.
renderedLinkUrl
=
renderedLinkUrl
;
}
}
public
Str
in
g
getKind
()
{
public
BadgeK
in
d
getKind
()
{
return
kind
;
return
kind
;
}
}
public
void
setKind
(
Str
in
g
kind
)
{
public
void
setKind
(
BadgeK
in
d
kind
)
{
this
.
kind
=
kind
;
this
.
kind
=
kind
;
}
}
@Override
@Override
public
String
toString
()
{
public
String
toString
()
{
return
(
JacksonJson
.
toJsonString
(
this
));
return
(
JacksonJson
.
toJsonString
(
this
));
}
}
}
}
src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java
View file @
775e147a
...
@@ -36,6 +36,7 @@ import org.gitlab4j.api.models.AccessRequest;
...
@@ -36,6 +36,7 @@ import org.gitlab4j.api.models.AccessRequest;
import
org.gitlab4j.api.models.Application
;
import
org.gitlab4j.api.models.Application
;
import
org.gitlab4j.api.models.ArtifactsFile
;
import
org.gitlab4j.api.models.ArtifactsFile
;
import
org.gitlab4j.api.models.AwardEmoji
;
import
org.gitlab4j.api.models.AwardEmoji
;
import
org.gitlab4j.api.models.Badge
;
import
org.gitlab4j.api.models.Board
;
import
org.gitlab4j.api.models.Board
;
import
org.gitlab4j.api.models.Branch
;
import
org.gitlab4j.api.models.Branch
;
import
org.gitlab4j.api.models.Comment
;
import
org.gitlab4j.api.models.Comment
;
...
@@ -118,6 +119,12 @@ public class TestGitLabApiBeans {
...
@@ -118,6 +119,12 @@ public class TestGitLabApiBeans {
assertTrue
(
compareJson
(
awardEmoji
,
"award-emoji.json"
));
assertTrue
(
compareJson
(
awardEmoji
,
"award-emoji.json"
));
}
}
@Test
public
void
testBadges
()
throws
Exception
{
List
<
Badge
>
badges
=
unmarshalResourceList
(
Badge
.
class
,
"badges.json"
);
assertTrue
(
compareJson
(
badges
,
"badges.json"
));
}
@Test
@Test
public
void
testBoard
()
throws
Exception
{
public
void
testBoard
()
throws
Exception
{
List
<
Board
>
boards
=
unmarshalResourceList
(
Board
.
class
,
"project-board.json"
);
List
<
Board
>
boards
=
unmarshalResourceList
(
Board
.
class
,
"project-board.json"
);
...
...
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment