Files
golib/artifact/github/github.go
Nicolas JUHEL 85090c51d7 PKG Artifact: add new package to manage artifact manager like github, gitlab, s3, ... some other will come after (big matter with jfrog because jfrog has badly code never seens, maybe need to do dedicated lib)
For now artifact is only to get all release, major, minor, latest, major latest, minor latest, artifact link and download. Upload and creation release will be allowed in future
2020-09-11 14:01:37 +02:00

79 lines
2.3 KiB
Go

/*
* MIT License
*
* Copyright (c) 2020 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
package github
import (
"context"
"net/http"
"strings"
"github.com/google/go-github/v32/github"
"github.com/nabbar/golib/artifact"
"github.com/nabbar/golib/artifact/client"
"github.com/nabbar/golib/errors"
)
func getOrgProjectFromRepos(repos string) (owner string, project string) {
if strings.HasPrefix(repos, "/") || strings.HasSuffix(repos, "/") {
repos = strings.Trim(repos, "/")
}
lst := strings.SplitN(repos, "/", 2)
return lst[0], lst[1]
}
func NewGithub(ctx context.Context, httpcli *http.Client, repos string) (cli artifact.Client, err errors.Error) {
o, p := getOrgProjectFromRepos(repos)
a := &githubModel{
ClientHelper: client.ClientHelper{},
c: github.NewClient(httpcli),
x: ctx,
o: o,
p: p,
}
a.F = a.ListReleases
return a, err
}
func NewGithubWithTokenOAuth(ctx context.Context, repos string, oauth2client *http.Client) (cli artifact.Client, err errors.Error) {
o, p := getOrgProjectFromRepos(repos)
a := &githubModel{
ClientHelper: client.ClientHelper{},
c: github.NewClient(oauth2client),
x: ctx,
o: o,
p: p,
}
a.F = a.ListReleases
return a, err
}