Skip to content
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
c9ea4ea
adding slack file send
senthilhns Oct 29, 2024
e613989
added all file send params
senthilhns Oct 29, 2024
1e8144d
adding slack file upload
senthilhns Oct 29, 2024
e9e524d
adding test files
senthilhns Oct 30, 2024
577015f
modifed plugin tests to check for request body similar to previous cases
senthilhns Oct 30, 2024
56308a2
adding output env vars
senthilhns Nov 1, 2024
794e5d8
updating slack file upload instructions
senthilhns Nov 1, 2024
8ed5a42
handling filename arg is empty case
senthilhns Nov 3, 2024
65e5b61
Update plugin.go
senthilhns Nov 6, 2024
1d2a653
not creating any tmp files if DRONE_OUTPUT not set
senthilhns Nov 6, 2024
ed9085e
removing DRONE_OUTPUT usage
senthilhns Nov 6, 2024
93cfc24
Update README.md
Ompragash Nov 6, 2024
f2a5e48
Update README.md
Ompragash Nov 6, 2024
26a9085
Update plugin.go
Ompragash Nov 6, 2024
0b3ad75
Update README.md
Ompragash Nov 6, 2024
834d356
Merge branch 'master' into master
Ompragash Nov 6, 2024
e4b93e3
Merge branch 'drone-plugins:master' into master
senthilhns Nov 26, 2024
3ff0afc
adding get slack id from email functionality
senthilhns Nov 26, 2024
f47fed0
getting all slack ids of commiters from git
senthilhns Nov 26, 2024
3570edf
updating arm64 docker file
senthilhns Nov 26, 2024
2582430
added windows git command for getting commiters
senthilhns Nov 26, 2024
54c92c0
Updating usage for get slack id support in README.MD
senthilhns Nov 26, 2024
edcaa08
test update for commiter log
mindzaurus Nov 27, 2024
56736d3
adding git commit id ranges for getting commiter ids
mindzaurus Nov 28, 2024
d00b486
adding HEAD and sha commit ranges, HEAD and number sha commit ranges
senthilhns Nov 28, 2024
0fb94d7
Updating usage from Get the Slack IDs of all committers
senthilhns Nov 28, 2024
b3f6356
getting old and new commit ids from HEAD^ and HEAD
senthilhns Nov 28, 2024
4658812
making slack id of commiters to json string
senthilhns Nov 28, 2024
1b29358
adding email len == 0 check and returning error
senthilhns Nov 28, 2024
9817613
taking csv list for getSlackid from emails
mindzaurus Dec 4, 2024
7313a6a
testing message sending to user
senthilhns Dec 4, 2024
2ebebdf
sending dm same as channel msg
senthilhns Dec 4, 2024
ccdb731
added fmt prints
senthilhns Dec 5, 2024
ad7014b
changing fmt.Print to log.Print
senthilhns Dec 5, 2024
1783391
removing bash git command with go git package
senthilhns Dec 5, 2024
995a22d
removing git bash install from linux docker files
senthilhns Dec 5, 2024
d75a643
adding test functions
senthilhns Dec 5, 2024
ada0510
removing debug print
senthilhns Dec 5, 2024
270c150
adding test cases for no commiters, slack lookup failure, rate limit …
senthilhns Dec 5, 2024
2516b21
Update plugin.go
senthilhns Dec 6, 2024
583f55e
Update plugin.go
senthilhns Dec 6, 2024
cb99f84
Update plugin.go
senthilhns Dec 6, 2024
7e81e26
Update plugin.go
senthilhns Dec 6, 2024
1836dc6
adding time package
senthilhns Dec 6, 2024
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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,26 @@ Make sure to replace `your_access_token` with your actual Slack access token and
If you provide an access token, it will use the Slack API to send the message.


### Get Slack Id of a user from a Email ID
```bash
docker run --network host --rm \
-e PLUGIN_ACCESS_TOKEN=your_access_token \
-e [email protected] \
plugins/slack
```
Output will be stored in the FOUND_SLACK_ID environment variable
Make sure to replace `your_access_token` with your actual Slack access token and adjust

### Get the Slack IDs of all commiters from a git repo
```bash
docker run --network host --rm \
-e PLUGIN_ACCESS_TOKEN=your_access_token \
-e PLUGIN_COMMITTER_LIST_GIT_PATH=/harness \
plugins/slack
```
Output will be stored in the COMMITTER_SLACK_ID_LIST environment variable as comma separated values.
Make sure to replace `your_access_token` with your actual Slack access token and adjust.

## Release Preparation

Run the changelog generator.
Expand Down
4 changes: 3 additions & 1 deletion docker/Dockerfile.linux.amd64
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
FROM plugins/base:multiarch
FROM alpine:3.20

LABEL maintainer="Drone.IO Community <[email protected]>" \
org.label-schema.name="Drone Slack" \
org.label-schema.vendor="Drone.IO Community" \
org.label-schema.schema-version="1.0"

RUN apk add --no-cache git bash

ADD release/linux/amd64/drone-slack /bin/
ENTRYPOINT ["/bin/drone-slack"]
4 changes: 3 additions & 1 deletion docker/Dockerfile.linux.arm64
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
FROM plugins/base:multiarch
FROM alpine:3.20

LABEL maintainer="Drone.IO Community <[email protected]>" \
org.label-schema.name="Drone Slack" \
org.label-schema.vendor="Drone.IO Community" \
org.label-schema.schema-version="1.0"

RUN apk add --no-cache git bash

