Skip to content

Commit

Permalink
feat: Extending API implementation around webhooks, pull-requests, us…
Browse files Browse the repository at this point in the history
…ers, and build-status
  • Loading branch information
langecode committed Apr 25, 2023
1 parent fb19038 commit f50564a
Show file tree
Hide file tree
Showing 17 changed files with 1,397 additions and 10 deletions.
13 changes: 13 additions & 0 deletions bitbucket/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ go_library(
"access_tokens_repos.go",
"access_tokens_users.go",
"bitbucket.go",
"events.go",
"keys.go",
"keys_repos.go",
"projects.go",
"projects_repos.go",
"projects_repos_branches.go",
"projects_repos_commits_builds.go",
"projects_repos_prs.go",
"projects_repos_webhooks.go",
"users.go",
"webhook.go",
],
importpath = "github.com/neticdk/go-bitbucket/bitbucket",
visibility = ["//visibility:public"],
Expand All @@ -23,8 +30,14 @@ go_test(
"access_tokens_repos_test.go",
"access_tokens_users_test.go",
"bitbucket_test.go",
"events_test.go",
"keys_repos_test.go",
"projects_repos_branches_test.go",
"projects_repos_commits_builds_test.go",
"projects_repos_test.go",
"projects_repos_webhooks_test.go",
"users_test.go",
"webhook_test.go",
],
embed = [":go_default_library"],
deps = ["@com_github_stretchr_testify//assert:go_default_library"],
Expand Down
6 changes: 0 additions & 6 deletions bitbucket/access_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,3 @@ type AccessToken struct {
Token string `json:"token,omitempty"`
User *User `json:"user,omitempty"`
}

type User struct {
ID uint64 `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
}
25 changes: 25 additions & 0 deletions bitbucket/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Client struct {
AccessTokens *AccessTokensService
Keys *KeysService
Projects *ProjectsService
Users *UsersService
}

type service struct {
Expand Down Expand Up @@ -96,6 +97,29 @@ func (t DateTime) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Time(t).Unix() * 1000)
}

type ISOTime time.Time

const isoLayout = "2006-01-02T15:04:05Z0700"

func (t *ISOTime) UnmarshalJSON(bytes []byte) error {
var raw string
err := json.Unmarshal(bytes, &raw)
if err != nil {
return err
}
parsed, err := time.Parse(isoLayout, raw)
if err != nil {
return err
}
*t = ISOTime(parsed)
return nil
}

func (t ISOTime) MarshalJSON() ([]byte, error) {
s := time.Time(t).Format(isoLayout)
return []byte(s), nil
}

type Permission string

const (
Expand Down Expand Up @@ -156,6 +180,7 @@ func NewClient(baseURL string, httpClient *http.Client) (*Client, error) {
c.AccessTokens = (*AccessTokensService)(&c.common)
c.Keys = (*KeysService)(&c.common)
c.Projects = (*ProjectsService)(&c.common)
c.Users = (*UsersService)(&c.common)
return c, nil
}

Expand Down
92 changes: 92 additions & 0 deletions bitbucket/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package bitbucket

type GitUser struct {
Name string `json:"name"`
Email string `json:"emailAddress"`
}

type CommitData struct {
ID string `json:"id"`
DisplayID string `json:"displayId"`
Message string `json:"message"`
Author GitUser `json:"author"`
Authored DateTime `json:"authorTimestamp"`
Comitter GitUser `json:"committer"`
Comitted DateTime `json:"committerTimestamp"`
}

type Commit struct {
CommitData
Parents []CommitData `json:"parents"`
}

type RepositoryPushEvent struct {
EventKey EventKey `json:"eventKey"`
Date ISOTime `json:"date"`
Actor User `json:"actor"`
Repository Repository `json:"repository"`
Changes []RepositoryPushEventChange `json:"changes"`
Commits []Commit `json:"commits"`
ToCommit Commit `json:"toCommit"`
}

type RepositoryPushEventChange struct {
Ref RepositoryPushEventRef `json:"ref"`
RefId string `json:"refId"`
FromHash string `json:"fromHash"`
ToHash string `json:"toHash"`
Type RepositoryPushEventChangeType `json:"type"`
}

type RepositoryPushEventChangeType string

const (
RepositoryPushEventChangeTypeAdd RepositoryPushEventChangeType = "ADD"
RepositoryPushEventChangeTypeUpdate RepositoryPushEventChangeType = "UPDATE"
RepositoryPushEventChangeTypeDelete RepositoryPushEventChangeType = "DELETE"
)

type RepositoryPushEventRef struct {
ID string `json:"id"`
DisplayID string `json:"displayId"`
Type RepositoryPushEventRefType `json:"type"`
}

type RepositoryPushEventRefType string

const (
RepositoryPushEventRefTypeBranch RepositoryPushEventRefType = "BRANCH"
RepositoryPushEventRefTypeTag RepositoryPushEventRefType = "TAG"
)

type PullRequestEvent struct {
EventKey EventKey `json:"eventKey"`
Date ISOTime `json:"date"`
Actor User `json:"actor"`
PullRequest PullRequest `json:"pullRequest"`
}

type EventKey string

const (
EventKeyRepoRefsChanged EventKey = "repo:refs_changed" // Repo push
EventKeyRepoModified EventKey = "repo:modified" // Repo changed (name)
EventKeyRepoFork EventKey = "repo:fork" // Repo forked
EventKeyCommentAdded EventKey = "repo:comment:added" // Repo comment on commit added
EventKeyCommentEdited EventKey = "repo:comment:edited" // Repo comment on commit edited
EventKeyCommentDeleted EventKey = "repo:comment:deleted" // Repo comment on commit deleted
EventKeyPullRequestOpened EventKey = "pr:opened" // Pull request opened
EventKeyPullRequestFrom EventKey = "pr:from_ref_updated" // Pull request source ref updated
EventKeyPullRequestTo EventKey = "pr:to_ref_updated" // Pull request target ref updated
EventkeyPullRequestModified EventKey = "pr:modified" // Pull request modified (title, description, target)
EventKeyPullRequestReviewer EventKey = "pr:reviewer:updated" // Pull request reviewers updated
EventKeyPullRequestApproved EventKey = "pr:reviewer:approved" // Pull request approved by reviewer
EventKeyPullRequestUnapproved EventKey = "pr:reviewer:unapproved" // Pull request approval withdrawn by reviewer
EventKeyPullRequestNeedsWork EventKey = "pr:reviewer:needs_work" // Pull request reviewer marked "needs work"
EventKeyPullRequestMerged EventKey = "pr:merged"
EventKeyPullRequestDeclined EventKey = "pr:declined"
EventKeyPullRequestDeleted EventKey = "pr:deleted"
EventKeyPullRequestCommentAdded EventKey = "pr:comment:added"
EventKeyPullRequestCommentEdited EventKey = "pr:comment:edited"
EventKeyPullRequestCommentDeleted EventKey = "pr:comment:deleted"
)
Loading

0 comments on commit f50564a

Please sign in to comment.