Skip to content
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ _testmain.go
*.exe
*.test
*.prof

*iml
30 changes: 29 additions & 1 deletion repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ func (c *Client) CreateOrgRepo(org string, opt CreateRepoOption) (*Repository, e
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), repo)
}

// GetRepo returns information of a repository of given owner.
func (c *Client) GetRepo(owner, reponame string) (*Repository, error) {
repo := new(Repository)
return repo, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s", owner, reponame),
Expand Down Expand Up @@ -108,3 +107,32 @@ func (c *Client) MigrateRepo(opt MigrateRepoOption) (*Repository, error) {
return repo, c.getParsedResponse("POST", "/repos/migrate",
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), repo)
}

type ForkRepoOption struct {
Name string `json:"name" binding:"Required"`
Description string `json:"description" binding:"MaxSize(255)"`
TargetUser string `json:"target_username"`
}

func (c *Client) ForkRepo(user string, repoName string, opt ForkRepoOption) (*Repository, error) {
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
repo := new(Repository)
return repo, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/forks", user, repoName),
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), repo)
}

type CollaboratorOption struct {
UserName string `json:"user_name" binding:"Required"`
}

func (c *Client) AddCollaborator(user string, repo string, opt CollaboratorOption) (error) {
body, err := json.Marshal(&opt)
if err != nil {
return err
}
_, err = c.getResponse("POST", fmt.Sprintf("/repos/%s/%s/collaboration", user, repo), http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body))
return err
}
62 changes: 62 additions & 0 deletions repo_commits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package gogs

import (
"fmt"
"time"
)

type Sha1 struct {
Sha1 string `json:"sha1"`
}

type Signature struct {
Email string `json:"email"`
Name string `json:"name"`
When time.Time `json:"when"`
}

type Commit struct {
ID string `json:"id"`
Author Signature `json:"author"`
Committer Signature `json:"committer"`
CommitMessage string `json:"commit_message"`
}

type DiffLine struct {
LeftIdx int `json:"left_idx"`
RightIdx int `json:"right_idx"`
Type string `json:"type"`
Content string `json:"content"`
}

type DiffSection struct {
Name string `json:"name"`
Lines []*DiffLine `json:"lines"`
}

type DiffFile struct {
Name string `json:"name"`
Index int `json:"index"`
Addition int `json:"addition"`
Deletion int `json:"deletion"`
Type string `json:"type"`
IsCreated bool `json:"created"`
IsDeleted bool `json:"deleted"`
IsBin bool `json:"bin"`
Sections []*DiffSection `json:"sections"`
}

type Diff struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this struct do? Seems not using it anywhere?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. I didn't create the client method in the gogs client.

The DiffFile struct is used in gogs.

I tested the service method from the java lib and forgot the client method in gogs-client

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK... should you remove it or...?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. It is used by Gogs, and is needed for my gogs pull request. See the DiffRange function in the that pull request.

This is also why the gogs pull request has dependency to kiliit/go-gogs-client. If it hasn't then compile fails.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I don't get it...

Gogs has these structs itself, why need import from this package?

TotalAddition int `json:"total_addition"`
TotalDeletion int `json:"total_deletion"`
Files []*DiffFile `json:"files"`
}

func (c *Client) CommitByID(user, repo, id string) (*Commit, error) {
commit := new(Commit)
return commit, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s", user, repo, id), nil, nil, commit)
}
60 changes: 60 additions & 0 deletions repo_releases.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package gogs

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

type Release struct {
ID int64 `json:"id"`
Publisher User `json:"publisher"`
TagName string `json:"tag_name"`
LowerTagName string `json:"lower_tag_name"`
Target string `json:"target"`
Title string `json:"title"`
Sha1 string `json:"sha1"`
NumCommits int `json:"num_commits"`
NumCommitsBehind int `json:"num_commits_behind"`
Note string `json:"note"`
IsDraft bool `json:"draft"`
IsPrerelease bool `json:"prerelease"`
Created time.Time `json:"created"`
}

func (c *Client) ReleaseByName(user, repo, name string) (*Release, error) {
release := new(Release)
return release, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/releases/%s", user, repo, name), nil, nil, release)
}

func (c *Client) ListReleases() ([]*Release, error) {
releases := make([]*Release, 0, 10)
err := c.getParsedResponse("GET", "/repos/%s/%s/releases", nil, nil, &releases)
return releases, err
}

type CreateReleaseOption struct {
TagName string `json:"tag_name"`
Target string `json:"target"`
Title string `json:"title"`
Note string `json:"note"`
IsDraft bool `json:"draft"`
IsPrerelease bool `json:"prerelease"`
}


func (c *Client) CreateRelease(user string, repo string, opt CreateReleaseOption) (*Release, error) {
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
release := new(Release)
return release, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/releases", user, repo),
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), release)
}
18 changes: 18 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ package gogs

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

// User represents a API user.
Expand All @@ -17,8 +20,23 @@ type User struct {
AvatarUrl string `json:"avatar_url"`
}

type CreateUserOption struct {
Name string `json:"name" binding:"Required;AlphaDashDot;MaxSize(35)"`
Email string `json:"email" binding:"Required;Email;MaxSize(254)"`
Password string `json:"password" binding:"Required;MaxSize(255)"`
}

func (c *Client) GetUserInfo(user string) (*User, error) {
u := new(User)
err := c.getParsedResponse("GET", fmt.Sprintf("/users/%s", user), nil, nil, u)
return u, err
}

func (c *Client) CreateUser(opt CreateUserOption) (*User, error) {
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
u := new(User)
return u, c.getParsedResponse("POST", "/users", http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), u)
}