ghclient

package
v0.0.0-...-cf6f0f7 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2020 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//ErrInvalidResp occurs when server reply does not contain all required fields
	ErrInvalidResp error = errors.New("did not receive required fields")
)

Functions

func NewTestClient

func NewTestClient(fn RoundTripFunc) *http.Client

NewTestClient returns *http.Client with Transport replaced to avoid making real calls

func RecursivePrint

func RecursivePrint(t Node) (string, error)

RecursivePrint helper function for printing out nodes in tree order

func TreeServer

func TreeServer(tree *Tree, repo *Repository) (map[string]func(*http.Request) (*http.Response, error), error)

TreeServer generates mock server tree responses based on input tree structure. Use this when function being tested makes API calls to retrieve a github tree. Use in conjunction with NewTestClient()

func WriteTreeToDirectory

func WriteTreeToDirectory(top Node, basePath string) error

WriteTreeToDirectory write tree to specified directory path

Types

type API

type API struct {
	Client  *http.Client
	BaseURL string
	// contains filtered or unexported fields
}

API generates github URLS

func NewAPI

func NewAPI() API

NewAPI creates new api type

func (*API) Authenticate

func (a *API) Authenticate(oauth io.Reader) error

Authenticate set and test authentication with Oauth token

func (*API) BlobURL

func (a *API) BlobURL(owner, repo, fileSha string) string

func (*API) GetBlob

func (a *API) GetBlob(owner, repo, sha string) ([]byte, error)

GetBlob retrieve github tree

func (*API) GetTree

func (a *API) GetTree(owner, repo, sha string) ([]byte, error)

GetTree retrieve github tree

func (*API) GetURL

func (a *API) GetURL(url string) ([]byte, error)

GetURL generic function for querying a preconcieved URL

func (*API) NewGistURL

func (a *API) NewGistURL() string

func (*API) PostGists

func (a *API) PostGists(body []byte) ([]byte, error)

PostGists sends post request to status

func (*API) PostStatus

func (a *API) PostStatus(owner, repo, commitSha string, body []byte) error

PostStatus sends post request to status

func (*API) PublishedGistURL

func (a *API) PublishedGistURL(id, user string) string

func (*API) StatusURL

func (a *API) StatusURL(owner, repo, sha string) string

StatusURL generates url for querying status

func (*API) TreeURL

func (a *API) TreeURL(owner, repo, treeSha string) string

func (*API) UpdateGist

func (a *API) UpdateGist(body []byte, ID string) ([]byte, error)

UpdateGist updates gist with ID

func (*API) UpdateGistURL

func (a *API) UpdateGistURL(ID string) string

type Blob

type Blob struct {
	Sha      string `json:"sha"`
	Encoding string `json:"encoding"`
	Content  string `json:"content"`

	Path string `json:"Path"`
	// contains filtered or unexported fields
}

Blob encoded contents of a file

func NewBlobFromJSON

func NewBlobFromJSON(bJSON []byte) (*Blob, error)

NewBlobFromJSON blob factory

func (*Blob) GetChildren

func (b *Blob) GetChildren() []Node

func (*Blob) GetParent

func (b *Blob) GetParent() Node

func (*Blob) GetPath

func (b *Blob) GetPath() string

func (*Blob) SetChild

func (b *Blob) SetChild(child Node)

func (*Blob) Write

func (b *Blob) Write(w io.Writer) error

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache one way cache for tracking github trees

func NewCache

func NewCache() Cache

NewCache create cache

func (*Cache) GetBlob

func (c *Cache) GetBlob(sha string) *Blob

func (*Cache) GetCommit

func (c *Cache) GetCommit(sha string) *Commit

func (*Cache) GetTree

func (c *Cache) GetTree(sha string) *Tree

func (*Cache) WriteBlob

func (c *Cache) WriteBlob(b *Blob)

func (*Cache) WriteCommits

func (c *Cache) WriteCommits(head *Commit)

func (*Cache) WriteTree

func (c *Cache) WriteTree(t *Tree)

type ChildRef

type ChildRef struct {
	Path string `json:"path"`
	Type string `json:"type"`
	Sha  string `json:"sha"`
}

Interim object for holding tree data from api calls in expected format

type Client

type Client struct {
	EventChan chan Event
	User      string

	Api          API
	Cache        Cache
	Repositories map[string]*Repository
	// contains filtered or unexported fields
}

Client github client object

func NewClient

func NewClient(eventChan chan Event, user string) *Client

NewClient create a new github Client

func (*Client) GetTree

func (c *Client) GetTree(sha string, repo Repository) (*Tree, error)

GetTree checks cache for tree, else pulls tree from github

func (*Client) Listen

func (c *Client) Listen(wg *sync.WaitGroup, address string, log *logging.Logger) *http.Server

Listen listen on address for webhooks

func (*Client) UpdateCommitStatus

func (c *Client) UpdateCommitStatus(repo Repository, commit Commit) error

UpdateCommitStatus update status of commit in repository

type Comment

type Comment struct {
	Ref  Reference
	Repo Repository

	Action    string
	RefName   string
	Body      string
	CommitSHA string
	User      string
}

Comment implements Event interface. Represents a github comment webhook

func (*Comment) Handle

func (c *Comment) Handle(client *Client, commentJSON []byte) error

Handle parses the contents of a github issue comment

type Commit

type Commit struct {
	Sha     string `json:"sha"`
	Message string `json:"message"`
	URL     string `json:"url"`
	ID      string `json:"id"`
	Status  Status
	Author  struct {
		Name     string `json:"name"`
		Email    string `json:"email"`
		Username string `json:"username"`
	}
	// contains filtered or unexported fields
}

