Skip to content

Commit faf8260

Browse files
committed
Merge branch 'main' into extend-repo-config-api-add_has_code
2 parents db7ba91 + becd15f commit faf8260

File tree

6 files changed

+169
-71
lines changed

6 files changed

+169
-71
lines changed

routers/api/v1/repo/action.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,18 +1132,23 @@ func GetWorkflowRun(ctx *context.APIContext) {
11321132
// "$ref": "#/responses/notFound"
11331133

11341134
runID := ctx.PathParamInt64("run")
1135-
job, _, err := db.GetByID[actions_model.ActionRun](ctx, runID)
1135+
job, has, err := db.GetByID[actions_model.ActionRun](ctx, runID)
1136+
if err != nil {
1137+
ctx.APIErrorInternal(err)
1138+
return
1139+
}
11361140

1137-
if err != nil || job.RepoID != ctx.Repo.Repository.ID {
1138-
ctx.APIError(http.StatusNotFound, util.ErrNotExist)
1141+
if !has || job.RepoID != ctx.Repo.Repository.ID {
1142+
ctx.APIErrorNotFound(util.ErrNotExist)
1143+
return
11391144
}
11401145

1141-
convertedArtifact, err := convert.ToActionWorkflowRun(ctx, ctx.Repo.Repository, job)
1146+
convertedRun, err := convert.ToActionWorkflowRun(ctx, ctx.Repo.Repository, job)
11421147
if err != nil {
11431148
ctx.APIErrorInternal(err)
11441149
return
11451150
}
1146-
ctx.JSON(http.StatusOK, convertedArtifact)
1151+
ctx.JSON(http.StatusOK, convertedRun)
11471152
}
11481153

11491154
// ListWorkflowRunJobs Lists all jobs for a workflow run.
@@ -1237,10 +1242,15 @@ func GetWorkflowJob(ctx *context.APIContext) {
12371242
// "$ref": "#/responses/notFound"
12381243

12391244
jobID := ctx.PathParamInt64("job_id")
1240-
job, _, err := db.GetByID[actions_model.ActionRunJob](ctx, jobID)
1245+
job, has, err := db.GetByID[actions_model.ActionRunJob](ctx, jobID)
1246+
if err != nil {
1247+
ctx.APIErrorInternal(err)
1248+
return
1249+
}
12411250

1242-
if err != nil || job.RepoID != ctx.Repo.Repository.ID {
1243-
ctx.APIError(http.StatusNotFound, util.ErrNotExist)
1251+
if !has || job.RepoID != ctx.Repo.Repository.ID {
1252+
ctx.APIErrorNotFound(util.ErrNotExist)
1253+
return
12441254
}
12451255

12461256
convertedWorkflowJob, err := convert.ToActionWorkflowJob(ctx, ctx.Repo.Repository, nil, job)
@@ -1251,7 +1261,7 @@ func GetWorkflowJob(ctx *context.APIContext) {
12511261
ctx.JSON(http.StatusOK, convertedWorkflowJob)
12521262
}
12531263

1254-
// GetArtifacts Lists all artifacts for a repository.
1264+
// GetArtifactsOfRun Lists all artifacts for a repository.
12551265
func GetArtifactsOfRun(ctx *context.APIContext) {
12561266
// swagger:operation GET /repos/{owner}/{repo}/actions/runs/{run}/artifacts repository getArtifactsOfRun
12571267
// ---
@@ -1354,7 +1364,7 @@ func DeleteActionRun(ctx *context.APIContext) {
13541364
runID := ctx.PathParamInt64("run")
13551365
run, err := actions_model.GetRunByRepoAndID(ctx, ctx.Repo.Repository.ID, runID)
13561366
if errors.Is(err, util.ErrNotExist) {
1357-
ctx.APIError(http.StatusNotFound, err)
1367+
ctx.APIErrorNotFound(err)
13581368
return
13591369
} else if err != nil {
13601370
ctx.APIErrorInternal(err)

routers/api/v1/repo/repo.go

Lines changed: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -772,13 +772,8 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
772772
var units []repo_model.RepoUnit
773773
var deleteUnitTypes []unit_model.Type
774774

775-
currHasIssues := repo.UnitEnabled(ctx, unit_model.TypeIssues)
776-
newHasIssues := currHasIssues
777775
if opts.HasIssues != nil {
778-
newHasIssues = *opts.HasIssues
779-
}
780-
if currHasIssues || newHasIssues {
781-
if newHasIssues && opts.ExternalTracker != nil && !unit_model.TypeExternalTracker.UnitGlobalDisabled() {
776+
if *opts.HasIssues && opts.ExternalTracker != nil && !unit_model.TypeExternalTracker.UnitGlobalDisabled() {
782777
// Check that values are valid
783778
if !validation.IsValidExternalURL(opts.ExternalTracker.ExternalTrackerURL) {
784779
err := errors.New("External tracker URL not valid")
@@ -802,7 +797,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
802797
},
803798
})
804799
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeIssues)
805-
} else if newHasIssues && opts.ExternalTracker == nil && !unit_model.TypeIssues.UnitGlobalDisabled() {
800+
} else if *opts.HasIssues && opts.ExternalTracker == nil && !unit_model.TypeIssues.UnitGlobalDisabled() {
806801
// Default to built-in tracker
807802
var config *repo_model.IssuesConfig
808803

@@ -829,7 +824,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
829824
Config: config,
830825
})
831826
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeExternalTracker)
832-
} else if !newHasIssues {
827+
} else if !*opts.HasIssues {
833828
if !unit_model.TypeExternalTracker.UnitGlobalDisabled() {
834829
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeExternalTracker)
835830
}
@@ -839,13 +834,8 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
839834
}
840835
}
841836