ADD release/linux/arm64/drone-slack /bin/
ENTRYPOINT ["/bin/drone-slack"]
28 changes: 20 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package main
import (
"errors"
"fmt"
"log"
"os"

"github.com/joho/godotenv"
"github.com/urfave/cli"
"log"
"os"
)

var (
Expand All @@ -16,6 +15,7 @@ var (
)

func main() {

app := cli.NewApp()
app.Name = "slack plugin"
app.Usage = "slack plugin"
Expand Down Expand Up @@ -239,6 +239,16 @@ func main() {
Usage: "fail build on error",
EnvVar: "PLUGIN_FAIL_ON_ERROR",
},
cli.StringFlag{
Name: "slack_id_of",
Usage: "slack id required for the user email id",
EnvVar: "PLUGIN_SLACK_ID_OF",
},
cli.StringFlag{
Name: "committer_list_git_path",
Usage: "git repo path holding the committers email id to fetch slack IDs from",
EnvVar: "PLUGIN_COMMITTER_LIST_GIT_PATH",
},
}

if _, err := os.Stat("/run/drone/env"); err == nil {
Expand Down Expand Up @@ -299,11 +309,13 @@ func run(c *cli.Context) error {
CustomTemplate: c.String("custom.template"),
Message: c.String("message"),
// File upload attributes
FilePath: c.String("filepath"),
FileName: c.String("filename"),
Title: c.String("title"),
InitialComment: c.String("initial_comment"),
FailOnError: c.Bool("fail_on_error"),
FilePath: c.String("filepath"),
FileName: c.String("filename"),
Title: c.String("title"),
InitialComment: c.String("initial_comment"),
FailOnError: c.Bool("fail_on_error"),
SlackIdOf: c.String("slack_id_of"),
CommitterListGitPath: c.String("committer_list_git_path"),
},
}

Expand Down
110 changes: 110 additions & 0 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
textTemplate "text/template"

Expand Down Expand Up @@ -77,6 +79,10 @@ type (
InitialComment string
Title string
FailOnError bool
// Get Slack ID of the user by email
SlackIdOf string
// Git path to get list of committer emails
CommitterListGitPath string
}

Job struct {
Expand Down Expand Up @@ -122,6 +128,14 @@ func (p Plugin) Exec() error {
return p.UploadFile()
}

if p.Config.SlackIdOf != "" {
return GetSlackIdFromEmail(&p)
}

if p.Config.CommitterListGitPath != "" {
return GetSlackIdsOfCommitters(&p)
}

// Determine the channel
if p.Config.Recipient != "" {
channel = prepend("@", p.Config.Recipient)
Expand Down Expand Up @@ -482,3 +496,99 @@ func prepend(prefix, s string) string {

return s
}

func GetSlackIdsOfCommitters(p *Plugin) error {
if p.Config.CommitterListGitPath == DefaultWorkspace {
p.Config.CommitterListGitPath = os.Getenv("DRONE_WORKSPACE")
}

emails, err := getGitEmails(p.Config.CommitterListGitPath)
if err != nil {
return fmt.Errorf("failed to get git emails: %w", err)
}
var slackIdsList []string
for _, email := range emails {
slackId, err := getSlackUserIDByEmail(p.Config.AccessToken, email)
if err != nil {
//log.Printf("Failed to get Slack ID for email %s: %s\n", email, err.Error())
continue
}

slackIdsList = append(slackIdsList, slackId)
}
slackIdsListStr := strings.Join(slackIdsList, ",")
err = WriteEnvToOutputFile("COMMITTER_SLACK_ID_LIST", slackIdsListStr)
if err != nil {
return fmt.Errorf("failed to write git emails to output file: %w", err)
}
return nil
}

func GetSlackIdFromEmail(p *Plugin) error {

slackId, err := getSlackUserIDByEmail(p.Config.AccessToken, p.Config.SlackIdOf)
if err != nil {
return fmt.Errorf("failed to get Slack ID by email: %w", err)
}
err = WriteEnvToOutputFile("FOUND_SLACK_ID", slackId)
if err != nil {
return fmt.Errorf("failed to write Slack ID to output file: %w", err)
}
return nil
}

func getSlackUserIDByEmail(accessToken, email string) (string, error) {
api := slack.New(accessToken)
if api == nil {
return "", fmt.Errorf("failed to create Slack client")
}

user, err := api.GetUserByEmail(email)
if err != nil {
return "", fmt.Errorf("failed to get user by email: %w", err)
}

return user.ID, nil
}

func getGitCommittersCommand(dir string) *exec.Cmd {
if runtime.GOOS == "windows" {
gitLogCmd := "git log --format='%ae' | Sort-Object | Get-Unique"
return exec.Command("powershell", "-Command", gitLogCmd)
}
return exec.Command("bash", "-c", "set -e; git log --format='%ae' | sort | uniq")
}

func getGitEmails(dir string) ([]string, error) {
var emailList []string

cmd := getGitCommittersCommand(dir)
cmd.Dir = dir

var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &out

if err := cmd.Run(); err != nil {
fmt.Println("Error: ", err)
return emailList, fmt.Errorf("failed to execute git command: %v\nOutput: %s", err, out.String())
}

return cleanupAndSplitEmails(out.String()), nil
}

func cleanupAndSplitEmails(emailString string) []string {
lines := strings.Fields(emailString)
var cleanedEmails []string
for _, line := range lines {
email := strings.TrimSpace(line)
if email != "" {
cleanedEmails = append(cleanedEmails, email)
}
}
return cleanedEmails
}

const (
DefaultWorkspace = "DRONE_WORKSPACE"
)