-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: git Access, AccessMethod through BlobAccess (#869)
#### What this PR does / why we need it: ~This is an extremely early (WIP) draft of implementing ctf archives in directory format tracked via git repositories, ultimately serving me as a playground to get familiar with OCM and also to play around with its feature set. So we do not need to merge this if there is no interest or disagreement on the implementation.~ This PR has developed into the development of a pure access method and input method that can be used to work with arbitrary git repositories. Examples for a component constructor could be: ``` name: test.de/x version: %s provider: name: ocm resources: - name: hello-world-access type: git version: 0.0.1 access: type: git commit: "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d" ref: refs/heads/master repository: https://github.com/octocat/Hello-World.git - name: hello-world-input type: git version: 0.0.1 input: type: git ref: refs/heads/master commit: "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d" repository: https://github.com/octocat/Hello-World.git ``` ~Not only does this allow using git repositories for storing blobs, it also attempts to add git downloader functionality for a generic git access method based on in memory cloning and I plan to add a OCM implementation on it as well.~ While the download is available, this is not laid out to the CLI, library only. ~Currently the implementation is written so that any modification to the CTF in the work directory also triggers a dedicated commit, and any lookup triggers a refres/pull cycle on git.~ For testing I use `file://` URLs which makes it quite easy to verify E2E in the test suites. TODO: - [X] Implement Access Credential mapping - [x] Make Git Client thread-safe, optionally in-memory and allow more customization (currently hard bount to one target ref that I am creating or syncing against, see tests for examples) - [X] Expand Git Options - Authentication support for repo and client #### Which issue(s) this PR fixes: <!-- Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`. --> ~As this is my own "experimentation" issue, it doesnt necessarily fix anything, it might as well be moved to a plugin for example or be scrapped alltogether~ Creates an access method for arbitrary git repositories using git-go. --------- Co-authored-by: Gergely Brautigam <182850+skarlso@users.noreply.github.com>
- Loading branch information
1 parent
d8559ce
commit 48c3ea2
Showing
45 changed files
with
2,458 additions
and
9 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package gitaccess | ||
|
||
import ( | ||
"github.com/mandelsoft/goutils/optionutils" | ||
) | ||
|
||
type Option = optionutils.Option[*Options] | ||
|
||
type Options struct { | ||
URL string | ||
Ref string | ||
Commit string | ||
} | ||
|
||
var _ Option = (*Options)(nil) | ||
|
||
func (o *Options) ApplyTo(opts *Options) { | ||
if o.URL != "" { | ||
opts.URL = o.URL | ||
} | ||
} | ||
|
||
func (o *Options) Apply(opts ...Option) { | ||
optionutils.ApplyOptions(o, opts...) | ||
} | ||
|
||
// ////////////////////////////////////////////////////////////////////////////// | ||
// Local options | ||
|
||
type url string | ||
|
||
func (h url) ApplyTo(opts *Options) { | ||
opts.URL = string(h) | ||
} | ||
|
||
func WithURL(h string) Option { | ||
return url(h) | ||
} | ||
|
||
type ref string | ||
|
||
func (h ref) ApplyTo(opts *Options) { | ||
opts.Ref = string(h) | ||
} | ||
|
||
func WithRef(h string) Option { | ||
return ref(h) | ||
} | ||
|
||
type commitSpec string | ||
|
||
func (h commitSpec) ApplyTo(opts *Options) { | ||
opts.Commit = string(h) | ||
} | ||
|
||
func WithCommit(c string) Option { | ||
return commitSpec(c) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package gitaccess | ||
|
||
import ( | ||
"github.com/mandelsoft/goutils/optionutils" | ||
|
||
"ocm.software/ocm/api/ocm" | ||
"ocm.software/ocm/api/ocm/compdesc" | ||
"ocm.software/ocm/api/ocm/cpi" | ||
"ocm.software/ocm/api/ocm/elements/artifactaccess/genericaccess" | ||
access "ocm.software/ocm/api/ocm/extensions/accessmethods/git" | ||
resourcetypes "ocm.software/ocm/api/ocm/extensions/artifacttypes" | ||
) | ||
|
||
const TYPE = resourcetypes.DIRECTORY_TREE | ||
|
||
func Access[M any, P compdesc.ArtifactMetaPointer[M]](ctx ocm.Context, meta P, opts ...Option) cpi.ArtifactAccess[M] { | ||
eff := optionutils.EvalOptions(opts...) | ||
if meta.GetType() == "" { | ||
meta.SetType(TYPE) | ||
} | ||
|
||
spec := access.New(eff.URL, access.WithRef(eff.Ref), access.WithCommit(eff.Commit)) | ||
// is global access, must work, otherwise there is an error in the lib. | ||
return genericaccess.MustAccess(ctx, meta, spec) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
|
||
# Access Method `git` - Git Commit Access | ||
|
||
## Synopsis | ||
|
||
```yaml | ||
type: git/v1 | ||
``` | ||
Provided blobs use the following media type for: `application/x-tgz` | ||
|
||
The artifact content is provided as gnu-zipped tar archive | ||
|
||
### Description | ||
|
||
This method implements the access of the content of a git commit stored in a | ||
git repository. | ||
|
||
Supported specification version is `v1alpha1` | ||
|
||
### Specification Versions | ||
|
||
#### Version `v1alpha1` | ||
|
||
The type specific specification fields are: | ||
|
||
- **`repository`** *string* | ||
|
||
Repository URL with or without scheme. | ||
|
||
- **`ref`** (optional) *string* | ||
|
||
Original ref used to get the commit from | ||
|
||
- **`commit`** *string* | ||
|
||
The sha/id of the git commit | ||
|
||
### Go Bindings | ||
|
||
The go binding can be found [here](method.go) | ||
|
||
#### Example | ||
|
||
```go | ||
package main | ||
import ( | ||
"archive/tar" | ||
"bytes" | ||
"compress/gzip" | ||
"fmt" | ||
"io" | ||
"ocm.software/ocm/api/ocm" | ||
"ocm.software/ocm/api/ocm/cpi" | ||
me "ocm.software/ocm/api/ocm/extensions/accessmethods/git" | ||
) | ||
func main() { | ||
ctx := ocm.New() | ||
accessSpec := me.New( | ||
"https://github.com/octocat/Hello-World.git", | ||
me.WithRef("refs/heads/master"), | ||
) | ||
method, err := accessSpec.AccessMethod(&cpi.DummyComponentVersionAccess{Context: ctx}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
content, err := method.GetContent() | ||
if err != nil { | ||
panic(err) | ||
} | ||
unzippedContent, err := gzip.NewReader(bytes.NewReader(content)) | ||
r := tar.NewReader(unzippedContent) | ||
file, err := r.Next() | ||
if err != nil { | ||
panic(err) | ||
} | ||
if file.Name != "README.md" { | ||
panic("Expected README.md") | ||
} | ||
data, err := io.ReadAll(r) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println(string(data)) | ||
} | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package git | ||
|
||
import ( | ||
"ocm.software/ocm/api/ocm/extensions/accessmethods/options" | ||
"ocm.software/ocm/api/utils/cobrautils/flagsets" | ||
) | ||
|
||
func ConfigHandler() flagsets.ConfigOptionTypeSetHandler { | ||
return flagsets.NewConfigOptionTypeSetHandler( | ||
Type, AddConfig, | ||
options.RepositoryOption, | ||
options.ReferenceOption, | ||
options.CommitOption, | ||
) | ||
} | ||
|
||
func AddConfig(opts flagsets.ConfigOptions, config flagsets.Config) error { | ||
flagsets.AddFieldByOptionP(opts, options.RepositoryOption, config, "repository", "repo", "repoUrl", "repoURL") | ||
flagsets.AddFieldByOptionP(opts, options.CommitOption, config, "commit") | ||
flagsets.AddFieldByOptionP(opts, options.ReferenceOption, config, "ref") | ||
return nil | ||
} | ||
|
||
var usage = ` | ||
This method implements the access of the content of a git commit stored in a | ||
Git repository. | ||
` | ||
|
||
var formatV1 = ` | ||
The type specific specification fields are: | ||
- **<code>repoUrl</code>** *string* | ||
Repository URL with or without scheme. | ||
- **<code>ref</code>** (optional) *string* | ||
Original ref used to get the commit from | ||
- **<code>commit</code>** *string* | ||
The sha/id of the git commit | ||
` |
Oops, something went wrong.