842-
currHasWiki := repo.UnitEnabled(ctx, unit_model.TypeWiki)
843-
newHasWiki := currHasWiki
844837
if opts.HasWiki != nil {
845-
newHasWiki = *opts.HasWiki
846-
}
847-
if currHasWiki || newHasWiki {
848-
if newHasWiki && opts.ExternalWiki != nil && !unit_model.TypeExternalWiki.UnitGlobalDisabled() {
838+
if *opts.HasWiki && opts.ExternalWiki != nil && !unit_model.TypeExternalWiki.UnitGlobalDisabled() {
849839
// Check that values are valid
850840
if !validation.IsValidExternalURL(opts.ExternalWiki.ExternalWikiURL) {
851841
err := errors.New("External wiki URL not valid")
@@ -861,15 +851,15 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
861851
},
862852
})
863853
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeWiki)
864-
} else if newHasWiki && opts.ExternalWiki == nil && !unit_model.TypeWiki.UnitGlobalDisabled() {
854+
} else if *opts.HasWiki && opts.ExternalWiki == nil && !unit_model.TypeWiki.UnitGlobalDisabled() {
865855
config := &repo_model.UnitConfig{}
866856
units = append(units, repo_model.RepoUnit{
867857
RepoID: repo.ID,
868858
Type: unit_model.TypeWiki,
869859
Config: config,
870860
})
871861
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeExternalWiki)
872-
} else if !newHasWiki {
862+
} else if !*opts.HasWiki {
873863
if !unit_model.TypeExternalWiki.UnitGlobalDisabled() {
874864
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeExternalWiki)
875865
}
@@ -879,30 +869,20 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
879869
}
880870
}
881871

882-
currHasCode := repo.UnitEnabled(ctx, unit_model.TypeCode)
883-
newHasCode := currHasCode
884-
if opts.HasCode != nil {
885-
newHasCode = *opts.HasCode
886-
}
887-
if currHasCode || newHasCode {
888-
if newHasCode && !unit_model.TypeCode.UnitGlobalDisabled() {
889-
units = append(units, repo_model.RepoUnit{
890-
RepoID: repo.ID,
891-
Type: unit_model.TypeCode,
892-
Config: &repo_model.UnitConfig{},
893-
})
894-
} else if !newHasCode && !unit_model.TypeCode.UnitGlobalDisabled() {
895-
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeCode)
896-
}
897-
}
898-
899-
currHasPullRequests := repo.UnitEnabled(ctx, unit_model.TypePullRequests)
900-
newHasPullRequests := currHasPullRequests
901-
if opts.HasPullRequests != nil {
902-
newHasPullRequests = *opts.HasPullRequests
903-
}
904-
if currHasPullRequests || newHasPullRequests {
905-
if newHasPullRequests && !unit_model.TypePullRequests.UnitGlobalDisabled() {
872+
if opts.HasCode != nil && !unit_model.TypeCode.UnitGlobalDisabled() {
873+
if *opts.HasCode {
874+
units = append(units, repo_model.RepoUnit{
875+
RepoID: repo.ID,
876+
Type: unit_model.TypeCode,
877+
Config: &repo_model.UnitConfig{},
878+
})
879+
} else {
880+
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeCode)
881+
}
882+
}
883+
884+
if opts.HasPullRequests != nil && !unit_model.TypePullRequests.UnitGlobalDisabled() {
885+
if *opts.HasPullRequests {
906886
// We do allow setting individual PR settings through the API, so
907887
// we get the config settings and then set them
908888
// if those settings were provided in the opts.
@@ -970,18 +950,13 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
970950
Type: unit_model.TypePullRequests,
971951
Config: config,
972952
})
973-
} else if !newHasPullRequests && !unit_model.TypePullRequests.UnitGlobalDisabled() {
953+
} else {
974954
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypePullRequests)
975955
}
976956
}
977957

