Skip to content

[BUG] The action replaces the current entry instead of adding a new oneΒ #5

@EDM115

Description

@EDM115

Expected Behavior

The action should update the list of Authors

Current Behavior

The action replaces the list of authors

Possible Solution

We could change the run function :

export async function run() {
  try {
    const { context } = github
    const options = getOptions()
    const octokit = getOctokit()
    const authors = await getAuthors()
    const lines: string[] = []

    core.debug(JSON.stringify(options, null, 2))
    core.debug(JSON.stringify(authors, null, 2))

    mustache.parse(options.template)

    if (options.sort === 'commits') {
      authors.sort((a, b) => a.commits - b.commits)
    }

    const filteredAuthors = authors.filter((author) =>
      options.bots || !author.name.includes('[bot]')
    )

    const getContent = async () => {
      try {
        return await octokit.rest.repos.getContent({
          ...github.context.repo,
          path: options.path,
        })
      } catch (err) {
        return null
      }
    }

    const res = await getContent()
    const oldContent = res
      ? Buffer.from((res.data as any).content, 'base64').toString()
      : ''
    
    core.debug(`previous content: \n${oldContent}`)

    const oldAuthors = oldContent
      .split('\n')
      .map(line => line.trim())
      .filter(line => line.length > 0)

    const newAuthorsSet = new Set(oldAuthors)

    filteredAuthors.forEach(author => {
      const renderedAuthor = mustache.render(options.template, author)
      newAuthorsSet.add(renderedAuthor)
    })

    const newAuthorsArray = Array.from(newAuthorsSet)
    
    if (options.sort === 'alphabet') {
      newAuthorsArray.sort()
    }

    const newContent = newAuthorsArray.join('\n')
    core.debug(`generated content: \n${newContent}`)

    if (oldContent !== newContent) {
      await octokit.rest.repos.createOrUpdateFileContents({
        ...context.repo,
        path: options.path,
        content: Buffer.from(newContent).toString('base64'),
        message: options.commit,
        sha: res ? (res.data as any).sha : undefined,
      })
      core.info(`${oldContent ? 'Updated' : 'Generated'}: "${options.path}"`)
    } else {
      core.info(`No changes to "${options.path}"`)
    }
  } catch (e) {
    core.error(e)
    core.setFailed(e.message)
  }
}

Steps To Reproduce

You can check here : https://github.com/EDM115/unzip-bot/commits/master/AUTHORS
Especially here : EDM115/unzip-bot@943cee9?diff=unified

Additional Context

Your Environment

  • action version: 1.1.4
  • OS: Linux Ubuntu latest
  • Browser: Chrome 131.0.6753.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions