-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change adds a configuration option to post comments to a chatter object instead of a casecomment object. The feature is disabled by default and has to be explicitly enabled via the configuration file: ```yaml salesforce: enable-chatter: true ``` The change also introduces the `salesforce-test` binary to test Salesforce queries. Closes: SET-627 Signed-off-by: Nicolas Bock <[email protected]>
- Loading branch information
1 parent
9cfc677
commit 6e17e83
Showing
8 changed files
with
208 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strconv" | ||
|
||
"github.com/canonical/athena-core/pkg/common" | ||
"github.com/canonical/athena-core/pkg/config" | ||
"gopkg.in/alecthomas/kingpin.v2" | ||
) | ||
|
||
var configs = common.StringList( | ||
kingpin.Flag("config", "Path to the athena configuration file").Default("/etc/athena/main.yaml").Short('c'), | ||
) | ||
|
||
var allCases = kingpin.Flag("all-cases", "Get all cases").Default("false").Bool() | ||
var allFeedComments = kingpin.Flag("all-feed-comments", "Get all FeedComments").Default("false").Bool() | ||
var allFeedItems = kingpin.Flag("all-feed-items", "Get all FeedItems").Default("false").Bool() | ||
var caseNumber = kingpin.Flag("case-id", "The case ID to query").Default("").String() | ||
var commentVisibility = kingpin.Flag("visibility", "Set the comment visibility {public, private)").Default("private").String() | ||
var getChatter = kingpin.Flag("chatter", "Get all chatter objects of case").Default("false").Bool() | ||
var getComments = kingpin.Flag("comments", "Get all comments of case").Default("false").Bool() | ||
var newChatter = kingpin.Flag("new-chatter", "Add a new chatter comment to the case").Default("").String() | ||
|
||
func main() { | ||
kingpin.HelpFlag.Short('h') | ||
kingpin.Parse() | ||
|
||
switch *commentVisibility { | ||
case "public", "private": | ||
// All good, do nothing. | ||
default: | ||
log.Fatal("Invalid visibility value. Allowed values are 'public' and 'private'.") | ||
} | ||
|
||
cfg, err := config.NewConfigFromFile(*configs) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
sfClient, err := common.NewSalesforceClient(cfg) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if *allCases { | ||
log.Println("All cases:") | ||
records, err := sfClient.Query("SELECT Id, CaseNumber from Case") | ||
if err != nil { | ||
log.Fatalln("Failed to query for all cases") | ||
} | ||
for _, result := range records.Records { | ||
log.Printf("%s: %s", result["Id"], result["CaseNumber"]) | ||
} | ||
} | ||
|
||
if *allFeedComments { | ||
log.Println("All FeedComments:") | ||
records, err := sfClient.Query("SELECT Id from FeedComment") | ||
if err != nil { | ||
log.Fatalln("Failed to query for all FeedComments") | ||
} | ||
for _, result := range records.Records { | ||
log.Printf("%s", result["Id"]) | ||
} | ||
} | ||
|
||
if *allFeedItems { | ||
log.Println("All FeedItems:") | ||
records, err := sfClient.Query("SELECT Id from FeedItem") | ||
if err != nil { | ||
log.Fatalln("Failed to query for all FeedItems") | ||
} | ||
for _, result := range records.Records { | ||
log.Printf("%s", result["Id"]) | ||
} | ||
} | ||
|
||
if len(*caseNumber) > 0 { | ||
// Query Salesforce for the case. | ||
caseNumberAsInt, err := strconv.ParseInt(*caseNumber, 10, 64) | ||
if err != nil { | ||
log.Fatalf("Failed to parse the case number %s", *caseNumber) | ||
} | ||
caseNumberFormatted := fmt.Sprintf("%08d", caseNumberAsInt) | ||
|
||
log.Printf("Searching for case %s", caseNumberFormatted) | ||
query := fmt.Sprintf("SELECT Id, CaseNumber FROM Case WHERE CaseNumber = '%s'", caseNumberFormatted) | ||
records, err := sfClient.Query(query) | ||
if err != nil { | ||
log.Fatalf("Failed to query Salesforce: %v", err) | ||
} | ||
if len(records.Records) > 0 { | ||
log.Printf("%s: %s", records.Records[0]["Id"], records.Records[0]["CaseNumber"]) | ||
} else { | ||
log.Fatalf("Case with ID %s does not exist.\n", *caseNumber) | ||
} | ||
caseId := records.Records[0]["Id"] | ||
|
||
if *getComments { | ||
log.Print("Getting case comments") | ||
query = fmt.Sprintf("SELECT Id, CommentBody FROM CaseComment WHERE ParentId = '%s'", caseId) | ||
records, err = sfClient.Query(query) | ||
if err != nil { | ||
log.Fatalf("Failed to get case comments: %v", err) | ||
} | ||
if len(records.Records) == 0 { | ||
log.Fatal("Could not find any case comments") | ||
} | ||
for _, comment := range records.Records { | ||
log.Printf("%s", comment["CommentBody"]) | ||
} | ||
} | ||
|
||
if *getChatter { | ||
log.Print("Getting case chatter comments") | ||
query = fmt.Sprintf("SELECT Id, Body FROM FeedItem WHERE ParentID = '%s'", caseId) | ||
records, err = sfClient.Query(query) | ||
if err != nil { | ||
log.Fatalf("Failed to get chatter comments: %v", err) | ||
} | ||
if len(records.Records) == 0 { | ||
log.Fatal("Could not find any chatter comments") | ||
} | ||
for _, comment := range records.Records { | ||
log.Printf("%s: %s", comment["Id"], comment["Body"]) | ||
} | ||
} | ||
|
||
if len(*newChatter) > 0 { | ||
log.Print("Added new chatter comment") | ||
visibility := "" | ||
switch *commentVisibility { | ||
case "public": | ||
visibility = "AllUsers" | ||
case "private": | ||
visibility = "InternalUsers" | ||
default: | ||
log.Fatal("Unknown visibility") | ||
} | ||
sfClient.SObject("FeedItem"). | ||
Set("ParentId", caseId). | ||
Set("Body", *newChatter). | ||
Set("Visibility", visibility). | ||
Create() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters