Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
timeout-minutes: 10
defaults:
run:
working-peerbonus: web
working-directory: web
steps:
- uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803
with:
Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,18 @@ statements. The Go server serves the React SPA, `/api/peerbonus/v1/*`, `/config`
`/launcher`, `/healthz`, `/readyz`, and `/releasez` from one origin. PostgreSQL
stores application data; Directory remains a separate app called over HTTP.
The SPA uses the session cookie behind the OIDC edge and never handles tokens.
Optional Slack credentials enable announcements, direct messages, and inbound
commands and interactions.
Optional Slack credentials enable announcements, direct messages, the
`/peerbonus` command, and giving inside a conversation: mentioning the bot
(`@Peer Bonus @recipient 30 message`; the bot asks for the amount when it is
omitted) or adding an admin-chosen reaction. Slack reaches the server through
the `slack` inbound endpoint declared in `catalog/entry.yaml` (`/slack/*`).

A sender can give only once from the same Slack message, regardless of the emoji
or whether the give started with a mention. A mention can still give to up to
five recipients atomically; retries replay that entire original batch. Stored
gives are unique by (channel, message timestamp, sender, recipient), with emoji
retained only for reference. Removing a reaction does not refund points or allow
another give. This replaces the per-emoji RL2 rule in `reaction-give.md`.

## Develop

