This repository has been archived by the owner before Nov 9, 2022. It is now read-only.
Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
drone-jsonnet-config/plugin/importer.go /
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
92 lines (75 sloc)
2.44 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Copyright 2018 Drone.IO Inc. All rights reserved. | |
| // Use of this source code is governed by a BSD-style | |
| // license that can be found in the LICENSE file. | |
| package plugin | |
| import ( | |
| "context" | |
| "errors" | |
| "fmt" | |
| "path" | |
| "strings" | |
| "github.com/drone/drone-go/drone" | |
| "github.com/google/go-github/github" | |
| "github.com/google/go-jsonnet" | |
| ) | |
| type importer struct { | |
| client *github.Client | |
| repo drone.Repo | |
| build drone.Build | |
| // jsonnet does not cache file imports and may request | |
| // the same file multiple times. We cache the files to | |
| // duplicate API calls. | |
| cache map[string]string | |
| // limit the number of outbound requests. github limits | |
| // the number of api requests per hour, so we should | |
| // make sure that a single build does not abuse the api | |
| // by imporing dozens of files. | |
| limit int | |
| // counts the number of outbound requests. if the count | |
| // exceeds the limit, the importer will return errors. | |
| count int | |
| } | |
| func (i *importer) Import(importedFrom, importedPath string) (contents jsonnet.Contents, foundAt string, err error) { | |
| if i.cache == nil { | |
| i.cache = map[string]string{} | |
| } | |
| // the import is relative to the imported from path. the | |
| // imported path must resolve to a filepath relative to | |
| // the root of the repository. | |
| importedPath = path.Join( | |
| path.Dir(importedFrom), | |
| importedPath, | |
| ) | |
| if strings.HasPrefix(importedFrom, "../") { | |
| err = fmt.Errorf("jsonnet: cannot resolve import: %s", importedPath) | |
| return contents, foundAt, err | |
| } | |
| // if the contents exist in the cache, return the | |
| // cached item. | |
| if contents, ok := i.cache[importedPath]; ok { | |
| return jsonnet.MakeContents(contents), importedPath, nil | |
| } | |
| defer func() { | |
| i.count++ | |
| }() | |
| // if the import limit is exceeded log an error message. | |
| if i.limit > 0 && i.count > i.limit { | |
| return contents, foundAt, errors.New("jsonnet: import limit exceeded") | |
| } | |
| // get the configuration file from the github repository | |
| // for the build ref. | |
| opts := &github.RepositoryContentGetOptions{Ref: i.build.After} | |
| data, _, _, err := i.client.Repositories.GetContents(context.Background(), i.repo.Namespace, i.repo.Name, importedPath, opts) | |
| if err != nil { | |
| return contents, foundAt, err | |
| } | |
| // extracts and decodes the base64-encoded file contents | |
| // from the response body. | |
| content, err := data.GetContent() | |
| if err != nil { | |
| return contents, foundAt, err | |
| } | |
| // caches the contents for future API calls. | |
| i.cache[importedPath] = content | |
| return jsonnet.MakeContents(content), importedPath, err | |
| } |