Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API endpoint to get token for registering runners for repos #23761

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions modules/structs/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package structs // import "code.gitea.io/gitea/modules/structs"

// ActionRunnerToken represents an action runner token
// swagger:model
type ActionRunnerToken struct {
ID int64 `json:"id"`
Token string `json:"token"`
}
5 changes: 5 additions & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,11 @@ func Routes(ctx gocontext.Context) *web.Route {
}, reqAnyRepoReader())
m.Get("/issue_templates", context.ReferencesGitRepo(), repo.GetIssueTemplates)
m.Get("/languages", reqRepoReader(unit.TypeCode), repo.GetLanguages)

// TODO: list runners
// TODO: update runner
// TODO: delete runner
m.Put("/runners", reqToken(auth_model.AccessTokenScopeRepo), reqRepoWriter(unit.TypeActions), repo.GenerateRunnerToken)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that only write or even admin?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, this route should probably be called /runners/new(-registration)-token or something similar.

}, repoAssignment())
})

Expand Down
52 changes: 52 additions & 0 deletions routers/api/v1/repo/runners.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package repo

import (
"errors"
"net/http"

actions_model "code.gitea.io/gitea/models/actions"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/services/convert"
)

// GenerateRunnerToken generate a new runner token for a repository.
func GenerateRunnerToken(ctx *context.APIContext) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any checks to see if Actions are enabled for the instance/org/repo?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, we should probably check that.

// swagger:operation PUT /repos/{owner}/{repo}/runners repository repoGenerateRunnerToken
// ---
// summary: Generate a new runner token for a repository.
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// responses:
// "200":
// "$ref": "#/responses/ActionRunnerToken"
// "404":
// "$ref": "#/responses/notFound"
Comment on lines +37 to +38
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about additionally returning a 404 or 403 in case actions are disabled?


token, err := actions_model.GetUnactivatedRunnerToken(ctx, ctx.Repo.Owner.ID, ctx.Repo.Repository.ID)
if errors.Is(err, util.ErrNotExist) {
token, err = actions_model.NewRunnerToken(ctx, ctx.Repo.Owner.ID, ctx.Repo.Repository.ID)
if err != nil {
ctx.ServerError("CreateRunnerToken", err)
return
}
} else if err != nil {
ctx.ServerError("GetUnactivatedRunnerToken", err)
return
}
ctx.JSON(http.StatusOK, convert.ToAPIActionRunnerToken(token))
}
16 changes: 16 additions & 0 deletions services/convert/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package convert

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

func ToAPIActionRunnerToken(token *actions_model.ActionRunnerToken) *api.ActionRunnerToken {
return &api.ActionRunnerToken{
ID: token.ID,
Token: token.Token,
}
}
36 changes: 36 additions & 0 deletions templates/swagger/v1_json.tmpl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.