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

Commit c197aaf

Browse files
author
Noah Lee
authored
Place the default branch at the top of options (#530)
* feat: Add GetDefaultBranch method to BranchSCM interface * feat: Add GetDefaultBranch method to BranchSCM interface * feat: Add getDefaultBranch method to branch API * feat: Add GetDefaultBranch method to BranchSCM interface
1 parent ded8e1b commit c197aaf

File tree

10 files changed

+130
-2
lines changed

10 files changed

+130
-2
lines changed

internal/interactor/interface.go

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ type (
6161
BranchSCM interface {
6262
ListBranches(ctx context.Context, u *ent.User, r *ent.Repo, opt *ListOptions) ([]*extent.Branch, error)
6363
GetBranch(ctx context.Context, u *ent.User, r *ent.Repo, branch string) (*extent.Branch, error)
64+
GetDefaultBranch(ctx context.Context, u *ent.User, r *ent.Repo) (*extent.Branch, error)
6465
}
6566

6667
TagSCM interface {

internal/interactor/mock/pkg.go

+30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/pkg/github/repos.go

+20
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,26 @@ func (g *Github) GetBranch(ctx context.Context, u *ent.User, r *ent.Repo, branch
157157
return mapGithubBranchToBranch(b), nil
158158
}
159159

160+
func (g *Github) GetDefaultBranch(ctx context.Context, u *ent.User, r *ent.Repo) (*extent.Branch, error) {
161+
rr, res, err := g.Client(ctx, u.Token).Repositories.Get(ctx, r.Namespace, r.Name)
162+
if res.StatusCode == http.StatusNotFound {
163+
return nil, e.NewErrorWithMessage(e.ErrorCodeEntityNotFound, "The default branch is not found.", err)
164+
} else if err != nil {
165+
return nil, e.NewError(e.ErrorCodeInternalError, err)
166+
}
167+
168+
b, res, err := g.Client(ctx, u.Token).
169+
Repositories.
170+
GetBranch(ctx, r.Namespace, r.Name, *rr.DefaultBranch, false)
171+
if res.StatusCode == http.StatusNotFound {
172+
return nil, e.NewErrorWithMessage(e.ErrorCodeEntityNotFound, "The default branch is not found.", err)
173+
} else if err != nil {
174+
return nil, e.NewError(e.ErrorCodeInternalError, err)
175+
}
176+
177+
return mapGithubBranchToBranch(b), nil
178+
}
179+
160180
// ListTags list up tags as ordered by commit date.
161181
// Github GraphQL explore - https://docs.github.com/en/graphql/overview/explorer
162182
func (g *Github) ListTags(ctx context.Context, u *ent.User, r *ent.Repo, opt *i.ListOptions) ([]*extent.Tag, error) {

internal/server/api/v1/repos/branch_get.go

+19
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,22 @@ func (s *BranchAPI) Get(c *gin.Context) {
3232

3333
gb.Response(c, http.StatusOK, b)
3434
}
35+
36+
func (s *BranchAPI) GetDefault(c *gin.Context) {
37+
ctx := c.Request.Context()
38+
39+
uv, _ := c.Get(gb.KeyUser)
40+
u := uv.(*ent.User)
41+
42+
rv, _ := c.Get(KeyRepo)
43+
repo := rv.(*ent.Repo)
44+
45+
b, err := s.i.GetDefaultBranch(ctx, u, repo)
46+
if err != nil {
47+
s.log.Check(gb.GetZapLogLevel(err), "Failed to get the branch.").Write(zap.Error(err))
48+
gb.ResponseWithError(c, err)
49+
return
50+
}
51+
52+
gb.Response(c, http.StatusOK, b)
53+
}

internal/server/api/v1/repos/interface.go

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type (
5555

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

5960
ListTags(ctx context.Context, u *ent.User, r *ent.Repo, opt *i.ListOptions) ([]*extent.Tag, error)
6061
GetTag(ctx context.Context, u *ent.User, r *ent.Repo, tag string) (*extent.Tag, error)

internal/server/api/v1/repos/mock/interactor.go

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/server/router.go

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ func NewRouter(c *RouterConfig) *gin.Engine {
133133
repov1.GET("/:namespace/:name/commits/:sha/statuses", rm.RepoReadPerm(), api.Commits.ListStatuses)
134134
repov1.GET("/:namespace/:name/branches", rm.RepoReadPerm(), api.Branch.List)
135135
repov1.GET("/:namespace/:name/branches/:branch", rm.RepoReadPerm(), api.Branch.Get)
136+
repov1.GET("/:namespace/:name/default-branch", rm.RepoReadPerm(), api.Branch.GetDefault)
136137
repov1.GET("/:namespace/:name/tags", rm.RepoReadPerm(), api.Tag.List)
137138
repov1.GET("/:namespace/:name/tags/:tag", rm.RepoReadPerm(), api.Tag.Get)
138139
repov1.GET("/:namespace/:name/deployments", rm.RepoReadPerm(), api.Deployment.List)

ui/src/apis/branch.ts

+24
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,27 @@ export const getBranch = async (
5252

5353
return ret;
5454
};
55+
56+
export const getDefaultBranch = async (
57+
namespace: string,
58+
name: string
59+
): Promise<Branch> => {
60+
const response = await _fetch(
61+
`${instance}/api/v1/repos/${namespace}/${name}/default-branch`,
62+
{
63+
headers,
64+
credentials: 'same-origin',
65+
}
66+
);
67+
68+
if (response.status === StatusCodes.NOT_FOUND) {
69+
const message = await response.json().then((data) => data.message);
70+
throw new HttpNotFoundError(message);
71+
}
72+
73+
const ret: Branch = await response
74+
.json()
75+
.then((b: any) => mapDataToBranch(b));
76+
77+
return ret;
78+
};

ui/src/apis/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export {
2020
} from './deployment';
2121
export { getConfig } from './config';
2222
export { listCommits, getCommit, listStatuses } from './commit';
23-
export { listBranches, getBranch } from './branch';
23+
export { listBranches, getBranch, getDefaultBranch } from './branch';
2424
export { listTags, getTag } from './tag';
2525
export { listUsers, updateUser, deleteUser, getMe, getRateLimit } from './user';
2626
export { checkSlack } from './chat';

ui/src/redux/repoDeploy.tsx

+18-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
listDeployments,
2424
listBranches,
2525
getBranch,
26+
getDefaultBranch,
2627
listCommits,
2728
getCommit,
2829
listStatuses,
@@ -121,7 +122,23 @@ export const fetchBranches = createAsyncThunk<
121122
const { namespace, name } = getState().repoDeploy;
122123

123124
const branches = await listBranches(namespace, name, firstPage, perPage);
124-
return branches;
125+
126+
const defaultBranch = await getDefaultBranch(namespace, name);
127+
128+
// Add the default branch, and remove the duplicated one.
129+
branches.unshift(defaultBranch);
130+
131+
const reduced = branches.reduce((acc, cur) => {
132+
if (acc.findIndex((b) => b.name === cur.name) === -1) {
133+
acc.push(cur);
134+
}
135+
136+
return acc;
137+
}, [] as Branch[]);
138+
139+
console.log(reduced);
140+
141+
return reduced;
125142
});
126143

127144
export const checkBranch = createAsyncThunk<

0 commit comments

Comments
 (0)