Skip to content

Commit

Permalink
Merge pull request #282 from vishnoianil/readiness-status
Browse files Browse the repository at this point in the history
Use github status for checking bot readiness.
  • Loading branch information
mergify[bot] authored Apr 19, 2024
2 parents 157205c + 42140e7 commit f6edb9f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 47 deletions.
16 changes: 13 additions & 3 deletions gobot/handlers/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ func (h *PRCommentHandler) Handle(ctx context.Context, eventType, deliveryID str
prComment.labels = pr.Labels

switch words[1] {
case "help":
return h.helpCommand(ctx, client, &prComment)
case "enable":
return h.enableCommand(ctx, client, &prComment)
case "generate-local":
Expand Down Expand Up @@ -204,9 +206,6 @@ func (h *PRCommentHandler) queueGenerateJob(ctx context.Context, client *github.
case "sdg-svc":
checkName = util.GenerateSDGCheck
statusName = util.GenerateSDGStatus
case "enable":
checkName = util.TriageReadinessCheck
statusName = util.TriageReadinessStatus
default:
h.Logger.Errorf("Unknown job type: %s", jobType)
}
Expand Down Expand Up @@ -277,6 +276,17 @@ func (h *PRCommentHandler) checkAuthorPermission(ctx context.Context, client *gi
return isAllowed
}

func (h *PRCommentHandler) helpCommand(ctx context.Context, client *github.Client, prComment *PRComment) error {
h.Logger.Infof("Help command received on %s/%s#%d by %s",
prComment.repoOwner, prComment.repoName, prComment.prNum, prComment.author)
err := util.PostBotWelcomeMessage(ctx, client, prComment.repoOwner, prComment.repoName, prComment.prNum, prComment.prSha, h.BotUsername, h.Maintainers)
if err != nil {
h.Logger.Errorf("Failed to post welcome message on PR %s/%s#%d: %v", prComment.repoOwner, prComment.repoName, prComment.prNum, err)
return err
}
return nil
}

func (h *PRCommentHandler) enableCommand(ctx context.Context, client *github.Client, prComment *PRComment) error {
h.Logger.Infof("Enable command received on %s/%s#%d by %s",
prComment.repoOwner, prComment.repoName, prComment.prNum, prComment.author)
Expand Down
40 changes: 5 additions & 35 deletions gobot/handlers/pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package handlers
import (
"context"
"encoding/json"
"fmt"

"github.com/google/go-github/v60/github"
"github.com/instructlab/instructlab-bot/gobot/util"
Expand All @@ -12,10 +11,6 @@ import (
"go.uber.org/zap"
)

const (
TriageReadinessMsg = "PR is ready for evaluation."
)

type PullRequestHandler struct {
githubapp.ClientCreator
Logger *zap.SugaredLogger
Expand Down Expand Up @@ -67,15 +62,15 @@ func (h *PullRequestHandler) Handle(ctx context.Context, eventType, deliveryID s
}

params := util.PullRequestStatusParams{
CheckName: util.TriageReadinessCheck,
CheckName: util.BotReadyStatus,
RepoOwner: repoOwner,
RepoName: repoName,
PrNum: prNum,
PrSha: prSha,
}

// Check if the triage readiness check already exists
enable, err := util.CheckBotEnableStatus(ctx, client, params)
// Check if the bot readiness status already exists
enable, err := util.StatusExist(ctx, client, params, util.BotReadyStatus)
if err != nil {
h.Logger.Errorf("Failed to check bot enable status: %v", err)
return nil
Expand All @@ -84,34 +79,9 @@ func (h *PullRequestHandler) Handle(ctx context.Context, eventType, deliveryID s
return nil
}

detailsMsg := fmt.Sprintf("Beep, boop 🤖, Hi, I'm %s and I'm going to help you"+
" with your pull request. Thanks for you contribution! 🎉\n\n", h.BotUsername)
detailsMsg += fmt.Sprintf("I support the following commands:\n\n"+
"* `%s precheck` -- Check existing model behavior using the questions in this proposed change.\n"+
"* `%s generate` -- Generate a sample of synthetic data using the synthetic data generation backend infrastructure.\n"+
"* `%s generate-local` -- Generate a sample of synthetic data using a local model.\n"+
"> [!NOTE] \n > **Results or Errors of these commands will be posted as a pull request check in the Checks section below**\n\n",
h.BotUsername, h.BotUsername, h.BotUsername)

if len(h.Maintainers) > 0 {
detailsMsg += fmt.Sprintf("> [!NOTE] \n > **Currently only maintainers belongs to [%v] teams are allowed to run these commands**.\n", h.Maintainers)
}

params.Status = util.CheckComplete
params.Conclusion = util.CheckStatusSuccess
params.CheckSummary = TriageReadinessMsg
params.CheckDetails = detailsMsg
params.Comment = detailsMsg

err = util.PostPullRequestComment(ctx, client, params)
if err != nil {
h.Logger.Errorf("Failed to post comment on PR %s/%s#%d: %v", params.RepoOwner, params.RepoName, params.PrNum, err)
return err
}

err = util.PostPullRequestCheck(ctx, client, params)
err = util.PostBotWelcomeMessage(ctx, client, repoOwner, repoName, prNum, prSha, h.BotUsername, h.Maintainers)
if err != nil {
h.Logger.Errorf("Failed to post check on PR %s/%s#%d: %v", params.RepoOwner, params.RepoName, params.PrNum, err)
h.Logger.Errorf("Failed to post bot welcome message on PR %s/%s#%d: %v", repoOwner, repoName, prNum, err)
return err
}
return nil
Expand Down
77 changes: 68 additions & 9 deletions gobot/util/pr_updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ const (
CheckStatusError = "error"
CheckStatusPending = "pending"

TriageReadinessCheck = "Triage Readiness Check"
PrecheckCheck = "Precheck Check"
GenerateLocalCheck = "Generate Local Check"
GenerateSDGCheck = "Generate SDG Check"

TriageReadinessStatus = "Triage Readiness Status"
PrecheckStatus = "Precheck Status"
GenerateLocalStatus = "Generate Local Status"
GenerateSDGStatus = "Generate SDG Status"
BotReadyStatus = "InstructLab Bot"
BotReadyStatusMsg = "InstructLab bot is ready to assist!!"

PrecheckCheck = "Precheck Check"
GenerateLocalCheck = "Generate Local Check"
GenerateSDGCheck = "Generate SDG Check"

PrecheckStatus = "Precheck Status"
GenerateLocalStatus = "Generate Local Status"
GenerateSDGStatus = "Generate SDG Status"

InstructLabBotUrl = "https://github.com/instructlab/instructlab-bot"
)

type PullRequestStatusParams struct {
Expand All @@ -37,6 +40,7 @@ type PullRequestStatusParams struct {
CheckSummary string
CheckDetails string
Comment string
StatusDesc string

JobType string
JobID string
Expand Down Expand Up @@ -107,3 +111,58 @@ func PostPullRequestCheck(ctx context.Context, client *github.Client, params Pul
}
return nil
}

func PostPullRequestStatus(ctx context.Context, client *github.Client, params PullRequestStatusParams) error {
status := &github.RepoStatus{
State: github.String(params.Conclusion), // Status state: success, failure, error, or pending
Description: github.String(params.StatusDesc), // Status description
Context: github.String(params.CheckName), // Status context
TargetURL: github.String(InstructLabBotUrl), // Target URL to redirect
}
_, _, err := client.Repositories.CreateStatus(ctx, params.RepoOwner, params.RepoName, params.PrSha, status)
if err != nil {
return err
}
return nil
}

func PostBotWelcomeMessage(ctx context.Context, client *github.Client, repoOwner string, repoName string, prNum int, prSha string, botName string, maintainers []string) error {
params := PullRequestStatusParams{
CheckName: BotReadyStatus,
RepoOwner: repoOwner,
RepoName: repoName,
PrNum: prNum,
PrSha: prSha,
}
detailsMsg := fmt.Sprintf("Beep, boop 🤖, Hi, I'm %s and I'm going to help you"+
" with your pull request. Thanks for you contribution! 🎉\n\n", botName)
detailsMsg += fmt.Sprintf("I support the following commands:\n\n"+
"* `%s precheck` -- Check existing model behavior using the questions in this proposed change.\n"+
"* `%s generate` -- Generate a sample of synthetic data using the synthetic data generation backend infrastructure.\n"+
"* `%s generate-local` -- Generate a sample of synthetic data using a local model.\n"+
"* `%s help` -- Print this help message again.\n"+
"> [!NOTE] \n > **Results or Errors of these commands will be posted as a pull request check in the Checks section below**\n\n",
botName, botName, botName, botName)

if len(maintainers) > 0 {
detailsMsg += fmt.Sprintf("> [!NOTE] \n > **Currently only maintainers belongs to [%v] teams are allowed to run these commands**.\n", maintainers)
}
params.Status = CheckComplete
params.Conclusion = CheckStatusSuccess
params.CheckSummary = BotReadyStatusMsg
params.CheckDetails = detailsMsg
params.Comment = detailsMsg
params.StatusDesc = BotReadyStatusMsg

err := PostPullRequestComment(ctx, client, params)
if err != nil {
return err
}

err = PostPullRequestStatus(ctx, client, params)
if err != nil {
return err
}
return nil

}

0 comments on commit f6edb9f

Please sign in to comment.