Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Place the default branch at the top of options #530

Merged
merged 4 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/interactor/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type (
BranchSCM interface {
ListBranches(ctx context.Context, u *ent.User, r *ent.Repo, opt *ListOptions) ([]*extent.Branch, error)
GetBranch(ctx context.Context, u *ent.User, r *ent.Repo, branch string) (*extent.Branch, error)
GetDefaultBranch(ctx context.Context, u *ent.User, r *ent.Repo) (*extent.Branch, error)
}

TagSCM interface {
Expand Down
30 changes: 30 additions & 0 deletions internal/interactor/mock/pkg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions internal/pkg/github/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,26 @@ func (g *Github) GetBranch(ctx context.Context, u *ent.User, r *ent.Repo, branch
return mapGithubBranchToBranch(b), nil
}

func (g *Github) GetDefaultBranch(ctx context.Context, u *ent.User, r *ent.Repo) (*extent.Branch, error) {
rr, res, err := g.Client(ctx, u.Token).Repositories.Get(ctx, r.Namespace, r.Name)
if res.StatusCode == http.StatusNotFound {
return nil, e.NewErrorWithMessage(e.ErrorCodeEntityNotFound, "The default branch is not found.", err)
} else if err != nil {
return nil, e.NewError(e.ErrorCodeInternalError, err)
}

b, res, err := g.Client(ctx, u.Token).
Repositories.
GetBranch(ctx, r.Namespace, r.Name, *rr.DefaultBranch, false)
if res.StatusCode == http.StatusNotFound {
return nil, e.NewErrorWithMessage(e.ErrorCodeEntityNotFound, "The default branch is not found.", err)
} else if err != nil {
return nil, e.NewError(e.ErrorCodeInternalError, err)
}

return mapGithubBranchToBranch(b), nil
}

// ListTags list up tags as ordered by commit date.
// Github GraphQL explore - https://docs.github.com/en/graphql/overview/explorer
func (g *Github) ListTags(ctx context.Context, u *ent.User, r *ent.Repo, opt *i.ListOptions) ([]*extent.Tag, error) {
Expand Down
19 changes: 19 additions & 0 deletions internal/server/api/v1/repos/branch_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,22 @@ func (s *BranchAPI) Get(c *gin.Context) {

gb.Response(c, http.StatusOK, b)
}

func (s *BranchAPI) GetDefault(c *gin.Context) {
ctx := c.Request.Context()

uv, _ := c.Get(gb.KeyUser)
u := uv.(*ent.User)

rv, _ := c.Get(KeyRepo)
repo := rv.(*ent.Repo)

b, err := s.i.GetDefaultBranch(ctx, u, repo)
if err != nil {
s.log.Check(gb.GetZapLogLevel(err), "Failed to get the branch.").Write(zap.Error(err))
gb.ResponseWithError(c, err)
return
}

gb.Response(c, http.StatusOK, b)
}
1 change: 1 addition & 0 deletions internal/server/api/v1/repos/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type (

ListBranches(ctx context.Context, u *ent.User, r *ent.Repo, opt *i.ListOptions) ([]*extent.Branch, error)
GetBranch(ctx context.Context, u *ent.User, r *ent.Repo, branch string) (*extent.Branch, error)
GetDefaultBranch(ctx context.Context, u *ent.User, r *ent.Repo) (*extent.Branch, error)

ListTags(ctx context.Context, u *ent.User, r *ent.Repo, opt *i.ListOptions) ([]*extent.Tag, error)
GetTag(ctx context.Context, u *ent.User, r *ent.Repo, tag string) (*extent.Tag, error)
Expand Down
15 changes: 15 additions & 0 deletions internal/server/api/v1/repos/mock/interactor.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func NewRouter(c *RouterConfig) *gin.Engine {
repov1.GET("/:namespace/:name/commits/:sha/statuses", rm.RepoReadPerm(), api.Commits.ListStatuses)
repov1.GET("/:namespace/:name/branches", rm.RepoReadPerm(), api.Branch.List)
repov1.GET("/:namespace/:name/branches/:branch", rm.RepoReadPerm(), api.Branch.Get)
repov1.GET("/:namespace/:name/default-branch", rm.RepoReadPerm(), api.Branch.GetDefault)
repov1.GET("/:namespace/:name/tags", rm.RepoReadPerm(), api.Tag.List)
repov1.GET("/:namespace/:name/tags/:tag", rm.RepoReadPerm(), api.Tag.Get)
repov1.GET("/:namespace/:name/deployments", rm.RepoReadPerm(), api.Deployment.List)
Expand Down
24 changes: 24 additions & 0 deletions ui/src/apis/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,27 @@ export const getBranch = async (

return ret;
};

export const getDefaultBranch = async (
namespace: string,
name: string
): Promise<Branch> => {
const response = await _fetch(
`${instance}/api/v1/repos/${namespace}/${name}/default-branch`,
{
headers,
credentials: 'same-origin',
}
);

if (response.status === StatusCodes.NOT_FOUND) {
const message = await response.json().then((data) => data.message);
throw new HttpNotFoundError(message);
}

const ret: Branch = await response
.json()
.then((b: any) => mapDataToBranch(b));

return ret;
};
2 changes: 1 addition & 1 deletion ui/src/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export {
} from './deployment';
export { getConfig } from './config';
export { listCommits, getCommit, listStatuses } from './commit';
export { listBranches, getBranch } from './branch';
export { listBranches, getBranch, getDefaultBranch } from './branch';
export { listTags, getTag } from './tag';
export { listUsers, updateUser, deleteUser, getMe, getRateLimit } from './user';
export { checkSlack } from './chat';
Expand Down
19 changes: 18 additions & 1 deletion ui/src/redux/repoDeploy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
listDeployments,
listBranches,
getBranch,
getDefaultBranch,
listCommits,
getCommit,
listStatuses,
Expand Down Expand Up @@ -121,7 +122,23 @@ export const fetchBranches = createAsyncThunk<
const { namespace, name } = getState().repoDeploy;

const branches = await listBranches(namespace, name, firstPage, perPage);
return branches;

const defaultBranch = await getDefaultBranch(namespace, name);

// Add the default branch, and remove the duplicated one.
branches.unshift(defaultBranch);

const reduced = branches.reduce((acc, cur) => {
if (acc.findIndex((b) => b.name === cur.name) === -1) {
acc.push(cur);
}

return acc;
}, [] as Branch[]);

console.log(reduced);

return reduced;
});

export const checkBranch = createAsyncThunk<
Expand Down
Loading