mirror of
https://github.com/go-gitea/gitea.git
synced 2025-10-05 22:35:11 +02:00
Move GetActionWorkflow to convert
This commit is contained in:
parent
392baec50b
commit
fb8a221d7f
@ -5,9 +5,6 @@ package actions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"path"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
actions_model "code.gitea.io/gitea/models/actions"
|
actions_model "code.gitea.io/gitea/models/actions"
|
||||||
@ -31,61 +28,8 @@ import (
|
|||||||
"github.com/nektos/act/pkg/model"
|
"github.com/nektos/act/pkg/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getActionWorkflowPath(commit *git.Commit) string {
|
|
||||||
paths := []string{".gitea/workflows", ".github/workflows"}
|
|
||||||
for _, treePath := range paths {
|
|
||||||
if _, err := commit.SubTree(treePath); err == nil {
|
|
||||||
return treePath
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func getActionWorkflowEntry(ctx *context.APIContext, commit *git.Commit, folder string, entry *git.TreeEntry) *api.ActionWorkflow {
|
|
||||||
cfgUnit := ctx.Repo.Repository.MustGetUnit(ctx, unit.TypeActions)
|
|
||||||
cfg := cfgUnit.ActionsConfig()
|
|
||||||
|
|
||||||
defaultBranch, _ := commit.GetBranchName()
|
|
||||||
|
|
||||||
workflowURL := fmt.Sprintf("%s/actions/workflows/%s", ctx.Repo.Repository.APIURL(), url.PathEscape(entry.Name()))
|
|
||||||
workflowRepoURL := fmt.Sprintf("%s/src/branch/%s/%s/%s", ctx.Repo.Repository.HTMLURL(ctx), util.PathEscapeSegments(defaultBranch), util.PathEscapeSegments(folder), url.PathEscape(entry.Name()))
|
|
||||||
badgeURL := fmt.Sprintf("%s/actions/workflows/%s/badge.svg?branch=%s", ctx.Repo.Repository.HTMLURL(ctx), url.PathEscape(entry.Name()), url.QueryEscape(ctx.Repo.Repository.DefaultBranch))
|
|
||||||
|
|
||||||
// See https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#get-a-workflow
|
|
||||||
// State types:
|
|
||||||
// - active
|
|
||||||
// - deleted
|
|
||||||
// - disabled_fork
|
|
||||||
// - disabled_inactivity
|
|
||||||
// - disabled_manually
|
|
||||||
state := "active"
|
|
||||||
if cfg.IsWorkflowDisabled(entry.Name()) {
|
|
||||||
state = "disabled_manually"
|
|
||||||
}
|
|
||||||
|
|
||||||
// The CreatedAt and UpdatedAt fields currently reflect the timestamp of the latest commit, which can later be refined
|
|
||||||
// by retrieving the first and last commits for the file history. The first commit would indicate the creation date,
|
|
||||||
// while the last commit would represent the modification date. The DeletedAt could be determined by identifying
|
|
||||||
// the last commit where the file existed. However, this implementation has not been done here yet, as it would likely
|
|
||||||
// cause a significant performance degradation.
|
|
||||||
createdAt := commit.Author.When
|
|
||||||
updatedAt := commit.Author.When
|
|
||||||
|
|
||||||
return &api.ActionWorkflow{
|
|
||||||
ID: entry.Name(),
|
|
||||||
Name: entry.Name(),
|
|
||||||
Path: path.Join(folder, entry.Name()),
|
|
||||||
State: state,
|
|
||||||
CreatedAt: createdAt,
|
|
||||||
UpdatedAt: updatedAt,
|
|
||||||
URL: workflowURL,
|
|
||||||
HTMLURL: workflowRepoURL,
|
|
||||||
BadgeURL: badgeURL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func EnableOrDisableWorkflow(ctx *context.APIContext, workflowID string, isEnable bool) error {
|
func EnableOrDisableWorkflow(ctx *context.APIContext, workflowID string, isEnable bool) error {
|
||||||
workflow, err := GetActionWorkflow(ctx, workflowID)
|
workflow, err := convert.GetActionWorkflow(ctx, ctx.Repo.GitRepo, ctx.Repo.Repository, workflowID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -102,44 +46,6 @@ func EnableOrDisableWorkflow(ctx *context.APIContext, workflowID string, isEnabl
|
|||||||
return repo_model.UpdateRepoUnit(ctx, cfgUnit)
|
return repo_model.UpdateRepoUnit(ctx, cfgUnit)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ListActionWorkflows(ctx *context.APIContext) ([]*api.ActionWorkflow, error) {
|
|
||||||
defaultBranchCommit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
|
|
||||||
if err != nil {
|
|
||||||
ctx.APIErrorInternal(err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
entries, err := actions.ListWorkflows(defaultBranchCommit)
|
|
||||||
if err != nil {
|
|
||||||
ctx.APIError(http.StatusNotFound, err.Error())
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
folder := getActionWorkflowPath(defaultBranchCommit)
|
|
||||||
|
|
||||||
workflows := make([]*api.ActionWorkflow, len(entries))
|
|
||||||
for i, entry := range entries {
|
|
||||||
workflows[i] = getActionWorkflowEntry(ctx, defaultBranchCommit, folder, entry)
|
|
||||||
}
|
|
||||||
|
|
||||||
return workflows, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetActionWorkflow(ctx *context.APIContext, workflowID string) (*api.ActionWorkflow, error) {
|
|
||||||
entries, err := ListActionWorkflows(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, entry := range entries {
|
|
||||||
if entry.Name == workflowID {
|
|
||||||
return entry, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, util.NewNotExistErrorf("workflow %q not found", workflowID)
|
|
||||||
}
|
|
||||||
|
|
||||||
func DispatchActionWorkflow(ctx reqctx.RequestContext, doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, workflowID, ref string, processInputs func(model *model.WorkflowDispatch, inputs map[string]any) error) error {
|
func DispatchActionWorkflow(ctx reqctx.RequestContext, doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, workflowID, ref string, processInputs func(model *model.WorkflowDispatch, inputs map[string]any) error) error {
|
||||||
if workflowID == "" {
|
if workflowID == "" {
|
||||||
return util.ErrorWrapLocale(
|
return util.ErrorWrapLocale(
|
||||||
|
@ -7,6 +7,8 @@ package convert
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -23,6 +25,7 @@ import (
|
|||||||
repo_model "code.gitea.io/gitea/models/repo"
|
repo_model "code.gitea.io/gitea/models/repo"
|
||||||
"code.gitea.io/gitea/models/unit"
|
"code.gitea.io/gitea/models/unit"
|
||||||
user_model "code.gitea.io/gitea/models/user"
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
|
"code.gitea.io/gitea/modules/actions"
|
||||||
"code.gitea.io/gitea/modules/container"
|
"code.gitea.io/gitea/modules/container"
|
||||||
"code.gitea.io/gitea/modules/git"
|
"code.gitea.io/gitea/modules/git"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
@ -350,6 +353,95 @@ func ToActionWorkflowJob(ctx context.Context, repo *repo_model.Repository, task
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getActionWorkflowPath(commit *git.Commit) string {
|
||||||
|
paths := []string{".gitea/workflows", ".github/workflows"}
|
||||||
|
for _, treePath := range paths {
|
||||||
|
if _, err := commit.SubTree(treePath); err == nil {
|
||||||
|
return treePath
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func getActionWorkflowEntry(ctx context.Context, repo *repo_model.Repository, commit *git.Commit, folder string, entry *git.TreeEntry) *api.ActionWorkflow {
|
||||||
|
cfgUnit := repo.MustGetUnit(ctx, unit.TypeActions)
|
||||||
|
cfg := cfgUnit.ActionsConfig()
|
||||||
|
|
||||||
|
defaultBranch, _ := commit.GetBranchName()
|
||||||
|
|
||||||
|
workflowURL := fmt.Sprintf("%s/actions/workflows/%s", repo.APIURL(), url.PathEscape(entry.Name()))
|
||||||
|
workflowRepoURL := fmt.Sprintf("%s/src/branch/%s/%s/%s", repo.HTMLURL(ctx), util.PathEscapeSegments(defaultBranch), util.PathEscapeSegments(folder), url.PathEscape(entry.Name()))
|
||||||
|
badgeURL := fmt.Sprintf("%s/actions/workflows/%s/badge.svg?branch=%s", repo.HTMLURL(ctx), url.PathEscape(entry.Name()), url.QueryEscape(repo.DefaultBranch))
|
||||||
|
|
||||||
|
// See https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#get-a-workflow
|
||||||
|
// State types:
|
||||||
|
// - active
|
||||||
|
// - deleted
|
||||||
|
// - disabled_fork
|
||||||
|
// - disabled_inactivity
|
||||||
|
// - disabled_manually
|
||||||
|
state := "active"
|
||||||
|
if cfg.IsWorkflowDisabled(entry.Name()) {
|
||||||
|
state = "disabled_manually"
|
||||||
|
}
|
||||||
|
|
||||||
|
// The CreatedAt and UpdatedAt fields currently reflect the timestamp of the latest commit, which can later be refined
|
||||||
|
// by retrieving the first and last commits for the file history. The first commit would indicate the creation date,
|
||||||
|
// while the last commit would represent the modification date. The DeletedAt could be determined by identifying
|
||||||
|
// the last commit where the file existed. However, this implementation has not been done here yet, as it would likely
|
||||||
|
// cause a significant performance degradation.
|
||||||
|
createdAt := commit.Author.When
|
||||||
|
updatedAt := commit.Author.When
|
||||||
|
|
||||||
|
return &api.ActionWorkflow{
|
||||||
|
ID: entry.Name(),
|
||||||
|
Name: entry.Name(),
|
||||||
|
Path: path.Join(folder, entry.Name()),
|
||||||
|
State: state,
|
||||||
|
CreatedAt: createdAt,
|
||||||
|
UpdatedAt: updatedAt,
|
||||||
|
URL: workflowURL,
|
||||||
|
HTMLURL: workflowRepoURL,
|
||||||
|
BadgeURL: badgeURL,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListActionWorkflows(ctx context.Context, gitrepo *git.Repository, repo *repo_model.Repository) ([]*api.ActionWorkflow, error) {
|
||||||
|
defaultBranchCommit, err := gitrepo.GetBranchCommit(repo.DefaultBranch)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
entries, err := actions.ListWorkflows(defaultBranchCommit)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
folder := getActionWorkflowPath(defaultBranchCommit)
|
||||||
|
|
||||||
|
workflows := make([]*api.ActionWorkflow, len(entries))
|
||||||
|
for i, entry := range entries {
|
||||||
|
workflows[i] = getActionWorkflowEntry(ctx, repo, defaultBranchCommit, folder, entry)
|
||||||
|
}
|
||||||
|
|
||||||
|
return workflows, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetActionWorkflow(ctx context.Context, gitrepo *git.Repository, repo *repo_model.Repository, workflowID string) (*api.ActionWorkflow, error) {
|
||||||
|
entries, err := ListActionWorkflows(ctx, gitrepo, repo)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, entry := range entries {
|
||||||
|
if entry.Name == workflowID {
|
||||||
|
return entry, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, util.NewNotExistErrorf("workflow %q not found", workflowID)
|
||||||
|
}
|
||||||
|
|
||||||
// ToActionArtifact convert a actions_model.ActionArtifact to an api.ActionArtifact
|
// ToActionArtifact convert a actions_model.ActionArtifact to an api.ActionArtifact
|
||||||
func ToActionArtifact(repo *repo_model.Repository, art *actions_model.ActionArtifact) (*api.ActionArtifact, error) {
|
func ToActionArtifact(repo *repo_model.Repository, art *actions_model.ActionArtifact) (*api.ActionArtifact, error) {
|
||||||
url := fmt.Sprintf("%s/actions/artifacts/%d", repo.APIURL(), art.ID)
|
url := fmt.Sprintf("%s/actions/artifacts/%d", repo.APIURL(), art.ID)
|
||||||
|
@ -986,6 +986,9 @@ func (*webhookNotifier) WorkflowRunStatusUpdate(ctx context.Context, repo *repo_
|
|||||||
|
|
||||||
status, _ := convert.ToActionsStatus(run.Status)
|
status, _ := convert.ToActionsStatus(run.Status)
|
||||||
|
|
||||||
|
// TODO get gitrepo instance
|
||||||
|
// convertedWorkflow, err := convert.GetActionWorkflow(ctx, nil, nil, run.WorkflowID)
|
||||||
|
|
||||||
convertedRun, err := convert.ToActionWorkflowRun(repo, run)
|
convertedRun, err := convert.ToActionWorkflowRun(repo, run)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("ToActionWorkflowRun: %v", err)
|
log.Error("ToActionWorkflowRun: %v", err)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user