71 lines
2 KiB
Go
71 lines
2 KiB
Go
// Copyright 2019 Drone IO, Inc.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package contents
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"github.com/drone/drone/core"
|
|
"github.com/drone/go-scm/scm"
|
|
)
|
|
|
|
// New returns a new FileService.
|
|
func New(client *scm.Client, renewer core.Renewer) core.FileService {
|
|
return &service{
|
|
client: client,
|
|
renewer: renewer,
|
|
}
|
|
}
|
|
|
|
type service struct {
|
|
renewer core.Renewer
|
|
client *scm.Client
|
|
}
|
|
|
|
func (s *service) Find(ctx context.Context, user *core.User, repo, commit, ref, path string) (*core.File, error) {
|
|
// TODO(gogs) ability to fetch a yaml by pull request ref.
|
|
// it is not currently possible to fetch the yaml
|
|
// configuation file from a pull request sha. This
|
|
// workaround defaults to master.
|
|
if s.client.Driver == scm.DriverGogs &&
|
|
strings.HasPrefix(ref, "refs/pull") {
|
|
commit = "master"
|
|
}
|
|
// TODO(gogs) ability to fetch a file in tag from commit sha.
|
|
// this is a workaround for gogs which does not allow
|
|
// fetching a file by commit sha for a tag. This forces
|
|
// fetching a file by reference instead.
|
|
if s.client.Driver == scm.DriverGogs &&
|
|
strings.HasPrefix(ref, "refs/tag") {
|
|
commit = ref
|
|
}
|
|
err := s.renewer.Renew(ctx, user, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ctx = context.WithValue(ctx, scm.TokenKey{}, &scm.Token{
|
|
Token: user.Token,
|
|
Refresh: user.Refresh,
|
|
})
|
|
content, _, err := s.client.Contents.Find(ctx, repo, path, commit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &core.File{
|
|
Data: content.Data,
|
|
Hash: []byte{},
|
|
}, nil
|
|
}
|