Skip to content

Commit 87bcd3b

Browse files
Router API changes to support releases attachments in apis
1 parent 041b64e commit 87bcd3b

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

routers/api/v1/api.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,16 @@ func RegisterRoutes(m *macaron.Macaron) {
461461
m.Combo("/:id").Get(repo.GetRelease).
462462
Patch(reqToken(), reqRepoWriter(), bind(api.EditReleaseOption{}), repo.EditRelease).
463463
Delete(reqToken(), reqRepoWriter(), repo.DeleteRelease)
464+
m.Combo("/latest").Get(repo.GetLatestRelease)
465+
m.Group("/:id", func() {
466+
m.Combo("").Get(repo.GetRelease).
467+
Patch(bind(api.EditReleaseOption{}), repo.EditRelease).
468+
Delete(repo.DeleteRelease)
469+
m.Group("/assets", func() {
470+
m.Combo("").Get(repo.ListReleaseAttachments)
471+
m.Combo("/:assetId").Get(repo.GetReleaseAttachment)
472+
})
473+
})
464474
})
465475
m.Post("/mirror-sync", reqToken(), reqRepoWriter(), repo.MirrorSync)
466476
m.Get("/editorconfig/:filename", context.RepoRef(), repo.GetEditorconfig)

routers/api/v1/repo/release.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,56 @@ func GetRelease(ctx *context.APIContext) {
5454
ctx.JSON(200, release.APIFormat())
5555
}
5656

57+
// ListReleaseAttachments get all the attachments of a release
58+
func ListReleaseAttachments(ctx *context.APIContext) {
59+
id := ctx.ParamsInt64(":id")
60+
release, err := models.GetReleaseByID(id)
61+
if err != nil {
62+
ctx.Error(500, "GetReleaseByID", err)
63+
return
64+
}
65+
if release.RepoID != ctx.Repo.Repository.ID {
66+
ctx.Status(404)
67+
return
68+
}
69+
// load the attachments of this release
70+
attachments, err := models.GetAttachmentsByReleaseID(id)
71+
if err != nil {
72+
ctx.Error(500, "GetAttachmentsByReleaseID", err)
73+
return
74+
}
75+
// build the attachment list
76+
apiAttachments := make([]*api.Attachment, len(attachments))
77+
for i := range attachments {
78+
apiAttachments[i] = attachments[i].APIFormat()
79+
}
80+
ctx.JSON(200, apiAttachments)
81+
}
82+
83+
// GetReleaseAttachment get a single attachment of a release
84+
func GetReleaseAttachment(ctx *context.APIContext) {
85+
id := ctx.ParamsInt64(":id")
86+
attachmentID := ctx.ParamsInt64(":assetId")
87+
release, err := models.GetReleaseByID(id)
88+
if err != nil {
89+
ctx.Error(500, "GetReleaseByID", err)
90+
return
91+
}
92+
if release.RepoID != ctx.Repo.Repository.ID {
93+
ctx.Status(404)
94+
return
95+
}
96+
// load the attachments of this release
97+
attachment, err := models.GetAttachmentByID(attachmentID)
98+
// if the attachment was not found, or it was found but is not associated with this release, then throw 404
99+
if err != nil || id != attachment.ReleaseID {
100+
ctx.Status(404)
101+
return
102+
}
103+
104+
ctx.JSON(200, attachment.APIFormat())
105+
}
106+
57107
// ListReleases list a repository's releases
58108
func ListReleases(ctx *context.APIContext) {
59109
// swagger:operation GET /repos/{owner}/{repo}/releases repository repoListReleases
@@ -94,6 +144,29 @@ func ListReleases(ctx *context.APIContext) {
94144
ctx.JSON(200, rels)
95145
}
96146

147+
// GetLatestRelease Gets the latest release in a repository. Draft releases and prereleases are not returned
148+
func GetLatestRelease(ctx *context.APIContext) {
149+
// we set the pageSize to 1 to get back only one release
150+
releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, models.FindReleasesOptions{
151+
IncludeDrafts: false,
152+
ExcludePrereleases: true,
153+
}, 1, 1)
154+
if err != nil {
155+
ctx.Error(500, "GetReleasesByRepoID", err)
156+
return
157+
}
158+
if len(releases) <= 0 {
159+
// no releases found, just return 404
160+
ctx.Status(404)
161+
return
162+
}
163+
if err := releases[0].LoadAttributes(); err != nil {
164+
ctx.Error(500, "LoadAttributes", err)
165+
return
166+
}
167+
ctx.JSON(200, releases[0].APIFormat())
168+
}
169+
97170
// CreateRelease create a release
98171
func CreateRelease(ctx *context.APIContext, form api.CreateReleaseOption) {
99172
// swagger:operation GET /repos/{owner}/{repo}/releases repository repoCreateRelease

0 commit comments

Comments
 (0)