Skip to content

Instantly share code, notes, and snippets.

@ulve
Created May 17, 2017 12:31
Show Gist options
  • Save ulve/505877b6dceedb72bc8e0409236037e7 to your computer and use it in GitHub Desktop.
Save ulve/505877b6dceedb72bc8e0409236037e7 to your computer and use it in GitHub Desktop.
JIra
// Learn more about F# at http://fsharp.org
// See the 'F# Tutorial' project for more help.
open FSharp.Data
open Microsoft.FSharp.Data.TypeProviders
open HttpFs.Client
open HttpFs
open Hopac
open Argu
open System
open System.Collections.Generic
type Issue = JsonProvider<"c:\\ws\\issue.json">
type Status =
| Pågående
| Granskning
| Test
| Merge
| Demo
type CLIArguments =
| [<MainCommand; ExactlyOnce; First>] Issue of id:string
| [<Unique; AltCommandLine("-c")>]Comment of comment:string option
| [<Unique; AltCommandLine("-s")>]Status of Status option
| [<Unique; AltCommandLine("-u")>]UserName of username:string option
| [<Unique; AltCommandLine("-p")>]Password of password:string option
with
interface IArgParserTemplate with
member s.Usage =
match s with
| Comment _ -> "Kommentera en story"
| Status _ -> "Ändra status på en story"
| UserName _ -> "Ditt jira-användarnamn, om den ej anges så använder den env-variabeln JiraUserName"
| Password _ -> "Ditt jira-lösenord, om den ej anges så använder den env-variabeln JiraPassword"
| Issue _ -> "Den story man vill ändra på, anger man inget annat än detta så får man se status"
[<EntryPoint>]
let main argv =
let errorHandler = ProcessExiter(colorizer = function ErrorCode.HelpText -> None | _ -> Some ConsoleColor.Red)
let parser = ArgumentParser.Create<CLIArguments>(programName = "DogCatapult.exe", errorHandler=errorHandler)
let optionParserResults = parser.ParseCommandLine argv
let envVars key =
System.Environment.GetEnvironmentVariables()
|> Seq.cast<System.Collections.DictionaryEntry>
|> Seq.map (fun d -> d.Key :?> string, d.Value :?> string)
|> Map.ofSeq
|> Map.tryFind key
// Opressivly ugly!
let username = optionParserResults.GetResult(<@ UserName @>, defaultValue = (envVars "JiraUserName"))
let password = optionParserResults.GetResult(<@ Password @>, defaultValue = (envVars "JiraPassword"))
if username.IsNone then failwith "Inget användarnamn angivet"
if password.IsNone then failwith "Inget lösenord angivet"
let auth = Request.basicAuthentication username.Value password.Value
let proxy =
Request.proxy {
Address = "localhost";
Port = 8888;
Credentials = Credentials.Default }
let postRequest url body =
Request.createUrl Post url
|> proxy
|> Request.setHeader (ContentType (ContentType.create("application", "json")))
|> auth
|> Request.bodyString body
let getRequest url =
Request.createUrl Get url
|> proxy
|> auth
|> Request.responseAsString
// Hämta en issue
let getIssue id =
let url = "https://jira-kfm.riada.se/rest/api/latest/issue/" + id
getRequest url |> run
// Kommentera en issue
let postComment id comment =
let url = "https://jira-kfm.riada.se/rest/api/latest/issue/" + id + "/comment"
let body = "{\"body\": \"" + comment + "\"}"
postRequest url body |> getResponse |> run |> ignore
// byt status på en issue
let changeStatus id status =
let url = "https://jira-kfm.riada.se/rest/api/latest/issue/" + id + "/transitions"
let body = "{\"transition\": {\"id\":\"" + status + "\"}}"
postRequest url body |> getResponse |> run |> ignore
// Översätter till statusid
let translateStatus (status : Status option) : (string option) =
match status with
| None -> None
| Some s ->
match s with
| Pågående -> Some "3"
| Granskning -> Some "71"
| Test -> Some "61"
| Merge -> Some "131"
| Demo -> Some "141"
// Skriver ut en issue
let displayIssue (i : Issue.Root) =
printfn "%A" i.Fields.Summary
printfn "%A" i.Fields.Description
for s in i.Fields.Subtasks do
printfn "\t%A %A" s.Key s.Fields.Summary
for c in i.Fields.Comment.Comments do
printfn "%A: %A" c.Author.Name c.Body
// Kör först en kommentar om den finns om den inte finns kör vi en status
let comment = optionParserResults.GetResult(<@ Comment @>, defaultValue = None)
let issue = optionParserResults.GetResult(<@ Issue @>)
let status = optionParserResults.GetResult(<@ Status @>, defaultValue = None) |> translateStatus
match comment with
| Some s -> postComment issue s
| None ->
match status with
| Some s -> changeStatus issue s
| None -> ()
// Avsluta med att alltid hämta och visa den vi ändrade
getIssue issue |> Issue.Parse |> displayIssue
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment