Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API endpoints for stars #100

Merged
merged 1 commit into from
Nov 17, 2016
Merged

Conversation

ethantkoenig
Copy link
Member

This pull request adds the following API endpoints:

  • GET /users/:username/starred: List repositories starred by a user
  • GET /user/starred: List repositories starred by authenticated user
  • GET /user/starred/:owner/:repo: If authenticated user is starring a repository
  • PUT /user/starred/:owner/:repo: Star a repository (as authenticated user)
  • DELETE /user/starred/:owner/:repo: Unstar a repository (as authenticated user)

Each of these endpoints already exists in the Github API.

@codecov-io
Copy link

codecov-io commented Nov 6, 2016

Current coverage is 3.03% (diff: 0.00%)

Merging #100 into master will decrease coverage by <.01%

@@            master      #100   diff @@
========================================
  Files           33        33          
  Lines         8096      8106    +10   
  Methods          0         0          
  Messages         0         0          
  Branches         0         0          
========================================
  Hits           246       246          
- Misses        7830      7840    +10   
  Partials        20        20          

Powered by Codecov. Last update 871c964...0834e49

@@ -1121,3 +1121,22 @@ func UnfollowUser(userID, followID int64) (err error) {
}
return sess.Commit()
}

func GetStarredRepos(userID int64) ([]*Repository, error) {
Copy link
Member

Choose a reason for hiding this comment

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

A join query is more efficiency

Copy link
Member

Choose a reason for hiding this comment

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

Please add comments too for linting

@lunny lunny added the type/feature Completely new functionality. Can only be merged if feature freeze is not active. label Nov 7, 2016
@lunny lunny added this to the 1.0.0 milestone Nov 7, 2016
@strk
Copy link
Member

strk commented Nov 7, 2016

Unit tests would be needed for the new APIs

Copy link
Member

@bkcsoft bkcsoft left a comment

Choose a reason for hiding this comment

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

Routers shouldn't do cross-route function-calls

@@ -238,7 +238,7 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) {
ctx.JSON(201, repo.APIFormat(&api.Permission{true, true, true}))
}

func parseOwnerAndRepo(ctx *context.APIContext) (*models.User, *models.Repository) {
func ParseOwnerAndRepo(ctx *context.APIContext) (*models.User, *models.Repository) {
Copy link
Member

Choose a reason for hiding this comment

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

This should be moved to a middleware instead.

Copy link
Member

Choose a reason for hiding this comment

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

Also I don't think "Parse" is the correct word for what it does either :)

Copy link
Member

Choose a reason for hiding this comment

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

Please add comments for the function for linting


"github.com/go-gitea/gitea/modules/context"
"github.com/go-gitea/gitea/models"
"github.com/go-gitea/gitea/routers/api/v1/repo"
Copy link
Member

Choose a reason for hiding this comment

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

Because of this...

Copy link
Member

@DblK DblK left a comment

Choose a reason for hiding this comment

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

Additional comments for linting would be a plus.

@@ -1121,3 +1121,22 @@ func UnfollowUser(userID, followID int64) (err error) {
}
return sess.Commit()
}

func GetStarredRepos(userID int64) ([]*Repository, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Please add comments too for linting

@@ -238,7 +238,7 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) {
ctx.JSON(201, repo.APIFormat(&api.Permission{true, true, true}))
}

func parseOwnerAndRepo(ctx *context.APIContext) (*models.User, *models.Repository) {
func ParseOwnerAndRepo(ctx *context.APIContext) (*models.User, *models.Repository) {
Copy link
Member

Choose a reason for hiding this comment

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

Please add comments for the function for linting

"github.com/go-gitea/gitea/routers/api/v1/repo"
)

func getStarredRepos(userID int64) ([]*api.Repository, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Since you are at it, please add comments for linting.

return repos, nil
}

func GetStarredRepos(ctx *context.APIContext) {
Copy link
Member

Choose a reason for hiding this comment

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

Since you are at it, please add comments for linting.

ctx.JSON(200, &repos)
}

func GetMyStarredRepos(ctx *context.APIContext) {
Copy link
Member

Choose a reason for hiding this comment

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

Since you are at it, please add comments for linting.

ctx.JSON(200, &repos)
}

func IsStarring(ctx *context.APIContext) {
Copy link
Member

Choose a reason for hiding this comment

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

Since you are at it, please add comments for linting.

}
}

func Star(ctx *context.APIContext) {
Copy link
Member

Choose a reason for hiding this comment

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

Since you are at it, please add comments for linting.

ctx.Status(204)
}

func Unstar(ctx *context.APIContext) {
Copy link
Member

Choose a reason for hiding this comment

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

Since you are at it, please add comments for linting.

@ethantkoenig
Copy link
Member Author

@lunny @bkcsoft @DblK I've added all of the requested changes. The only comment I haven't addressed is @strk's request for unit tests. I'm not sure how to mock out the database for unit tests, and I couldn't find any existing unit tests to use as examples.

// Get the repos starred by a particular user
func GetStarredRepos(userID int64) ([]*Repository, error) {
sess := x.Where("star.uid=?", userID)
if setting.UsePostgreSQL {
Copy link
Member

Choose a reason for hiding this comment

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

please remove database condition, just use

sess.Join("LEFT", "star", "`repository`.id=star.uid")

is OK.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't understand. repository.id is a repo id, and star.uid is a user id; why are we comparing them? I thought that repo ids and user ids were different things. It also seems that your suggestion ignores the userID variable, which doesn't seem right.

Please correct me if I'm wrong.

Copy link
Member

Choose a reason for hiding this comment

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

sess.Join("LEFT", "star", "`repository`.id=star. repo_id")

mistake. I mean we don't need if setting.UsePostgreSQL

@lunny
Copy link
Member

lunny commented Nov 11, 2016

Great work! For unit test with database. I think that should be next work.

@ethantkoenig
Copy link
Member Author

@lunny Fixed if setting.UsePostgreSQL issue.

sess = sess.Join("LEFT", "star", `"repository".id=star.repo_id`)
repos := make([]*Repository, 0, 10)
err := sess.Find(&repos)
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

I think it could be simpler

repos := make([]*Repository, 0, 10)
err := x.Where("star.uid=?", userID).
        Join("LEFT", "star", `"repository".id=star.repo_id`).
        Find(&repos)

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

func GetStarredRepos(userID int64) ([]*Repository, error) {
repos := make([]*Repository, 0, 10)
err := x.Where("star.uid=?", userID).
Join("LEFT", "star", `"repository".id=star.repo_id`).
Copy link
Member

Choose a reason for hiding this comment

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

this line should be

Join("LEFT", "star", "`repository`.id=`star`.repo_id").

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated

@lunny
Copy link
Member

lunny commented Nov 11, 2016

LGTM

func GetStarredRepos(userID int64) ([]*Repository, error) {
repos := make([]*Repository, 0, 10)
err := x.Where("star.uid=?", userID).
Join("LEFT", "star", "`repository`.id=`star`.repo_id").
Copy link
Member

Choose a reason for hiding this comment

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

Does this quoting work for all 3 databases?

Copy link
Member Author

Choose a reason for hiding this comment

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

According to @lunny's comment from earlier today, yes.

Copy link
Member

Choose a reason for hiding this comment

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

Ok

Copy link
Member

Choose a reason for hiding this comment

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

Yes

@tboerger
Copy link
Member

LGTM

@lunny
Copy link
Member

lunny commented Nov 12, 2016

@bkcsoft @DblK Please review again.

Copy link
Member

@bkcsoft bkcsoft left a comment

Choose a reason for hiding this comment

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

Please read this: https://blog.golang.org/godoc-documenting-go-code especially this:

Notice this comment is a complete sentence that begins with the name of the element it describes. This important convention allows us to generate documentation in a variety of formats, from plain text to HTML to UNIX man pages, and makes it read better when tools truncate it for brevity, such as when they extract the first line or sentence.

@@ -70,3 +71,29 @@ func APIContexter() macaron.Handler {
c.Map(ctx)
}
}

// Extracts the owner and repo from an APIContext, or issues an error status
Copy link
Member

Choose a reason for hiding this comment

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

Needs to start with ExtractOwnerAndRepo 😒

return repos, nil
}

// Returns the repos that the user specified by the APIContext has starred
Copy link
Member

Choose a reason for hiding this comment

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

Lint-comment

ctx.JSON(200, &repos)
}

// Returns the repos that the authenticated user has starred
Copy link
Member

Choose a reason for hiding this comment

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

Lint-comment

ctx.JSON(200, &repos)
}

// Returns whether the authenticated is starring the repo
Copy link
Member

Choose a reason for hiding this comment

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

Lint-comment

"code.gitea.io/gitea/models"
)

// Returns the repos that the user with the specified userID has starred
Copy link
Member

Choose a reason for hiding this comment

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

While this function isn't exported (therefore doesn't need a lint-comment), it would be nice to follow conventions everywhere :)

