Publish DEV articles from Github repo
@dylan
code:
data:privatelast updated:3 years ago
today
Build integrations remarkably fast!
You're viewing a public workflow template.
Sign up to customize, add steps, modify code and more.
Join 1,000,000+ developers using the Pipedream platform
steps.
trigger
active
last updated:-last event:-
steps.
create_and_update_dev_posts
auth
to use OAuth tokens and API keys in code via theauths object
(auths.dev_to)
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
}
100
const { parse } = require("path")
const get = require("lodash.get")

const { addedFileContents, modifiedFileContents } = event

// Pull existing file-article mappings from workflow state:
// https://docs.pipedream.com/workflows/steps/code/state/
const fileArticleMappings = get($checkpoint, 'fileArticleMappings', {})

// ADDED FILES - Create new article
for (const file of addedFileContents) {
  // Parse markdown to see if front-matter exists. If it doesn't,
  // use the filename as the title.
  const { contents, path } = file

  // Generate a title from the filename, as a fallback.
  // DEV prefers the title present in the YAML front matter,
  // so the filename-based title will be ignored if front matter is present.

  const { name } = parse(path)
  // my-first-post.md -> my first post
  // My_First_Post.md -> My First Post
  // My First Post.md -> My First Post
  title = name.replace(/[-_]/g, " ")

  // Create article in dev.to
  const { id } = await createArticle(contents, title)
  console.log(`Created new article tied to file ${path}`)

  // Once the article has been successfully created, store its article ID
  // in $checkpoint so we know what file is matched to what dev.to article.
  fileArticleMappings[path] = id
}

// MODIFIED FILES - Update article
for (const file of modifiedFileContents) {
  const { path, contents } = file

  // See above for title-generation logic
  const { name } = parse(path)
  title = name.replace(/[-_]/g, " ")

  // If this workflow was added to an existing git repo, there may be files
  // previously added to git that weren't yet added to DEV.
  // In that case, we create a new article and store its file -> article mapping
  if (!(path in fileArticleMappings)) {
    console.log(`Updated file ${path} not tracked in dev.to. Creating new article!`)
    const { id } = await createArticle(contents, title)
    fileArticleMappings[path] = id
    continue
  }

  // Otherwise, we already have an article associated with this file 
  // in DEV, so we update
  console.log(`Updating article tied to file ${path}`)
  await updateArticle(fileArticleMappings[path], contents, title)
}

// Save file -> article mappings back to $checkpoint
$checkpoint = {
  ...$checkpoint,
  fileArticleMappings,
}

// HELPER FUNCTIONS
async function createArticle(body_markdown, title) {
  return await require("@pipedreamhq/platform").axios(this, {
    method: "POST",
    url: `https://dev.to/api/articles`,
    headers: {
      "api-key": `${auths.dev_to.api_key}`,
      "Content-Type": "application/json"
    },
    data: {
      article: {
        body_markdown,
        title,
      }
    }
  })
}

async function updateArticle(id, body_markdown, title) {
  return await require("@pipedreamhq/platform").axios(this, {
    method: "PUT",
    url: `https://dev.to/api/articles/${id}`,
    headers: {
      "api-key": `${auths.dev_to.api_key}`,
      "Content-Type": "application/json"
    },
    data: {
      article: {
        body_markdown,
        title,
      }
    }
  })
}