Skip to content

Commit

Permalink
Merge branch 'main' into actionbg
Browse files Browse the repository at this point in the history
  • Loading branch information
silverwind authored Mar 22, 2024
2 parents 37207b9 + 226231e commit 1750756
Show file tree
Hide file tree
Showing 17 changed files with 154 additions and 53 deletions.
9 changes: 4 additions & 5 deletions models/asymkey/ssh_key_authorized_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ func RegeneratePublicKeys(ctx context.Context, t io.StringWriter) error {
if err != nil {
return err
}
defer f.Close()

scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
Expand All @@ -148,15 +150,12 @@ func RegeneratePublicKeys(ctx context.Context, t io.StringWriter) error {
}
_, err = t.WriteString(line + "\n")
if err != nil {
f.Close()
return err
}
}
err = scanner.Err()
if err != nil {
return fmt.Errorf("scan: %w", err)
if err = scanner.Err(); err != nil {
return fmt.Errorf("RegeneratePublicKeys scan: %w", err)
}
f.Close()
}
return nil
}
2 changes: 1 addition & 1 deletion modules/actions/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func ReadLogs(ctx context.Context, inStorage bool, filename string, offset, limi
}

if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("scan: %w", err)
return nil, fmt.Errorf("ReadLogs scan: %w", err)
}

return rows, nil
Expand Down
5 changes: 2 additions & 3 deletions modules/git/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,8 @@ func (c *Commit) GetSubModules() (*ObjectCache, error) {
}
}
}
err = scanner.Err()
if err != nil {
return nil, fmt.Errorf("scan: %w", err)
if err = scanner.Err(); err != nil {
return nil, fmt.Errorf("GetSubModules scan: %w", err)
}

return c.submoduleCache, nil
Expand Down
6 changes: 3 additions & 3 deletions modules/git/repo_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string)
}
}
}
err = scanner.Err()
if err != nil {
return fmt.Errorf("scan: %w", err)
if err = scanner.Err(); err != nil {
_ = stdoutReader.Close()
return fmt.Errorf("GetCodeActivityStats scan: %w", err)
}
a := make([]*CodeActivityAuthor, 0, len(authors))
for _, v := range authors {
Expand Down
5 changes: 2 additions & 3 deletions modules/markup/csv/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,8 @@ func (Renderer) fallbackRender(input io.Reader, tmpBlock *bufio.Writer) error {
return err
}
}
err = scan.Err()
if err != nil {
return fmt.Errorf("scan: %w", err)
if err = scan.Err(); err != nil {
return fmt.Errorf("fallbackRender scan: %w", err)
}

_, err = tmpBlock.WriteString("</pre>")
Expand Down
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3626,6 +3626,7 @@ runs.scheduled = Scheduled
runs.pushed_by = pushed by
runs.invalid_workflow_helper = Workflow config file is invalid. Please check your config file: %s
runs.no_matching_online_runner_helper = No matching online runner with label: %s
runs.no_job_without_needs = The workflow must contain at least one job without dependencies.
runs.actor = Actor
runs.status = Status
runs.actors_no_select = All actors
Expand Down
10 changes: 9 additions & 1 deletion routers/web/repo/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,13 @@ func List(ctx *context.Context) {
workflows = append(workflows, workflow)
continue
}
// Check whether have matching runner
// The workflow must contain at least one job without "needs". Otherwise, a deadlock will occur and no jobs will be able to run.
hasJobWithoutNeeds := false
// Check whether have matching runner and a job without "needs"
for _, j := range wf.Jobs {
if !hasJobWithoutNeeds && len(j.Needs()) == 0 {
hasJobWithoutNeeds = true
}
runsOnList := j.RunsOn()
for _, ro := range runsOnList {
if strings.Contains(ro, "${{") {
Expand All @@ -123,6 +128,9 @@ func List(ctx *context.Context) {
break
}
}
if !hasJobWithoutNeeds {
workflow.ErrMsg = ctx.Locale.TrString("actions.runs.no_job_without_needs")
}
workflows = append(workflows, workflow)
}
}
Expand Down
26 changes: 21 additions & 5 deletions routers/web/repo/actions/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,25 @@ func Rerun(ctx *context_module.Context) {
return
}

if jobIndexStr != "" {
jobs = []*actions_model.ActionRunJob{job}
if jobIndexStr == "" { // rerun all jobs
for _, j := range jobs {
// if the job has needs, it should be set to "blocked" status to wait for other jobs
shouldBlock := len(j.Needs) > 0
if err := rerunJob(ctx, j, shouldBlock); err != nil {
ctx.Error(http.StatusInternalServerError, err.Error())
return
}
}
ctx.JSON(http.StatusOK, struct{}{})
return
}

for _, j := range jobs {
if err := rerunJob(ctx, j); err != nil {
rerunJobs := actions_service.GetAllRerunJobs(job, jobs)

for _, j := range rerunJobs {
// jobs other than the specified one should be set to "blocked" status
shouldBlock := j.JobID != job.JobID
if err := rerunJob(ctx, j, shouldBlock); err != nil {
ctx.Error(http.StatusInternalServerError, err.Error())
return
}
Expand All @@ -317,14 +330,17 @@ func Rerun(ctx *context_module.Context) {
ctx.JSON(http.StatusOK, struct{}{})
}

func rerunJob(ctx *context_module.Context, job *actions_model.ActionRunJob) error {
func rerunJob(ctx *context_module.Context, job *actions_model.ActionRunJob, shouldBlock bool) error {
status := job.Status
if !status.IsDone() {
return nil
}

job.TaskID = 0
job.Status = actions_model.StatusWaiting
if shouldBlock {
job.Status = actions_model.StatusBlocked
}
job.Started = 0
job.Stopped = 0

Expand Down
5 changes: 2 additions & 3 deletions routers/web/repo/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -980,9 +980,8 @@ func getExcerptLines(commit *git.Commit, filePath string, idxLeft, idxRight, chu
}
diffLines = append(diffLines, diffLine)
}
err = scanner.Err()
if err != nil {
return nil, fmt.Errorf("scan: %w", err)
if err = scanner.Err(); err != nil {
return nil, fmt.Errorf("getExcerptLines scan: %w", err)
}
return diffLines, nil
}
4 changes: 2 additions & 2 deletions routers/web/repo/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,9 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
ctx.Error(http.StatusInternalServerError, err.Error())
}
} else if models.IsErrCommitIDDoesNotMatch(err) {
ctx.RenderWithErr(ctx.Tr("repo.editor.commit_id_not_matching", ctx.Repo.RepoLink+"/compare/"+util.PathEscapeSegments(form.LastCommit)+"..."+util.PathEscapeSegments(ctx.Repo.CommitID)), tplEditFile, &form)
ctx.RenderWithErr(ctx.Tr("repo.editor.commit_id_not_matching"), tplEditFile, &form)
} else if git.IsErrPushOutOfDate(err) {
ctx.RenderWithErr(ctx.Tr("repo.editor.push_out_of_date", ctx.Repo.RepoLink+"/compare/"+util.PathEscapeSegments(form.LastCommit)+"..."+util.PathEscapeSegments(form.NewBranchName)), tplEditFile, &form)
ctx.RenderWithErr(ctx.Tr("repo.editor.push_out_of_date"), tplEditFile, &form)
} else if git.IsErrPushRejected(err) {
errPushRej := err.(*git.ErrPushRejected)
if len(errPushRej.Message) == 0 {
Expand Down
38 changes: 38 additions & 0 deletions services/actions/rerun.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package actions

import (
actions_model "code.gitea.io/gitea/models/actions"
"code.gitea.io/gitea/modules/container"
)

// GetAllRerunJobs get all jobs that need to be rerun when job should be rerun
func GetAllRerunJobs(job *actions_model.ActionRunJob, allJobs []*actions_model.ActionRunJob) []*actions_model.ActionRunJob {
rerunJobs := []*actions_model.ActionRunJob{job}
rerunJobsIDSet := make(container.Set[string])
rerunJobsIDSet.Add(job.JobID)

for {
found := false
for _, j := range allJobs {
if rerunJobsIDSet.Contains(j.JobID) {
continue
}
for _, need := range j.Needs {
if rerunJobsIDSet.Contains(need) {
found = true
rerunJobs = append(rerunJobs, j)
rerunJobsIDSet.Add(j.JobID)
break
}
}
}
if !found {
break
}
}

return rerunJobs
}
48 changes: 48 additions & 0 deletions services/actions/rerun_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package actions

import (
"testing"

actions_model "code.gitea.io/gitea/models/actions"

"github.com/stretchr/testify/assert"
)

func TestGetAllRerunJobs(t *testing.T) {
job1 := &actions_model.ActionRunJob{JobID: "job1"}
job2 := &actions_model.ActionRunJob{JobID: "job2", Needs: []string{"job1"}}
job3 := &actions_model.ActionRunJob{JobID: "job3", Needs: []string{"job2"}}
job4 := &actions_model.ActionRunJob{JobID: "job4", Needs: []string{"job2", "job3"}}

jobs := []*actions_model.ActionRunJob{job1, job2, job3, job4}

testCases := []struct {
job *actions_model.ActionRunJob
rerunJobs []*actions_model.ActionRunJob
}{
{
job1,
[]*actions_model.ActionRunJob{job1, job2, job3, job4},
},
{
job2,
[]*actions_model.ActionRunJob{job2, job3, job4},
},
{
job3,
[]*actions_model.ActionRunJob{job3, job4},
},
{
job4,
[]*actions_model.ActionRunJob{job4},
},
}

for _, tc := range testCases {
rerunJobs := GetAllRerunJobs(tc.job, jobs)
assert.ElementsMatch(t, tc.rerunJobs, rerunJobs)
}
}
9 changes: 4 additions & 5 deletions services/asymkey/ssh_key_authorized_principals.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ func regeneratePrincipalKeys(ctx context.Context, t io.StringWriter) error {
if err != nil {
return err
}
defer f.Close()

scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
Expand All @@ -118,15 +120,12 @@ func regeneratePrincipalKeys(ctx context.Context, t io.StringWriter) error {
}
_, err = t.WriteString(line + "\n")
if err != nil {
f.Close()
return err
}
}
err = scanner.Err()
if err != nil {
return fmt.Errorf("scan: %w", err)
if err = scanner.Err(); err != nil {
return fmt.Errorf("regeneratePrincipalKeys scan: %w", err)
}
f.Close()
}
return nil
}
6 changes: 3 additions & 3 deletions services/doctor/authorizedkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ func checkAuthorizedKeys(ctx context.Context, logger log.Logger, autofix bool) e
}
linesInAuthorizedKeys.Add(line)
}
err = scanner.Err()
if err != nil {
if err = scanner.Err(); err != nil {
return fmt.Errorf("scan: %w", err)
}
f.Close()
// although there is a "defer close" above, here close explicitly before the generating, because it needs to open the file for writing again
_ = f.Close()

// now we regenerate and check if there are any lines missing
regenerated := &bytes.Buffer{}
Expand Down
20 changes: 9 additions & 11 deletions templates/repo/issue/view_content/pull.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -374,17 +374,15 @@
*/}}
{{if and $.StillCanManualMerge (not $showGeneralMergeForm)}}
<div class="divider"></div>
<div class="ui form">
<form action="{{.Link}}/merge" method="post">
{{.CsrfTokenHtml}}
<div class="field">
<input type="text" name="merge_commit_id" placeholder="{{ctx.Locale.Tr "repo.pulls.merge_commit_id"}}">
</div>
<button class="ui red button" type="submit" name="do" value="manually-merged">
{{ctx.Locale.Tr "repo.pulls.merge_manually"}}
</button>
</form>
</div>
<form class="ui form form-fetch-action" action="{{.Link}}/merge" method="post">{{/* another similar form is in PullRequestMergeForm.vue*/}}
{{.CsrfTokenHtml}}
<div class="field">
<input type="text" name="merge_commit_id" placeholder="{{ctx.Locale.Tr "repo.pulls.merge_commit_id"}}">
</div>
<button class="ui red button" type="submit" name="do" value="manually-merged">
{{ctx.Locale.Tr "repo.pulls.merge_manually"}}
</button>
</form>
{{end}}

{{if and .Issue.PullRequest.HeadRepo (not .Issue.PullRequest.HasMerged) (not .Issue.IsClosed)}}
Expand Down
12 changes: 4 additions & 8 deletions templates/status/404.tmpl
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
{{template "base/head" .}}
<div role="main" aria-label="{{.Title}}" class="page-content ui container center tw-w-screen {{if .IsRepo}}repository{{end}}">
<div role="main" aria-label="{{.Title}}" class="page-content {{if .IsRepo}}repository{{end}}">
{{if .IsRepo}}{{template "repo/header" .}}{{end}}
<div class="ui container center">
<p style="margin-top: 100px"><img src="{{AssetUrlPrefix}}/img/404.png" alt="404"></p>
<div class="ui container tw-text-center">
<img class="tw-max-w-[80vw] tw-py-16" src="{{AssetUrlPrefix}}/img/404.png" alt="404">
<p>{{if .NotFoundPrompt}}{{.NotFoundPrompt}}{{else}}{{ctx.Locale.Tr "error404"}}{{end}}</p>
{{if .NotFoundGoBackURL}}<a class="ui button green" href="{{.NotFoundGoBackURL}}">{{ctx.Locale.Tr "go_back"}}</a>{{end}}

<div class="divider"></div>
<br>
{{if .ShowFooterVersion}}<p>{{ctx.Locale.Tr "admin.config.app_ver"}}: {{AppVer}}</p>{{end}}
{{if .NotFoundGoBackURL}}<a class="ui button" href="{{.NotFoundGoBackURL}}">{{ctx.Locale.Tr "go_back"}}</a>{{end}}
</div>
</div>
{{template "base/footer" .}}
1 change: 1 addition & 0 deletions web_src/js/components/PullRequestMergeForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export default {
<!-- eslint-disable-next-line vue/no-v-html -->
<div v-if="mergeForm.hasPendingPullRequestMerge" v-html="mergeForm.hasPendingPullRequestMergeTip" class="ui info message"/>
<!-- another similar form is in pull.tmpl (manual merge)-->
<form class="ui form form-fetch-action" v-if="showActionForm" :action="mergeForm.baseLink+'/merge'" method="post">
<input type="hidden" name="_csrf" :value="csrfToken">
<input type="hidden" name="head_commit_id" v-model="mergeForm.pullHeadCommitID">
Expand Down

0 comments on commit 1750756

Please sign in to comment.