feat: pass commit info from runner

This commit is contained in:
Denis Gukov 2024-12-14 13:18:17 +05:00
parent 48f43aec37
commit 58cbad7f0f
7 changed files with 38 additions and 4 deletions

View File

@ -3,12 +3,12 @@ package runners
import (
"net/http"
"github.com/gorilla/context"
"github.com/semaphoreui/semaphore/api/helpers"
"github.com/semaphoreui/semaphore/db"
"github.com/semaphoreui/semaphore/pkg/task_logger"
"github.com/semaphoreui/semaphore/services/runners"
"github.com/semaphoreui/semaphore/util"
"github.com/gorilla/context"
)
func RunnerMiddleware(next http.Handler) http.Handler {
@ -160,6 +160,10 @@ func UpdateRunner(w http.ResponseWriter, r *http.Request) {
}
tsk.SetStatus(job.Status)
if job.Commit != nil {
tsk.SetCommit(job.Commit.Hash, job.Commit.Message)
}
}
w.WriteHeader(http.StatusNoContent)

View File

@ -57,4 +57,6 @@ type Logger interface {
SetStatus(status TaskStatus)
AddStatusListener(l StatusListener)
AddLogListener(l LogListener)
SetCommit(hash, message string)
}

View File

@ -250,6 +250,7 @@ func (p *JobPool) sendProgress() {
ID: id,
LogRecords: j.logRecords,
Status: j.status,
Commit: j.commit,
})
j.logRecords = make([]LogRecord, 0)
@ -387,7 +388,7 @@ func (p *JobPool) checkNewJobs() {
resp, err := client.Do(req)
if err != nil {
logger.ActionError(err, "send request", "upexpected error")
logger.ActionError(err, "send request", "unexpected error")
return
}

View File

@ -16,6 +16,7 @@ type runningJob struct {
status task_logger.TaskStatus
logRecords []LogRecord
job *tasks.LocalJob
commit *CommitInfo
statusListeners []task_logger.StatusListener
logListeners []task_logger.LogListener
@ -62,6 +63,13 @@ func (p *runningJob) LogCmd(cmd *exec.Cmd) {
go p.logPipe(bufio.NewReader(stdout))
}
func (p *runningJob) SetCommit(hash, message string) {
p.commit = &CommitInfo{
Hash: hash,
Message: message,
}
}
func (p *runningJob) SetStatus(status task_logger.TaskStatus) {
if p.status == status {
return

View File

@ -35,6 +35,11 @@ type LogRecord struct {
Message string `json:"message" binding:"required"`
}
type CommitInfo struct {
Hash string `json:"hash" binding:"required"`
Message string `json:"message" binding:"required"`
}
type RunnerProgress struct {
Jobs []JobProgress
}
@ -43,6 +48,7 @@ type JobProgress struct {
ID int
Status task_logger.TaskStatus
LogRecords []LogRecord
Commit *CommitInfo
}
type RunnerRegistration struct {

View File

@ -53,6 +53,10 @@ func (t *LocalJob) SetStatus(status task_logger.TaskStatus) {
t.Logger.SetStatus(status)
}
func (t *LocalJob) SetCommit(hash, message string) {
t.Logger.SetCommit(hash, message)
}
func (t *LocalJob) getEnvironmentExtraVars(username string, incomingVersion *string) (extraVars map[string]interface{}, err error) {
extraVars = make(map[string]interface{})
@ -611,8 +615,7 @@ func (t *LocalJob) checkoutRepository() error {
commitMessage, _ := repo.GetLastCommitMessage()
t.Log("Commit hash: " + commitHash)
t.Log("Commit message: " + commitMessage)
t.SetCommit(commitHash, commitMessage)
return nil
}

View File

@ -58,6 +58,16 @@ func (t *TaskRunner) LogCmd(cmd *exec.Cmd) {
go t.logPipe(bufio.NewReader(stdout))
}
func (t *TaskRunner) SetCommit(hash, message string) {
t.Task.CommitHash = &hash
t.Task.CommitMessage = message
if err := t.pool.store.UpdateTask(t.Task); err != nil {
t.panicOnError(err, "Failed to update task commit")
}
}
func (t *TaskRunner) SetStatus(status task_logger.TaskStatus) {
if status == t.Task.Status {
return