Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
EDM115 opened this issue Oct 15, 2024 · 0 comments
Open
Labels
bug Something isn't working

Comments

@EDM115
Copy link

EDM115 commented Oct 15, 2024

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
@EDM115 EDM115 added the bug Something isn't working label Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant