1

Background

I want to use org-mode to create a WBS to then create JIRA issues.

org-babel looks like the right option as I can use shell to invoke curl commands to invoke the JIRA API for create issues.

So this is a multi-step process:

  • write the WBS as a normal org-mode hierarchy
  • use org-babel to process the tree and generate the curl commands
  • use org-babel to invoke the curl commands

Problem

My problem, I can't find any documentation that tells me how to do this in two passes, that is being able to manually invoke the results of one org-block.

It's not really source code blocks ability to pass variables, functions, and results to one another

Section 15.6 Results of Evaluation doesn't show which magic incantation of collection, type, format is needed either.

The Worg's The Library of Babel or Worg's Babel: Introduction didn't have any examples.

It's almost Babel src block inside src block which was useful for the links on escaping org-mode special syntax, and the link to 12.6 Literal Examples

2

Which 15.6 Results of Evaluation options are needed?

Collection: value

Why? Because output puts it into scripting mode and you need to write to the standard output stream for the results.

My initial problems with value was that I was trying to return an elisp list as the result of the function, and org-mode was pretty printing the list onto a single line needing me to find How do I prevent extremely long lines making Emacs slow?

I was relying on org determining the correct type and format for me, which may have contributed to misunderstandings.

Type: unspecified

Manually setting this to each of the values: ‘table’, ‘vector’, ‘list’, ‘scalar’, ‘verbatim’ produced incorrect results.

Format: org with wrap: SRC shell

The output is a list of shell commands, so it needs to be wrapped in a #+begin_src block. The wrap header changes this from the default #+begin_org to #+begin_src and appends shell to the wrapped block.

Results of Function: multi-line string

This bit caused a lot of problems.

I assumed I could build up my list structure and org-mode would do the right thing. Having the long lines issue making Emacs slow also didn't help trial-and-error coding.

Example code

Please excuse my poor Emacs-fu. This code works, I have no idea whether it conforms to anything remotely like decent elisp style.

#+NAME: generate-jira-create-issue-commands
#+BEGIN_SRC emacs-lisp :results value org :wrap SRC shell
  (require 's)
  (let* ((base-url "https://jira.example.com/rest/api/2")
         (resource-name "issue")
         (defaults '(("projectId" . 12345) ; your project id in your JIRA instance
                     ("issuetypeId" . 3) ; JIRA "Task"
                     ("assigneeName" . "<your user id>")))
         (commands '())
         (headings '())
         (api-url (s-format "${base-url}/${resource-name}" 'aget `(("base-url" . ,base-url) ("resource-name" . ,resource-name))))
         (jira-create-issue-template "curl --insecure -b ~/jira-session.txt --request POST --header \"Content-Type: application/json\" --data '{\"fields\": {\"project\": {\"id\": \"${projectId}\"}, \"summary\": \"${summary}\", \"description\": \"${description}\", \"assignee\": {\"name\": \"${assigneeName}\"}, \"issuetype\": {\"id\": \"${issuetypeId}\"}}}' ${api-url}"))
    (org-element-map
        (org-element-parse-buffer)
        'headline
      (lambda (heading)
        (push (org-element-property :raw-value heading) headings)))
    (dolist (heading headings)
      (let* ((template-values `(("api-url" . ,api-url)
                                ("summary" . ,heading)
                                ("description" . ".")
                                ,@defaults))
             (command (s-format jira-create-issue-template 'aget template-values)))
        (push command commands)
        (push (concat "# " heading) commands)))
    (s-join "\n" commands))
#+END_SRC

I can then run C-c C-c inside this block to process the entire org buffer and the RESULTS will contain all the curl commands I need to run wrapped in #+begin_SRC shell.

#+RESULTS: generate-jira-create-issue-commands
#+begin_SRC shell
# Heading 1
curl --insecure -b ~/jira-session.txt --request POST --header "Content-Type: application/json" --data '{"fields": {"project": {"id": "12345"}, "summary": "Heading 1", "description": ".", "assignee": {"name": "<your user id>"}, "issuetype": {"id": "3"}}}' https://jira.example.com/rest/api/2/issue
...
# Heading N
curl --insecure -b ~/jira-session.txt --request POST --header "Content-Type: application/json" --data '{"fields": {"project": {"id": "12345"}, "summary": "Heading N", "description": ".", "assignee": {"name": "<your user id>"}, "issuetype": {"id": "3"}}}' https://jira.example.com/rest/api/2/issue
#+end_SRC

I can then run C-c C-c inside the RESULTS block to run the curl commands.

| improve this answer | |
  • That's amazing. I agree there should be more examples out there using :wrap SRC ... – gregoryg Sep 7 at 23:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.