gitapi

package module
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2023 License: MIT Imports: 8 Imported by: 4

README

go-gitapi Paypal donate

Golang Github/Gitea api library.

Table Of Content
Features
  • API action
    • Do
    • Get
    • Del
    • Patch
    • Post
    • Put
gitApi.go
  • type GitApiReq struct
  • type GitApiRes struct
  • type GitApi struct
  • func GitApiNew(
  • func (self *GitApi) EndpointUserRepos() *GitApi
  • func (self *GitApi) EndpointRepos() *GitApi
  • func (self *GitApi) EndpointReposTopics() *GitApi
  • func (self *GitApi) EndpointReposSecrets() *GitApi
  • func (self *GitApi) EndpointReposSecretsPubkey() *GitApi
  • func (self *GitApi) HeaderGithub() *GitApi
  • func (self *GitApi) HeaderInit() *GitApi
  • func (self *GitApi) Do() *GitApi
  • func (self *GitApi) Get() *GitApi
  • func (self *GitApi) Del() *GitApi
  • func (self *GitApi) Patch() *GitApi
  • func (self *GitApi) Post() *GitApi
  • func (self *GitApi) Put() *GitApi
  • func (self *GitApi) ProcessOutput() *GitApi
  • func (self *GitApiReq) UrlValInit()
  • func (self *GitApiRes) Ok() bool
gitApiDataStruct.go
  • type RepoEncryptedPair struct
  • func (self *RepoEncryptedPair) StringP() *string
  • func (self *RepoEncryptedPair) String() string
  • type RepoPublicKey struct
  • func (self *RepoPublicKey) StringP() *string
  • func (self *RepoPublicKey) String() string
  • type RepoPrivate struct
  • func (self *RepoPrivate) StringP() *string
  • func (self *RepoPrivate) String() string
  • type RepoVisibility struct
  • func (self *RepoVisibility) StringP() *string
  • func (self *RepoVisibility) String() string
  • type RepoDescription struct
  • func (self *RepoDescription) StringP() *string
  • func (self *RepoDescription) String() string
  • type RepoTopics struct
  • func (self *RepoTopics) StringP() *string
  • func (self *RepoTopics) String() string
  • type RepoInfo struct
  • func (self *RepoInfo) StringP() *string
  • func (self *RepoInfo) String() string
  • type RepoInfoList []RepoInfo
  • func (self *RepoInfoList) StringP() *string
  • func (self *RepoInfoList) String() string
  • type NilType struct
  • func (self *NilType) StringP() *string
  • func (self *NilType) String() string
  • func Nil() *NilType
  • type GitApiInfo interface
Pro
  • Easy to extend
  • Small size
Doc
Dependency
Supported git repository services
  • gitea
  • github
  • gogs
Usage Example

Following is code to create a new repository:

  1. Prepare a GitApi data structure

    var info gitapi.RepoInfo
    info.Name = "test"
    info.Private = remote.Private
    
  2. Setup and execute

    // Get instance
    gitApi := gitapi.GitApiNew(
      "Test",   // Connection name for debug print out purpose
      "01234567890123456789012345678912", // API token,
      "https://api.github.com", // API entrypoint
      "J-Siu",  // user
      "github", // vendor/brand
      &info)    // data for request
    // Setup endpoint
    gitApi.EndpointRepos()
    // Setup Github header
    gitApi.HeaderGithub()
    // Do post request
    success := gitApi.Post().Res.Ok()
    
  3. Print out using helper function

    helper.ReportStatus(success, gitApi.Name)
    helper.ReportStatus(gitApi.Res.Output, gitApi.Name)
    
Debug

Enable debug

helper.Debug = true
Used By Project
Repository
Contributors
Change Log
  • v1.0.0
    • Feature complete
  • v1.0.1
    • Fix data struct *.StringP() output
  • v1.1.0
    • Consolidate output processing
  • v1.2.0
    • GitApiOut
      • move output from GitApi
      • add Success GitApi struct
      • Change Header to non-pointer
      • Use path and url package to handle endpoint and url
      • Add HeaderGithub()
  • v1.2.4
    • Update go-helper package for bug fix
  • v1.2.5
    • GitApiIn
      • Add UrlVal(url.Values)
  • v1.2.6
    • Update go-helper package for bug fix
  • v1.3.0
    • All GitApi methods reutrn self pointer
    • Interface GitApiInfo remove type restrictions
    • Member GitApi.In -> GitApi.Req
    • Member GitApi.Out -> GitApi.Res
    • Type GitApiIn -> GitApiReq
    • Type GitApiOut -> GitApiRes
  • v1.3.1
    • Improve README.md
  • v1.3.2
    • Fix GitApi.Do() wiping GitApi.Req.Data if GitApi.Info is nil
  • v1.4.0
    • upgrade helper to 1.1.6
    • GitApi struct and GitApiMew()
      • add SkipVerify to support self-signed cert
License

The MIT License (MIT)

Copyright © 2023 John, Sing Dao, Siu john.sd.siu@gmail.com

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.

Documentation

Overview

Copyright © 2022 John, Sing Dao, Siu <john.sd.siu@gmail.com>

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.

Index

Constants

View Source
const (
	Vendor_Github = "github"
	Vendor_Gitea  = "gitea"
	Vendor_Gogs   = "gogs"
)

GitApi supperted vendors

Variables

This section is empty.

Functions

This section is empty.

Types

type GitApi

type GitApi struct {
	Req        GitApiReq  `json:"In"`         // Api http input
	Res        GitApiRes  `json:"Out"`        // Api http output
	Name       string     `json:"Name"`       // Name of connection
	User       string     `json:"User"`       // Api username
	Vendor     string     `json:"Vendor"`     // github/gitea
	SkipVerify bool       `json:"skipverify"` // Api request skip cert verify (allow self-signed cert)
	Repo       string     `json:"Repo"`       // Repository name
	Info       GitApiInfo `json:"Info"`       // Pointer to structure. Use NilType.Nil() for nil pointer
}

GitApi

func GitApiNew

func GitApiNew(
	name string,
	token string,
	entrypoint string,
	user string,
	vendor string,
	skipverify bool,
	repo string,
	info GitApiInfo) *GitApi

Setup a *GitApi

func (*GitApi) Del

func (self *GitApi) Del() *GitApi

GitApi Del action wrapper

func (*GitApi) Do

func (self *GitApi) Do() *GitApi

Execute http request using info in GitApi.Req. Then put response info in GitApi.Res.

GitApi.Info, if not nil, will be
		- auto marshal for send other than "GET"
		- auto unmarshal from http response body

func (*GitApi) EndpointRepos

func (self *GitApi) EndpointRepos() *GitApi

Initialize endpoint /repos/OWNER/REPO

Use current directory if GitApi.Repo is empty

func (*GitApi) EndpointReposSecrets

func (self *GitApi) EndpointReposSecrets() *GitApi

Initialize endpoint /repos/OWNER/REPO/actions/secrets

func (*GitApi) EndpointReposSecretsPubkey

func (self *GitApi) EndpointReposSecretsPubkey() *GitApi

Initialize endpoint /repos/OWNER/REPO/actions/secrets/public-key

func (*GitApi) EndpointReposTopics

func (self *GitApi) EndpointReposTopics() *GitApi

Initialize endpoint /repos/OWNER/REPO/topics

func (*GitApi) EndpointUserRepos

func (self *GitApi) EndpointUserRepos() *GitApi

Initialize endpoint /user/repos

func (*GitApi) Get

func (self *GitApi) Get() *GitApi

GitApi Get action wrapper

func (*GitApi) HeaderGithub added in v1.2.0

func (self *GitApi) HeaderGithub() *GitApi

Set github/gitea header

GitApi.Req.Token, if empty, authorization header will not be set.

func (*GitApi) HeaderInit added in v1.2.5

func (self *GitApi) HeaderInit() *GitApi

Setup empty API header

func (*GitApi) Patch

func (self *GitApi) Patch() *GitApi

GitApi Patch action wrapper

func (*GitApi) Post

func (self *GitApi) Post() *GitApi

GitApi Post action wrapper

func (*GitApi) ProcessOutput added in v1.1.0

func (self *GitApi) ProcessOutput() *GitApi

Print both Body and Err into string pointer

func (*GitApi) Put

func (self *GitApi) Put() *GitApi

GitApi Put action wrapper

type GitApiInfo

type GitApiInfo interface {
	// *RepoEncryptedPair |
	// *RepoPublicKey |
	// *RepoPrivate |
	// *RepoVisibility |
	// *RepoDescription |
	// *RepoTopics |
	// *RepoInfo |
	// *RepoInfoList |
	// *NilType
	StringP() *string
	String() string
}

GitApi structures interface

type GitApiReq added in v1.3.0

type GitApiReq struct {
	Data       string       `json:"Data"`       // Json marshaled Info
	Entrypoint string       `json:"Entrypoint"` // Api base url
	Endpoint   string       `json:"Endpoint"`   // Api endpoint
	Header     *http.Header `json:"Header"`     // Http request header
	Method     string       `json:"Method"`     // Http request method
	Token      string       `json:"Token"`      // Api auth token
	UrlVal     *url.Values  `json:"UrlVal"`     // Api url values
}

GitApi http input structure

func (*GitApiReq) UrlValInit added in v1.3.0

func (self *GitApiReq) UrlValInit()

Setup empty API url values

type GitApiRes added in v1.3.0

type GitApiRes struct {
	Body   *[]byte      `json:"Body"`
	Err    string       `json:"Err"`
	Header *http.Header `json:"Header"` // Http response header
	Url    *url.URL     `json:"Url"`    // In.Uri + In.Endpoint
	Output *string      `json:"Output"` // Api response body in string
	Status string       `json:"Status"` // Http response status
}

GitApi http output structure

func (*GitApiRes) Ok added in v1.3.0

func (self *GitApiRes) Ok() bool

Check response status == 2xx

type NilType

type NilType struct {
	Nil *byte // Dummy pointer
}

This is used as a dummy type for nil GitApiInfo parameter

func Nil

func Nil() *NilType

Return a *NilType(nil) dummy pointer

func (*NilType) String

func (self *NilType) String() string

func (*NilType) StringP

func (self *NilType) StringP() *string

type RepoDescription

type RepoDescription struct {
	Description string `json:"description"`
}

Github repository description structure

func (*RepoDescription) String

func (self *RepoDescription) String() string

func (*RepoDescription) StringP

func (self *RepoDescription) StringP() *string

type RepoEncryptedPair

type RepoEncryptedPair struct {
	Encrypted_value string `json:"encrypted_value"` // Encrypted value
	Key_id          string `json:"key_id"`          // Public key id
}

Github repository action secret structure

func (*RepoEncryptedPair) String

func (self *RepoEncryptedPair) String() string

func (*RepoEncryptedPair) StringP

func (self *RepoEncryptedPair) StringP() *string

type RepoInfo

type RepoInfo struct {
	Name    string `json:"name"`
	Private bool   `json:"private"`
}

Github repository(creation) info structure

func (*RepoInfo) String

func (self *RepoInfo) String() string

func (*RepoInfo) StringP

func (self *RepoInfo) StringP() *string

type RepoInfoList

type RepoInfoList []RepoInfo

Github repository(creation) info array

func (*RepoInfoList) String

func (self *RepoInfoList) String() string

func (*RepoInfoList) StringP

func (self *RepoInfoList) StringP() *string

type RepoPrivate

type RepoPrivate struct {
	Private bool `json:"private"`
}

Github repository private structure

func (*RepoPrivate) String

func (self *RepoPrivate) String() string

func (*RepoPrivate) StringP

func (self *RepoPrivate) StringP() *string

type RepoPublicKey

type RepoPublicKey struct {
	Key_id string `json:"key_id"`
	Key    string `json:"key"`
}

Github repository public key structure

func (*RepoPublicKey) String

func (self *RepoPublicKey) String() string

func (*RepoPublicKey) StringP

func (self *RepoPublicKey) StringP() *string

type RepoTopics

type RepoTopics struct {
	Topics *[]string `json:"topics"` // Github topics is "Topics"
	Names  *[]string `json:"names"`  // Gitea topics is "Names"
}

Github repository topics structure

func (*RepoTopics) String

func (self *RepoTopics) String() string

func (*RepoTopics) StringP

func (self *RepoTopics) StringP() *string

type RepoVisibility

type RepoVisibility struct {
	Visibility string `json:"visibility"`
}

Github repository visibility structure

func (*RepoVisibility) String

func (self *RepoVisibility) String() string

func (*RepoVisibility) StringP

func (self *RepoVisibility) StringP() *string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL