From 166890451377c76d8a2a122a12fb27dd15a5e822 Mon Sep 17 00:00:00 2001 From: "Panagiotis \"Ivory\" Vasilopoulos" Date: Wed, 14 Feb 2024 12:12:45 +0100 Subject: [PATCH 1/4] [UI] Actions: Link to Workflow in View --- options/locale/locale_en-US.ini | 1 + routers/web/repo/actions/view.go | 17 ++++++++--- templates/repo/actions/view.tmpl | 2 ++ tests/integration/actions_route_test.go | 36 ++++++++++++++++++------ web_src/js/components/RepoActionView.vue | 13 +++++++-- 5 files changed, 54 insertions(+), 15 deletions(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index bdae9a29ac..af046f6a29 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -3627,6 +3627,7 @@ runs.all_workflows = All Workflows runs.commit = Commit runs.scheduled = Scheduled runs.pushed_by = pushed by +runs.workflow = Workflow 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.actor = Actor diff --git a/routers/web/repo/actions/view.go b/routers/web/repo/actions/view.go index ba2e63c3cc..99ad4356b5 100644 --- a/routers/web/repo/actions/view.go +++ b/routers/web/repo/actions/view.go @@ -1,4 +1,5 @@ // Copyright 2022 The Gitea Authors. All rights reserved. +// Copyright 2024 The Forgejo Authors. All rights reserved. // SPDX-License-Identifier: MIT package actions @@ -35,13 +36,19 @@ func View(ctx *context_module.Context) { ctx.Data["PageIsActions"] = true runIndex := ctx.ParamsInt64("run") jobIndex := ctx.ParamsInt64("job") + + job, _ := getRunJobs(ctx, runIndex, jobIndex) + if ctx.Written() { + return + } + + workflowName := job.Run.WorkflowID + ctx.Data["RunIndex"] = runIndex ctx.Data["JobIndex"] = jobIndex ctx.Data["ActionsURL"] = ctx.Repo.RepoLink + "/actions" - - if getRunJobs(ctx, runIndex, jobIndex); ctx.Written() { - return - } + ctx.Data["WorkflowName"] = workflowName + ctx.Data["WorkflowURL"] = ctx.Repo.RepoLink + "/actions?workflow=" + workflowName ctx.HTML(http.StatusOK, tplViewActions) } @@ -130,6 +137,7 @@ type ViewJob struct { type ViewCommit struct { LocaleCommit string `json:"localeCommit"` LocalePushedBy string `json:"localePushedBy"` + LocaleWorkflow string `json:"localeWorkflow"` ShortSha string `json:"shortSHA"` Link string `json:"link"` Pusher ViewUser `json:"pusher"` @@ -211,6 +219,7 @@ func ViewPost(ctx *context_module.Context) { resp.State.Run.Commit = ViewCommit{ LocaleCommit: ctx.Locale.TrString("actions.runs.commit"), LocalePushedBy: ctx.Locale.TrString("actions.runs.pushed_by"), + LocaleWorkflow: ctx.Locale.TrString("actions.runs.workflow"), ShortSha: base.ShortSha(run.CommitSHA), Link: fmt.Sprintf("%s/commit/%s", run.Repo.Link(), run.CommitSHA), Pusher: pusher, diff --git a/templates/repo/actions/view.tmpl b/templates/repo/actions/view.tmpl index 6b07e7000a..1cade96f01 100644 --- a/templates/repo/actions/view.tmpl +++ b/templates/repo/actions/view.tmpl @@ -6,6 +6,8 @@ data-run-index="{{.RunIndex}}" data-job-index="{{.JobIndex}}" data-actions-url="{{.ActionsURL}}" + data-workflow-name="{{.WorkflowName}}" + data-workflow-url="{{.WorkflowURL}}" data-locale-approve="{{ctx.Locale.Tr "repo.diff.review.approve"}}" data-locale-cancel="{{ctx.Locale.Tr "cancel"}}" data-locale-rerun="{{ctx.Locale.Tr "rerun"}}" diff --git a/tests/integration/actions_route_test.go b/tests/integration/actions_route_test.go index c941fca2e5..aca67a40c0 100644 --- a/tests/integration/actions_route_test.go +++ b/tests/integration/actions_route_test.go @@ -27,7 +27,7 @@ func TestActionsWebRouteLatestWorkflowRun(t *testing.T) { user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) // create the repo - repo, _, f := CreateDeclarativeRepo(t, user2, "", + repo, _, f := CreateDeclarativeRepo(t, user2, "actionsTestRepo", []unit_model.Type{unit_model.TypeActions}, nil, []*files_service.ChangeRepoFile{ { @@ -44,17 +44,17 @@ func TestActionsWebRouteLatestWorkflowRun(t *testing.T) { ) defer f() + // helpers + getWorkflowRunRedirectURI := func(workflow string) string { + req := NewRequest(t, "GET", fmt.Sprintf("%s/actions/workflows/%s/runs/latest", repo.HTMLURL(), workflow)) + resp := MakeRequest(t, req, http.StatusTemporaryRedirect) + + return resp.Header().Get("Location") + } + t.Run("valid workflows", func(t *testing.T) { defer tests.PrintCurrentTest(t)() - // helpers - getWorkflowRunRedirectURI := func(workflow string) string { - req := NewRequest(t, "GET", fmt.Sprintf("%s/actions/workflows/%s/runs/latest", repo.HTMLURL(), workflow)) - resp := MakeRequest(t, req, http.StatusTemporaryRedirect) - - return resp.Header().Get("Location") - } - // two runs have been created assert.Equal(t, 2, unittest.GetCount(t, &actions_model.ActionRun{RepoID: repo.ID})) @@ -77,6 +77,24 @@ func TestActionsWebRouteLatestWorkflowRun(t *testing.T) { assert.Equal(t, workflowTwoURI, workflowTwo.HTMLURL()) }) + t.Run("check if workflow page shows file name", func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + + // Get the redirect URI + workflow := "workflow-1.yml" + workflowOneURI := getWorkflowRunRedirectURI(workflow) + + // Fetch the page that shows information about the run initiated by "workflow-1.yml". + // routers/web/repo/actions/view.go: data-workflow-url is constructed using data-workflow-name. + req := NewRequest(t, "GET", workflowOneURI) + resp := MakeRequest(t, req, http.StatusOK) + htmlDoc := NewHTMLParser(t, resp.Body) + + // Verify that URL of the workflow is shown correctly. + rightURL := fmt.Sprintf("/user2/actionsTestRepo/actions?workflow=%s", workflow) + htmlDoc.AssertElement(t, fmt.Sprintf("#repo-action-view[data-workflow-url=\"%s\"]", rightURL), true) + }) + t.Run("existing workflow, non-existent branch", func(t *testing.T) { defer tests.PrintCurrentTest(t)() diff --git a/web_src/js/components/RepoActionView.vue b/web_src/js/components/RepoActionView.vue index 797869b78c..6155fb22bd 100644 --- a/web_src/js/components/RepoActionView.vue +++ b/web_src/js/components/RepoActionView.vue @@ -17,6 +17,8 @@ const sfc = { runIndex: String, jobIndex: String, actionsURL: String, + workflowName: String, + workflowURL: String, locale: Object, }, @@ -56,6 +58,7 @@ const sfc = { commit: { localeCommit: '', localePushedBy: '', + localeWorkflow: '', shortSHA: '', link: '', pusher: { @@ -324,6 +327,8 @@ export function initRepositoryActionView() { runIndex: el.getAttribute('data-run-index'), jobIndex: el.getAttribute('data-job-index'), actionsURL: el.getAttribute('data-actions-url'), + workflowName: el.getAttribute('data-workflow-name'), + workflowURL: el.getAttribute('data-workflow-url'), locale: { approve: el.getAttribute('data-locale-approve'), cancel: el.getAttribute('data-locale-cancel'), @@ -369,7 +374,7 @@ export function initRepositoryActionView() { {{ locale.rerun_all }} -
+
{{ run.commit.localeCommit }} {{ run.commit.shortSHA }} {{ run.commit.localePushedBy }} @@ -378,6 +383,10 @@ export function initRepositoryActionView() { {{ run.commit.branch.name }}
+
+ {{ run.commit.localeWorkflow }} + {{ workflowName }} +
@@ -500,7 +509,7 @@ export function initRepositoryActionView() { flex: 1; } -.action-commit-summary { +.action-summary { display: flex; gap: 5px; margin: 0 0 0 28px; From 785f336c127b8c6f050defacfe9a6b4b67f306d2 Mon Sep 17 00:00:00 2001 From: "Panagiotis \"Ivory\" Vasilopoulos" Date: Thu, 22 Feb 2024 22:12:44 +0100 Subject: [PATCH 2/4] [UI] Actions: Improve frontend testing --- tests/integration/actions_route_test.go | 31 ++++++++++++++----------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/tests/integration/actions_route_test.go b/tests/integration/actions_route_test.go index aca67a40c0..aed9327a88 100644 --- a/tests/integration/actions_route_test.go +++ b/tests/integration/actions_route_test.go @@ -22,6 +22,15 @@ import ( "github.com/stretchr/testify/assert" ) +func GetWorkflowRunRedirectURI(t *testing.T, repo_url string, workflow string) string { + t.Helper() + + req := NewRequest(t, "GET", fmt.Sprintf("%s/actions/workflows/%s/runs/latest", repo_url, workflow)) + resp := MakeRequest(t, req, http.StatusTemporaryRedirect) + + return resp.Header().Get("Location") +} + func TestActionsWebRouteLatestWorkflowRun(t *testing.T) { onGiteaRun(t, func(t *testing.T, u *url.URL) { user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) @@ -44,13 +53,7 @@ func TestActionsWebRouteLatestWorkflowRun(t *testing.T) { ) defer f() - // helpers - getWorkflowRunRedirectURI := func(workflow string) string { - req := NewRequest(t, "GET", fmt.Sprintf("%s/actions/workflows/%s/runs/latest", repo.HTMLURL(), workflow)) - resp := MakeRequest(t, req, http.StatusTemporaryRedirect) - - return resp.Header().Get("Location") - } + repoURL := repo.HTMLURL() t.Run("valid workflows", func(t *testing.T) { defer tests.PrintCurrentTest(t)() @@ -59,8 +62,8 @@ func TestActionsWebRouteLatestWorkflowRun(t *testing.T) { assert.Equal(t, 2, unittest.GetCount(t, &actions_model.ActionRun{RepoID: repo.ID})) // Get the redirect URIs for both workflows - workflowOneURI := getWorkflowRunRedirectURI("workflow-1.yml") - workflowTwoURI := getWorkflowRunRedirectURI("workflow-2.yml") + workflowOneURI := GetWorkflowRunRedirectURI(t, repoURL, "workflow-1.yml") + workflowTwoURI := GetWorkflowRunRedirectURI(t, repoURL, "workflow-2.yml") // Verify that the two are different. assert.NotEqual(t, workflowOneURI, workflowTwoURI) @@ -82,7 +85,7 @@ func TestActionsWebRouteLatestWorkflowRun(t *testing.T) { // Get the redirect URI workflow := "workflow-1.yml" - workflowOneURI := getWorkflowRunRedirectURI(workflow) + workflowOneURI := GetWorkflowRunRedirectURI(t, repoURL, workflow) // Fetch the page that shows information about the run initiated by "workflow-1.yml". // routers/web/repo/actions/view.go: data-workflow-url is constructed using data-workflow-name. @@ -91,21 +94,21 @@ func TestActionsWebRouteLatestWorkflowRun(t *testing.T) { htmlDoc := NewHTMLParser(t, resp.Body) // Verify that URL of the workflow is shown correctly. - rightURL := fmt.Sprintf("/user2/actionsTestRepo/actions?workflow=%s", workflow) - htmlDoc.AssertElement(t, fmt.Sprintf("#repo-action-view[data-workflow-url=\"%s\"]", rightURL), true) + expectedURL := fmt.Sprintf("/user2/actionsTestRepo/actions?workflow=%s", workflow) + htmlDoc.AssertElement(t, fmt.Sprintf("#repo-action-view[data-workflow-url=\"%s\"]", expectedURL), true) }) t.Run("existing workflow, non-existent branch", func(t *testing.T) { defer tests.PrintCurrentTest(t)() - req := NewRequest(t, "GET", fmt.Sprintf("%s/actions/workflows/workflow-1.yml/runs/latest?branch=foobar", repo.HTMLURL())) + req := NewRequest(t, "GET", fmt.Sprintf("%s/actions/workflows/workflow-1.yml/runs/latest?branch=foobar", repoURL)) MakeRequest(t, req, http.StatusNotFound) }) t.Run("non-existing workflow", func(t *testing.T) { defer tests.PrintCurrentTest(t)() - req := NewRequest(t, "GET", fmt.Sprintf("%s/actions/workflows/workflow-3.yml/runs/latest", repo.HTMLURL())) + req := NewRequest(t, "GET", fmt.Sprintf("%s/actions/workflows/workflow-3.yml/runs/latest", repoURL)) MakeRequest(t, req, http.StatusNotFound) }) }) From 2a0a5c6ec0cf61b97d7b8ec74bff6ab6a8adc0be Mon Sep 17 00:00:00 2001 From: "Panagiotis \"Ivory\" Vasilopoulos" Date: Thu, 22 Feb 2024 22:26:11 +0100 Subject: [PATCH 3/4] [UI] Actions: Oops, forgot to lint the tests. --- tests/integration/actions_route_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/actions_route_test.go b/tests/integration/actions_route_test.go index aed9327a88..059f5bf334 100644 --- a/tests/integration/actions_route_test.go +++ b/tests/integration/actions_route_test.go @@ -22,7 +22,7 @@ import ( "github.com/stretchr/testify/assert" ) -func GetWorkflowRunRedirectURI(t *testing.T, repo_url string, workflow string) string { +func GetWorkflowRunRedirectURI(t *testing.T, repo_url, workflow string) string { t.Helper() req := NewRequest(t, "GET", fmt.Sprintf("%s/actions/workflows/%s/runs/latest", repo_url, workflow)) From 62f3ff607424263014a0c5eb6b0507f13e757a5e Mon Sep 17 00:00:00 2001 From: "Panagiotis \"Ivory\" Vasilopoulos" Date: Thu, 22 Feb 2024 22:31:26 +0100 Subject: [PATCH 4/4] [UI] Actions: I will always run make fmt before pushing I will always run make fmt before pushing I will always run make fmt before pushing I will always run make fmt before pushing --- tests/integration/actions_route_test.go | 4 ++-- tests/integration/pull_reopen_test.go | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/integration/actions_route_test.go b/tests/integration/actions_route_test.go index 059f5bf334..b4c3db4b4b 100644 --- a/tests/integration/actions_route_test.go +++ b/tests/integration/actions_route_test.go @@ -22,10 +22,10 @@ import ( "github.com/stretchr/testify/assert" ) -func GetWorkflowRunRedirectURI(t *testing.T, repo_url, workflow string) string { +func GetWorkflowRunRedirectURI(t *testing.T, repoURL, workflow string) string { t.Helper() - req := NewRequest(t, "GET", fmt.Sprintf("%s/actions/workflows/%s/runs/latest", repo_url, workflow)) + req := NewRequest(t, "GET", fmt.Sprintf("%s/actions/workflows/%s/runs/latest", repoURL, workflow)) resp := MakeRequest(t, req, http.StatusTemporaryRedirect) return resp.Header().Get("Location") diff --git a/tests/integration/pull_reopen_test.go b/tests/integration/pull_reopen_test.go index d8dfffc36a..51f208794e 100644 --- a/tests/integration/pull_reopen_test.go +++ b/tests/integration/pull_reopen_test.go @@ -26,6 +26,7 @@ import ( repo_service "code.gitea.io/gitea/services/repository" files_service "code.gitea.io/gitea/services/repository/files" "code.gitea.io/gitea/tests" + "github.com/stretchr/testify/assert" )