978-
currHasProjects := repo.UnitEnabled(ctx, unit_model.TypeProjects)
979-
newHasProjects := currHasProjects
980-
if opts.HasProjects != nil {
981-
newHasProjects = *opts.HasProjects
982-
}
983-
if currHasProjects || newHasProjects {
984-
if newHasProjects && !unit_model.TypeProjects.UnitGlobalDisabled() {
958+
if opts.HasProjects != nil && !unit_model.TypeProjects.UnitGlobalDisabled() {
959+
if *opts.HasProjects {
985960
unit, err := repo.GetUnit(ctx, unit_model.TypeProjects)
986961
var config *repo_model.ProjectsConfig
987962
if err != nil {
@@ -1001,7 +976,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
1001976
Type: unit_model.TypeProjects,
1002977
Config: config,
1003978
})
1004-
} else if !newHasProjects && !unit_model.TypeProjects.UnitGlobalDisabled() {
979+
} else {
1005980
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeProjects)
1006981
}
1007982
}

tests/integration/api_actions_delete_run_test.go renamed to tests/integration/api_actions_run_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,44 @@ import (
1818
"github.com/stretchr/testify/assert"
1919
)
2020

21+
func TestAPIActionsGetWorkflowRun(t *testing.T) {
22+
defer prepareTestEnvActionsArtifacts(t)()
23+
24+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
25+
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
26+
session := loginUser(t, user.Name)
27+
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
28+
29+
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/actions/runs/802802", repo.FullName())).
30+
AddTokenAuth(token)
31+
MakeRequest(t, req, http.StatusNotFound)
32+
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/actions/runs/802", repo.FullName())).
33+
AddTokenAuth(token)
34+
MakeRequest(t, req, http.StatusNotFound)
35+
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/actions/runs/803", repo.FullName())).
36+
AddTokenAuth(token)
37+
MakeRequest(t, req, http.StatusOK)
38+
}
39+
40+
func TestAPIActionsGetWorkflowJob(t *testing.T) {
41+
defer prepareTestEnvActionsArtifacts(t)()
42+
43+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
44+
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
45+
session := loginUser(t, user.Name)
46+
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
47+
48+
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/actions/jobs/198198", repo.FullName())).
49+
AddTokenAuth(token)
50+
MakeRequest(t, req, http.StatusNotFound)
51+
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/actions/jobs/198", repo.FullName())).
52+
AddTokenAuth(token)
53+
MakeRequest(t, req, http.StatusOK)
54+
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/actions/jobs/196", repo.FullName())).
55+
AddTokenAuth(token)
56+
MakeRequest(t, req, http.StatusNotFound)
57+
}
58+
2159
func TestAPIActionsDeleteRunCheckPermission(t *testing.T) {
2260
defer prepareTestEnvActionsArtifacts(t)()
2361

tests/integration/api_repo_edit_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ func getRepoEditOptionFromRepo(repo *repo_model.Repository) *api.EditRepoOption
5353
hasWiki = true
5454
} else if unit, err := repo.GetUnit(db.DefaultContext, unit_model.TypeExternalWiki); err == nil {
5555
hasWiki = true
56-
config := unit.ExternalWikiConfig()
5756
externalWiki = &api.ExternalWiki{
58-
ExternalWikiURL: config.ExternalWikiURL,
57+
ExternalWikiURL: unit.ExternalWikiConfig().ExternalWikiURL,
5958
}
6059
}
6160
defaultBranch := repo.DefaultBranch

tests/integration/editor_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func TestEditor(t *testing.T) {
5656

5757
func testEditorCreateFile(t *testing.T) {
5858
session := loginUser(t, "user2")
59-
testCreateFile(t, session, "user2", "repo1", "master", "test.txt", "Content")
59+
testCreateFile(t, session, "user2", "repo1", "master", "", "test.txt", "Content")
6060
testEditorActionPostRequestError(t, session, "/user2/repo1/_new/master/", map[string]string{
6161
"tree_path": "test.txt",
6262
"commit_choice": "direct",
@@ -69,11 +69,16 @@ func testEditorCreateFile(t *testing.T) {
6969
}, `Branch "master" already exists in this repository.`)
7070
}
7171

72-
func testCreateFile(t *testing.T, session *TestSession, user, repo, branch, filePath, content string) {
73-
testEditorActionEdit(t, session, user, repo, "_new", branch, "", map[string]string{
74-
"tree_path": filePath,
75-
"content": content,
76-
"commit_choice": "direct",
72+
func testCreateFile(t *testing.T, session *TestSession, user, repo, baseBranchName, newBranchName, filePath, content string) {
73+
commitChoice := "direct"
74+
if newBranchName != "" && newBranchName != baseBranchName {
75+
commitChoice = "commit-to-new-branch"
76+
}
77+
testEditorActionEdit(t, session, user, repo, "_new", baseBranchName, "", map[string]string{
78+
"tree_path": filePath,
79+
"content": content,
80+
"commit_choice": commitChoice,
81+
"new_branch_name": newBranchName,
7782
})
7883
}
7984

0 commit comments

Comments
 (0)