Skip to content

Commit

Permalink
feat: add a new /auth/artifact/:sha/circleci/:jobname/:filename gener…
Browse files Browse the repository at this point in the history
…ic endpoint
  • Loading branch information
moul committed Jul 25, 2019
1 parent 6c5e515 commit 4b652b2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
66 changes: 66 additions & 0 deletions server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math"
"math/rand"
"net/http"
"path"
"regexp"
"sort"
"strconv"
Expand Down Expand Up @@ -45,6 +46,71 @@ var (
slackFloodMutex = sync.Mutex{}
)

func (s *Server) downloadSpecificArtifact(c echo.Context) error {
if provider := c.Param("provider"); provider != "circleci" {
return fmt.Errorf("unsupported provider %q", provider)
}
jobName := c.Param("jobname")
filename := c.Param("filename")
sha := c.Param("sha")

shaFound := false
jobFound := false
availableFilenames := []string{}
for buildID, build := range s.cache.builds {
if build.VcsRevision != sha {
continue
}
shaFound = true
if build.BuildParameters["CIRCLE_JOB"] != jobName {
continue
}
jobFound = true
artifacts, err := s.circleClient.GetArtifacts(fmt.Sprintf("%d", buildID), true)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
for _, artifact := range artifacts {
base := path.Base(artifact.Path)
availableFilenames = append(availableFilenames, base)
if artifact.Path == filename || base == filename {
rc, err := s.circleClient.GetArtifact(artifact)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
contentType := "application/octet-stream"
switch path.Ext(base) {
case ".dmg":
contentType = "application/x-apple-diskimage"
case ".ipa":
contentType = "application/octet-stream"
case ".apk":
contentType = "application/vnd.android.package-archive"
case ".jar":
contentType = "application/java-archive"
case ".txt":
contentType = "text/plain"
case ".json":
contentType = "application/json"
case ".zip":
contentType = "application/zip"
}
// proxy
c.Response().Header().Set("Content-Disposition", fmt.Sprintf("inline; filename=%s", base))
// FIXME: set Content-Length
return c.Stream(http.StatusOK, contentType, rc)
}
}
}
if !shaFound {
return echo.NewHTTPError(http.StatusNotFound, "no such sha")
}
if !jobFound {
return echo.NewHTTPError(http.StatusNotFound, "no such job")
}
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("no such artifact filename (available options: %s)", strings.Join(availableFilenames, ", ")))
}

func (s *Server) Build(c echo.Context) error {
id := c.Param("build_id")
ret, err := s.circleClient.Build(id)
Expand Down
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ func NewServer(cfg *ServerConfig) (*Server, error) {
tokenPaths := regexp.MustCompile("^/auth/ipa/build/.+$|^/auth/dmg/build/.+$|^/auth/apk/build/.+$|^/auth/itms/release/.+$")
auth.Use(s.tokenMiddleware(tokenPaths))

auth.GET("/artifact/:sha/:provider/:jobname/:filename", s.downloadSpecificArtifact)
auth.GET("/build/:build_id", s.Build)
auth.GET("/builds/*", s.Builds)
auth.GET("/artifacts/:build_id", s.Artifacts)
Expand Down

0 comments on commit 4b652b2

Please sign in to comment.