Add proposal approval api endpoint
Change-Id: I3ab864af51735ee7c484df9c66d0b3ec2f6acb36
diff --git a/server/api/api.go b/server/api/api.go
index fd78ee4..ae0af36 100644
--- a/server/api/api.go
+++ b/server/api/api.go
@@ -13,10 +13,10 @@
const HealthCheckPath = "/healthcheck"
type API struct {
- Logger *log.Logger
- Root *gin.Engine
- APIRoot *gin.RouterGroup // 'api/v1'
- Auth *gin.RouterGroup // 'api/v1/auth'
+ Logger *log.Logger
+ Root *gin.Engine
+ APIRoot *gin.RouterGroup // 'api/v1'
+ Proposal *gin.RouterGroup // 'api/v1/proposal'
}
// Init initializes api
@@ -29,6 +29,7 @@
})
apiObj.APIRoot = router.Group(APIURLSuffix)
apiObj.initHealthCheck()
+ apiObj.initProposal()
apiObj.Root.NoRoute(func(c *gin.Context) {
c.JSON(http.StatusNotFound, "Page not found")
diff --git a/server/api/proposal.go b/server/api/proposal.go
new file mode 100644
index 0000000..2adc126
--- /dev/null
+++ b/server/api/proposal.go
@@ -0,0 +1,43 @@
+package api
+
+import (
+ "fmt"
+ "net/http"
+
+ "github.com/gin-gonic/gin"
+)
+
+func (apiObj *API) initProposal() {
+ apiObj.Proposal = apiObj.APIRoot.Group("/proposal")
+ apiObj.Proposal.POST("/approve", proposalApprovalHandler)
+}
+
+func proposalApprovalHandler(c *gin.Context) {
+ app, err := getApp(c)
+ if err != nil {
+ responseFormat(c, http.StatusInternalServerError, err.Error())
+ return
+ }
+
+ // Read the raw body for signature validation
+ body, err := c.GetRawData()
+ if err != nil {
+ responseFormat(c, http.StatusBadRequest, "Failed to read request body")
+ return
+ }
+
+ // Get the signature from headers
+ signature := c.GetHeader("X-Hub-Signature-256")
+ if signature == "" {
+ // Fallback to the older signature header
+ signature = c.GetHeader("X-Hub-Signature")
+ }
+
+ err = app.ProposalApproval(body, signature)
+ if err != nil {
+ responseFormat(c, http.StatusBadRequest, fmt.Sprintf("Failed to process webhook: %v", err))
+ return
+ }
+
+ responseFormat(c, http.StatusOK, "Proposal approved successfully")
+}
diff --git a/server/app/proposal.go b/server/app/proposal.go
new file mode 100644
index 0000000..4b2acc4
--- /dev/null
+++ b/server/app/proposal.go
@@ -0,0 +1,26 @@
+package app
+
+import (
+ "fmt"
+
+ "github.com/iomodo/staff/git"
+)
+
+func (a *App) ProposalApproval(body []byte, signature string) error {
+ // Validate the webhook signature
+ if err := git.ValidateSignature(body, signature, a.config.GitHub.WebhookSecret); err != nil {
+ return fmt.Errorf("invalid webhook signature: %w", err)
+ }
+
+ // Process the webhook payload
+ taskID, err := git.ProcessMergeWebhook(body, signature, a.config.GitHub.WebhookSecret)
+ if err != nil {
+ return fmt.Errorf("Failed to process webhook: %w", err)
+ }
+
+ // Log the successful approval
+ a.logger.Info("Proposal approved via webhook",
+ "task_id", taskID,
+ )
+ return nil
+}
diff --git a/server/config.yaml b/server/config.yaml
index 0842f0e..5ce3fec 100644
--- a/server/config.yaml
+++ b/server/config.yaml
@@ -13,6 +13,7 @@
token: "${GITHUB_TOKEN}"
owner: "iomodo" # Replace with your GitHub username
repo: "staff" # Replace with your repository name
+ webhook_secret: "${GITHUB_WEBHOOK_SECRET}" # Secret for webhook signature validation
git:
repo_path: "/Users/shota/github/staff/"
diff --git a/server/config/config.go b/server/config/config.go
index 1a39c6f..2a39741 100644
--- a/server/config/config.go
+++ b/server/config/config.go
@@ -34,9 +34,10 @@
// GitHubConfig represents GitHub integration configuration
type GitHubConfig struct {
- Token string `yaml:"token"`
- Owner string `yaml:"owner"`
- Repo string `yaml:"repo"`
+ Token string `yaml:"token"`
+ Owner string `yaml:"owner"`
+ Repo string `yaml:"repo"`
+ WebhookSecret string `yaml:"webhook_secret"` // Secret for webhook signature validation
}
// GerritConfig represents Gerrit integration configuration
@@ -118,6 +119,9 @@
if repo := os.Getenv("GITHUB_REPO"); repo != "" {
config.GitHub.Repo = repo
}
+ if webhookSecret := os.Getenv("GITHUB_WEBHOOK_SECRET"); webhookSecret != "" {
+ config.GitHub.WebhookSecret = webhookSecret
+ }
if gerritUsername := os.Getenv("GERRIT_USERNAME"); gerritUsername != "" {
config.Gerrit.Username = gerritUsername
}