Expand Down
8 changes: 8 additions & 0 deletions catalog/entry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ spec:
installPolicy: Automatic
publicURLEnv: PEERBONUS_DIRECTORY_PUBLIC_URL
urlEnv: PEERBONUS_DIRECTORY_URL
inbound:
- name: slack
path: /slack
limits:
methods:
- POST
maxBodyBytes: 65536
timeout: 10s
display:
category: collaborate
description: Send points and thanks to colleagues every week; close each month
Expand Down
4 changes: 4 additions & 0 deletions cmd/server/internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type API struct {
slack slackAdminClient
slackStore slackAdminStore
slackSigningSecretConfigured bool
slackInbound *slackInboundClient
slackChannelsMu sync.Mutex
slackChannels []slack.Channel
slackChannelsExpires time.Time
Expand All @@ -58,6 +59,9 @@ func New(database *store.Store, auth *identity.Authenticator, upstream *platform
if slackOptions.Client != nil {
a.slack = slackOptions.Client
}
if slackOptions.PlatformAPIURL != "" {
a.slackInbound = newSlackInboundClient(slackOptions.PlatformAPIURL, upstream.ProjectID, auth.HTTPClient().Transport)
}
a.mux.HandleFunc("GET /api/peerbonus/v1/openapi.json", a.openapi)
a.route("GET /api/peerbonus/v1/me", false, true, a.me)
a.route("GET /api/peerbonus/v1/program", false, false, a.program)
Expand Down
52 changes: 52 additions & 0 deletions cmd/server/internal/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,3 +704,55 @@ func TestV2InputAndResponseContractWithoutDatabase(t *testing.T) {
require.NotNil(t, insights.TopSenders)
require.NotNil(t, a.renderParticipation(domain.Participation{}).Items)
}

func TestSlackConversationPostJSON(t *testing.T) {
a := &API{}
for _, permalink := range []*string{nil, new("https://acme.slack.com/archives/C123/p123456")} {
body, err := json.Marshal(a.renderPost(domain.Post{ID: "post", SlackPermalink: permalink}))
require.NoError(t, err)
var result map[string]json.RawMessage
require.NoError(t, json.Unmarshal(body, &result))
require.Contains(t, result, "slackPermalink")
expected, err := json.Marshal(permalink)
require.NoError(t, err)
require.JSONEq(t, string(expected), string(result["slackPermalink"]))
}
}

func TestSlackConversationOpenAPI(t *testing.T) {
type schema struct {
Type string `json:"type"`
Ref string `json:"$ref"`
Nullable bool `json:"nullable"`
Required []string `json:"required"`
Properties map[string]*schema `json:"properties"`
Items *schema `json:"items"`
MinItems int `json:"minItems"`
MaxItems int `json:"maxItems"`
}
var document struct {
Components struct {
Schemas map[string]*schema `json:"schemas"`
} `json:"components"`
}
require.NoError(t, json.Unmarshal(openapiDocument, &document))
schemas := document.Components.Schemas
settings := schemas["SlackIntegration"].Properties["settings"]
patch := schemas["SlackSettingsPatch"]
for _, field := range []string{"giveInConversation", "mentionPointChoices", "reactionRules"} {
require.Contains(t, settings.Required, field)
require.Contains(t, settings.Properties, field)
require.Contains(t, patch.Properties, field)
}
require.Equal(t, "boolean", patch.Properties["giveInConversation"].Type)
require.Equal(t, "#/components/schemas/SlackMentionPointChoices", patch.Properties["mentionPointChoices"].Ref)
require.Equal(t, 1, schemas["SlackMentionPointChoices"].MinItems)
require.Equal(t, 5, schemas["SlackMentionPointChoices"].MaxItems)
require.Equal(t, 8, patch.Properties["reactionRules"].MaxItems)
require.Equal(t, "#/components/schemas/SlackReactionRule", patch.Properties["reactionRules"].Items.Ref)
require.ElementsMatch(t, []string{"emoji", "points"}, schemas["SlackReactionRule"].Required)
require.True(t, schemas["SlackReactionRule"].Properties["valueKey"].Nullable)
require.Contains(t, schemas["Post"].Required, "slackPermalink")
require.Equal(t, "string", schemas["Post"].Properties["slackPermalink"].Type)
require.True(t, schemas["Post"].Properties["slackPermalink"].Nullable)
}
40 changes: 36 additions & 4 deletions cmd/server/internal/api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@
"get": {
"operationId": "getAdminSlack",
"summary": "Read Slack integration settings and delivery health",
"description": "Team Admin only. Credentials expose presence booleans, never secret values. State is unconfigured without a client, configured without a verified workspace, team_changed when lastError.code is team_changed, otherwise connected. Inbound is null until platform inbound discovery is available.",
"description": "Team Admin only. Credentials expose presence booleans, never secret values. State is unconfigured without a client, configured without a verified workspace, team_changed when lastError.code is team_changed, otherwise connected. Inbound is discovered from the Platform API peerbonus-slack endpoint using the viewer's bearer: absent returns null; found returns its URL, generation-aware ready flag and Ready condition reason. Discovery failure returns {url:null,ready:false,reason:\"platform_unavailable\"} without failing this response.",
"x-appthrust-link": true,
"responses": {
"200": {
Expand All @@ -1056,7 +1056,7 @@
"patch": {
"operationId": "patchAdminSlack",
"summary": "Update Slack settings",
"description": "Team Admin only. Channel and flag changes require connected state. Channel IDs must resolve to a public channel; null clears the channel. announcePosts requires a channel. giveFromSlack requires a configured signing secret. Locale may be changed before connecting.",
"description": "Team Admin only. Channel and flag changes require connected state (409 slack_not_connected). Channel IDs must resolve to a public channel; null clears the channel. announcePosts requires a channel. Enabling giveFromSlack or giveInConversation requires a configured signing secret (409 slack_signing_secret_missing). Mention point choices contain 1-5 unique amounts. Reaction rules replace all existing rules, preserve order and contain at most 8 unique normalized emoji names. Emoji names are lowercased and stripped of surrounding colons and skin-tone modifiers. Amounts must be positive and no greater than the current Program maxPointsPerPost; non-null valueKey must exist in the current Program. Locale, point choices and rules may be changed before connecting.",
"requestBody": {
"required": true,
"content": {"application/json": {"schema": {"$ref": "#/components/schemas/SlackSettingsPatch"}}}
Expand All @@ -1067,7 +1067,7 @@
"content": {"application/json": {"schema": {"$ref": "#/components/schemas/SlackIntegration"}}}
},
"400": {
"description": "invalid_request: invalid channel, missing announce channel, or invalid locale",
"description": "invalid_request: invalid channel, missing announce channel, invalid locale, invalid point choices or invalid reaction rules; unknown_value: a rule valueKey is absent from the current Program",
"content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}
},
"409": {
Expand Down Expand Up @@ -1245,7 +1245,7 @@
},
"settings": {
"type": "object",
"required": ["announceChannel", "announcePosts", "dmRecipientOnPost", "giveFromSlack", "locale"],
"required": ["announceChannel", "announcePosts", "dmRecipientOnPost", "giveFromSlack", "giveInConversation", "mentionPointChoices", "reactionRules", "locale"],
"properties": {
"announceChannel": {
"type": "object",
Expand All @@ -1256,12 +1256,16 @@
"announcePosts": {"type": "boolean"},
"dmRecipientOnPost": {"type": "boolean"},
"giveFromSlack": {"type": "boolean"},
"giveInConversation": {"type": "boolean"},
"mentionPointChoices": {"$ref": "#/components/schemas/SlackMentionPointChoices"},
"reactionRules": {"type": "array", "maxItems": 8, "items": {"$ref": "#/components/schemas/SlackReactionRule"}},
"locale": {"type": "string", "enum": ["ja", "en"]}
}
},
"inbound": {
"type": "object",
"nullable": true,
"description": "Null when discovery is not configured or the endpoint is absent. Platform failures return url=null, ready=false and reason=platform_unavailable.",
"required": ["url", "ready", "reason"],
"properties": {
"url": {"type": "string", "nullable": true},
Expand All @@ -1282,6 +1286,25 @@
}
}
},
"SlackMentionPointChoices": {
"type": "array",
"description": "Unique amounts offered when a mention omits points, each no greater than the current Program maxPointsPerPost when patched.",
"minItems": 1,
"maxItems": 5,
"uniqueItems": true,
"items": {"type": "integer", "minimum": 1},
"example": [10, 30, 50]
},
"SlackReactionRule": {
"type": "object",
"additionalProperties": false,
"required": ["emoji", "points"],
"properties": {
"emoji": {"type": "string", "description": "Normalized to lowercase without colons or skin-tone modifiers; the resulting name must be unique across rules and contain 1-100 characters without whitespace.", "example": "pb-10"},
"points": {"type": "integer", "minimum": 1, "description": "No greater than the current Program maxPointsPerPost when patched."},
"valueKey": {"type": "string", "nullable": true, "description": "Optional key of a current Program value; null assigns no value."}
}
},
"SlackSettingsPatch": {
"type": "object",
"additionalProperties": false,
Expand All @@ -1290,6 +1313,9 @@
"announcePosts": {"type": "boolean"},
"dmRecipientOnPost": {"type": "boolean"},
"giveFromSlack": {"type": "boolean"},
"giveInConversation": {"type": "boolean", "description": "Requires connected state; enabling also requires the signing secret."},
"mentionPointChoices": {"$ref": "#/components/schemas/SlackMentionPointChoices"},
"reactionRules": {"type": "array", "maxItems": 8, "description": "Replaces all rules in the supplied order; emoji names must be unique after normalization.", "items": {"$ref": "#/components/schemas/SlackReactionRule"}},
"locale": {"type": "string", "enum": ["ja", "en"]}
}
},
Expand Down Expand Up @@ -1722,6 +1748,11 @@
"message": {
"type": "string"
},
"slackPermalink": {
"type": "string",
"nullable": true,
"description": "Original Slack message URL for a conversation give; null for posts without a recorded permalink."
},
"values": {
"type": "array",
"items": {
Expand Down Expand Up @@ -1764,6 +1795,7 @@
"recipient",
"points",
"message",
"slackPermalink",
"values",
"clapCount",
"clapPoints",
Expand Down
Loading