Skip to content

Commit

Permalink
Merge pull request #1 from iver-wharf/kalle/added-repo
Browse files Browse the repository at this point in the history
Copied over repo (without Git history)
  • Loading branch information
applejag authored Mar 15, 2021
2 parents efc6c7b + c82dacf commit fd76053
Show file tree
Hide file tree
Showing 14 changed files with 840 additions and 0 deletions.
80 changes: 80 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Wharf API Go client changelog

This project tries to follow [SemVer 2.0.0](https://semver.org/).

<!--
When composing new changes to this list, try to follow convention.
The WIP release shall be updated just before adding the Git tag.
From (WIP) to (YYYY-MM-DD), ex: (2021-02-09) for 9th of Febuary, 2021
A good source on conventions can be found here:
https://changelog.md/
-->

## v1.2.0 (2021-02-25)

- Added CHANGELOG.md to repository. (!10)

- Added support for endpoint `PUT /api/build/{buildid}?status={status}` (via function `PutStatus`). (!11)

- Added support for endpoint `POST /api/build/{buildid}/log` (via function `PostLog`). (!11)

## v1.1.0 (2021-01-07)

- Fixed all endpoint functions not checking status code on HTTP responses. (!9)

## v1.0.0 (2020-12-02)

- Changed package name from `client` to `wharf` and changed the type name
`WharfClient` to just `Client`, resulting in usage changing from
`client.WharfClient` to `wharf.Client`. (!7, !8)

## v0.1.5 (2020-11-24)

- Removed group endpoints support, a reflection of the changes from the API
v1.0.0. (!6)
- `GET /api/group` (via `GetGroupById`)
- `GET /api/groups/search` (via `GetGroup`)
- `PUT /api/group` (via `PutGroup`)

## v0.1.4 (2020-10-23)

- Added support for endpoint `PUT /api/branches` (via function `PutBranches`)
that was introduced in the API v1.0.0. (!5)

- Added `README.md` to document that this package is only meant to be used to
talk to the main API and not to the provider/plugin APIs just to avoid
circular dependencies. (!3)

## v0.1.3 (2020-04-30)

- Added support for endpoint `POST /api/project/{projectid}/{stage}/run`
(via `PostProjectRun` function). (!1)

## v0.1.2 (2019-11-21)

- Fixed `/api/providers/search` function (`GetProvider`) to only filter by
token ID `if tokenID > 0`. (a57a10d2)

## v0.1.1 (2019-11-21)

- Changed module name and moved repository from `/tools/wharf-project/client`
to `/tools/wharf-client` due to restrictions in how Go handles Git paths.
(755a2fc4)

## v0.1.0 (2019-11-21)

- Added initial commit. Endpoints supported: (df25308e)
- `POST /api/branch` (via `PutBranch`)
- `GET /api/group/{groupId}` (via `GetGroupById`)
- `GET /api/groups/search` (via `GetGroup`)
- `PUT /api/group` (via `PutGroup`)
- `GET /api/project/{projectId}` (via `GetProjectById`)
- `PUT /api/project` (via `PutProject`)
- `GET /api/provider/{providerId}` (via `GetProviderById`)
- `POST /api/providers/search` (via `GetProvider`)
- `POST /api/provider` (via `PostProvider`)
- `GET /api/token/{tokenId}` (via `GetTokenById`)
- `POST /api/tokens/search` (via `GetToken`)
- `POST /api/token` (via `PostToken`)
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Go-lang RPC client for Wharf

A library to talk to Wharf via Wharf's main API written in Go.

Uses `net/http` to send HTTP requests and `encoding/json` to
serialize/deserialize each message back and forth.

This project is for example used inside the providers to create projects
into the database when importing from GitLab, GitHub, or Azure DevOps.

## Usage

```go
package main

import (
"fmt"
"github.com/iver-wharf/wharf-api-client-go/pkg/wharfapi"
)

func main() {
client := wharfapi.Client{
ApiUrl: "https://example.wharf.com",
AuthHeader: "Bearer some-auth-token",
}

project,err := client.GetProjectById(125)

if err != nil {
fmt.Printf("Unable to find project\n")
} else {
fmt.Printf("Project #%d: %s\n", project.ProjectID, project.Name)
}
}
```

### Sample output

```
GET | PROJECT | 125
Project #125: MyProject
```

---

Maintained by [Iver](https://www.iver.com/en).
Licensed under the [MIT license](./LICENSE).
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/iver-wharf/wharf-api-client-go

go 1.13

require (
github.com/sirupsen/logrus v1.7.0
github.com/stretchr/testify v1.2.2
gopkg.in/guregu/null.v4 v4.0.0
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/guregu/null.v4 v4.0.0 h1:1Wm3S1WEA2I26Kq+6vcW+w0gcDo44YKYD7YIEJNHDjg=
gopkg.in/guregu/null.v4 v4.0.0/go.mod h1:YoQhUrADuG3i9WqesrCmpNRwm1ypAgSHYqoOcTu/JrI=
62 changes: 62 additions & 0 deletions pkg/wharfapi/branch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package wharfapi

import (
"encoding/json"
"fmt"
"net/http"
)

type Branch struct {
BranchID uint `json:"branchId"`
ProjectID uint `json:"projectId"`
Name string `json:"name"`
TokenID uint `json:"tokenId"`
Default bool `json:"default"`
}

func (c Client) PutBranch(branch Branch) (Branch, error) {
newBranch := Branch{}

body, err := json.Marshal(branch)
if err != nil {
return newBranch, err
}

url := fmt.Sprintf("%s/api/branch", c.ApiUrl)
ioBody, err := doRequest("POST | BRANCH |", http.MethodPost, url, body, c.AuthHeader)
if err != nil {
return newBranch, err
}

defer (*ioBody).Close()

err = json.NewDecoder(*ioBody).Decode(&newBranch)
if err != nil {
return newBranch, err
}

return newBranch, nil
}

func (c Client) PutBranches(branches []Branch) ([]Branch, error) {
var newBranches []Branch
body, err := json.Marshal(branches)
if err != nil {
return newBranches, err
}

url := fmt.Sprintf("%s/api/branches", c.ApiUrl)
ioBody, err := doRequest("PUT | BRANCHES |", http.MethodPut, url, body, c.AuthHeader)
if err != nil {
return newBranches, err
}

defer (*ioBody).Close()

err = json.NewDecoder(*ioBody).Decode(&newBranches)
if err != nil {
return newBranches, err
}

return newBranches, nil
}
27 changes: 27 additions & 0 deletions pkg/wharfapi/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package wharfapi

import (
"time"

"gopkg.in/guregu/null.v4"
)

type Build struct {
BuildID uint `json:"buildId"`
StatusID BuildStatus `json:"statusId"`
ProjectID uint `json:"projectId"`
ScheduledOn *time.Time `json:"scheduledOn"`
StartedOn *time.Time `json:"startedOn"`
CompletedOn *time.Time `json:"finishedOn"`
GitBranch string `json:"gitBranch"`
Environment null.String `json:"environment"`
Stage string `json:"stage"`
Params []BuildParam `json:"params"`
IsInvalid bool `json:"isInvalid"`
}

type BuildParam struct {
BuildID uint `json:"buildId"`
Name string `json:"name"`
Value string `json:"value"`
}
47 changes: 47 additions & 0 deletions pkg/wharfapi/buildlog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package wharfapi

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
)

type BuildLog struct {
Message string `json:"message"`
Timestamp time.Time `json:"timestamp"`
}

func (c Client) PostLog(buildID uint, buildLog BuildLog) error {
body, err := json.Marshal(buildLog)
if err != nil {
return err
}

url := fmt.Sprintf("%s/api/build/%d/log", c.ApiUrl, buildID)
_, err = doRequest("POST | LOG", http.MethodPost, url, body, c.AuthHeader)
if err != nil {
return err
}

return nil
}

func (c Client) PutStatus(buildID uint, statusID BuildStatus) (Build, error) {
uri := fmt.Sprintf("%s/api/build/%d?status=%s", c.ApiUrl, buildID, url.QueryEscape(statusID.String()))

ioBody, err := doRequest("PUT | STATUS", http.MethodPut, uri, nil, c.AuthHeader)
if err != nil {
return Build{}, err
}
defer (*ioBody).Close()

build := Build{}
err = json.NewDecoder(*ioBody).Decode(&build)
if err != nil {
return Build{}, err
}

return build, nil
}
27 changes: 27 additions & 0 deletions pkg/wharfapi/buildstatus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package wharfapi

import "strconv"

type BuildStatus int

const (
BuildScheduling = BuildStatus(iota)
BuildRunning
BuildCompleted
BuildFailed
)

func (bs BuildStatus) String() string {
switch bs {
case BuildScheduling:
return "Scheduling"
case BuildRunning:
return "Running"
case BuildCompleted:
return "Completed"
case BuildFailed:
return "Failed"
default:
return strconv.Itoa(int(bs))
}
}
22 changes: 22 additions & 0 deletions pkg/wharfapi/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package wharfapi

type AuthError struct {
Realm string
}

func (e *AuthError) Error() string {
return e.Realm
}

// WharfClient contains authentication and API URLs used to access
// the Wharf main API.
type Client struct {
AuthHeader string
ApiUrl string
}

// WharfClient contains authentication and API URLs used to access
// the Wharf main API.
//
// Deprecated: This type has been renamed to Client and may be removed in a future release.
type WharfClient Client
Loading

0 comments on commit fd76053

Please sign in to comment.