Commit resource tracking a github commit

func NewCommitFromJSON

func NewCommitFromJSON(commitJSON []byte) (*Commit, error)

NewCommitFromJSON build commit from json byte slice

func (*Commit) GetParent

func (c *Commit) GetParent() *Commit

GetParent returns copy of parent commit

func (*Commit) SetContext

func (c *Commit) SetContext(context string)

SetContext set context of commit status

func (*Commit) SetStatus

func (c *Commit) SetStatus(state CommitState, message string, targetURL string)

SetStatus sets status of commit with a message

func (*Commit) String

func (c *Commit) String() string

type CommitState

type CommitState int
const (
	ERROR CommitState = iota
	FAILURE
	PENDING
	SUCCESS
	NONE
)

func (CommitState) String

func (cs CommitState) String() string

type Event

type Event interface {
	Handle(*Client, []byte) error
}

Event definitions from github

func EventFactory

func EventFactory(incoming string) (Event, error)

EventFactory create github event based on string name

type File

type File struct {
	Content string `json:"content"`
}

File represents file object in github gist

type GhObject

type GhObject interface {
	String() string
}

GhObject generic type representing all github objects

type Gist

type Gist struct {
	Description string           `json:"description"`
	Public      bool             `json:"public"`
	Files       map[string]*File `json:"files"`
}

Gist github gist object. Contains collection of files

func NewGist

func NewGist() Gist

NewGist gist factory

func (*Gist) DeleteFile

func (g *Gist) DeleteFile(name string)

DeleteFile delete file from gist

func (*Gist) WriteFile

func (g *Gist) WriteFile(name, content string)

WriteFile appends content to gist file. Create new file if it does not already exist

type GistWriter

type GistWriter struct {
	API *API
	// contains filtered or unexported fields
}

GistWriter implements io.Writer type for writing to a single file in a github gists. Gists last the lifetime of this object. That is, first calls to write will create a new gist, and subsequent calls will update the existing gist until this object is destroyed.

This should be used in conjuction with a buffered writer to avoid frequent API calls

func NewGistWriter

func NewGistWriter(api *API, g Gist, filename string) (*GistWriter, error)

NewGistWriter GistWriter constructor

func (*GistWriter) GetServerGistID

func (gw *GistWriter) GetServerGistID() string

GetServerGistID returns ID of gist on github server

func (*GistWriter) Write

func (gw *GistWriter) Write(p []byte) (int, error)

Write implements io.Writer

type GithubClientError

type GithubClientError struct {
	// contains filtered or unexported fields
}

GithubClientError all returned errors from this package are of this type

func (*GithubClientError) Error

func (e *GithubClientError) Error() string

type Node

type Node interface {
	GetParent() Node
	GetChildren() []Node
	GetPath() string
	SetChild(Node)
	// contains filtered or unexported methods
}

Node represents node in file tree

type Push

type Push struct {
	Ref     Reference
	RefName string
	Repo    Repository
	User    string
}

Push implements github Event interface

func (*Push) Handle

func (p *Push) Handle(client *Client, pushJSON []byte) error

type Reference

type Reference struct {
	// contains filtered or unexported fields
}

Reference analogous to a git reference. Points to a commit list

func (*Reference) GetHead

func (r *Reference) GetHead() *Commit

GetHead get head commit pointed at by ref

func (*Reference) Register

func (r *Reference) Register(incHead *Commit)

Register register commits to reference

func (*Reference) String

func (r *Reference) String() string

type Repository

type Repository struct {
	Name  string `json:"name"`
	Fork  bool   `json:"fork"`
	Owner struct {
		Login string `json:"login"`
	}
	// contains filtered or unexported fields
}

Repository object for tracking remote repository

func NewRepositoryFromJSON

func NewRepositoryFromJSON(repoJSON []byte) (*Repository, error)

NewRepositoryFromJSON create new repository from json byte slice

func NewRepositoryFromMap

func NewRepositoryFromMap(m map[string]interface{}) (*Repository, error)

NewRepositoryFromMap create new repository from map

func (*Repository) GetReference

func (r *Repository) GetReference(refName string) *Reference

GetReference retrieve git reference by ID

func (*Repository) String

func (r *Repository) String() string

type RoundTripFunc

type RoundTripFunc func(req *http.Request) *http.Response

RoundTripFunc mock http Transport

func (RoundTripFunc) RoundTrip

func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip .

type Status

type Status struct {
	State       string `json:"state"`
	TargetURL   string `json:"target_url"`
	Description string `json:"description"`
	Context     string `json:"context"`
}

Status github status object

type Tree

type Tree struct {
	Sha  string `json:"sha"`
	Path string `json:"path"`
	// contains filtered or unexported fields
}

Tree github tree type

func NewTreeFromJSON

func NewTreeFromJSON(tJSON []byte) (*Tree, error)

NewTreeFromJSON tree factory

func (*Tree) GetChildren

func (t *Tree) GetChildren() []Node

func (*Tree) GetParent

func (t *Tree) GetParent() Node

GetParent implements type Node

func (*Tree) GetPath

func (t *Tree) GetPath() string

func (*Tree) SetChild

func (t *Tree) SetChild(child Node)

type TreeMarshal

type TreeMarshal struct {
	Sha  string     `json:"sha"`
	Tree []ChildRef `json:"tree"`
}

Jump to

Keyboard shortcuts

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