github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/circle/internal/orbs/slack/slack.go (about)

     1  // Copyright 2022 Harness, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package slack
    16  
    17  import (
    18  	circle "github.com/drone/go-convert/convert/circle/yaml"
    19  	harness "github.com/drone/spec/dist/go"
    20  )
    21  
    22  // Convert converts an Orb step to a Harness step.
    23  func Convert(command string, step *circle.Custom) *harness.Step {
    24  	switch command {
    25  	case "":
    26  		return nil // not supported
    27  	case "on-hold":
    28  		return nil // not supported
    29  	case "notify":
    30  		return convertNotify(step)
    31  	default:
    32  		return convertNotify(step)
    33  	}
    34  }
    35  
    36  // helper function converts a slack/notify
    37  // orb to a run step.
    38  func convertNotify(step *circle.Custom) *harness.Step {
    39  	customMessage, _ := step.Params["custom"].(string)
    40  	channel, _ := step.Params["channel"].(string)
    41  	accessToken, _ := step.Params["access_token"].(string)
    42  	mentions, _ := step.Params["mentions"].(string)
    43  	template, _ := step.Params["template"].(string)
    44  	webhook, _ := step.Params["webhook"].(string)
    45  	message, _ := step.Params["message"].(string)
    46  	color, _ := step.Params["color"].(string)
    47  
    48  	withMap := map[string]interface{}{}
    49  
    50  	if channel != "" {
    51  		withMap["channel"] = channel
    52  	}
    53  	if color != "" {
    54  		withMap["color"] = color
    55  	}
    56  	if message != "" {
    57  		withMap["message"] = message
    58  	}
    59  	if accessToken != "" {
    60  		withMap["access.token"] = accessToken
    61  	}
    62  	if webhook != "" {
    63  		withMap["webhook"] = webhook
    64  	}
    65  	if customMessage != "" {
    66  		withMap["custom.block"] = customMessage
    67  	}
    68  	if mentions != "" {
    69  		withMap["recipient"] = mentions
    70  	}
    71  	if template != "" {
    72  		withMap["template"] = template
    73  	}
    74  
    75  	return &harness.Step{
    76  		Type: "plugin",
    77  		Name: "notify_slack",
    78  		Spec: &harness.StepPlugin{
    79  			Image: "plugins/slack",
    80  			With:  withMap,
    81  		},
    82  	}
    83  }