Copy link
Member

@bkcsoft bkcsoft left a comment

Choose a reason for hiding this comment

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

consistency for middlewares

return nil, nil
}

return owner, repo
Copy link
Member

Choose a reason for hiding this comment

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

Preferably, a middleware injects this into ctx.Data["Owner"], ctx.Data["Repo"] and returns nothing. Then it can be used as follows:

m.Group("/starred", context.ExtractOwnerAndRepo(), func() {
    m.Get("", user.GetMyStarredRepos)
    m.Group("/:username/:reponame", func() {
        m.Get("", user.IsStarring)
        m.Put("", user.Star)
        m.Delete("", user.Unstar)
    })
})

see context.RepoRef() for reference 🙂

Copy link
Member Author

Choose a reason for hiding this comment

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

@bkcsoft Would be appropriate to use context.RepoAssignment() for populating ctx.Repo.Owner and ctx.Repo.Repository? It seems to already do what we need ExtractRepoAndOwner() to do. This way, we could get rid of ExtractRepoAndOwner() completely.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, please do :)

@bkcsoft bkcsoft added issue/needs-feedback For bugs, we need more details. For features, the feature must be described in more detail pr/wip This PR is not ready for review labels Nov 14, 2016
@ethantkoenig
Copy link
Member Author

@bkcsoft Unfortunately, context.RepoAssignment does not seem to work; I made the change and tried running locally, but was hitting errors at the line that calls git.OpenRepository(..) (context/repo.go:233).

Regardless, I've updated ExtractRepoAndOwner() to return a macaron.Handler.

@@ -8,6 +8,7 @@ import (
"fmt"
"strings"

"code.gitea.io/gitea/models"
Copy link
Contributor

@metalmatze metalmatze Nov 15, 2016

Choose a reason for hiding this comment

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

this line isn't gofmt. I would merge anyway, as this is minor and we can update with another PR. Probably there's more like that anyway which we can push all together.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch! I've fixed several gofmt issues (mostly spaces vs. tabs)

@@ -1179,3 +1179,15 @@ func UnfollowUser(userID, followID int64) (err error) {
}
return sess.Commit()
}

// GetStarredRepos returns the repos starred by a particular user
func GetStarredRepos(userID int64) ([]*Repository, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This function should return only stars of public repos by default, unless the user is the logged in user.

See #181 as example

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed, thanks for the heads up!

@andreynering
Copy link
Contributor

LGTM

@andreynering andreynering added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed issue/needs-feedback For bugs, we need more details. For features, the feature must be described in more detail pr/wip This PR is not ready for review labels Nov 17, 2016
@andreynering andreynering merged commit d884312 into go-gitea:master Nov 17, 2016
@andreynering andreynering removed the lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. label Nov 17, 2016
@lunny lunny modified the milestones: 1.0.0, 1.1.0 Nov 17, 2016
@tboerger tboerger added the lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. label Nov 29, 2016
@lunny lunny added the modifies/api This PR adds API routes or modifies them label Jan 25, 2017
@go-gitea go-gitea locked and limited conversation to collaborators Nov 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/api This PR adds API routes or modifies them type/feature Completely new functionality. Can only be merged if feature freeze is not active.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants